yt.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. <?php
  2. //$yt = new youtube();
  3. //header("Content-Type: application/json");
  4. //echo json_encode($yt->video("minecraft", null, "today", "any", "any", "live", "relevance"));
  5. class yt{
  6. public function __construct(){
  7. include "lib/backend.php";
  8. $this->backend = new backend("yt");
  9. }
  10. public function getfilters($page){
  11. if($page != "videos"){
  12. return [];
  13. }
  14. return [
  15. "date" => [
  16. "display" => "Time posted",
  17. "option" => [
  18. "any" => "Any time",
  19. "hour" => "Last hour",
  20. "today" => "Today",
  21. "week" => "This week",
  22. "month" => "This month",
  23. "year" => "This year"
  24. ]
  25. ],
  26. "type" => [
  27. "display" => "Type",
  28. "option" => [
  29. "video" => "Video",
  30. "channel" => "Channel",
  31. "playlist" => "Playlist",
  32. "Movie" => "Movie"
  33. ]
  34. ],
  35. "duration" => [
  36. "display" => "Duration",
  37. "option" => [
  38. "any" => "Any duration",
  39. "short" => "Short (>4min)",
  40. "medium" => "Medium (4-20min)",
  41. "long" => "Long (<20min)"
  42. ]
  43. ],
  44. "feature" => [
  45. "display" => "Feature",
  46. "option" => [
  47. "any" => "No features",
  48. "live" => "Live",
  49. "4k" => "4K",
  50. "hd" => "HD",
  51. "subtitles" => "Subtitles/CC",
  52. "creativecommons" => "Creative Commons",
  53. "360" => "VR 360°",
  54. "vr180" => "VR 180°",
  55. "3d" => "3D",
  56. "hdr" => "HDR"
  57. ]
  58. ],
  59. "sort" => [
  60. "display" => "Sort by",
  61. "option" => [
  62. "relevance" => "Relevance",
  63. "upload_date" => "Upload date",
  64. "view_count" => "View count",
  65. "rating" => "Rating"
  66. ]
  67. ]
  68. ];
  69. }
  70. private function ytfilter($date, $type, $duration, $feature, $sort){
  71. // ------------
  72. // INCOMPATIBLE FILTERS
  73. // channel,playlist DURATION, FEATURES, SORT BY
  74. // Movie Features=[live, subtitles, creative commons, 3d]
  75. // live, 3D
  76. // Type[channel, playlist, movie]
  77. // UPLOAD DATE, DURATION, 4k, 360, VR180, HDR
  78. // Type[channel, playlist]
  79. // -----------
  80. // MUST BE TOGETHER
  81. // Relevance,upload date Type=Video
  82. switch($type){
  83. case "channel":
  84. case "playlist":
  85. if($duration != "any"){ $duration = "any"; }
  86. if($feature != "any"){ $feature = "any"; }
  87. if($sort != "any"){ $sort = "any"; }
  88. break;
  89. case "movie":
  90. if(
  91. in_array(
  92. $feature,
  93. [
  94. "live",
  95. "subtitles",
  96. "creative_commons",
  97. "3d"
  98. ],
  99. )
  100. ){
  101. $feature = "any";
  102. }
  103. break;
  104. }
  105. switch($feature){
  106. case "live":
  107. case "3d":
  108. if(
  109. in_array(
  110. $type,
  111. [
  112. "channel",
  113. "playlist",
  114. "movie"
  115. ],
  116. )
  117. ){
  118. $type = "video";
  119. }
  120. break;
  121. }
  122. if(
  123. (
  124. $date != "any" ||
  125. $duration != "any" ||
  126. $feature == "4k" ||
  127. $feature == "360" ||
  128. $feature == "vr180" ||
  129. $feature == "hdr"
  130. ) &&
  131. (
  132. $type == "channel" ||
  133. $type == "playlist"
  134. )
  135. ){
  136. $type = "video";
  137. }
  138. if(
  139. $date == "any" &&
  140. $type == "video" &&
  141. $duration == "any" &&
  142. $feature == "any" &&
  143. $sort == "relevance"
  144. ){
  145. return null;
  146. }
  147. //print_r([$date, $type, $duration, $feature, $sort]);
  148. /*
  149. Encode hex data
  150. */
  151. // UPLOAD DATE
  152. // hour EgQIARAB 12 04 08 01 10 01
  153. // today EgQIAhAB 12 04 08 02 10 01
  154. // week EgQIAxAB 12 04 08 03 10 01
  155. // month EgQIBBAB 12 04 08 04 10 01
  156. // year EgQIBRAB 12 04 08 05 10 01
  157. // TYPE
  158. // video EgIQAQ%253D%253D 12 02 10 01
  159. // channel EgIQAg%253D%253D 12 02 10 02
  160. // playlist EgIQAw%253D%253D 12 02 10 03
  161. // movie EgIQBA%253D%253D 12 02 10 04
  162. // DURATION
  163. // -4min EgIYAQ%253D%253D 12 02 18 01
  164. // 4-20min EgIYAw%253D%253D 12 02 18 03
  165. // 20+min EgIYAg%253D%253D 12 02 18 02
  166. // FEATURE
  167. // live EgJAAQ%253D%253D 12 02 40 01
  168. // 4K EgJwAQ%253D%253D 12 02 70 01
  169. // HD EgIgAQ%253D%253D 12 02 20 01
  170. // Subtitles/CC EgIoAQ%253D%253D 12 02 28 01
  171. // Creative Commons EgIwAQ%253D%253D 12 02 30 01
  172. // 360 EgJ4AQ%253D%253D 12 02 78 01
  173. // VR180 EgPQAQE%253D 12 03 d0 01 01
  174. // 3D EgI4AQ%253D%253D 12 02 38 01
  175. // HDR EgPIAQE%253D 12 03 c8 01 01
  176. // (location & purchased unused)
  177. // SORT BY
  178. // Relevance CAASAhAB 08 00 12 02 10 01 (is nothing by default)
  179. // Upload date CAI%253D 08 02
  180. // View count CAM%253D 08 03
  181. // Rating CAE%253D 08 01
  182. // video
  183. // 12 02 10 01
  184. // under 4 minutes
  185. // 12 02 18 01
  186. // video + under 4 minutes
  187. // 12 04 10 01 18 01
  188. // video + under 4 minutes + HD
  189. // 08 00 12 06 10 01 18 01 20 01
  190. // video + under 4 minutes + upload date
  191. // 08 02 12 04 10 01 18 01
  192. // video + under 4 minutes + HD + upload date
  193. // 08 02 12 06 10 01 18 01 20 01
  194. // this year + video + under 4 minutes + HD + upload date
  195. // 08 02 12 08 08 05 10 01 18 01 20 01
  196. // this week + video + over 20 minutes + HD + view count
  197. // 08 03 12 08 08 03 10 01 18 02 20 01
  198. //echo urlencode(urlencode(base64_encode(hex2bin($str))));
  199. //echo bin2hex(base64_decode(urldecode(urldecode("CAI%253D"))));
  200. // week + video + 20min + rating
  201. // 08 01 12 06 08 03 10 01 18 02
  202. // week + video + 20min + live + rating
  203. // 08 01 12 08 08 03 10 01 18 02 40 01
  204. // live 12 02 40 01
  205. $hex = null;
  206. if(
  207. $date == "any" &&
  208. $type == "video" &&
  209. $duration == "any" &&
  210. $feature == "any" &&
  211. $sort == "relevance"
  212. ){
  213. return $hex;
  214. }
  215. $opcode = 0;
  216. if($date != "any"){ $opcode += 2; }
  217. if($type != "any"){ $opcode += 2; }
  218. if($duration != "any"){ $opcode += 2; }
  219. switch($feature){
  220. case "live":
  221. case "4k":
  222. case "hd":
  223. case "subtitles":
  224. case "creativecommons":
  225. case "360":
  226. case "3d":
  227. $opcode += 2;
  228. break;
  229. case "hdr":
  230. case "vr180":
  231. $opcode += 3;
  232. break;
  233. }
  234. switch($sort){
  235. case "relevance": $hex .= "0800"; break;
  236. case "upload_date": $hex .= "0802"; break;
  237. case "view_count": $hex .= "0803"; break;
  238. case "rating": $hex .= "0801"; break;
  239. }
  240. $hex .= "12" . "0".$opcode;
  241. switch($date){
  242. case "hour": $hex .= "0801"; break;
  243. case "today": $hex .= "0802"; break;
  244. case "week": $hex .= "0803"; break;
  245. case "month": $hex .= "0804"; break;
  246. case "year": $hex .= "0805"; break;
  247. }
  248. switch($type){
  249. case "video": $hex .= "1001"; break;
  250. case "channel": $hex .= "1002"; break;
  251. case "playlist": $hex .= "1003"; break;
  252. case "movie": $hex .= "1004"; break;
  253. }
  254. switch($duration){
  255. case "short": $hex .= "1801"; break;
  256. case "medium": $hex .= "1803"; break;
  257. case "long": $hex .= "1802"; break;
  258. }
  259. switch($feature){
  260. case "live": $hex .= "4001"; break;
  261. case "4k": $hex .= "7001"; break;
  262. case "hd": $hex .= "2001"; break;
  263. case "subtitles": $hex .= "2801"; break;
  264. case "creativecommons": $hex .= "3001"; break;
  265. case "360": $hex .= "7801"; break;
  266. case "vr180": $hex .= "d00101"; break;
  267. case "3d": $hex .= "3801"; break;
  268. case "hdr": $hex .= "c80101"; break;
  269. }
  270. //echo $hex . "\n\n";
  271. return urlencode(base64_encode(hex2bin($hex)));
  272. }
  273. // me reading youtube's json
  274. // https://imgur.com/X9hVlFX
  275. const req_web = 0;
  276. const req_xhr = 1;
  277. private function get($proxy, $url, $get = [], $reqtype = self::req_web, $continuation = null){
  278. $curlproc = curl_init();
  279. if($get !== []){
  280. $get = http_build_query($get);
  281. $url .= "?" . $get;
  282. }
  283. curl_setopt($curlproc, CURLOPT_URL, $url);
  284. switch($reqtype){
  285. case self::req_web:
  286. $headers =
  287. ["User-Agent: " . config::USER_AGENT,
  288. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  289. "Accept-Language: en-US,en;q=0.5",
  290. "Accept-Encoding: gzip",
  291. "Cookie: PREF=tz=America.New_York",
  292. "DNT: 1",
  293. "Connection: keep-alive",
  294. "Upgrade-Insecure-Requests: 1",
  295. "Sec-Fetch-Dest: document",
  296. "Sec-Fetch-Mode: navigate",
  297. "Sec-Fetch-Site: none",
  298. "Sec-Fetch-User: ?1"];
  299. break;
  300. case self::req_xhr:
  301. $headers =
  302. ["User-Agent: " . config::USER_AGENT,
  303. "Accept: */*",
  304. "Accept-Language: en-US,en;q=0.5",
  305. "Accept-Encoding: gzip",
  306. "Cookie: PREF=tz=America.New_York",
  307. "Referer: https://youtube.com.com/",
  308. "Content-Type: application/json",
  309. "Content-Length: " . strlen($continuation),
  310. "DNT: 1",
  311. "Connection: keep-alive",
  312. "Sec-Fetch-Dest: empty",
  313. "Sec-Fetch-Mode: same-origin",
  314. "Sec-Fetch-Site: same-origin"];
  315. curl_setopt($curlproc, CURLOPT_POST, true);
  316. curl_setopt($curlproc, CURLOPT_POSTFIELDS, $continuation);
  317. break;
  318. }
  319. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  320. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  321. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  322. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  323. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  324. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  325. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  326. $this->backend->assign_proxy($curlproc, $proxy);
  327. $data = curl_exec($curlproc);
  328. if(curl_errno($curlproc)){
  329. throw new Exception(curl_error($curlproc));
  330. }
  331. curl_close($curlproc);
  332. return $data;
  333. }
  334. public function video($get){
  335. $this->out = [
  336. "status" => "ok",
  337. "npt" => null,
  338. "video" => [],
  339. "author" => [],
  340. "livestream" => [],
  341. "playlist" => [],
  342. "reel" => []
  343. ];
  344. if($get["npt"]){
  345. // parse nextPage
  346. // https://www.youtube.com/youtubei/v1/search?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false
  347. /*
  348. $handle = fopen("nextpage.json", "r");
  349. $json = fread($handle, filesize("nextpage.json"));
  350. fclose($handle);*/
  351. [$npt, $proxy] =
  352. $this->backend->get(
  353. $get["npt"],
  354. "videos"
  355. );
  356. $npt = json_decode($npt, true);
  357. try{
  358. $json = $this->get(
  359. $proxy,
  360. "https://www.youtube.com/youtubei/v1/search",
  361. [
  362. "key" => $npt["key"],
  363. "prettyPrint" => "false"
  364. ],
  365. self::req_xhr,
  366. json_encode($npt["post"])
  367. );
  368. }catch(Exception $error){
  369. throw new Exception("Could not fetch results page");
  370. }
  371. $json = json_decode($json);
  372. foreach(
  373. $json
  374. ->onResponseReceivedCommands[0]
  375. ->appendContinuationItemsAction
  376. ->continuationItems[0]
  377. ->itemSectionRenderer
  378. ->contents
  379. as $video
  380. ){
  381. $this->parsevideoobject($video);
  382. }
  383. if(
  384. !isset(
  385. $json
  386. ->onResponseReceivedCommands[0]
  387. ->appendContinuationItemsAction
  388. ->continuationItems[1]
  389. ->continuationItemRenderer
  390. ->continuationEndpoint
  391. ->continuationCommand
  392. ->token
  393. )
  394. ){
  395. $npt = null;
  396. }else{
  397. // prepare nextpage for later..
  398. $npt["post"]["continuation"] =
  399. $json
  400. ->onResponseReceivedCommands[0]
  401. ->appendContinuationItemsAction
  402. ->continuationItems[1]
  403. ->continuationItemRenderer
  404. ->continuationEndpoint
  405. ->continuationCommand
  406. ->token;
  407. }
  408. $this->out["npt"] = $npt;
  409. }else{
  410. $search = $get["s"];
  411. if(strlen($search) === 0){
  412. throw new Exception("Search term is empty!");
  413. }
  414. $proxy = $this->backend->get_ip();
  415. $date = $get["date"];
  416. $type = $get["type"];
  417. $duration = $get["duration"];
  418. $feature = $get["feature"];
  419. $sort = $get["sort"];
  420. // parse ytInitialData
  421. $get = [
  422. "search_query" => $search
  423. ];
  424. if(
  425. (
  426. $filter =
  427. $this->ytfilter(
  428. $date,
  429. $type,
  430. $duration,
  431. $feature,
  432. $sort
  433. )
  434. ) !== null
  435. ){
  436. $get["sp"] = $filter;
  437. }
  438. try{
  439. $json = $this->get(
  440. $proxy,
  441. "https://www.youtube.com/results",
  442. $get
  443. );
  444. }catch(Exception $error){
  445. throw new Exception("Could not fetch results page");
  446. }
  447. /*
  448. $handle = fopen("test.html", "r");
  449. $json = fread($handle, filesize("test.html"));
  450. fclose($handle);
  451. */
  452. if(
  453. !preg_match(
  454. '/ytcfg\.set\(({".*})\); *window\.ytcfg/',
  455. $json,
  456. $ytconfig
  457. )
  458. ){
  459. throw new Exception("Could not get ytcfg");
  460. }
  461. $ytconfig = json_decode($ytconfig[1]);
  462. if(
  463. !preg_match(
  464. '/ytInitialData *= *({.*});<\/script>/',
  465. $json,
  466. $json
  467. )
  468. ){
  469. throw new Exception("Could not get ytInitialData");
  470. }
  471. $json = json_decode($json[1]);
  472. // generate POST data for nextpage
  473. $ytconfig->INNERTUBE_CONTEXT->client->screenWidthPoints = 1239;
  474. $ytconfig->INNERTUBE_CONTEXT->client->screenHeightPoints = 999;
  475. $ytconfig->INNERTUBE_CONTEXT->client->screenPixelDensity = 1;
  476. $ytconfig->INNERTUBE_CONTEXT->client->screenDensityFloat = 1;
  477. $ytconfig->INNERTUBE_CONTEXT->client->utcOffsetMinutes = -240;
  478. $ytconfig->INNERTUBE_CONTEXT->request->internalExperimentFlags = [];
  479. $ytconfig->INNERTUBE_CONTEXT->request->consistencyTokenJars = [];
  480. $ytconfig->INNERTUBE_CONTEXT->client->mainAppWebInfo = [
  481. "graftUrl" => $ytconfig->INNERTUBE_CONTEXT->client->originalUrl,
  482. "webDisplayMode" => "WEB_DISPLAY_MODE_BROWSER",
  483. "isWebNativeShareAvailable" => false
  484. ];
  485. $ytconfig->INNERTUBE_CONTEXT->adSignalsInfo = [
  486. "params" => [
  487. [
  488. "key" => "dt",
  489. "value" => (string)$ytconfig->TIME_CREATED_MS
  490. ],
  491. [
  492. "key" => "flash",
  493. "value" => "0"
  494. ],
  495. [
  496. "key" => "frm",
  497. "value" => "0"
  498. ],
  499. [
  500. "key" => "u_tz",
  501. "value" => "-240"
  502. ],
  503. [
  504. "key" => "u_his",
  505. "value" => "3"
  506. ],
  507. [
  508. "key" => "u_h",
  509. "value" => "1080"
  510. ],
  511. [
  512. "key" => "u_w",
  513. "value" => "1920"
  514. ],
  515. [
  516. "key" => "u_ah",
  517. "value" => "1080"
  518. ],
  519. [
  520. "key" => "u_cd",
  521. "value" => "24"
  522. ],
  523. [
  524. "key" => "bc",
  525. "value" => "31"
  526. ],
  527. [
  528. "key" => "bih",
  529. "value" => "999"
  530. ],
  531. [
  532. "key" => "biw",
  533. "value" => "1239"
  534. ],
  535. [
  536. "key" => "brdim",
  537. "value" => "0,0,0,0,1920,0,1920,1061,1239,999"
  538. ],
  539. [
  540. "key" => "vis",
  541. "value" => "1"
  542. ],
  543. [
  544. "key" => "wgl",
  545. "value" => "true"
  546. ],
  547. [
  548. "key" => "ca_type",
  549. "value" => "image"
  550. ]
  551. ]
  552. ];
  553. /*
  554. echo json_encode($json);
  555. die();*/
  556. // *inhales*
  557. foreach(
  558. $json
  559. ->contents
  560. ->twoColumnSearchResultsRenderer
  561. ->primaryContents
  562. ->sectionListRenderer
  563. ->contents[0]
  564. ->itemSectionRenderer
  565. ->contents
  566. as $video
  567. ){
  568. $this->parsevideoobject($video);
  569. }
  570. // get additional data from secondaryContents
  571. if(
  572. isset(
  573. $json
  574. ->contents
  575. ->twoColumnSearchResultsRenderer
  576. ->secondaryContents
  577. ->secondarySearchContainerRenderer
  578. ->contents[0]
  579. ->universalWatchCardRenderer
  580. )
  581. ){
  582. $video =
  583. $json
  584. ->contents
  585. ->twoColumnSearchResultsRenderer
  586. ->secondaryContents
  587. ->secondarySearchContainerRenderer
  588. ->contents[0]
  589. ->universalWatchCardRenderer;
  590. /*
  591. echo json_encode($video);
  592. die();*/
  593. $author =
  594. [
  595. "name" =>
  596. $video
  597. ->header
  598. ->watchCardRichHeaderRenderer
  599. ->title
  600. ->simpleText,
  601. "url" =>
  602. "https://www.youtube.com/channel/" .
  603. $video
  604. ->header
  605. ->watchCardRichHeaderRenderer
  606. ->titleNavigationEndpoint
  607. ->browseEndpoint
  608. ->browseId,
  609. "avatar" => null
  610. ];
  611. if(
  612. isset(
  613. $video
  614. ->header
  615. ->watchCardRichHeaderRenderer
  616. ->avatar
  617. ->thumbnails[0]
  618. ->url
  619. )
  620. ){
  621. $author["avatar"] =
  622. $video
  623. ->header
  624. ->watchCardRichHeaderRenderer
  625. ->avatar
  626. ->thumbnails[0]
  627. ->url;
  628. }
  629. // add video in callToAction if present
  630. if(
  631. isset(
  632. $video
  633. ->callToAction
  634. ->watchCardHeroVideoRenderer
  635. ->lengthText
  636. )
  637. ){
  638. array_push(
  639. $this->out["video"],
  640. [
  641. "title" =>
  642. $video
  643. ->callToAction
  644. ->watchCardHeroVideoRenderer
  645. ->title
  646. ->simpleText,
  647. "description" => null,
  648. "author" => $author,
  649. "date" =>
  650. $this->textualdate2unix(
  651. trim(
  652. explode(
  653. "•",
  654. $video
  655. ->callToAction
  656. ->watchCardHeroVideoRenderer
  657. ->subtitle
  658. ->simpleText
  659. )[2]
  660. )
  661. ),
  662. "duration" =>
  663. $this->hms2int(
  664. $video
  665. ->callToAction
  666. ->watchCardHeroVideoRenderer
  667. ->lengthText
  668. ->simpleText
  669. ),
  670. "views" =>
  671. $this->truncatedcount2int(
  672. trim(
  673. explode(
  674. "•",
  675. $video
  676. ->callToAction
  677. ->watchCardHeroVideoRenderer
  678. ->subtitle
  679. ->simpleText,
  680. 2
  681. )[1]
  682. )
  683. ),
  684. "thumb" => [
  685. "url" =>
  686. $video
  687. ->callToAction
  688. ->watchCardHeroVideoRenderer
  689. ->heroImage
  690. ->singleHeroImageRenderer
  691. ->thumbnail
  692. ->thumbnails[0]
  693. ->url,
  694. "ratio" => "16:9"
  695. ],
  696. "url" =>
  697. "https://www.youtube.com/watch?v=" .
  698. $video
  699. ->callToAction
  700. ->watchCardHeroVideoRenderer
  701. ->navigationEndpoint
  702. ->watchEndpoint
  703. ->videoId
  704. ]
  705. );
  706. }
  707. // get all playlists, ignore videos
  708. $out = null;
  709. foreach(
  710. $video
  711. ->sections
  712. as $section
  713. ){
  714. if(
  715. isset(
  716. $section
  717. ->watchCardSectionSequenceRenderer
  718. ->lists[0]
  719. ->horizontalCardListRenderer
  720. ->cards
  721. )
  722. ){
  723. $out =
  724. $section
  725. ->watchCardSectionSequenceRenderer
  726. ->lists[0]
  727. ->horizontalCardListRenderer
  728. ->cards;
  729. break;
  730. }
  731. }
  732. if($out !== null){
  733. foreach(
  734. $out as $video
  735. ){
  736. if(
  737. !isset(
  738. $video
  739. ->searchRefinementCardRenderer
  740. )
  741. ){
  742. continue;
  743. }
  744. $video =
  745. $video
  746. ->searchRefinementCardRenderer;
  747. array_push(
  748. $this->out["playlist"],
  749. [
  750. "title" =>
  751. $video
  752. ->query
  753. ->runs[0]
  754. ->text,
  755. "description" => null,
  756. "author" => $author,
  757. "date" => null,
  758. "duration" => null,
  759. "views" => null,
  760. "thumb" => [
  761. "url" =>
  762. $video
  763. ->thumbnail
  764. ->thumbnails[0]
  765. ->url,
  766. "ratio" => "1:1"
  767. ],
  768. "url" =>
  769. "https://www.youtube.com" .
  770. $video
  771. ->searchEndpoint
  772. ->commandMetadata
  773. ->webCommandMetadata
  774. ->url
  775. ]
  776. );
  777. }
  778. }
  779. }
  780. foreach(
  781. $json
  782. ->contents
  783. ->twoColumnSearchResultsRenderer
  784. ->primaryContents
  785. ->sectionListRenderer
  786. ->contents
  787. as $cont
  788. ){
  789. if(isset($cont->continuationItemRenderer)){
  790. $this->out["npt"] = [
  791. "key" =>
  792. $ytconfig
  793. ->INNERTUBE_API_KEY,
  794. "post" => [
  795. "context" =>
  796. $ytconfig
  797. ->INNERTUBE_CONTEXT,
  798. "continuation" =>
  799. $cont
  800. ->continuationItemRenderer
  801. ->continuationEndpoint
  802. ->continuationCommand
  803. ->token
  804. ]
  805. ];
  806. break;
  807. }
  808. }
  809. }
  810. if($this->out["npt"] !== null){
  811. $this->out["npt"] =
  812. $this->backend->store(
  813. json_encode(
  814. $this->out["npt"]
  815. ),
  816. "videos",
  817. $proxy
  818. );
  819. }
  820. return $this->out;
  821. }
  822. private function parsevideoobject($video){
  823. if(isset($video->videoRenderer)){
  824. $video = $video->videoRenderer;
  825. $description = null;
  826. if(isset($video->detailedMetadataSnippets)){
  827. foreach(
  828. $video
  829. ->detailedMetadataSnippets[0]
  830. ->snippetText
  831. ->runs
  832. as $description_part
  833. ){
  834. $description .= $description_part->text;
  835. }
  836. }
  837. if(
  838. isset(
  839. $video
  840. ->badges[0]
  841. ->metadataBadgeRenderer
  842. ->icon
  843. ->iconType
  844. ) &&
  845. $video
  846. ->badges[0]
  847. ->metadataBadgeRenderer
  848. ->icon
  849. ->iconType
  850. == "LIVE"
  851. ){
  852. $type = "livestream";
  853. $date = null;
  854. $duration = "_LIVE";
  855. if(isset($video->viewCountText->runs[0]->text)){
  856. $views =
  857. $this->views2int(
  858. $video
  859. ->viewCountText
  860. ->runs[0]
  861. ->text
  862. );
  863. }else{
  864. $views = null;
  865. }
  866. }else{
  867. $type = "video";
  868. if(isset($video->publishedTimeText->simpleText)){
  869. $date = $this->textualdate2unix(
  870. $video
  871. ->publishedTimeText
  872. ->simpleText
  873. );
  874. }else{
  875. $date = null;
  876. }
  877. if(isset($video->lengthText->simpleText)){
  878. $duration =
  879. $this->hms2int(
  880. $video
  881. ->lengthText
  882. ->simpleText
  883. );
  884. }else{
  885. $duration = null;
  886. }
  887. if(isset($video->viewCountText->simpleText)){
  888. $views =
  889. $this->views2int(
  890. $video
  891. ->viewCountText
  892. ->simpleText
  893. );
  894. }else{
  895. $views = null;
  896. }
  897. }
  898. if(
  899. $video
  900. ->navigationEndpoint
  901. ->commandMetadata
  902. ->webCommandMetadata
  903. ->webPageType
  904. == "WEB_PAGE_TYPE_SHORTS"
  905. ){
  906. // haha you thought you could get me, youtube
  907. // jokes on you i dont go outside
  908. $type = "reel";
  909. }
  910. array_push(
  911. $this->out[$type],
  912. [
  913. "title" =>
  914. $video
  915. ->title
  916. ->runs[0]
  917. ->text,
  918. "description" =>
  919. $this->titledots($description),
  920. "author" => [
  921. "name" =>
  922. $video
  923. ->longBylineText
  924. ->runs[0]
  925. ->text,
  926. "url" =>
  927. "https://www.youtube.com/channel/" .
  928. $video
  929. ->longBylineText
  930. ->runs[0]
  931. ->navigationEndpoint
  932. ->browseEndpoint
  933. ->browseId,
  934. "avatar" =>
  935. $this->checkhttpspresence(
  936. $video
  937. ->channelThumbnailSupportedRenderers
  938. ->channelThumbnailWithLinkRenderer
  939. ->thumbnail
  940. ->thumbnails[0]
  941. ->url
  942. )
  943. ],
  944. "date" => $date,
  945. "duration" => $duration,
  946. "views" => $views,
  947. "thumb" => [
  948. "url" =>
  949. $video
  950. ->thumbnail
  951. ->thumbnails[0]
  952. ->url,
  953. "ratio" => "16:9"
  954. ],
  955. "url" =>
  956. "https://www.youtube.com/watch?v=" .
  957. $video
  958. ->videoId
  959. ]
  960. );
  961. }elseif(isset($video->watchCardCompactVideoRenderer)){
  962. $video =
  963. $video
  964. ->watchCardCompactVideoRenderer;
  965. array_push(
  966. $this->out["video"],
  967. [
  968. "title" =>
  969. $video
  970. ->title
  971. ->simpleText,
  972. "description" => null,
  973. "author" => [
  974. "name" =>
  975. $video
  976. ->byline
  977. ->runs[0]
  978. ->text,
  979. "url" =>
  980. "https://www.youtube.com/channel/" .
  981. $video
  982. ->byline
  983. ->runs[0]
  984. ->navigationEndpoint
  985. ->browseEndpoint
  986. ->browseId,
  987. "avatar" => null
  988. ],
  989. "date" =>
  990. $this->textualdate2unix(
  991. trim(
  992. explode(
  993. "•",
  994. $video
  995. ->subtitle
  996. ->simpleText,
  997. 2
  998. )[1]
  999. )
  1000. ),
  1001. "duration" =>
  1002. $this->hms2int(
  1003. $video
  1004. ->lengthText
  1005. ->simpleText
  1006. ),
  1007. "views" =>
  1008. $this->truncatedcount2int(
  1009. trim(
  1010. explode(
  1011. "•",
  1012. $video
  1013. ->subtitle
  1014. ->simpleText,
  1015. 2
  1016. )[0]
  1017. )
  1018. ),
  1019. "thumb" => [
  1020. "url" =>
  1021. $video
  1022. ->thumbnail
  1023. ->thumbnails[0]
  1024. ->url,
  1025. "ratio" => "16:9"
  1026. ],
  1027. "url" =>
  1028. "https://www.youtube.com/watch?v=" .
  1029. $video
  1030. ->navigationEndpoint
  1031. ->watchEndpoint
  1032. ->videoId
  1033. ]
  1034. );
  1035. }elseif(isset($video->reelShelfRenderer)){
  1036. foreach(
  1037. $video
  1038. ->reelShelfRenderer
  1039. ->items
  1040. as $reel
  1041. ){
  1042. $reel =
  1043. $reel
  1044. ->shortsLockupViewModel;
  1045. array_push(
  1046. $this->out["reel"],
  1047. [
  1048. "title" =>
  1049. $reel
  1050. ->overlayMetadata
  1051. ->primaryText
  1052. ->content,
  1053. "description" => null,
  1054. "author" => [
  1055. "name" => null,
  1056. "url" => null,
  1057. "avatar" => null
  1058. ],
  1059. "date" => null,
  1060. "duration" => null,
  1061. "views" => null,
  1062. "thumb" => [
  1063. "url" =>
  1064. $reel
  1065. ->thumbnail
  1066. ->sources[0]
  1067. ->url,
  1068. "ratio" => "9:16"
  1069. ],
  1070. "url" =>
  1071. "https://www.youtube.com/watch?v=" .
  1072. $reel
  1073. ->onTap
  1074. ->innertubeCommand
  1075. ->reelWatchEndpoint
  1076. ->videoId
  1077. ]
  1078. );
  1079. }
  1080. }
  1081. elseif(isset($video->channelRenderer)){
  1082. $video = $video->channelRenderer;
  1083. $description = null;
  1084. if(isset($video->descriptionSnippet)){
  1085. foreach(
  1086. $video
  1087. ->descriptionSnippet
  1088. ->runs
  1089. as $description_part
  1090. ){
  1091. $description .= $description_part->text;
  1092. }
  1093. }
  1094. array_push(
  1095. $this->out["author"],
  1096. [
  1097. "title" =>
  1098. $video
  1099. ->title
  1100. ->simpleText,
  1101. "followers" =>
  1102. isset(
  1103. $video
  1104. ->videoCountText
  1105. ->simpleText
  1106. ) ?
  1107. $this->truncatedcount2int(
  1108. $video
  1109. ->videoCountText
  1110. ->simpleText
  1111. ) :
  1112. 0,
  1113. "description" => $this->titledots($description),
  1114. "thumb" =>
  1115. [
  1116. "url" =>
  1117. $this->checkhttpspresence(
  1118. $video
  1119. ->thumbnail
  1120. ->thumbnails[
  1121. count(
  1122. $video
  1123. ->thumbnail
  1124. ->thumbnails
  1125. ) - 1
  1126. ]
  1127. ->url
  1128. ),
  1129. "ratio" => "1:1"
  1130. ],
  1131. "url" =>
  1132. "https://www.youtube.com/channel/" .
  1133. $video
  1134. ->channelId
  1135. ]
  1136. );
  1137. }
  1138. elseif(isset($video->shelfRenderer)){
  1139. if(
  1140. !is_object(
  1141. $video
  1142. ->shelfRenderer
  1143. ->content
  1144. ->verticalListRenderer
  1145. )
  1146. ){
  1147. return;
  1148. }
  1149. foreach(
  1150. $video
  1151. ->shelfRenderer
  1152. ->content
  1153. ->verticalListRenderer
  1154. ->items
  1155. as $shelfvideo
  1156. ){
  1157. $this->parsevideoobject($shelfvideo);
  1158. }
  1159. }elseif(isset($video->radioRenderer)){
  1160. $video = $video->radioRenderer;
  1161. $description =
  1162. $video
  1163. ->videoCountText
  1164. ->runs[0]
  1165. ->text
  1166. . ".";
  1167. $tmp = [];
  1168. foreach(
  1169. $video->videos
  1170. as $childvideo
  1171. ){
  1172. $tmp[] =
  1173. $childvideo
  1174. ->childVideoRenderer
  1175. ->title
  1176. ->simpleText;
  1177. }
  1178. if(count($tmp) !== 0){
  1179. $description .=
  1180. " " . implode(", ", $tmp);
  1181. }
  1182. array_push(
  1183. $this->out["playlist"],
  1184. [
  1185. "title" =>
  1186. $video
  1187. ->title
  1188. ->simpleText,
  1189. "description" => $description,
  1190. "author" => [
  1191. "name" =>
  1192. $video
  1193. ->longBylineText
  1194. ->simpleText,
  1195. "url" => null,
  1196. "avatar" => null
  1197. ],
  1198. "date" => null,
  1199. "duration" => null,
  1200. "views" => null,
  1201. "thumb" => [
  1202. "url" =>
  1203. $video
  1204. ->thumbnail
  1205. ->thumbnails[
  1206. count(
  1207. $video
  1208. ->thumbnail
  1209. ->thumbnails
  1210. ) - 1
  1211. ]
  1212. ->url,
  1213. "ratio" => "16:9"
  1214. ],
  1215. "url" =>
  1216. "https://www.youtube.com/watch?v=" .
  1217. $video
  1218. ->videos[0]
  1219. ->childVideoRenderer
  1220. ->videoId .
  1221. "&list=" .
  1222. $video
  1223. ->playlistId .
  1224. "&start_radio=1"
  1225. ]
  1226. );
  1227. }elseif(isset($video->playlistRenderer)){
  1228. $video = $video->playlistRenderer;
  1229. $description = $video->videoCount . " videos.";
  1230. $tmp = [];
  1231. foreach(
  1232. $video
  1233. ->videos
  1234. as $childvideo
  1235. ){
  1236. $tmp[] =
  1237. $childvideo
  1238. ->childVideoRenderer
  1239. ->title
  1240. ->simpleText;
  1241. }
  1242. if(count($tmp) !== 0){
  1243. $description .=
  1244. " " . implode(", ", $tmp);
  1245. }
  1246. array_push(
  1247. $this->out["playlist"],
  1248. [
  1249. "title" =>
  1250. $video
  1251. ->title
  1252. ->simpleText,
  1253. "description" => $description,
  1254. "author" => [
  1255. "name" =>
  1256. $video
  1257. ->longBylineText
  1258. ->runs[0]
  1259. ->text,
  1260. "url" =>
  1261. "https://www.youtube.com/channel/" .
  1262. $video
  1263. ->longBylineText
  1264. ->runs[0]
  1265. ->navigationEndpoint
  1266. ->browseEndpoint
  1267. ->browseId,
  1268. "picture" => null
  1269. ],
  1270. "date" => null,
  1271. "duration" => null,
  1272. "views" => null,
  1273. "thumb" =>
  1274. [
  1275. "url" =>
  1276. $video
  1277. ->thumbnails[0]
  1278. ->thumbnails[
  1279. count(
  1280. $video
  1281. ->thumbnails[0]
  1282. ->thumbnails
  1283. ) - 1
  1284. ]
  1285. ->url,
  1286. "ratio" => "16:9"
  1287. ],
  1288. "url" =>
  1289. "https://www.youtube.com/watch?v=" .
  1290. $video
  1291. ->videos[0]
  1292. ->childVideoRenderer
  1293. ->videoId .
  1294. "&list=" .
  1295. $video
  1296. ->playlistId .
  1297. "&start_radio=1"
  1298. ]
  1299. );
  1300. }/*else{
  1301. if(!isset($video->searchPyvRenderer)){
  1302. echo json_encode($video);
  1303. die();}
  1304. }*/
  1305. }
  1306. private function textualdate2unix($number){
  1307. $number =
  1308. explode(
  1309. " ",
  1310. str_replace(
  1311. [
  1312. " ago",
  1313. "seconds",
  1314. "minutes",
  1315. "hours",
  1316. "days",
  1317. "weeks",
  1318. "months",
  1319. "years"
  1320. ],
  1321. [
  1322. "",
  1323. "second",
  1324. "minute",
  1325. "hour",
  1326. "day",
  1327. "week",
  1328. "month",
  1329. "year"
  1330. ],
  1331. $number
  1332. ),
  1333. 2
  1334. );
  1335. $time = 0;
  1336. switch($number[1]){
  1337. case "second":
  1338. $time = (int)$number[0];
  1339. break;
  1340. case "minute":
  1341. $time = (int)$number[0] * 60;
  1342. break;
  1343. case "hour":
  1344. $time = (int)$number[0] * 3600;
  1345. break;
  1346. case "day":
  1347. $time = (int)$number[0] * 86400;
  1348. break;
  1349. case "week":
  1350. $time = (int)$number[0] * 604800;
  1351. break;
  1352. case "month":
  1353. $time = (int)$number[0] * 2629746;
  1354. break;
  1355. case "year":
  1356. $time = (int)$number[0] * 31556952;
  1357. break;
  1358. }
  1359. return time() - $time;
  1360. }
  1361. private function checkhttpspresence($link){
  1362. if(substr($link, 0, 2) == "//"){
  1363. return "https:" . $link;
  1364. }
  1365. return $link;
  1366. }
  1367. private function textualtime2int($number){
  1368. $number = explode(" - ", $number);
  1369. if(count($number) >= 2){
  1370. $number = $number[count($number) - 2];
  1371. }else{
  1372. $number = $number[0];
  1373. }
  1374. $number =
  1375. str_replace(
  1376. [
  1377. " ",
  1378. "seconds",
  1379. "minutes",
  1380. "hours",
  1381. ],
  1382. [
  1383. "",
  1384. "second",
  1385. "minute",
  1386. "hour"
  1387. ],
  1388. $number
  1389. );
  1390. preg_match_all(
  1391. '/([0-9]+)(second|minute|hour)/',
  1392. $number,
  1393. $number
  1394. );
  1395. $time = 0;
  1396. for($i=0; $i<count($number[0]); $i++){
  1397. switch($number[2][$i]){
  1398. case "second":
  1399. $time = $time + (int)$number[1][$i];
  1400. break;
  1401. case "minute":
  1402. $time = $time + ((int)$number[1][$i] * 60);
  1403. break;
  1404. case "hour":
  1405. $time = $time + ((int)$number[1][$i] * 3600);
  1406. break;
  1407. }
  1408. }
  1409. return $time;
  1410. }
  1411. private function views2int($views){
  1412. return
  1413. (int)str_replace(
  1414. ",", "",
  1415. explode(" ", $views, 2)[0]
  1416. );
  1417. }
  1418. private function hms2int($time){
  1419. $parts = explode(":", $time, 3);
  1420. $time = 0;
  1421. if(count($parts) === 3){
  1422. // hours
  1423. $time = $time + ((int)$parts[0] * 3600);
  1424. array_shift($parts);
  1425. }
  1426. if(count($parts) === 2){
  1427. // minutes
  1428. $time = $time + ((int)$parts[0] * 60);
  1429. array_shift($parts);
  1430. }
  1431. // seconds
  1432. $time = $time + (int)$parts[0];
  1433. return $time;
  1434. }
  1435. private function truncatedcount2int($number){
  1436. // decimal should always be 1 number long
  1437. $number = explode(" ", $number, 2);
  1438. $number = $number[0];
  1439. $unit = strtolower($number[strlen($number) - 1]);
  1440. $tmp = explode(".", $number, 2);
  1441. $number = (int)$number;
  1442. if(count($tmp) === 2){
  1443. $decimal = (int)$tmp[1];
  1444. }else{
  1445. $decimal = 0;
  1446. }
  1447. switch($unit){
  1448. case "k":
  1449. $exponant = 1000;
  1450. break;
  1451. case "m":
  1452. $exponant = 1000000;
  1453. break;
  1454. case "b";
  1455. $exponant = 1000000000;
  1456. break;
  1457. default:
  1458. $exponant = 1;
  1459. break;
  1460. }
  1461. return ($number * $exponant) + ($decimal * ($exponant / 10));
  1462. }
  1463. private function titledots($title){
  1464. $substr = substr($title, -3);
  1465. if(
  1466. $substr == "..." ||
  1467. $substr == "…"
  1468. ){
  1469. return trim(substr($title, 0, -3), " \n\r\t\v\x00\0\x0B\xc2\xa0");
  1470. }
  1471. return trim($title, " \n\r\t\v\x00\0\x0B\xc2\xa0");
  1472. }
  1473. }