Skip to content

Instantly share code, notes, and snippets.

@fjallstrom
Last active March 6, 2017 19:59
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 fjallstrom/ff07c75fa0cd5c2b32a057220f734147 to your computer and use it in GitHub Desktop.
Save fjallstrom/ff07c75fa0cd5c2b32a057220f734147 to your computer and use it in GitHub Desktop.
<?php
# fetch and cache artist id by doing a search
$spotresults = @json_decode(shell_exec('curl "https://api.spotify.com/v1/search?q='.$artistNameFromScrape.'&type=artist" -H "Accept: application/json"'));
$artistmeta = json_encode($spotresults->artists->items[0]);
$artistid = (string)$spotresults->artists->items[0]->id;
$artistname = (string)$spotresults->artists->items[0]->name;
$result = $db->prepare("INSERT INTO artists(artist, url, parent_artist, json) VALUES(?,?,?,?)");
$result->execute(array($artistname, $val, 0, $artistmeta));
$insert_id = $db->lastInsertId();
# fetch and cache related artists
if($artistid){
$spotresults = @json_decode(shell_exec('curl "https://api.spotify.com/v1/artists/'.$artistid.'/related-artists" -H Accept: application/json'));
if($spotresults->artists){
foreach($spotresults->artists as $currartist){
$result = $db->prepare("INSERT INTO artists(artist, url, parent_artist, json) VALUES(?,?,?,?)");
$result->execute(array($currartist->name, '', $insert_id, json_encode($currartist)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment