Subversion Repositories yt_downloader

Rev

Rev 6 | Rev 8 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6 Rev 7
Line 1... Line 1...
1
#!/usr/bin/php
1
#!/usr/bin/php
2
<?php
2
<?php
3
 
3
 
4
// ViaThinkSoft YouTube Downloader Util 2.2
4
// ViaThinkSoft YouTube Downloader Util 2.2
5
// Revision: 2020-07-25
5
// Revision: 2021-05-09
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
 
11
// ------------------------------------------------------------------------------------------------
11
// ------------------------------------------------------------------------------------------------
12
 
12
 
Line 55... Line 55...
55
 
55
 
56
// Parse arguments
56
// Parse arguments
57
// We do not use getopt() at the moment, because the important functionality "optind" is only available in PHP 7.1, which is not yet distributed with most of the stable Linux distros
57
// We do not use getopt() at the moment, because the important functionality "optind" is only available in PHP 7.1, which is not yet distributed with most of the stable Linux distros
58
 
58
 
59
$init_extra_args = false;
59
$init_extra_args = false;
60
$argv_bak = $argv;
60
$argv_bak = $_SERVER['argv'];
61
array_shift($argv_bak);
61
array_shift($argv_bak);
62
while (count($argv_bak) > 0) {
62
while (count($argv_bak) > 0) {
63
	$arg = array_shift($argv_bak);
63
	$arg = array_shift($argv_bak);
64
	$arg2 = $arg . ' ' . (isset($argv_bak[0]) ? $argv_bak[0] : '');
64
	$arg2 = $arg . ' ' . (isset($argv_bak[0]) ? $argv_bak[0] : '');
-
 
65
	$m = null;
65
	if (preg_match('@^(/t|\-t|\-\-type)(\s+|=)(.*)$@s', $arg2, $m)) {
66
	if (preg_match('@^(/t|\-t|\-\-type)(\s+|=)(.*)$@s', $arg2, $m)) {
66
		array_shift($argv_bak);
67
		array_shift($argv_bak);
67
		if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
68
		if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
68
		$type = $m[3];
69
		$type = $m[3];
69
	} else if (preg_match('@^(/o|\-o|\-\-outputDir)(\s+|=)(.*)$@s', $arg2, $m)) {
70
	} else if (preg_match('@^(/o|\-o|\-\-outputDir)(\s+|=)(.*)$@s', $arg2, $m)) {
Line 223... Line 224...
223
			ytdwn_playlist_id($playlist_id);
224
			ytdwn_playlist_id($playlist_id);
224
		}
225
		}
225
	} else if ($resourceType == 'cid') {
226
	} else if ($resourceType == 'cid') {
226
		$channel_id = parse_quoting($resourceValue);
227
		$channel_id = parse_quoting($resourceValue);
227
 
228
 
-
 
229
		$m = null;
228
		if (preg_match('@\[search=(.+)\]@ismU', $channel_id, $m)) {
230
		if (preg_match('@\[search=(.+)\]@ismU', $channel_id, $m)) {
229
			$search = $m[1];
231
			$search = $m[1];
230
			$channel_id = preg_replace('@\[search=(.+)\]@ismU', '', $channel_id);
232
			$channel_id = preg_replace('@\[search=(.+)\]@ismU', '', $channel_id);
231
		} else {
233
		} else {
232
			$search = ''; // default
234
			$search = ''; // default
Line 235... Line 237...
235
 
237
 
236
		ytdwn_channel_id($channel_id, $search);
238
		ytdwn_channel_id($channel_id, $search);
237
	} else if ($resourceType == 'cname') {
239
	} else if ($resourceType == 'cname') {
238
		$channel_name = parse_quoting($resourceValue);
240
		$channel_name = parse_quoting($resourceValue);
239
 
241
 
-
 
242
		$m = null;
240
		if (preg_match('@\[search=(.+)\]@ismU', $channel_name, $m)) {
243
		if (preg_match('@\[search=(.+)\]@ismU', $channel_name, $m)) {
241
			$search = $m[1];
244
			$search = $m[1];
242
			$channel_name = preg_replace('@\[search=(.+)\]@ismU', '', $channel_name);
245
			$channel_name = preg_replace('@\[search=(.+)\]@ismU', '', $channel_name);
243
		} else {
246
		} else {
244
			$search = ''; // default
247
			$search = ''; // default
Line 253... Line 256...
253
			ytdwn_channel_id($channel_id, $search);
256
			ytdwn_channel_id($channel_id, $search);
254
		}
257
		}
255
	} else if ($resourceType == 'curl') {
258
	} else if ($resourceType == 'curl') {
256
		$channel_url = parse_quoting($resourceValue);
259
		$channel_url = parse_quoting($resourceValue);
257
 
260
 
-
 
261
		$m = null;
258
		if (preg_match('@\[search=(.+)\]@ismU', $channel_url, $m)) {
262
		if (preg_match('@\[search=(.+)\]@ismU', $channel_url, $m)) {
259
			$search = $m[1];
263
			$search = $m[1];
260
			$channel_url = preg_replace('@\[search=(.+)\]@ismU', '', $channel_url);
264
			$channel_url = preg_replace('@\[search=(.+)\]@ismU', '', $channel_url);
261
		} else {
265
		} else {
262
			$search = ''; // default
266
			$search = ''; // default
Line 272... Line 276...
272
		}
276
		}
273
	} else if ($resourceType == 'search') {
277
	} else if ($resourceType == 'search') {
274
		$searchterm = parse_quoting($resourceValue);
278
		$searchterm = parse_quoting($resourceValue);
275
 
279
 
276
		$order = '';
280
		$order = '';
-
 
281
		$m = null;
277
		if (preg_match('@\[order=(.+)\]@ismU', $searchterm, $m)) {
282
		if (preg_match('@\[order=(.+)\]@ismU', $searchterm, $m)) {
278
			$order = $m[1];
283
			$order = $m[1];
279
			$searchterm = preg_replace('@\[order=(.+)\]@ismU', '', $searchterm);
284
			$searchterm = preg_replace('@\[order=(.+)\]@ismU', '', $searchterm);
280
		} else {
285
		} else {
281
			$order = DEFAULT_SEARCH_ORDER; // default
286
			$order = DEFAULT_SEARCH_ORDER; // default
Line 532... Line 537...
532
 
537
 
533
	return true;
538
	return true;
534
}
539
}
535
 
540
 
536
function vid_to_vurl($video_id) {
541
function vid_to_vurl($video_id) {
537
	return "http://www.youtube.com/watch/?v=$video_id";
542
	return "https://www.youtube.com/watch?v=$video_id";
538
}
543
}
539
 
544
 
540
function EndsWith($Haystack, $Needle){
545
function EndsWith($Haystack, $Needle){
541
	return strrpos($Haystack, $Needle) === strlen($Haystack)-strlen($Needle);
546
	return strrpos($Haystack, $Needle) === strlen($Haystack)-strlen($Needle);
542
}
547
}
Line 556... Line 561...
556
	}
561
	}
557
 
562
 
558
	$allok = true;
563
	$allok = true;
559
	$files = glob(rtrim(_getOutputDir(), '/').'/*-???????????.mp3');
564
	$files = glob(rtrim(_getOutputDir(), '/').'/*-???????????.mp3');
560
	foreach ($files as $filename) {
565
	foreach ($files as $filename) {
-
 
566
		$m = null;
561
		if (!preg_match('@-([a-zA-Z0-9\-_]{11})\.mp3$@ismU', $filename, $m)) continue;
567
		if (!preg_match('@-([a-zA-Z0-9\-_]{11})\.mp3$@ismU', $filename, $m)) continue;
562
		$yt_id = $m[1];
568
		$yt_id = $m[1];
563
 
569
 
564
		if (!yt_check_video_id($yt_id)) continue; // just to be sure
570
		if (!yt_check_video_id($yt_id)) continue; // just to be sure
565
 
571
 
566
		$orig_ts = filemtime($filename);
572
		$orig_ts = filemtime($filename);
-
 
573
		$ec = -1;
567
		system('id3v2 -c '.escapeshellarg($yt_id).' '.escapeshellarg($filename), $ec);
574
		system('id3v2 -c '.escapeshellarg($yt_id).' '.escapeshellarg($filename), $ec);
568
		touch($filename, $orig_ts);
575
		touch($filename, $orig_ts);
569
		if ($ec != 0) {
576
		if ($ec != 0) {
570
			fwrite(STDERR, "Cannot set ID tag for file $filename\n");
577
			fwrite(STDERR, "Cannot set ID tag for file $filename\n");
571
			$allok = false;
578
			$allok = false;
Line 581... Line 588...
581
	}
588
	}
582
	return $allok;
589
	return $allok;
583
}
590
}
584
 
591
 
585
function curl_to_cid($channel_url) {
592
function curl_to_cid($channel_url) {
-
 
593
	$m = null;
586
	if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/user/(.*)(&|\\?)@ismU", $channel_url.'&', $m)) {
594
	if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/user/(.*)(&|\\?)@ismU", $channel_url.'&', $m)) {
587
		$username = $m[2];
595
		$username = $m[2];
588
		$channel_id = yt_get_channel_id($username);
596
		$channel_id = yt_get_channel_id($username);
589
		return $channel_id;
597
		return $channel_id;
590
	} else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/channel/(.*)(&|\\?)@ismU", $channel_url.'&', $m)) {
598
	} else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/channel/(.*)(&|\\?)@ismU", $channel_url.'&', $m)) {
Line 618... Line 626...
618
function _help() {
626
function _help() {
619
	global $argv;
627
	global $argv;
620
	$out = '';
628
	$out = '';
621
	$own = file_get_contents($argv[0]);
629
	$own = file_get_contents($argv[0]);
622
	$help = explode('// ----', $own, 2)[0];
630
	$help = explode('// ----', $own, 2)[0];
-
 
631
	$m = null;
623
	$help = preg_match_all('@^//(.*)$@mU', $help, $m);
632
	$help = preg_match_all('@^//(.*)$@mU', $help, $m);
624
	foreach ($m[1] as $line) {
633
	foreach ($m[1] as $line) {
625
		$out .= substr($line,1)."\n";
634
		$out .= substr($line,1)."\n";
626
	}
635
	}
627
	return $out;
636
	return $out;
Line 821... Line 830...
821
function ytdwn_register_fail($type, $video_id, $code) {
830
function ytdwn_register_fail($type, $video_id, $code) {
822
	// Note: Error code $code ist currently not used
831
	// Note: Error code $code ist currently not used
823
 
832
 
824
	$file = _getFailList();
833
	$file = _getFailList();
825
	$cont = file_get_contents($file);
834
	$cont = file_get_contents($file);
-
 
835
	$m = null;
826
	if (preg_match("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU", $cont, $m)) {
836
	if (preg_match("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU", $cont, $m)) {
827
		$cont = preg_replace("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU",
837
		$cont = preg_replace("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU",
828
		                     "Video ID $video_id failed ".($m[1]+1)." time(s) with type $type", $cont);
838
		                     "Video ID $video_id failed ".($m[1]+1)." time(s) with type $type", $cont);
829
		file_put_contents($file, $cont);
839
		file_put_contents($file, $cont);
830
	} else {
840
	} else {
Line 833... Line 843...
833
}
843
}
834
 
844
 
835
function ytdwn_fail_counter($type, $video_id) {
845
function ytdwn_fail_counter($type, $video_id) {
836
	$file = _getFailList();
846
	$file = _getFailList();
837
	$cont = file_get_contents($file);
847
	$cont = file_get_contents($file);
-
 
848
	$m = null;
838
	if (preg_match("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU", $cont, $m)) {
849
	if (preg_match("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU", $cont, $m)) {
839
		return $m[1];
850
		return $m[1];
840
	} else {
851
	} else {
841
		return 0;
852
		return 0;
842
	}
853
	}
Line 860... Line 871...
860
	curl_setopt($ch, CURLOPT_URL, 'https://yt-dl.org/downloads/latest/MD5SUMS');
871
	curl_setopt($ch, CURLOPT_URL, 'https://yt-dl.org/downloads/latest/MD5SUMS');
861
	#curl_setopt($ch, CURLOPT_HEADER, false);
872
	#curl_setopt($ch, CURLOPT_HEADER, false);
862
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
873
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
863
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
874
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
864
	$cont = curl_exec($ch);
875
	$cont = curl_exec($ch);
-
 
876
	$m = null;
865
	if (preg_match('@^(.+)  youtube\-dl$@ismU', $cont, $m)) {
877
	if (preg_match('@^(.+)  youtube\-dl$@ismU', $cont, $m)) {
866
		return $m[1];
878
		return $m[1];
867
	} else {
879
	} else {
868
		return false;
880
		return false;
869
	}
881
	}