4get/scraper/sc.php

513 lines
12 KiB
PHP
Raw Normal View History

2023-09-04 02:41:44 +00:00
<?php
class sc{
public function __construct(){
2023-11-07 13:04:56 +00:00
include "lib/backend.php";
$this->backend = new backend("sc");
2024-06-29 16:55:28 +00:00
include "lib/fuckhtml.php";
$this->fuckhtml = new fuckhtml();
2023-09-04 02:41:44 +00:00
}
public function getfilters($page){
return [
"type" => [
"display" => "Type",
"option" => [
"any" => "Any type",
"track" => "Tracks",
2024-02-18 04:22:19 +00:00
"author" => "People",
2023-09-04 02:41:44 +00:00
"album" => "Albums",
"playlist" => "Playlists",
"goplus" => "Go+ Tracks"
]
]
];
}
2024-06-29 16:55:28 +00:00
private function get($proxy, $url, $get = [], $web_req = false){
2023-09-04 02:41:44 +00:00
$curlproc = curl_init();
if($get !== []){
$get = http_build_query($get);
$url .= "?" . $get;
}
curl_setopt($curlproc, CURLOPT_URL, $url);
curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
2024-06-29 16:55:28 +00:00
// use http2
curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
if($web_req === false){
curl_setopt($curlproc, CURLOPT_HTTPHEADER,
["User-Agent: " . config::USER_AGENT,
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip",
"Referer: https://soundcloud.com/",
"Origin: https://soundcloud.com",
"DNT: 1",
"Connection: keep-alive",
"Sec-Fetch-Dest: empty",
"Sec-Fetch-Mode: cors",
2024-06-29 21:58:05 +00:00
"Sec-Fetch-Site: same-site",
"Priority: u=1"]
2024-06-29 16:55:28 +00:00
);
}else{
curl_setopt($curlproc, CURLOPT_HTTPHEADER,
["User-Agent: " . config::USER_AGENT,
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip",
"DNT: 1",
"Connection: keep-alive",
"Upgrade-Insecure-Requests: 1",
"Sec-Fetch-Dest: document",
"Sec-Fetch-Mode: navigate",
"Sec-Fetch-Site: cross-site",
"Priority: u=1",
"TE: trailers"]
);
}
2023-09-04 02:41:44 +00:00
curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
2023-11-07 13:04:56 +00:00
$this->backend->assign_proxy($curlproc, $proxy);
2023-09-04 02:41:44 +00:00
$data = curl_exec($curlproc);
if(curl_errno($curlproc)){
throw new Exception(curl_error($curlproc));
}
curl_close($curlproc);
return $data;
}
2024-04-21 23:31:56 +00:00
public function music($get, $last_attempt = false){
2023-09-04 02:41:44 +00:00
if($get["npt"]){
2023-11-07 13:04:56 +00:00
[$params, $proxy] = $this->backend->get($get["npt"], "music");
2023-09-04 02:41:44 +00:00
$params = json_decode($params, true);
$url = $params["url"];
unset($params["url"]);
}else{
// normal search:
// https://api-v2.soundcloud.com/search?q=freddie%20dredd&variant_ids=&facet=model&user_id=351062-302234-707916-795081&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
// soundcloud go+ search:
// https://api-v2.soundcloud.com/search/tracks?q=freddie%20dredd&variant_ids=&filter.content_tier=SUB_HIGH_TIER&facet=genre&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
// tracks search:
// https://api-v2.soundcloud.com/search/tracks?q=freddie%20dredd&variant_ids=&facet=genre&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
// users search:
// https://api-v2.soundcloud.com/search/users?q=freddie%20dredd&variant_ids=&facet=place&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
// albums search:
// https://api-v2.soundcloud.com/search/albums?q=freddie%20dredd&variant_ids=&facet=genre&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
// playlists search:
// https://api-v2.soundcloud.com/search/playlists_without_albums?q=freddie%20dredd&variant_ids=&facet=genre&user_id=630591-269800-703400-765403&client_id=iMxZgT5mfGstBj8GWJbYMvpzelS8ne0E&limit=20&offset=0&linked_partitioning=1&app_version=1693487844&app_locale=en
$search = $get["s"];
2023-11-07 13:04:56 +00:00
if(strlen($search) === 0){
throw new Exception("Search term is empty!");
}
2023-09-04 02:41:44 +00:00
$type = $get["type"];
2023-11-07 13:04:56 +00:00
$proxy = $this->backend->get_ip();
2024-04-21 23:31:56 +00:00
$token = $this->get_token($proxy);
2023-09-04 02:41:44 +00:00
switch($type){
case "any":
$url = "https://api-v2.soundcloud.com/search";
$params = [
"q" => $search,
"variant_ids" => "",
"facet" => "model",
2024-04-21 23:31:56 +00:00
"client_id" => $token,
2023-09-04 02:41:44 +00:00
"limit" => 20,
"offset" => 0,
"linked_partitioning" => 1,
2024-04-21 23:31:56 +00:00
"app_version" => 1713542117,
2023-09-04 02:41:44 +00:00
"app_locale" => "en"
];
break;
case "track":
$url = "https://api-v2.soundcloud.com/search/tracks";
$params = [
"q" => $search,
"variant_ids" => "",
"facet_genre" => "",
2024-04-21 23:31:56 +00:00
"client_id" => $token,
2023-09-04 02:41:44 +00:00
"limit" => 20,
"offset" => 0,
"linked_partitioning" => 1,
2024-04-21 23:31:56 +00:00
"app_version" => 1713542117,
2023-09-04 02:41:44 +00:00
"app_locale" => "en"
];
break;
2024-02-18 04:22:19 +00:00
case "author":
2023-09-04 02:41:44 +00:00
$url = "https://api-v2.soundcloud.com/search/users";
$params = [
"q" => $search,
"variant_ids" => "",
"facet" => "place",
2024-04-21 23:31:56 +00:00
"client_id" => $token,
2023-09-04 02:41:44 +00:00
"limit" => 20,
"offset" => 0,
"linked_partitioning" => 1,
2024-04-21 23:31:56 +00:00
"app_version" => 1713542117,
2023-09-04 02:41:44 +00:00
"app_locale" => "en"
];
break;
case "album":
$url = "https://api-v2.soundcloud.com/search/albums";
$params = [
"q" => $search,
"variant_ids" => "",
"facet" => "genre",
2024-04-21 23:31:56 +00:00
"client_id" => $token,
2023-09-04 02:41:44 +00:00
"limit" => 20,
"offset" => 0,
"linked_partitioning" => 1,
2024-04-21 23:31:56 +00:00
"app_version" => 1713542117,
2023-09-04 02:41:44 +00:00
"app_locale" => "en"
];
break;
case "playlist":
$url = "https://api-v2.soundcloud.com/search/playlists_without_albums";
$params = [
"q" => $search,
"variant_ids" => "",
"facet" => "genre",
2024-04-21 23:31:56 +00:00
"client_id" => $token,
2023-09-04 02:41:44 +00:00
"limit" => 20,
"offset" => 0,
"linked_partitioning" => 1,
2024-04-21 23:31:56 +00:00
"app_version" => 1713542117,
2023-09-04 02:41:44 +00:00
"app_locale" => "en"
];
break;
case "goplus":
$url = "https://api-v2.soundcloud.com/search/tracks";
$params = [
"q" => $search,
"variant_ids" => "",
"filter.content_tier" => "SUB_HIGH_TIER",
"facet" => "genre",
2024-04-21 23:31:56 +00:00
"client_id" => $token,
2023-09-04 02:41:44 +00:00
"limit" => 20,
"offset" => 0,
"linked_partitioning" => 1,
2024-04-21 23:31:56 +00:00
"app_version" => 1713542117,
2023-09-04 02:41:44 +00:00
"app_locale" => "en"
];
break;
}
}
try{
2023-11-07 13:04:56 +00:00
$json = $this->get($proxy, $url, $params);
2023-09-04 02:41:44 +00:00
}catch(Exception $error){
throw new Exception("Failed to fetch JSON");
}
/*
$handle = fopen("scraper/soundcloud.json", "r");
$json = fread($handle, filesize("scraper/soundcloud.json"));
fclose($handle);
*/
$json = json_decode($json, true);
if($json === null){
2024-04-21 23:31:56 +00:00
if($last_attempt === true){
throw new Exception("Fetched an invalid token (please report!!)");
}
// token might've expired, get a new one and re-try search
2024-05-24 12:00:00 +00:00
$this->get_token($proxy);
2024-04-21 23:31:56 +00:00
return $this->music($get, true);
2023-09-04 02:41:44 +00:00
}
$out = [
"status" => "ok",
"npt" => null,
"song" => [],
"playlist" => [],
2024-02-18 04:22:19 +00:00
"album" => [],
"podcast" => [],
"author" => [],
"user" => []
2023-09-04 02:41:44 +00:00
];
/*
Get next page
*/
if(isset($json["next_href"])){
$params["query_urn"] = $json["query_urn"];
$params["offset"] = $params["offset"] + 20;
$params["url"] = $url; // we will remove this later
$out["npt"] =
2023-11-07 13:04:56 +00:00
$this->backend->store(
2023-09-04 02:41:44 +00:00
json_encode($params),
2023-11-07 13:04:56 +00:00
"music",
$proxy
2023-09-04 02:41:44 +00:00
);
}
/*
Scrape items
*/
foreach($json["collection"] as $item){
switch($item["kind"]){
case "user":
// parse author
$out["author"][] = [
"title" => $item["username"],
"followers" => $item["followers_count"],
2023-09-04 04:44:04 +00:00
"description" => trim($item["track_count"] . " songs. " . $this->limitstrlen($item["description"])),
2023-09-04 02:41:44 +00:00
"thumb" => [
"url" => $item["avatar_url"],
"ratio" => "1:1"
],
"url" => $item["permalink_url"]
];
break;
case "playlist":
// parse playlist
$description = [];
$count = 0;
foreach($item["tracks"] as $song){
$count++;
if(!isset($song["title"])){
continue;
}
$description[] = $song["title"];
}
2024-06-29 22:03:10 +00:00
if(count($description) !== 0){
2023-09-04 02:41:44 +00:00
2023-09-13 13:01:23 +00:00
$description = trim($count . " songs. " . implode(", ", $description));
2024-06-29 22:03:10 +00:00
}else{
$description = "";
2023-09-04 02:41:44 +00:00
}
if(
isset($item["artwork_url"]) &&
!empty($item["artwork_url"])
){
$thumb = [
"ratio" => "1:1",
"url" => $item["artwork_url"]
];
}elseif(
isset($item["tracks"][0]["artwork_url"]) &&
!empty($item["tracks"][0]["artwork_url"])
){
$thumb = [
"ratio" => "1:1",
"url" => $item["tracks"][0]["artwork_url"]
];
}else{
$thumb = [
"ratio" => null,
"url" => null
];
}
$out["playlist"][] = [
"title" => $item["title"],
2023-09-13 13:01:23 +00:00
"description" => $this->limitstrlen($description),
2023-09-04 02:41:44 +00:00
"author" => [
"name" => $item["user"]["username"],
"url" => $item["user"]["permalink_url"],
"avatar" => $item["user"]["avatar_url"]
],
"thumb" => $thumb,
"date" => strtotime($item["created_at"]),
"duration" => $item["duration"] / 1000,
"url" => $item["permalink_url"]
];
break;
case "track":
if(stripos($item["monetization_model"], "TIER") === false){
$stream = [
2024-02-18 04:22:19 +00:00
"endpoint" => "sc",
2023-09-04 02:41:44 +00:00
"url" =>
$item["media"]["transcodings"][0]["url"] .
2024-04-21 23:31:56 +00:00
"?client_id=" . $token .
2023-09-04 02:41:44 +00:00
"&track_authorization=" .
$item["track_authorization"]
];
}else{
$stream = [
"endpoint" => null,
"url" => null
];
}
// parse track
$out["song"][] = [
"title" => $item["title"],
"description" => $item["description"] == "" ? null : $this->limitstrlen($item["description"]),
"url" => $item["permalink_url"],
"views" => $item["playback_count"],
"author" => [
"name" => $item["user"]["username"],
"url" => $item["user"]["permalink_url"],
"avatar" => $item["user"]["avatar_url"]
],
"thumb" => [
"ratio" => "1:1",
"url" => $item["artwork_url"]
],
"date" => strtotime($item["created_at"]),
"duration" => (int)$item["full_duration"] / 1000,
"stream" => $stream
];
break;
}
}
return $out;
}
2024-04-21 23:31:56 +00:00
public function get_token($proxy){
$token = apcu_fetch("sc_token");
2024-06-29 16:55:28 +00:00
if($token !== false){
return $token;
}
// search through all javascript components on the main page
try{
$html =
$this->get(
$proxy,
"https://soundcloud.com",
2024-06-29 21:58:05 +00:00
[],
true
2024-06-29 16:55:28 +00:00
);
}catch(Exception $error){
throw new Exception("Failed to fetch front page");
}
$this->fuckhtml->load($html);
$scripts =
$this->fuckhtml
->getElementsByTagName(
"script"
);
foreach($scripts as $script){
if(
!isset($script["attributes"]["src"]) ||
strpos($script["attributes"]["src"], "sndcdn.com") === false
){
continue;
}
2024-04-21 23:31:56 +00:00
try{
$js =
$this->get(
$proxy,
2024-06-29 16:55:28 +00:00
$script["attributes"]["src"],
[]
);
}catch(Exception $error){
throw new Exception("Failed to fetch search token");
}
2024-04-21 23:31:56 +00:00
preg_match(
'/client_id=([^"]+)/',
$js,
$token
);
2024-06-29 16:55:28 +00:00
if(isset($token[1])){
2024-04-21 23:31:56 +00:00
2024-06-29 16:55:28 +00:00
apcu_store("sc_token", $token[1]);
return $token[1];
break;
2024-04-21 23:31:56 +00:00
}
}
2024-06-29 16:55:28 +00:00
throw new Exception("Did not find a Soundcloud token in the Javascript blobs");
2024-04-21 23:31:56 +00:00
}
2023-09-04 02:41:44 +00:00
private function limitstrlen($text){
return
explode(
"\n",
wordwrap(
str_replace(
2023-09-13 13:01:23 +00:00
["\n\r", "\r\n", "\n", "\r"],
2023-09-04 02:41:44 +00:00
" ",
$text
),
300,
"\n"
2023-09-13 13:01:23 +00:00
),
2
2023-09-04 02:41:44 +00:00
)[0];
}
}