Last active
March 6, 2017 19:59
-
-
Save fjallstrom/ff07c75fa0cd5c2b32a057220f734147 to your computer and use it in GitHub Desktop.
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 | |
# 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