-
Notifications
You must be signed in to change notification settings - Fork 12
/
YTPDL.php
170 lines (144 loc) · 5.01 KB
/
YTPDL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
define('YOUTUBE_API_KEY', '');
function downloadVideo($id, $dirName){
if(!is_dir($dirName) && !file_exists($dirName))
mkdir($dirName);
if(isset($id) && !empty($id)){
sleep(10);
$youtube = new YouTubeDownloader();
$links = $youtube->getDownloadLinks("https://www.youtube.com/watch?v=".$id, "mp4");
$link = $links->getFirstCombinedFormat();
if (!$link) {
die("no links..");
}
return copy($link->url, $dirName.'/'.$links->getInfo()->getTitle().'.mp4');
}
}
function getPlaylistVideos($playlistId){
if(empty(YOUTUBE_API_KEY)){
die('ERROR: YOUTUBE_API_KEY is empty!');
}
$videos = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' . $playlistId . '&key=' . YOUTUBE_API_KEY), true);
$result = array();
if (!empty($videos['items'])){
foreach ($videos['items'] as $video){
$result[] = array('title' => $video['snippet']['title'], 'id' => $video['snippet']['resourceId']['videoId']);
}
return $result;
}
return array();
}
function downloadListVideos($id, $playlistName, $mail){
$videos = getPlaylistVideos($id);
foreach ($videos as $video){
$success = downloadVideo($video['id'], $playlistName);
if(!isset($success) || !$success){
var_dump($video['title']);
file_put_contents($playlistName . '/err.log', $video['title']."\n\n", FILE_APPEND);
}
}
if(is_dir($playlistName)){
$zip = new Zipper();
$resZip = $zip->create($playlistName.".zip", $playlistName);
echo $resZip ? 'Zip Created!' : 'Zip not Created!';
$resDel = del($playlistName);
echo $resDel ? 'Dir Deleted!' : 'Dir not Deleted!';
if($resZip){
$path = $_SERVER['REQUEST_SCHEME'] ."://" . $_SERVER['SERVER_NAME'] . str_replace(basename($_SERVER['SCRIPT_FILENAME']), "", $_SERVER['SCRIPT_NAME']);
$filePath = $path . rawurlencode($playlistName) . ".zip";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: PlaylistDownloader@" . $_SERVER['SERVER_NAME'] . "\r\n";
$subject = "Your playlist " . $playlistName . " ready to download";
$body = "<h1>Your playlist " . $playlistName . " ready to download<br><br><a href='" . $filePath . "'>Click here</a><h1>";
mail($mail, $subject, $body, $headers);
}
}
else{
die('No Video...');
}
}
function del($path){
if (is_link($path)) {
return unlink($path);
} elseif (is_dir($path)) {
$objects = scandir($path);
$ok = true;
if (is_array($objects)) {
foreach ($objects as $file) {
if ($file != '.' && $file != '..') {
if (!del($path . '/' . $file)) {
$ok = false;
}
}
}
}
return ($ok) ? rmdir($path) : false;
} elseif (is_file($path)) {
return unlink($path);
}
return false;
}
class Zipper{
private $zip;
public function __construct()
{
$this->zip = new ZipArchive();
}
public function create($filename, $files)
{
$res = $this->zip->open($filename, ZipArchive::CREATE);
if ($res !== true) {
return false;
}
if (is_array($files)) {
foreach ($files as $f) {
if (!$this->addFileOrDir($f)) {
$this->zip->close();
return false;
}
}
$this->zip->close();
return true;
} else {
if ($this->addFileOrDir($files)) {
$this->zip->close();
return true;
}
return false;
}
}
private function addFileOrDir($filename)
{
if (is_file($filename)) {
return $this->zip->addFile($filename);
} elseif (is_dir($filename)) {
return $this->addDir($filename);
}
return false;
}
private function addDir($path)
{
if (!$this->zip->addEmptyDir($path)) {
return false;
}
$objects = scandir($path);
if (is_array($objects)) {
foreach ($objects as $file) {
if ($file != '.' && $file != '..') {
if (is_dir($path . '/' . $file)) {
if (!$this->addDir($path . '/' . $file)) {
return false;
}
} elseif (is_file($path . '/' . $file)) {
if (!$this->zip->addFile($path . '/' . $file)) {
return false;
}
}
}
}
return true;
}
return false;
}
}