2010-01-27
For a recent video editing project, I really wanted to have some mellow classical music playing in the background. Due to licensing restrictions, the classical music needed to be either in the Public Domain or released under a very permissive Creative Commons license.

Fortunately, the United States Air Force Band has quite a few Public Domain recordings, of Public Domain works, on their website. Unfortunately, the Air Force website leaves a lot to be desired regarding listening to and downloading tunes.

The website uses javascript to open a popup player, and this is terrible. Actually, it is good, because the javascript that launches the player requires the full URL of the mp3 file that the player is supposed to play.

What this means, is that I need only sift through the source of the page containing links to audio files and pull out the URL strings that start with "http://" and end with ".mp3". This sounds like a job for a non-greedy Regular Expression.

Enter the PHP: What? you don't write command line scripts in PHP?
#!/usr/bin/env php <?php     if count($argv)>)     {         $file $argv[1];         $text file_get_contents$file );         $matches="";         preg_match_all("/http:\/\/[A-Za-z\/_\.0-9]*?\.mp3/",$text,$matches);         if(count($matches[0]) )         {             foreach($matches[0as $match)             {                 echo "$match\n";             }         }     } ?>
The script (which I named echo_mp3.php) takes the URL of a webpage as an argument and prints out the URLs of the mp3s that are found in the webpages code. For my usage, I wrote the list of mp3s to a file by executing
./echo_mp3.php http://example.com/some/file.htm >> mp3list.txt
and then used wget to batch download the files in the list.

Now stop reading, and go listen to some Vivaldi.
Comments
2010-02-02 Windigo:
The network administrator at Adirondack Community College taught our PHP class. The first half of the class was basic Linux techniques, and the second half was PHP scripting.

He probably still does all of his administrative scripting in PHP, I bet. It didn't seem like a bad way to do business. :)
2010-02-03 jezra:
PHP is definitely under-rated as far as CLI programming is concerned. For many years, I was using PHP scripts run from Window's Scheduled Tasks to emulate various tasks one would normally do as a Cron job: backup directories, look for files to perform actions on, etc.
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
4 minus 2
Answer:
required
  • Tags:
  • PHP
subscribe
 
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008