/
download.php
175 lines (144 loc) · 6.09 KB
/
download.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
171
172
173
174
175
<?php
set_time_limit(0);
error_reporting(0);
include('PHP-Snippets/include_functions.php');
$API_KEY = file_get_contents('API_KEY.txt');
//Converts youtubes time format
function covtime($time, $format){
$ret = new DateTime('@0');
$ret->add(new DateInterval($time));
return $ret->format($format);
}
//Zip folder
function zip_folder($folder, $zipFile){
$root = realpath($folder);
$zip = new ZipArchive();
$zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root), RecursiveIteratorIterator::LEAVES_ONLY);
foreach($files as $name => $file){
if(!$file->isDir()){
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($root) + 1 );
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
}
//Delete directory with files recursivly
function delete_dir($dirPath) {
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
delete_dir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$badCharacters = array_merge(
array_map('chr', range(0,31)),
array("<", ">", ":", '"', "/", "\\", "|", "?", "*"));
$scroll = '<script>window.scrollTo(0,document.documentElement.clientHeight)</script>';
?>
<html><head><style>body{ color: #0F0; background: #000; }</style></head><body>
<?php
if(isset($_POST['ch'])){
$username = $_POST['ch'];
$user = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername='.$username.'&key='.$API_KEY, false, stream_context_create($arrContextOptions)),true);
$json = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.$user['items'][0]['contentDetails']['relatedPlaylists']['uploads'].'&key='.$API_KEY.'&maxResults=50', false, stream_context_create($arrContextOptions)),true);
$total = $json['pageInfo']['totalResults'].' (estimate)';
$loaded = count($json['items']);
$videos = array();
if($loaded < 50){
$total = $loaded;
}
echo 'Number of videos: '.$total .'<br/> <br/>'.$scroll;
$i=0;
//Get all videos from a channel
while(isset($json['nextPageToken'])){
if($i==0){ echo 'Loading videos... '; }else{ echo 'Loading more videos... '; }
echo $scroll;
echo '( '.$loaded.' )<br/>'.$scroll;
ob_flush(); flush();
$videos = array_merge($videos, $json['items']);
$json = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.$user['items'][0]['contentDetails']['relatedPlaylists']['uploads'].'&key='.$API_KEY.'&maxResults=50&pageToken='.$json['nextPageToken'], false, stream_context_create($arrContextOptions)),true);
$loaded = $loaded + count($json['items']);
$i++;
}
$videos = array_reverse(array_merge($videos, $json['items']));
echo 'Loaded '.count($videos).' videos in total. Starting generation...<br/>'.$scroll;
if($_POST['reset'] == '1'){ delete_dir('files/'.$username); echo 'Deleted old files on server<br/>'.$scroll; ob_flush(); flush(); } //Delete old versions before downloading
if(!file_exists('files/'.$username)){ mkdir('files/'.$username, 0777, true); } //Create folder if it doesn't exist
if(!file_exists('zip')){ mkdir('zip', 0777, true); } //Create folder if it doesn't exist
$yearCheck = '';
foreach($videos as $video){
$API_DIRECTLINK = $_POST['api_url'];
$print = '';
//Video info
$title = $video['snippet']['title'];
$title = str_replace($badCharacters, '', $title);
$videoId = $video['snippet']['resourceId']['videoId'];
$date = explode('T', $video['snippet']['publishedAt'])[0];
$year = explode('-', $date)[0];
$img = $video['snippet']['thumbnails']['standard']['url'];
if(!isset($video['snippet']['thumbnails']['standard'])){ $img = $video['snippet']['thumbnails']['medium']['url']; } //If standard image is not availiable use medium
$channel = $video['snippet']['channelTitle'];
$info = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/videos?id='.$videoId.'&part=contentDetails&key='.$API_KEY, false, stream_context_create($arrContextOptions)),true);
$duration = $info['items'][0]['contentDetails']['duration'];
if($year != $yearCheck){ $yearCheck = $year; $j=1; }else{ $j++; }
if($j>99){ $j = sprintf('%03d', $j); }else{ $j = sprintf('%02d', $j); }
//The nfo files
$xml = '
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<episodedetails>
<title>'.$title.'</title>
<year>'.$year.'</year>
<aired>'.$date.'</aired>
<runtime>'.covtime($duration, 's').'</runtime>
<season>'.$year.'</season>
<episode>'.$j.'</episode>
<fileinfo>
<streamdetails>
<video>
<duration>'.covtime($duration, 's').'</duration>
<durationinseconds>'.covtime($duration, 's').'</durationinseconds>
</video>
</streamdetails>
</fileinfo>
</episodedetails>
';
$folder = 'files/'.$username.'/'.$channel.'/'.$year;
$filename = $folder.'/[S'.$year.'E'.$j.'] '.$title;
if(!file_exists($filename.'.strm')){
if(!file_exists($folder)){ mkdir($folder, 0777, true); } //Create folder if it doesn't exist
file_write($filename.'.strm', $API_DIRECTLINK.$videoId);
file_write($filename.'.nfo', $xml);
file_put_contents($filename.'-thumb.jpg', file_get_contents($img, false, stream_context_create($arrContextOptions)));
$print .= 'Added: ';
}else{ $print .= 'Skipped: '; }
$print .= '[S'.$year.'E'.$j.'] '.$title.'<br/>';
$print .= $scroll;
echo $print;
ob_flush();
flush();
}//end foreach videos
echo 'Zipping it up<br/>'.$scroll;
zip_folder('files/'.$username, 'zip/'.$username.'.zip');
echo 'Redirect to download...<br/>'.$scroll.'
<script>
window.location.href="zip/'.$username.'.zip";
</script>
';
}
?>
</body></html>