Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.58 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use DBI;
  4. use HTTP::Tiny;
  5.  
  6. my $api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  7. my @databases = ("xxxxxxxxxxxxx", "xxxxxxxxxxx");
  8. my $http = HTTP::Tiny->new();
  9.  
  10. #globals
  11. my $response;
  12. my $do_log=0;
  13.  
  14. open(LOG,">out.log") if ($do_log);
  15.  
  16. foreach my $db (@databases) {
  17.   my $dbh = DBI->connect("DBI:mysql:database=$db;host=xxxxxxxxxxxxxxx",
  18.                           "xxxxx", "xxxxx",{'RaiseError' => 1});
  19.  
  20.   #TV Shows
  21.   my $sth = $dbh->prepare("SELECT media_id,value,type,count(media_id) AS nr, tvshow.c00 AS title FROM uniqueid, tvshow ".
  22.                           "WHERE media_type LIKE 'tvshow' AND media_id = idShow GROUP BY media_id;");
  23.   $sth->execute();
  24.  
  25.   while (my $ref = $sth->fetchrow_hashref()) {
  26.     if ($ref->{'nr'} > 1) { next; }
  27.     if ($ref->{'type'} eq 'unknown') {
  28.       print "DB $db, $ref->{'title'} (MEDIA_ID $ref->{'media_id'}):\n";
  29.       my $repeat = 1;
  30.       while ($repeat) {
  31.         print " -> asking TMDB for tvshow $ref->{'value'}\n";
  32.         my $url = "https://api.themoviedb.org/3/find/".$ref->{'value'}.
  33.           "?api_key=".$api_key."&language=en-US&external_source=tvdb_id";
  34.         $response = $http->get($url);
  35.         print LOG "$url\n$response->{content}\n" if ($do_log);
  36.         if ($response->{content} =~ /"status_code":25,/) { print " -> sleep\n"; sleep 1; }
  37.         else { $repeat = 0; }
  38.       }
  39.       if ($response->{content} =~ /"id":(\d+),"original_language"/) {
  40.         $repeat = 1;
  41.         while ($repeat) {
  42.           print " -> asking TMDB for TMDB_ID $1\n";
  43.           my $url = "https://api.themoviedb.org/3/tv/".$1."/external_ids".
  44.             "?api_key=".$api_key."&language=en-US";
  45.           $response = $http->get($url);
  46.           print LOG "$url\n$response->{content}\n" if ($do_log);
  47.           if ($response->{content} =~ /"status_code":25,/) { print " -> sleep\n"; sleep 1; }
  48.           else { $repeat = 0; }
  49.         }
  50.         if ($response->{content} =~ /"imdb_id":"(tt\d+)"/) {
  51.           print " -> putting IMDB_ID $1 to DB\n";
  52.           $dbh->do("INSERT INTO uniqueid VALUES (?, ?, ?, ?, ?)",
  53.             undef, undef, $ref->{'media_id'}, "tvshow", $1, "imdb");
  54.         }
  55.       }
  56.       print " done.\n";
  57.     }
  58.   }
  59.   $sth->finish;
  60.  
  61.   #episodes
  62.   my $sth = $dbh->prepare("SELECT media_id,value,type,count(media_id) AS nr, tvshow.c00 AS title, ".
  63.                           "episode.c00 AS eptitle FROM uniqueid, tvshow, episode ".
  64.                           "WHERE media_type LIKE 'episode' AND media_id = idEpisode ".
  65.                           "AND episode.idShow = tvshow.idShow GROUP BY media_id;");
  66.   $sth->execute();
  67.  
  68.   while (my $ref = $sth->fetchrow_hashref()) {
  69.     my ($show_id,$ep_nr,$se_nr);
  70.     if ($ref->{'nr'} > 1) { next; }
  71.     if ($ref->{'type'} eq 'unknown') {
  72.       print "DB $db, $ref->{'title'}, $ref->{'eptitle'} (MEDIA_ID $ref->{'media_id'}):\n";
  73.       my $repeat = 1;
  74.       while ($repeat) {
  75.         print " -> asking TMDB for tvshow $ref->{'value'}\n";
  76.         my $url = "https://api.themoviedb.org/3/find/".$ref->{'value'}.
  77.           "?api_key=".$api_key."&language=en-US&external_source=tvdb_id";
  78.         $response = $http->get($url);
  79.         print LOG "$url\n$response->{content}\n" if ($do_log);
  80.         if ($response->{content} =~ /"status_code":25,/) { print " -> sleep\n"; sleep 1; }
  81.         else { $repeat = 0; }
  82.       }
  83.       if ($response->{content} =~ /"show_id":(\d+),/) {
  84.         $show_id = $1;
  85.         if ($response->{content} =~ /"episode_number":(\d+),/) {
  86.           $ep_nr = $1;
  87.           if ($response->{content} =~ /"season_number":(\d+),/) {
  88.             $se_nr = $1;
  89.             $repeat = 1;
  90.             while ($repeat) {
  91.               print " -> asking TMDB for show_id $show_id season $se_nr episode $ep_nr\n";
  92.               my $url = "https://api.themoviedb.org/3/tv/".$show_id."/season/".$se_nr."/episode/".$ep_nr.
  93.                 "/external_ids?api_key=".$api_key."&language=en-US";
  94.               $response = $http->get($url);
  95.               print LOG "$url\n$response->{content}\n" if ($do_log);
  96.               if ($response->{content} =~ /"status_code":25,/) { print " -> sleep\n"; sleep 1; }
  97.               else { $repeat = 0; }
  98.             }
  99.             if ($response->{content} =~ /"imdb_id":"(tt\d+)"/) {
  100.               print " -> putting IMDB_ID $1 to DB\n";
  101.               $dbh->do("INSERT INTO uniqueid VALUES (?, ?, ?, ?, ?)",
  102.                 undef, undef, $ref->{'media_id'}, "episode", $1, "imdb");
  103.             }
  104.           }
  105.         }
  106.       }
  107.       print " done.\n";
  108.     }
  109.   }
  110.  
  111.   $sth->finish;
  112.  
  113.   $dbh->disconnect();
  114. }
  115.  
  116. close LOG if ($do_log);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement