Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, December 17, 2008

How to write a playlist maker script

I got an email asking for tips on writing a playlist maker script.

Using PHP, my process was this:
1. Choose a playlist format. My favorite playlists are Windows Media Player WPL files, which use the SMIL subtype of XML. There are tons of other types of playlists. Songbird and Winamp both use M3U, for example. Unfortunately, most playlist file types, including M3U, are not as "smart" as WPL files, in that they want the exact path to a single song on a user's computer, rather than just an artist name or song title. WPL files will take an artist or song name and give you a playlist of everything in your library that matches. I don't understand why Songbird and Winamp don't handle WPL or SMIL files (yet?). After I figured out which playlist format I wanted, I right-clicked a WPL file I'd made, opened it in Notepad, and used it to cut and paste the top, bottom, and repeating bits of the playlist code where needed in the following steps.

2. Find a source of artists or song names you want in your playlist. The best way was to use an RSS feed, but for my iLike and last.fm tag playlist makers, I scraped the source code of some web pages, which is messy, but it works. You can also offer a way to paste in or upload a list of artists.

3. Write an HTML form (called, for example, input.html) that takes user input (like a last.fm username) to get the source you want. The "Submit" button on the form will take you to your playlist making code (playlistmaker.php, for example).

4. Write code to convert your input (such as a last.fm username) into a URL to an XML or HTML file and get that file ready to be used. For example:
$handle = fopen("http://ws.audioscrobbler.com/1.0/user/".$username."/topartists.txt?type=overall", "r");

5. Write code to make an array of only artists or song names using your source. The exact code will vary depending on the source of artists or song names. This is the trickiest part, and I'd give examples of how I've done it, but it's different nearly every time, and I'm sure my ways are not especially elegant. One thing I'd like to do better is parsing the actual XML instead of finding where the artists turn up in the array and using numeric indices to grab the artists or song names.

6. Create a variable that will contain the entire text you want in the playlist file ($wpl, for example). Paste in the top bit of the playlist text. For a WPL file:
$wpl = "<?wpl version=\"1.0\"?>
<smil>
<head>
<meta name="\" content="\">
<title>".$username."</title>
</head>
<body>
<seq>
<smartplaylist version="\">";

7. Write a loop that adds the section of the playlist that repeats for each artist or song name to your playlist variable ($wpl., for example). Concatenate in the artist or song name variable. Make sure you convert HTML characters, as ampersands will render your playlist utterly worthless. For example, where the artist names are in $data[2]:
while ($data = fgetcsv($handle, 1000, ",")) {
$wpl.="<querySet>
<sourceFilter id=\"{4202947A-A563-4B05-A754-A1B4B5989849}\" name=\"Music in my library\">
<fragment name=\"Album Artist\">
<argument name=\"condition\"<Contains>/argument>
<argument name=\"value\">" . htmlspecialchars($data[2]) . "</argument>
</fragment>
</sourceFilter>
</querySet>";
}

8. Finish off your playlist text variable by pasting in the end text of the playlist variable (again, $wpl.). For a WPL file:
$wpl.="
</smartPlaylist>
</seq>
</body>
</smil>";

9. Create a new file containing your playlist variable text as follows:
$handle = fopen("filename.wpl","w");

10. Offer a link to download the new file.

11. Try it out!

Optional tweaks:
12. WPL playlist files offer you the option of finding an artist name that "Contains" the word "Bell" (for example), or finding an artist that "Is" the word "Bell." After the band "Bell" made Belle and Sebastian turn up in a festival playlist I made (oh, the brief crushing excitement), I added a conditional so that if the artist name is 5 characters or shorter, I use "Is", and if it's longer, I use "Contains". It's still inexact, and it's an area I'd like to improve. Handling "The" (Pixies, Beatles, Raveonettes) is another thing I'd like to work out at some point. Here's an example of one of my conditionals where the artist names are in $data[2]:
while ($data = fgetcsv($handle, 1000, ",")) {
$limit=6;
$num=count($data[2]);
for ($i=0;$i<$num;$i++) {
$len[$i]=strlen($data[2]);
if ($len[$i]<$limit) {
$wpl.="<querySet>
<sourceFilter id=\"{4202947A-A563-4B05-A754-A1B4B5989849}\" name=\"Music in my library\">
<fragment name=\"Album Artist\">
<argument name=\"condition\">Is</argument>
<argument name=\"value\">" . htmlspecialchars($data[2]) . "</argument>
</fragment>
</sourceFilter>
</querySet>";
}
else {
$wpl.="<querySet>
<sourceFilter id=\"{4202947A-A563-4B05-A754-A1B4B5989849}\" name=\"Music in my library\">
<fragment name=\"Album Artist\">
<argument name=\"condition\"<Contains>/argument>
<argument name=\"value\">" . htmlspecialchars($data[2]) . "</argument>
</fragment>
</sourceFilter>
</querySet>";
}
}
}

13. Greasemonkey! Using a tiny bit of Javascript, you can put a link to your playlist making code on relevant source pages. Clicking this link will give you the option to look at one of my Greasemonkey scripts. You'll especially want to change:
  • @include to reference the page you'd like to put a link on.
  • The link to the script that makes your playlist.
  • @name
  • @namespace
  • Colors (I'm using last.fm grey. Meh.)

You can grab the username for the last.fm URL they came from by putting this at the top of your playlist maker script (playlistmaker.php, for example):
$url = getenv("HTTP_REFERER");
$elements=explode("/", $url);
unset($url);
$username=$elements[4];
unset($elements);

Here's a zip folder with code for the input form and playlist maker, as well as a Greasemonkey playlist maker for a last.fm listener's most-played artists. This was one of the first things I did when I was learning PHP, so it's far, far, far from perfect. But feel free to use any bits of it you like. I'm not litigious.


I am crawling up a learning curve (Drupal) myself right now, but if you have questions or suggestions, comments are open.

For anyone more interested in making playlists than playlist-maker scripts, my playlist makers are here.

Monday, September 15, 2008

I shoulda learned Java

Alas, my bank account has little more than tumbleweeds in it, so my year or so of un-/self-employment is coming to an end. I must job search in earnest and stop turning up my liberal elite nose at corporations looking for automated pollutant emitters and chicken torturers.

The question now is what programming skills such corporations want me to use to design their new improved Humvee fan sites, and since I've sent out my quota of three resumes today, I get to blog those skills.

As I've opened various job postings, hoping the single handful of programming languages I'm truly comfortable with are just what they're looking for (FORTRAN anyone? Anyone?), I've been keeping track of what they are, in fact, looking for. This is completely non-scientific, of course. One glaring flaw, for example: every third company wants a straight-up Java programmer. I want to know Java (that's how the awesome, awesome, awesome Legend of Zelda for your phone! was written), but I don't honestly know Java, so I don't open those ones.

Here are the programming languages/skills companies are looking for in order of popularity in my unscientific survey:

Javascript - 8 companies
Green Ventures, Inc.
Mystery Company
Auction company
Smarsh
Mystery Company
Dealerpeak
Inspiration
ConceroTechnology

HTML - 6 companies
Sharepoint
Smarsh
Intersoft
Mystery Company
Auction company
ConceroTechnology

Java - 6 companies
Amazon
Sharepoint
Axiom
Mystery Company
Dealerpeak
DB Professionals

Linux - 4 companies
Amazon
Mystery Company
Intersoft
VxWorks

PHP - 3 companies
Green Ventures, Inc.
Mystery Company
Intersoft

MySQL - 3 companies
Green Ventures, Inc.
Mystery Company
Auction company

Visual Basic - 3 companies
Smarsh
Axiom
Selectron

Visual Basic.NET - 3 companies
Lifeport
Sharepoint
ConceroTechnology

XML - 2 companies
Intersoft
Dealerpeak

Perl - 2 companies
Amazon
Intersoft

C++ - 2 companies
Amazon
Axiom

C# - 2 companies
Selectron
ConceroTechnology

C - 2 companies
VxWorks
Mystery Company

Coldfusion - 2 companies
Mystery Company
Dealerpeak

.NET - 2 companies
Sharepoint
Intersoft

CGI - 1 company
Intersoft

CSS - 2 companies
Smarsh
Mystery Company

ASP - 2 companies
Sharepoint
Smarsh

Flash - 2 companies
Auction company
Inspiration

SQL - 1 company
Dealerpeak

AJAX - 1 company
Green Ventures, Inc.

Joomla - 1 company
Green Ventures, Inc.

Drupal - 1 company
Inspiration

SQL Server - 1 company
Smarsh

ActiveX - 1 company
Smarsh

Saturday, May 31, 2008

Server problems, episode III

If you've tried to use my playlist makers in the last week, I apologize that they didn't work. I went over my server quota and, as I was mostly way off-grid last week, I didn't realize things weren't working until yesterday. The problem should have been fixed yesterday, but it just got worse and worse. I deleted everything I could to free up space. But it still wouldn't let me create new files. Worse, trying to save changes to existing files resulted in those files getting emptied of all code.

Ultimately, my desperate attempts to regain control of my files resulted in my entire web folder turning inaccessible to me. Grr.

So once again I'm trying to move everything (oh yes I have backups) to a more reliable server (Freehostia doesn't pull this baffling crap) and I'm dreaming of turning my elderly Toshiba into my very own Linux server. And again, I'll get two things moved, the crappy server will start working again, and I'll want to write new code instead of tweaking old code for a new server or figuring out how to get Linux to authenticate to a shared wireless router...

Wednesday, May 7, 2008

Useful last.fm tools

Last.fm is doing right what Facebook did right -- letting developers use its data to make new applications as they see fit.

I've blogged, ad nauseam, my last.fm Podcast Finder and last.fm Windows Media Player playlist makers (by most-played artists, calendar, and tag).

In the last few days, I've come across a couple cool last.fm tools made by other folks (each causing me brief bouts of crushing envy and despair). One tool improves the mp3 recommendation process, allowing you to subscribe to last.fm mp3 recommendations like they were a blog. And now there's lasttube, which finds videos from artists you play most on last.fm.

These new tools have me curious what other useful gems are hidden in the graphs-and-pretty-pictures mess at build.last.fm.

I'll let you know what I find.

Friday, May 2, 2008

Last.fm Podcast Finder

I wrote some code I'm pretty excited about yesterday.

I've already blogged about my Podcast Finder, for podcast listeners in the market for recommendations.

This new bit of code makes podcast finding easier for last.fm users. Just put your last.fm username in here, and you'll get some podcast recommendations based on what you've "scrobbled" the most.

If you're a podcast listener who uses last.fm and Firefox with Greasemonkey installed (anyone left?), there's a Greasemonkey script that puts a link at the top of last.fm most-played artists pages, so you can just click to get podcast recommendations.

photo by Beard Papa

Tuesday, April 15, 2008

Programming server problems

The web server I'm using to do programming practice is giving me server errors when I load my PHP pages. This is the second time my site has been obliterated for at least a day by the server with no warning, and with lots of lecturing instead of trying to get my page running again. I don't write the tightest code yet, and I'm grateful for advice, but when three months' work is gone without warning, with no hint at when it will return, it might not be the best time for a lesson.

Basically, nothing I've written for the past three months works right now. It will -- I'm moving everything to a new server (and trying to install my own Linux server at home). But if you're having trouble using any of my playlist makers or Podcast Finder, it's because I'm moving everything to a place where it'll work. Sorry for the inconvenience.

Sunday, March 9, 2008

Podcast Finder

I've been making decent progress learning PHP and MySQL since I finally got started last month. My proudest achievement thus far is a podcast finder, called "Podcast Finder", that recommends podcasts based on a couple of podcasts you know you like.

I'm a total podcast junkie (try doing PCR in a dead-silent lab for two years, then winning an iPod shuffle, and see if you don't turn addict). But finding new podcasts when you've exhausted an archive or ten is not as simple as giving your favorites five stars on Amazon or Netflix and seeing what they recommend. My old "system" to find new podcasts basically added up to Googling combinations of my favorites to see what else came up (zip), combing through all the most-dugg podcasts on Digg, and scouring the last.fm artists of other big NPR listeners.

The last.fm method gives pretty good recommendations, but it's slow. Podcast Finder speeds it up by using a database (fairly small right now) of last.fm favorites. As with all my little projects, there's room for improvement, but if you're a podcast fan, I'd love for you to give it a try. If I'm missing your favorite (or you have any other feedback), feel free to leave me a comment here.

Tuesday, February 12, 2008

CSV to WPL

Here's my first PHP and SQL program, called CSV to WPL. It creates Windows Media Player playlists from comma-separated lists of bands.

It's a niche program that has football stadiums worth of room for improvement (it's not a beta -- it's an alpha), but I know I'll use it a lot just as it is.

Update: I did use it a lot, but I've written various improved playlist maker scripts, including a version that allows you to cut and paste a list of artists instead of making a spreadsheet of them.