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?
Now stop reading, and go listen to some Vivaldi.
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?
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#!/usr/bin/env php <?php if ( count($argv)>1 ) { $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[0] as $match) { echo "$match\n"; } } } ?>
and then used wget to batch download the files in the list../echo_mp3.php http://example.com/some/file.htm >> mp3list.txt
Now stop reading, and go listen to some Vivaldi.
He probably still does all of his administrative scripting in PHP, I bet. It didn't seem like a bad way to do business. :)