Skip to content

Instantly share code, notes, and snippets.

@firebus
Created May 29, 2012 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firebus/2828075 to your computer and use it in GitHub Desktop.
Save firebus/2828075 to your computer and use it in GitHub Desktop.
Bootie Top 10 Download Script
<?php
// usage: `php getBootie.php <url> <album-title>`
// for a url like: http://bootiemashup.com/blog/2012/05/triple-tribute-bootie-mashup-pays-tribute-to-mca-of-beastie-boys-robin-gibb-of-bee-gees-donna-summer.html
// @see http://www.getid3.org/
require_once(dirname(__FILE__) . '/getid3/getid3.php');
$url = $argv[1];
$album_title = $argv[2];
$bd = new BootieAlbum($url, $album_title);
$bd->bootleg();
/**
* A representation of an Bootie album - usually a monthly top-ten.
*/
class BootieAlbum {
/**
* @var string $url URL to bootleg the album from
* @var string $album_title Album title.
* - TODO: pull from $url <h2>
* @var int $track_count Number of tracks on the album
* @var array $tracks Array of BootieTracks
*/
private $url = '';
private $album_title = '';
private $track_count = 0;
private $tracks = array();
/**
* Create a new album
* @param type $url
* @param type $album_title
*/
public function __construct($url, $album_title) {
$this->url = $url;
$this->album_title = $album_title;
}
/**
* Find each track in the $url, and create a BootieTrack for each one
* Once tracks are downloaded, tag all successful downloads
*/
public function bootleg() {
$dom = new DOMDocument;
$dom->loadHTMLFile($this->url);
$track_number = 0;
// TODO: Why doesn't html_entity_decode() handle these?
// I must be doing it wrong...
$translations = array(
'&ndash;' => '-',
'&rsquo;' => "'",
);
foreach ($dom->getElementsByTagName('a') as $link) {
if (preg_match('/\.mp3$/', $link->getAttribute('href'))) {
$track_number++;
$track_url = $link->getAttribute('href');
$file_info = html_entity_decode(strip_tags($this->getInnerHTML($link)));
$file_info = str_replace(array_keys($translations), array_values($translations), $file_info);
// artist - title (contributors)
preg_match('/^(.*) - (.*) \((.*)\)$/', $file_info, $matches);
$track = new BootieTrack($track_url, $track_number, $matches[1], $matches[2], $matches[3]);
$track->download();
if ($track->getStatus() == 'success') {
$this->tracks[] = $track;
}
}
}
$this->track_count = $track_number;
foreach ($this->tracks as $track) {
$track->tag($this->album_title, $this->track_count);
}
}
// @see http://us2.php.net/manual/en/book.dom.php#105815
private function getInnerHTML($element) {
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child) {
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
$innerHTML.=trim($tmp_dom->saveHTML());
}
return $innerHTML;
}
}
class BootieTrack {
private $url = '';
private $track_number = 0;
private $artist = '';
private $title = '';
private $contributors = '';
private $filename = '';
private $status = '';
public function __construct($url, $track_number, $artist, $title, $contributors) {
$this->url = $url;
$this->track_number = $track_number;
$this->artist = $artist;
$this->title = $title;
$this->contributors = $contributors;
$this->status = 'new';
}
/**
* Download the mp3 for a track and update status
*/
public function download() {
$this->filename = implode(' - ', array(
$this->track_number,
$this->artist,
"$this->title ($this->contributors)",
));
$this->filename .= '.mp3';
print "saving $this->filename...";
if (file_put_contents('./' . $this->filename, file_get_contents($this->url))) {
print "success!\n";
$this->status = 'success';
}
else {
print "failure\n";
$this->status = 'failure';
}
}
public function tag($album_title, $track_count) {
$getID3 = new getID3;
$getID3->setOption(array('encoding' => 'UTF_8'));
getid3_lib::IncludeDependency(GETID3_INCLUDEPATH . 'write.php', __FILE__, true);
$tagwriter = new getid3_writetags;
$tagwriter->filename = './' . $this->filename;
$tagwriter->tagformats = array('id3v1', 'id3v2.3');
$tagwriter->overwrite_tags = true;
$tagwriter->tag_encoding = 'UTF-8';
$tagwriter->remove_other_tags = true;
$tag = array(
'title' => array("$this->title ($this->contributors)"),
'artist' => array($this->artist),
'album' => array($album_title),
'year' => array(date('Y')), // NOTE: hard-coded
'genre' => array('mashup'), // NOTE: hard-coded
'track' => array("$this->track_number/$track_count"),
'comment' => array($this->contributors),
);
$tagwriter->tag_data = $tag;
print "updating id3 tag for $this->filename...";
if ($tagwriter->WriteTags()) {
print "updated!\n";
}
else {
print "failed.\n";
}
}
public function getStatus() {
return $this->status;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment