Created
February 15, 2015 15:57
-
-
Save hubgit/f4eeb0b253533093d1d3 to your computer and use it in GitHub Desktop.
Build a graph of all artists played on a BBC Radio show and their last.fm tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('LASTFM_API_KEY', 'EDIT_THIS'); // get an API key from http://www.last.fm/api | |
$brand = 'EDIT_THIS'; // PID of the "brand" e.g. b00c72y1 for Marc Riley | |
$url = sprintf('http://open.live.bbc.co.uk/aps/programmes/%s/episodes/upcoming.json', $brand); | |
$data = get_json($url); | |
$count = count($data->broadcasts); | |
$programme = $data->broadcasts[$count - 1]->programme; // most recent broadcast | |
$output = fopen($brand . '-edges.csv', 'w'); | |
fputcsv($output, array('Source', 'Target', 'Weight')); | |
$pids = []; | |
$tags = []; | |
$plays = []; | |
$artists = []; | |
$tagged = []; | |
do { | |
$details = get_programme($programme->pid); | |
$canonicalPID = canonical_version($details); | |
if (!array_key_exists($canonicalPID, $pids)) { | |
$pids[$canonicalPID] = true; | |
$canonical = get_version($canonicalPID); | |
foreach ($canonical->segment_events as $event) { | |
$segment = $event->segment; | |
if ($segment->type !== 'music') { | |
continue; | |
} | |
if (!$contributor = $segment->primary_contributor) { | |
continue; | |
} | |
if (!$mbid = $contributor->musicbrainz_gid) { | |
continue; | |
} | |
if (!$name = $contributor->name) { | |
continue; | |
} | |
if (!array_key_exists($mbid, $artists)) { | |
$artists[$mbid] = $name; | |
} | |
if (!array_key_exists($mbid, $plays)) { | |
$plays[$mbid] = 0; | |
} | |
$plays[$mbid]++; | |
if (!array_key_exists($mbid, $tagged)) { | |
$tagged[$mbid] = true; | |
foreach (get_tags($mbid) as $tag) { | |
if ($tag->count < 10) { | |
break; | |
} | |
if (!array_key_exists($tag->name, $tags)) { | |
$tags[$tag->name] = 0; | |
} | |
$tags[$tag->name]++; | |
fputcsv($output, array($mbid, $tag->name, $tag->count)); | |
} | |
} | |
} | |
} | |
$programme = $details->peers->previous; | |
} while ($programme); | |
fclose($output); | |
$output = fopen($brand . '-nodes.csv', 'w'); | |
fputcsv($output, array('ID', 'Label', 'Type', 'Plays')); | |
foreach ($artists as $mbid => $name) { | |
fputcsv($output, array($mbid, $name, 'artist', $plays[$mbid])); | |
} | |
foreach ($tags as $tag => $count) { | |
fputcsv($output, array($tag, $tag, 'tag')); | |
} | |
fclose($output); | |
function get_json($url) { | |
print $url . "\n"; | |
$json = file_get_contents($url); | |
return json_decode($json); | |
} | |
function get_tags($mbid) { | |
$url = 'http://ws.audioscrobbler.com/2.0/?' . http_build_query(array( | |
'method' => 'artist.gettoptags', | |
'mbid' => $mbid, | |
'api_key' => LASTFM_API_KEY, | |
'format' => 'json', | |
)); | |
$data = get_json($url); | |
if (!$data->toptags) { | |
return []; | |
} | |
return $data->toptags->tag; | |
} | |
function get_programme($pid) { | |
$url = sprintf('http://open.live.bbc.co.uk/aps/programmes/%s.json', $pid); | |
$data = get_json($url); | |
return $data->programme; | |
} | |
function get_version($pid) { | |
$url = sprintf('http://open.live.bbc.co.uk/aps/programmes/%s.json', $pid); | |
$data = get_json($url); | |
return $data->version; | |
} | |
function canonical_version($details) { | |
foreach ($details->versions as $version) { | |
if ($version->canonical) { | |
return $version->pid; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment