Last active
May 11, 2020 14:08
-
-
Save JeroenSteen/9732558da0b64877ddb0274db9a5f32d to your computer and use it in GitHub Desktop.
Get songs from Soundcloud artist
This file contains hidden or 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 | |
//https://helgesverre.com/blog/fetch-info-from-soundcloud-api/ | |
$setting_ksart_user_id = 50; | |
$setting_ksart_creator_name = "cascosongs"; | |
$setting_soundcloud_user_name = "cascosongs"; | |
$setting_soundcloud_client_id = "MyeFEFZmDltbdw3XrgBZEUGlbDpDgV5p"; | |
// build our API URL | |
$url = "http://api.soundcloud.com/resolve.json?" | |
. "url=http://soundcloud.com/" | |
. $setting_soundcloud_user_name | |
. "&client_id=".$setting_soundcloud_client_id; | |
// Grab the contents of the URL | |
$user_json = file_get_contents($url); | |
// Decode the JSON to a PHP Object | |
$user = json_decode($user_json); | |
// Print out the User ID | |
$user_id = $user->id; | |
$soundcloud_url = "http://api.soundcloud.com/users/".$user_id."/tracks.json?client_id=".$setting_soundcloud_client_id; | |
$tracks_json = file_get_contents($soundcloud_url); | |
$tracks = json_decode($tracks_json); | |
//var_dump($tracks); | |
foreach ($tracks as $track) { | |
$double_quoted_tags = []; | |
//echo $track->tag_list."<br>"; | |
preg_match_all('~\"\w+\s+\w+\"~', $track->tag_list, $matches); | |
foreach ($matches[0] as $match) { | |
$double_quoted_tags[] = trim($match, "\""); | |
$track->tag_list = preg_replace("~".$match."~", '', $track->tag_list); | |
} | |
$single_quoted_tags = explode(" ", $track->tag_list); | |
$quoted_tags = array_merge($single_quoted_tags, $double_quoted_tags); | |
$quoted_tags = implode(array_filter($quoted_tags), ","); | |
//var_dump($track); exit(); | |
// echo $track->id."<br>"; | |
// echo $track->title; | |
// echo $quoted_tags; | |
$track_title = addslashes($track->title); | |
$embed_hash = preg_replace("~https:\/\/soundcloud.com\/~", "", $track->permalink_url); | |
echo "INSERT INTO audios (allow,user_id,title,description,tags,platform_id,embed_title,embed_id,embed_hash) | |
VALUES (1,{$setting_ksart_user_id},'{$track_title}','{$track_title} door {$setting_ksart_creator_name}','{$quoted_tags}',1,NULL,NULL,'{$embed_hash}');".PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment