Advertisement
tpaper

YTBack

Aug 30th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.84 KB | None | 0 0
  1. <?php
  2.     /*
  3.     YTB - YouTube Backup V0.01 Beta - 29 aug 2016
  4.    
  5.     This script was made by Enrico Ronconi <enrico.ronconi.p@gmail.com>
  6.     in aug 2016 for educational purposes only,
  7.     it comes with absolutely NO WARRANTY ;)
  8.    
  9.     This program is free software: you can redistribute it and/or modify
  10.     it under the terms of the GNU General Public License as published by
  11.     the Free Software Foundation, either version 3 of the License, or
  12.     (at your option) any later version.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program.  If not, see <http://www.gnu.org/licenses/>.  
  16.     */
  17.    
  18.     /*
  19.     No MySQL db required, instead this script saves a sort of db in json format,
  20.     the array structure associated with a video is
  21.             $array      [video-id]  -> video id
  22.                         [title]     -> Title
  23.                         [date]      -> Upload date
  24.                         [desc]      -> Description
  25.     */
  26.    
  27.    
  28.     //Settings - API
  29.     $API_key = "zeb89";
  30.     $API_channel_id = "UCq5p-pTnsbjMX3k2A2RuYRw";
  31.     $API_other_opts = "&part=snippet,id&order=date&maxResults=50";      //don't touch this
  32.     $API_url_start = "https://www.googleapis.com/youtube/v3/search";    //don't touch this - remember that ssl/https is required
  33.    
  34.     //Settings - Other
  35.     $db_file = 'YTB.dat';
  36.     $log_file = 'YTB.log';
  37.     $fold = "/large/YTB";                                               //destination folder
  38.     $verbose = FALSE;
  39.     $log_success = FALSE;                                               //If true logs event if there is no new video an there is no errors
  40.    
  41.     function new_videos($saved_db,$api_response) {                      //compares the api response with saved db to see what's new
  42.         $videos = $api_response['items'];
  43.         $existing = array();
  44.         foreach ($saved_db as $ex){
  45.             array_push($existing,$ex['id']);
  46.         }
  47.         $new_vids = array();
  48.         foreach ($videos as $video){
  49.             if ($video['id']['kind'] == 'youtube#video') {  //analyze only the videos
  50.                 if(in_array($video['id']['videoId'],$existing) == FALSE){
  51.                     unset($new_vids_tmp);
  52.                     $new_vids_tmp['id']     = $video['id']['videoId'];
  53.                     $new_vids_tmp['title']  = $video['snippet']['title'];          
  54.                     $new_vids_tmp['date']   = $video['snippet']['publishedAt'];
  55.                     $new_vids_tmp['desc']   = $video['snippet']['description'];
  56.                     array_push($new_vids,$new_vids_tmp);
  57.                 }
  58.             }
  59.         }      
  60.         return $new_vids;
  61.     }
  62.    
  63.     function say($text,$printdate=TRUE) {
  64.         global $verbose,$log_handle;
  65.         if($printdate) $text = date('c').' :: '.$text;
  66.         if ($verbose) echo $text;
  67.         fwrite($log_handle,$text);
  68.     }
  69.    
  70.     function save_video($id) {
  71.         $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
  72.         say("Downloading https://www.youtube.com/watch?v=$id...");
  73.         exec("timeout -k 1 10m youtube-dl --no-warnings -f bestvideo+bestaudio \"https://www.youtube.com/watch?v=$id\"",$dummy,$resp);
  74.         say(" Done! Exit status: $resp\n",FALSE);
  75.         return $resp;
  76.     }
  77.    
  78.     function clean_quit($status) {
  79.         global $log_handle,$lock;
  80.         if($lock) fclose($lock);
  81.         if($log_handle) fclose($log_handle);
  82.         exit($status);
  83.     }      
  84.    
  85.     //MAIN
  86.    
  87.     $log_handle = fopen($log_file,'a+');
  88.     if($log_handle == FALSE) {
  89.         die("Impossibile aprire il file di log!");
  90.     }
  91.    
  92.     $lock = fopen('/tmp/YTB.lock', 'w+');
  93.     if ((!flock($lock, LOCK_EX | LOCK_NB)) OR ($lock == false)) {
  94.         say("\n",FALSE);
  95.         say("Another instance! Terminating... \n");
  96.         exit(98);
  97.     }
  98.     if($log_success) say("\n",FALSE);
  99.    
  100.     if($log_success) say("Reading db...");
  101.     if(file_exists($db_file) == FALSE) {
  102.         file_put_contents($db_file,json_encode(array()));
  103.     }  
  104.     $saved_db = @file_get_contents($db_file);
  105.     if ($saved_db === FALSE) {
  106.         say(" Unable to read the database file! Terminating...\n");
  107.         clean_quit(1);
  108.     }
  109.     $saved_db = json_decode($saved_db, $assoc = TRUE);
  110.     if($log_success) say(" Done!\n",FALSE);
  111.    
  112.     if($log_success) say("Retriving video list... ");
  113.     $API_resp = @file_get_contents("$API_url_start?key=$API_key&channelId=$API_channel_id$API_other_opts");
  114.     if ($API_resp == FALSE) {
  115.         say(" Unable to connect to YT! Terminating...\n");
  116.         clean_quit(2);
  117.     }  
  118.     $API_resp = json_decode($API_resp, $assoc = TRUE);
  119.     if (array_key_exists("error",$API_resp)){
  120.         say(" YouTube returned error! Terminating...\n");
  121.         clean_quit(3);
  122.     }
  123.     if($log_success) say(" Done!\n",FALSE);
  124.    
  125.     $new_vids = new_videos($saved_db,$API_resp);
  126.  
  127.     if (count($new_vids) == 0) {
  128.         if($log_success) say("DB is up to date! Bye ;)\n");
  129.         clean_quit(0);
  130.     }
  131.    
  132.     if(!$log_success) say("\n",FALSE);
  133.     say("New video(s) found!\n");
  134.     $old_d = getcwd();
  135.     chdir($fold);
  136.     foreach ($new_vids as $vid){
  137.         $r = save_video($vid['id']);
  138.         if($r == 0) array_push($saved_db, $vid);
  139.     }
  140.     chdir($old_d);
  141.    
  142.     say("Saving db... ");
  143.     $saved_db = json_encode($saved_db);
  144.     $r = @file_put_contents($db_file,$saved_db);
  145.     if($r === FALSE) {
  146.         say("Error writing file! :(\n",FALSE);
  147.         clean_quit(4);
  148.     }
  149.    
  150.     say("All Done! ;)\n",FALSE);
  151.     say("Terminating...\n");
  152.     clean_quit(0);
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement