Subversion Repositories yt_downloader

Rev

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

Rev 2 Rev 5
Line 1... Line 1...
1
#!/usr/bin/php
1
#!/usr/bin/php
2
<?php
2
<?php
3
 
3
 
4
// ViaThinkSoft YouTube Downloader Util 2.1
4
// ViaThinkSoft YouTube Downloader Util 2.1.1
5
// Revision: 2018-04-16
5
// Revision: 2019-08-05
6
// Author: Daniel Marschall <www.daniel-marschall.de>
6
// Author: Daniel Marschall <www.daniel-marschall.de>
7
// Licensed under the terms of GPLv3
7
// Licensed under the terms of the Apache 2.0 license
8
//
8
//
9
// Syntax:
9
// Syntax:
10
// ./ytdwn [-t|--type v:[ext]|a:[ext]] (default v:)
10
// ./ytdwn [-t|--type v:[ext]|a:[ext]] (default v:)
11
//         [-o|--outputDir <dir>]      (default current working directory)
11
//         [-o|--outputDir <dir>]      (default current working directory)
12
//         [-a|--alreadyDownloaded <file>]
12
//         [-a|--alreadyDownloaded <file>]
Line 199... Line 199...
199
 
199
 
200
if (count($rest_args) == 0) syntax_error("Please enter at least one desired video for downloading");
200
if (count($rest_args) == 0) syntax_error("Please enter at least one desired video for downloading");
201
 
201
 
202
if ($failTreshold <= 0) syntax_error("Fail treshold has invalid value. Must be >0.");
202
if ($failTreshold <= 0) syntax_error("Fail treshold has invalid value. Must be >0.");
203
 
203
 
204
// Try to install youtube-dl, if it does not exist
204
// Try to download/update youtube-dl into local directory
205
 
205
 
-
 
206
$newest_version_md5 = get_latest_ytdl_md5sum();
206
if (command_exists(__DIR__.'/youtube-dl')) {
207
if (!$newest_version_md5) {
-
 
208
	fwrite(STDERR, "Failed to get MD5 sum of latest version of 'youtube-dl' from GitHub. Will not try to download/update 'youtube-dl' into local directory.\n");
-
 
209
} else {
-
 
210
	if (!file_exists(__DIR__.'/youtube-dl') || ($newest_version_md5 != md5_file(__DIR__.'/youtube-dl'))) {
-
 
211
		// Try to download/update the file in our directory. It should be the newest available, since YT often breaks downloader
207
	define('YTDL_EXE', __DIR__.'/youtube-dl');
212
		if (file_exists(__DIR__.'/youtube-dl')) {
208
} else if (command_exists('youtube-dl')) {
213
			echo "Trying to update 'youtube-dl' in local directory...\n";
-
 
214
		} else {
209
	define('YTDL_EXE', 'youtube-dl');
215
			echo "Trying to download 'youtube-dl' into local directory...\n";
-
 
216
		}
-
 
217
		if (!@file_put_contents(__DIR__.'/youtube-dl', file_get_contents('https://yt-dl.org/downloads/latest/youtube-dl'))) {
-
 
218
			fwrite(STDERR, "Failed to download 'youtube-dl' into local directory (file_get_contents).\n");
210
} else {
219
		} else {
211
	@file_put_contents(__DIR__.'/youtube-dl', file_get_contents('https://yt-dl.org/downloads/latest/youtube-dl'));
-
 
212
	@chmod(__DIR__.'/youtube-dl', 0544);
220
			if (!@chmod(__DIR__.'/youtube-dl', 0544)) {
-
 
221
				fwrite(STDERR, "Failed to download 'youtube-dl' into local directory (chmod 544).\n");
-
 
222
				@unlink(__DIR__.'/youtube-dl'); // try to delete, otherwise we might try to execute a non-executable file
-
 
223
			}
-
 
224
		}
-
 
225
	}
-
 
226
}
-
 
227
 
213
	if (command_exists(__DIR__.'/youtube-dl')) {
228
if (command_exists(__DIR__.'/youtube-dl')) {
-
 
229
	echo "Will use 'youtube-dl' from local directory\n";
214
		define('YTDL_EXE', __DIR__.'/youtube-dl');
230
	define('YTDL_EXE', __DIR__.'/youtube-dl');
215
	} else {
231
} else {
-
 
232
	// Download failed. Is at least a package installed?
-
 
233
	if (command_exists('youtube-dl')) {
-
 
234
		echo "Will use 'youtube-dl' from Linux package\n";
-
 
235
		define('YTDL_EXE', 'youtube-dl');
-
 
236
	} else {
216
		fwrite(STDERR, "This script requires the tool/package 'youtube-dl'. Please install it first.\n");
237
		fwrite(STDERR, "This script requires the tool/package 'youtube-dl'. Please install it first.\n");
217
		exit(1);
238
		exit(1);
218
	}
239
	}
219
}
240
}
220
 
241
 
Line 874... Line 895...
874
		$failcnt++;
895
		$failcnt++;
875
		$dest_neu = "$namewoext ($failcnt)$ext";
896
		$dest_neu = "$namewoext ($failcnt)$ext";
876
	}
897
	}
877
	return rename($src, $dest_neu);
898
	return rename($src, $dest_neu);
878
}
899
}
-
 
900
 
-
 
901
function get_latest_ytdl_md5sum() {
-
 
902
	$ch = curl_init();
-
 
903
	curl_setopt($ch, CURLOPT_URL, 'https://yt-dl.org/downloads/latest/MD5SUMS');
-
 
904
	#curl_setopt($ch, CURLOPT_HEADER, false);
-
 
905
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
-
 
906
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
 
907
	$cont = curl_exec($ch);
-
 
908
	if (preg_match('@^(.+)  youtube\-dl$@ismU', $cont, $m)) {
-
 
909
		return $m[1];
-
 
910
	} else {
-
 
911
		return false;
-
 
912
	}
-
 
913
}