Rev 17 | Rev 19 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 17 | Rev 18 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #!/usr/bin/php |
1 | #!/usr/bin/php |
2 | <?php |
2 | <?php |
3 | 3 | ||
4 | // ViaThinkSoft YouTube Downloader Util 2.3.1 |
4 | // ViaThinkSoft YouTube Downloader Util 2.3.2 |
5 | // Revision: 2022-03-24 |
5 | // Revision: 2022-09-21 |
6 | // Author: Daniel Marschall <www.daniel-marschall.de> |
6 | // Author: Daniel Marschall <www.daniel-marschall.de> |
7 | // Licensed under the terms of the Apache 2.0 License |
7 | // Licensed under the terms of the Apache 2.0 License |
8 | // |
8 | // |
9 | // For syntax and other documentation, please read the file README. |
9 | // For syntax and other documentation, please read the file README. |
10 | 10 | ||
Line 343... | Line 343... | ||
343 | $out = json_decode($cont, true); |
343 | $out = json_decode($cont, true); |
344 | if ($out == NULL) $out = array(); |
344 | if ($out == NULL) $out = array(); |
345 | 345 | ||
346 | if ($use_cache) { |
346 | if ($use_cache) { |
347 | $stats = yt_get_channel_stats($channel_id); |
347 | $stats = yt_get_channel_stats($channel_id); |
348 | if ($stats === false) { |
348 | if (!$stats) { |
349 | fwrite(STDERR, "Cannot get stats for channel with ID '$channel_id'\n"); |
349 | fwrite(STDERR, "Cannot get stats for channel with ID '$channel_id'\n"); |
350 | return; |
350 | return; |
351 | } |
351 | } |
352 | $videocount = $stats['videoCount']; |
352 | $videocount = $stats['videoCount']; |
353 | 353 | ||
Line 367... | Line 367... | ||
367 | if (!empty($search)) { |
367 | if (!empty($search)) { |
368 | $out[$key]['results'] = yt_channel_items($channel_id, $search); |
368 | $out[$key]['results'] = yt_channel_items($channel_id, $search); |
369 | } else { |
369 | } else { |
370 | $out[$key]['results'] = yt_channel_items($channel_id); |
370 | $out[$key]['results'] = yt_channel_items($channel_id); |
371 | } |
371 | } |
372 | if ($out[$key]['results'] === false) { |
372 | if (!$out[$key]['results']) { |
373 | fwrite(STDERR, "Cannot get result for channel with ID '$channel_id'\n"); |
373 | fwrite(STDERR, "Cannot get result for channel with ID '$channel_id'\n"); |
374 | return; |
374 | return; |
375 | } |
375 | } |
376 | } else { |
376 | } else { |
377 | if ($verbose) echo "Video count for channel is still $videocount, keep ".count($out[$key]['results'])." results.\n"; |
377 | if ($verbose) echo "Video count for channel is still $videocount, keep ".count($out[$key]['results'])." results.\n"; |
Line 385... | Line 385... | ||
385 | fwrite(STDERR, "Cannot write result cache\n"); |
385 | fwrite(STDERR, "Cannot write result cache\n"); |
386 | } |
386 | } |
387 | 387 | ||
388 | // Now download |
388 | // Now download |
389 | 389 | ||
390 | if ($out[$key]['results'] === false) { |
390 | if (!$out[$key]['results']) { |
391 | fwrite(STDERR, "Cannot get result for channel with ID '$channel_id'\n"); |
391 | fwrite(STDERR, "Cannot get result for channel with ID '$channel_id'\n"); |
392 | return; |
392 | return; |
393 | } |
393 | } |
394 | foreach ($out[$key]['results'] as list($id, $title)) { |
394 | foreach ($out[$key]['results'] as list($id, $title)) { |
395 | if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n"; |
395 | if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n"; |
Line 410... | Line 410... | ||
410 | $out = json_decode($cont, true); |
410 | $out = json_decode($cont, true); |
411 | if ($out == NULL) $out = array(); |
411 | if ($out == NULL) $out = array(); |
412 | 412 | ||
413 | if ($use_cache) { |
413 | if ($use_cache) { |
414 | $stats = yt_get_playlist_stats($playlist_id); |
414 | $stats = yt_get_playlist_stats($playlist_id); |
415 | if ($stats === false) { |
415 | if (!$stats) { |
416 | fwrite(STDERR, "Cannot get stats for playlist with ID '$playlist_id'\n"); |
416 | fwrite(STDERR, "Cannot get stats for playlist with ID '$playlist_id'\n"); |
417 | return; |
417 | return; |
418 | } |
418 | } |
419 | $videocount = $stats['itemCount']; |
419 | $videocount = $stats['itemCount']; |
420 | 420 | ||
Line 430... | Line 430... | ||
430 | 430 | ||
431 | if ($videocount_old != $videocount) { // Attention: This might not work if videos are deleted and added (= the count stays the same) |
431 | if ($videocount_old != $videocount) { // Attention: This might not work if videos are deleted and added (= the count stays the same) |
432 | if ($verbose && $use_cache) echo "Video count changed from $videocount_old to $videocount\n"; |
432 | if ($verbose && $use_cache) echo "Video count changed from $videocount_old to $videocount\n"; |
433 | $out[$key]['count'] = $videocount; |
433 | $out[$key]['count'] = $videocount; |
434 | $out[$key]['results'] = yt_playlist_items($playlist_id); |
434 | $out[$key]['results'] = yt_playlist_items($playlist_id); |
435 | if ($out[$key]['results'] === false) { |
435 | if (!$out[$key]['results']) { |
436 | fwrite(STDERR, "Cannot get result for playlist with ID '$playlist_id'\n"); |
436 | fwrite(STDERR, "Cannot get result for playlist with ID '$playlist_id'\n"); |
437 | return; |
437 | return; |
438 | } |
438 | } |
439 | } else { |
439 | } else { |
440 | if ($verbose) echo "Video count for playlist is still $videocount, keep ".count($out[$key]['results'])." results.\n"; |
440 | if ($verbose) echo "Video count for playlist is still $videocount, keep ".count($out[$key]['results'])." results.\n"; |
Line 448... | Line 448... | ||
448 | fwrite(STDERR, "Cannot write result cache\n"); |
448 | fwrite(STDERR, "Cannot write result cache\n"); |
449 | } |
449 | } |
450 | 450 | ||
451 | // Now download |
451 | // Now download |
452 | 452 | ||
453 | if ($out[$key]['results'] === false) { |
453 | if (!$out[$key]['results']) { |
454 | fwrite(STDERR, "Cannot get result for playlist with ID '$playlist_id'\n"); |
454 | fwrite(STDERR, "Cannot get result for playlist with ID '$playlist_id'\n"); |
455 | return; |
455 | return; |
456 | } |
456 | } |
457 | foreach ($out[$key]['results'] as list($id, $title)) { |
457 | foreach ($out[$key]['results'] as list($id, $title)) { |
458 | if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n"; |
458 | if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n"; |
Line 470... | Line 470... | ||
470 | 470 | ||
471 | $results = yt_search_items($search, $order, $maxresults); |
471 | $results = yt_search_items($search, $order, $maxresults); |
472 | 472 | ||
473 | // Now download |
473 | // Now download |
474 | 474 | ||
475 | if ($results === false) { |
475 | if (!$results) { |
476 | fwrite(STDERR, "Cannot get data for search '$search'\n"); |
476 | fwrite(STDERR, "Cannot get data for search '$search'\n"); |
477 | return; |
477 | return; |
478 | } |
478 | } |
479 | foreach ($results as list($id, $title)) { |
479 | foreach ($results as list($id, $title)) { |
480 | if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n"; |
480 | if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n"; |
Line 551... | Line 551... | ||
551 | } |
551 | } |
552 | 552 | ||
553 | $written_file = $code == 0 ? ytdwn_get_downloaded_filename($outputTemplate, $video_id) : false; |
553 | $written_file = $code == 0 ? ytdwn_get_downloaded_filename($outputTemplate, $video_id) : false; |
554 | 554 | ||
555 | if (($code == 0) && ($mp3id_transfer) && (strtolower($format) == 'mp3')) { |
555 | if (($code == 0) && ($mp3id_transfer) && (strtolower($format) == 'mp3')) { |
556 | if ($written_file === false) { |
556 | if (!$written_file) { |
557 | fwrite(STDERR, "Cannot include YouTube ID to MP3, because the default template does not contain '%(id)s', or the downloaded file could not be determined for another reason.\n"); |
557 | fwrite(STDERR, "Cannot include YouTube ID to MP3, because the default template does not contain '%(id)s', or the downloaded file could not be determined for another reason.\n"); |
558 | } else { |
558 | } else { |
559 | mp3_transfer_vid_to_id($written_file, $video_id); |
559 | mp3_transfer_vid_to_id($written_file, $video_id); |
560 | } |
560 | } |
561 | } |
561 | } |
Line 565... | Line 565... | ||
565 | } |
565 | } |
566 | 566 | ||
567 | if ($code == 0) { |
567 | if ($code == 0) { |
568 | if ($verbose) { |
568 | if ($verbose) { |
569 | fwrite(STDOUT, "Successfully downloaded video ID $video_id as ".hf_type($type)."\n"); |
569 | fwrite(STDOUT, "Successfully downloaded video ID $video_id as ".hf_type($type)."\n"); |
570 | if ($written_file !== false) fwrite(STDOUT, "Output file name: $written_file\n"); |
570 | if ($written_file) fwrite(STDOUT, "Output file name: $written_file\n"); |
571 | } |
571 | } |
572 | if (!empty(_getAlreadyDownloaded())) { |
572 | if (!empty(_getAlreadyDownloaded())) { |
573 | try { |
573 | try { |
574 | addto_alreadydownloaded_file($type, $video_id); |
574 | addto_alreadydownloaded_file($type, $video_id); |
575 | } catch(Exception $e) { |
575 | } catch(Exception $e) { |
Line 578... | Line 578... | ||
578 | } |
578 | } |
579 | 579 | ||
580 | // Now do the checksums |
580 | // Now do the checksums |
581 | foreach (explode(',',$checksumMode) as $mode) { |
581 | foreach (explode(',',$checksumMode) as $mode) { |
582 | if (strtolower($mode) === 'none') continue; |
582 | if (strtolower($mode) === 'none') continue; |
583 | if ($written_file === false) { |
583 | if (!$written_file) { |
584 | fwrite(STDERR, "Cannot add to the '$mode' checksum file, because the default template does not contain '%(id)s', or the downloaded file could not be determined for another reason.\n"); |
584 | fwrite(STDERR, "Cannot add to the '$mode' checksum file, because the default template does not contain '%(id)s', or the downloaded file could not be determined for another reason.\n"); |
585 | } else if (!cs_add_automatically($written_file, $mode)) { |
585 | } else if (!cs_add_automatically($written_file, $mode)) { |
586 | fwrite(STDERR, "Could not write to '$mode' checksum file!\n"); |
586 | fwrite(STDERR, "Could not write to '$mode' checksum file!\n"); |
587 | } |
587 | } |
588 | } |
588 | } |
Line 1003... | Line 1003... | ||
1003 | $download_url = 'https://yt-dl.org/latest/youtube-dl'; |
1003 | $download_url = 'https://yt-dl.org/latest/youtube-dl'; |
1004 | } |
1004 | } |
1005 | } |
1005 | } |
1006 | } |
1006 | } |
1007 | 1007 | ||
1008 | if ($download_url !== false) { |
1008 | if ($download_url) { |
1009 | // Try to download/update the file in our directory. It should be the newest available, since YT often breaks downloader |
1009 | // Try to download/update the file in our directory. It should be the newest available, since YT often breaks downloader |
1010 | if (file_exists(__DIR__.'/'.DOWNLOAD_YT_FORK)) { |
1010 | if (file_exists(__DIR__.'/'.DOWNLOAD_YT_FORK)) { |
1011 | echo "Trying to update '".DOWNLOAD_YT_FORK."' in local directory...\n"; |
1011 | echo "Trying to update '".DOWNLOAD_YT_FORK."' in local directory...\n"; |
1012 | } else { |
1012 | } else { |
1013 | echo "Trying to download '".DOWNLOAD_YT_FORK."' into local directory...\n"; |
1013 | echo "Trying to download '".DOWNLOAD_YT_FORK."' into local directory...\n"; |