Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- YTB - YouTube Backup V0.01 Beta - 29 aug 2016
- This script was made by Enrico Ronconi <enrico.ronconi.p@gmail.com>
- in aug 2016 for educational purposes only,
- it comes with absolutely NO WARRANTY ;)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /*
- No MySQL db required, instead this script saves a sort of db in json format,
- the array structure associated with a video is
- $array [video-id] -> video id
- [title] -> Title
- [date] -> Upload date
- [desc] -> Description
- */
- //Settings - API
- $API_key = "zeb89";
- $API_channel_id = "UCq5p-pTnsbjMX3k2A2RuYRw";
- $API_other_opts = "&part=snippet,id&order=date&maxResults=50"; //don't touch this
- $API_url_start = "https://www.googleapis.com/youtube/v3/search"; //don't touch this - remember that ssl/https is required
- //Settings - Other
- $db_file = 'YTB.dat';
- $log_file = 'YTB.log';
- $fold = "/large/YTB"; //destination folder
- $verbose = FALSE;
- $log_success = FALSE; //If true logs event if there is no new video an there is no errors
- function new_videos($saved_db,$api_response) { //compares the api response with saved db to see what's new
- $videos = $api_response['items'];
- $existing = array();
- foreach ($saved_db as $ex){
- array_push($existing,$ex['id']);
- }
- $new_vids = array();
- foreach ($videos as $video){
- if ($video['id']['kind'] == 'youtube#video') { //analyze only the videos
- if(in_array($video['id']['videoId'],$existing) == FALSE){
- unset($new_vids_tmp);
- $new_vids_tmp['id'] = $video['id']['videoId'];
- $new_vids_tmp['title'] = $video['snippet']['title'];
- $new_vids_tmp['date'] = $video['snippet']['publishedAt'];
- $new_vids_tmp['desc'] = $video['snippet']['description'];
- array_push($new_vids,$new_vids_tmp);
- }
- }
- }
- return $new_vids;
- }
- function say($text,$printdate=TRUE) {
- global $verbose,$log_handle;
- if($printdate) $text = date('c').' :: '.$text;
- if ($verbose) echo $text;
- fwrite($log_handle,$text);
- }
- function save_video($id) {
- $id = preg_replace("/[^A-Za-z0-9 _-]/", '', $id); //just to be safe. I don't want that someone somehow let me get an id like '; rm -rf /*' LEL
- say("Downloading https://www.youtube.com/watch?v=$id...");
- exec("timeout -k 1 10m youtube-dl --no-warnings -f bestvideo+bestaudio \"https://www.youtube.com/watch?v=$id\"",$dummy,$resp);
- say(" Done! Exit status: $resp\n",FALSE);
- return $resp;
- }
- function clean_quit($status) {
- global $log_handle,$lock;
- if($lock) fclose($lock);
- if($log_handle) fclose($log_handle);
- exit($status);
- }
- //MAIN
- $log_handle = fopen($log_file,'a+');
- if($log_handle == FALSE) {
- die("Impossibile aprire il file di log!");
- }
- $lock = fopen('/tmp/YTB.lock', 'w+');
- if ((!flock($lock, LOCK_EX | LOCK_NB)) OR ($lock == false)) {
- say("\n",FALSE);
- say("Another instance! Terminating... \n");
- exit(98);
- }
- if($log_success) say("\n",FALSE);
- if($log_success) say("Reading db...");
- if(file_exists($db_file) == FALSE) {
- file_put_contents($db_file,json_encode(array()));
- }
- $saved_db = @file_get_contents($db_file);
- if ($saved_db === FALSE) {
- say(" Unable to read the database file! Terminating...\n");
- clean_quit(1);
- }
- $saved_db = json_decode($saved_db, $assoc = TRUE);
- if($log_success) say(" Done!\n",FALSE);
- if($log_success) say("Retriving video list... ");
- $API_resp = @file_get_contents("$API_url_start?key=$API_key&channelId=$API_channel_id$API_other_opts");
- if ($API_resp == FALSE) {
- say(" Unable to connect to YT! Terminating...\n");
- clean_quit(2);
- }
- $API_resp = json_decode($API_resp, $assoc = TRUE);
- if (array_key_exists("error",$API_resp)){
- say(" YouTube returned error! Terminating...\n");
- clean_quit(3);
- }
- if($log_success) say(" Done!\n",FALSE);
- $new_vids = new_videos($saved_db,$API_resp);
- if (count($new_vids) == 0) {
- if($log_success) say("DB is up to date! Bye ;)\n");
- clean_quit(0);
- }
- if(!$log_success) say("\n",FALSE);
- say("New video(s) found!\n");
- $old_d = getcwd();
- chdir($fold);
- foreach ($new_vids as $vid){
- $r = save_video($vid['id']);
- if($r == 0) array_push($saved_db, $vid);
- }
- chdir($old_d);
- say("Saving db... ");
- $saved_db = json_encode($saved_db);
- $r = @file_put_contents($db_file,$saved_db);
- if($r === FALSE) {
- say("Error writing file! :(\n",FALSE);
- clean_quit(4);
- }
- say("All Done! ;)\n",FALSE);
- say("Terminating...\n");
- clean_quit(0);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement