Subversion Repositories yt_downloader

Compare Revisions

Regard whitespace Rev 12 → Rev 13

/trunk/youtube_functions.inc.phps
1,7 → 1,7
<?php
 
// ViaThinkSoft YouTube Downloader Functions 2.1
// Revision: 2021-07-15
// Revision: 2022-02-06
// Author: Daniel Marschall <www.daniel-marschall.de>
// Licensed under the terms of the Apache 2.0 License
 
233,6 → 233,47
return preg_match('@^[a-zA-Z0-9\-_]{11}$@', $video_id);
}
 
function yt_get_channel_id_from_custom_url($custom_url) {
// TODO: is there any API possibility??? API only accepts username and id ?!
 
// https://www.youtube.com/c/SASASMR
// <link rel="canonical" href="https://www.youtube.com/channel/UCp4LfMtDfoa29kTlLnqQ5Mg">
// https://www.youtube.com/impaulsive
// <link rel="canonical" href="https://www.youtube.com/channel/UCGeBogGDZ9W3dsGx-mWQGJA">
 
$cont = file_get_contents($custom_url);
if ($cont === false) {
throw new Exception("Cannot open $custom_url using file_get_contents.");
}
if (!preg_match('@<link rel="canonical" href="https://www.youtube.com/channel/([^"]+)">@ismU', $cont, $m)) {
return false;
}
 
return $m[1];
}
 
function yt_get_channel_id_from_url($channel_url) {
$m = null;
if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/user/(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
// Username (deprecated feature. Not every channel has a username associated with it)
// https://www.youtube.com/user/USERNAME
$username = $m[2];
$channel_id = yt_get_channel_id($username);
return $channel_id;
} else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/channel/(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
// Real channel ID
// https://www.youtube.com/channel/ID
$channel_id = $m[2];
return $channel_id;
} else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(c/){0,1}(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
// Channel custom URL
// https://www.youtube.com/NAME or https://www.youtube.com/c/NAME
return yt_get_channel_id_from_custom_url($channel_url);
} else {
return false;
}
}
 
// Examples:
//yt_set_apikey(trim(file_get_contents(__DIR__ . '/.yt_api_key')));
//print_r(yt_playlist_items('PL9GbGAd-gY1pyxZJIX5MOdYdRbdweVAID'));
/trunk/ytdwn
2,7 → 2,7
<?php
 
// ViaThinkSoft YouTube Downloader Util 2.2
// Revision: 2022-02-05
// Revision: 2022-02-06
// Author: Daniel Marschall <www.daniel-marschall.de>
// Licensed under the terms of the Apache 2.0 License
//
159,8 → 159,13
} else {
echo "Trying to download 'youtube-dl' into local directory...\n";
}
if (!@file_put_contents(__DIR__.'/youtube-dl', file_get_contents('https://yt-dl.org/downloads/latest/youtube-dl'))) {
 
@chmod(__DIR__.'/youtube-dl', 0777); // otherwise we might not be able to write to it
 
if (!($binary = file_get_contents('https://yt-dl.org/latest/youtube-dl'))) {
fwrite(STDERR, "Failed to download 'youtube-dl' into local directory (file_get_contents).\n");
} else if (!@file_put_contents(__DIR__.'/youtube-dl', $binary)) {
fwrite(STDERR, "Failed to download 'youtube-dl' into local directory (file_put_contents).\n");
} else {
if (!@chmod(__DIR__.'/youtube-dl', 0544)) {
fwrite(STDERR, "Failed to download 'youtube-dl' into local directory (chmod 544).\n");
251,10 → 256,9
$channel_name = parse_quoting($channel_name);
$channel_id = yt_get_channel_id($channel_name);
if (!$channel_id) {
fwrite(STDERR, "URL $channel_name is a valid YouTube channel or username. Skipping.\n");
} else {
fwrite(STDERR, "URL $channel_name is a valid YouTube username. Will now try to interprete it as channel ID instead....\n");
}
ytdwn_channel_id($channel_id, $search);
}
} else if ($resourceType == 'curl') {
$channel_url = parse_quoting($resourceValue);
 
598,18 → 602,8
}
 
function curl_to_cid($channel_url) {
$m = null;
if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/user/(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
$username = $m[2];
$channel_id = yt_get_channel_id($username);
return $channel_id;
} else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(channel|c)/(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
$channel_id = $m[3];
return $channel_id;
} else {
return false;
return yt_get_channel_id_from_url($channel_url);
}
}
 
function in_alreadydownloaded_file($type, $video_id) {
$lines = file(_getAlreadyDownloaded());