Subversion Repositories yt_downloader

Compare Revisions

Regard whitespace Rev 13 → Rev 14

/trunk/README.md
1,5 → 1,5
 
# ViaThinkSoft YouTube Downloader Util 2.2
# ViaThinkSoft YouTube Downloader Util 2.3
 
## Syntax
 
13,6 → 13,8
[-h|--help] (shows help)
[-N|--no-mp3-tagtransfer] (disables transfer of video ID to MP3 ID tag)
(This feature requires the package "id3v2")
[-H|--checksumMode] (Which checksum files shall be written for new files.
Must be 'None', 'MD5', 'SFV', or 'MD5,SFV')
[-T|--default-template <t>] (Sets default filename template.)
(Default: '%(title)s-%(id)s.%(ext)s')
[-X|--extra-args <args>] (Additional arguments passed through)
/trunk/checksum_functions.inc.phps
0,0 → 1,120
<?php
 
// ViaThinkSoft YouTube Downloader Util 2.3
// Revision: 2022-02-06
// Author: Daniel Marschall <www.daniel-marschall.de>
// Licensed under the terms of the Apache 2.0 License
 
function cs_get_checksumfilename($file, $type='sfv') {
$dir = dirname($file);
$files = preg_grep('/\.'.preg_quote($type,'/').'$/i', glob($dir.'/*'));
 
if (count($files) > 0) {
$cs_file = array_pop($files);
} else {
$cs_file = $dir.'/'.basename(dirname($file)).'.'.$type;
}
return $cs_file;
}
 
function cs_add_automatically($file, $type='sfv') {
$cs_file = cs_get_checksumfilename($file, $type);
// echo "Checksum file: $cs_file\n";
if (strtolower($type) == 'sfv') {
if (file_exists($cs_file)) {
$files = sfv_get_files($cs_file);
if (in_arrayi(basename($file), $files)) return true;
} else {
file_put_contents($cs_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
$files = array();
}
$hash = crc32_file($file);
if ($hash === false) {
fwrite(STDERR, "Cannot calculate hash of file '$file'\n");
return false;
}
$crc32 = strtoupper($hash);
file_put_contents($cs_file, basename($file)." $crc32\r\n", FILE_APPEND);
return true;
}
else if (strtolower($type) == 'md5') {
if (file_exists($cs_file)) {
$files = md5_get_files($cs_file);
if (in_arrayi(basename($file), $files)) return true;
} else {
file_put_contents($cs_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
$files = array();
}
$hash = md5_file($file);
if ($hash === false) {
fwrite(STDERR, "Cannot calculate hash of file '$file'\n");
return false;
}
$md5 = strtolower($hash);
file_put_contents($cs_file, "$md5 *".basename($file)."\r\n", FILE_APPEND);
return true;
}
else if (strtolower($type) == 'none') {
return true;
}
else {
fwrite(STDERR, "Unknown checksum type '$type'. Must be SFV, MD5 or None\n");
return false;
}
}
 
function md5_get_files($filename) {
// Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/md5_generate.php
$out = array();
$lines = file($filename);
foreach ($lines as $line) {
$line = str_replace("\xEF\xBB\xBF",'',$line);
$line = trim($line);
if ($line == '') continue;
$line = str_replace('*', ' ', $line);
$line = str_replace("\t", ' ', $line);
list($checksum, $origname) = explode(' ', $line, 2);
$origname = dirname($filename) . '/' . trim($origname);
$checksum = trim($checksum);
$out[] = $origname;
}
 
return $out;
}
 
function sfv_get_files($filename) {
// Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php
$out = array();
$lines = file($filename);
foreach ($lines as $line) {
$line = rtrim($line);
if ($line == '') continue;
if (substr($line,0,1) == ';') continue;
$out[] = substr($line, 0, strrpos($line, ' '));
 
}
return $out;
}
 
function swapEndianness($hex) {
// Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php
return implode('', array_reverse(str_split($hex, 2)));
}
 
function crc32_file($filename, $rawOutput = false) {
// Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php
$hash = hash_file('crc32b', $filename, true);
if ($hash === false) return false;
$out = bin2hex($hash);
if (hash('crc32b', 'TEST') == 'b893eaee') {
// hash_file() in PHP 5.2 has the wrong Endianess!
// https://bugs.php.net/bug.php?id=47467
$out = swapEndianness($out);
}
return $out;
}
 
function in_arrayi($needle, $haystack) {
// Source: https://www.php.net/manual/en/function.in-array.php#89256
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
/trunk/youtube_functions.inc.phps
1,6 → 1,6
<?php
 
// ViaThinkSoft YouTube Downloader Functions 2.1
// ViaThinkSoft YouTube Downloader Functions 2.3
// Revision: 2022-02-06
// Author: Daniel Marschall <www.daniel-marschall.de>
// Licensed under the terms of the Apache 2.0 License
/trunk/ytdwn
1,7 → 1,7
#!/usr/bin/php
<?php
 
// ViaThinkSoft YouTube Downloader Util 2.2
// ViaThinkSoft YouTube Downloader Util 2.3
// Revision: 2022-02-06
// Author: Daniel Marschall <www.daniel-marschall.de>
// Licensed under the terms of the Apache 2.0 License
21,6 → 21,7
putenv("LANG=de_DE.UTF-8"); // required if video titles contain non-ASCII symbols
 
require_once __DIR__ . '/youtube_functions.inc.phps';
require_once __DIR__ . '/checksum_functions.inc.phps';
 
// Check if we are running in command line
 
39,6 → 40,7
$type = 'v:';
$outputDir = '';
$alreadyDownloaded = '';
$checksumMode = 'none';
$failList = '';
$failTreshold = 3;
$rest_args = array();
87,6 → 89,15
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$resultcache = $m[3];
} else if (preg_match('@^(/H|\-H|\-\-checksumMode)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$checksumMode = $m[3];
if ((strtolower($checksumMode) != 'none')
&& (strtolower($checksumMode) != 'sfv')
&& (strtolower($checksumMode) != 'md5')
&& (strtolower($checksumMode) != 'sfv,md5')
&& (strtolower($checksumMode) != 'md5,sfv')) syntax_error("Checksum mode needs to be either 'None', 'MD5', 'SFV', or 'MD5,SFV'.");
} else if (preg_match('@^(/T|\-T|\-\-default-template)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
488,6 → 499,7
global $default_template;
global $failTreshold;
global $cookie_file;
global $checksumMode;
 
if (DOWNLOAD_SIMULATION_MODE) {
echo "SIMULATE download of video id $video_id as ".hf_type($type)." to "._getOutputDir()."\n";
532,9 → 544,21
try {
addto_alreadydownloaded_file($type, $video_id);
} catch(Exception $e) {
fwrite(STDERR, "Cannot add to already downloaded file\n");
fwrite(STDERR, "Cannot add to 'already downloaded' file\n");
}
}
 
// Now do the checksums
foreach (explode(',',$checksumMode) as $mode) {
$test = glob(rtrim(_getOutputDir(), '/').'/*-'.$video_id.'.*');
if (count($test) == 0) {
fwrite(STDERR, "Cannot determine output file name.\n");
}
$written_file = $test[0];
if (!cs_add_automatically($written_file, $mode)) {
fwrite(STDERR, "Could not write to '$mode' checksum file!\n");
}
}
} else {
fwrite(STDERR, "Error downloading $video_id! (Code $code)\n");
if (!empty(_getFailList())) {