Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: Perl  |  size: 5.40 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. sub download_gft {
  2.  
  3.         $tid=shift;
  4.         $config=shift;
  5.  
  6.         my $gft_config=$config;
  7.         $gft_config->{paths}->{cookie_jar}='';
  8.  
  9.         #use ClientFunctions;
  10.         my $r=ClientFunctions->new('download', 'gft', $gft_config);
  11.  
  12.         ###################################
  13.         # Request page
  14.         my $eh=$r->get("http://www.thegft.org/details.php?id=".$tid);
  15.         return 0 if($eh==0);
  16. #$r->err($r->{curldat});
  17.         ###################################
  18.         # Search for nologin
  19.         my $match_nologin=qr/<form method='post' action='takelogin.php'>/ms;
  20.         my $nologin_matches=$r->match('nologin', $match_nologin);
  21.         if($nologin_matches!=0){ $r->err('      Can not continue without login, aborting!'); return 0;}
  22.  
  23.         ###################################
  24.         # Search for notorrent
  25.         my $match_notorrent=qr/No torrent with ID.<\/td><\/tr>/m;
  26.         my $notorrent_matches=$r->match('notorrent', $match_notorrent);
  27.         if($notorrent_matches!=0){ $r->err('    Can not continue without torrent, aborting!'); return 0;}
  28.  
  29.         ###################################
  30.         # Search for name
  31.         my $match_name=qr/<h2>(.*?)<\/h2>/ms;
  32.         my $name_matches=$r->match('name', $match_name);
  33.         if($name_matches==0){ $r->err(' Can not continue without name, aborting!'); return 0;}
  34.         my $name=@$name_matches[0];
  35.     $name =~ s/<br.\/>//ig;
  36.         $r->err("       Name: $name");
  37.  
  38.         ###################################
  39.         # Search for filename
  40.         my $match_filename=qr/Download<\/td><td width='99%' align='left'><a class='index' href='download.php\?torrent=(.*?)'>(.*?)<\/a><\/td><\/tr>/ms;
  41.         my $filename_matches=$r->match('filename', $match_filename);
  42.         if($filename_matches==0){ $r->err('     Can not continue without filename, aborting!'); return 0;}
  43.         my $filename=substr(@$filename_matches[1], 0, -8);
  44.         #$filename=$r->urldecode($filename);
  45.         $filename=$r->html_unescape($filename);
  46.         $r->err("       Filename: $filename");
  47.  
  48.         ###################################
  49.         # Search for category
  50.         my $match_category=qr/align='right'>Type<\/td><td valign='top' align='left'>(.*?)<\/td><\/tr>/ms;
  51.         my $category_matches=$r->match('category', $match_category);
  52.         if($category_matches==0){ $r->err('     Can not continue without category, aborting!'); return 0;}
  53.         my $category=$r->html_unescape(@$category_matches[0]);
  54.         $r->err("       Category: $category...");
  55.  
  56.         ###################################
  57.         # Search for description
  58.         my $match_description1=qr/<b>Description<\/b><\/td>\n                                           <td>(.*?)<\/td>\n/ms;
  59.         my $match_description2=qr/<b>Description<\/b><\/td>\n                                           <td>(.*?)<\/td>\n/ms;
  60.         my $description_matches=$r->match('description', $match_description1);
  61.         my $description;
  62.         if ($description_matches==0){$description_matches=$r->match('description', $match_description2);
  63.                 if($description_matches==0){ $r->err('  No description found, using default one'); $description=$config->{tuper}->{no_descr_txt}; }
  64.                 else{ $description=@$description_matches[0]; }
  65.         }
  66.         else{ $description=@$description_matches[0]; }
  67.  
  68.         ##### Destroy curl session #######
  69.         undef $r;
  70. #       delete $r;
  71.         my $r=ClientFunctions->new('download', 'gft', $gft_config);
  72.  
  73.         ###################################
  74.         # Request NFO page
  75.         $eh=$r->get("http://www.thegft.org/viewnfo.php?id=".$tid);
  76. #$r->err($r->{curldat});
  77.         ###################################
  78.         # Search for NFO
  79.         my $match_nfo=qr/<pre>(.*?)<\/pre>/ms;
  80.         my $nfo_matches=$r->match('nfo', $match_nfo);
  81.         my $nfo;
  82.         if($nfo_matches==0){ $r->err('  No nfo found, using description instead'); $nfo=$description; }
  83.         else{ $nfo=@$nfo_matches[0]; }
  84.         $nfo =~ s/^\s+//;
  85.         $nfo =~ s/\s+$//;
  86.         if($nfo eq ''){ $nfo=$description; }
  87.  
  88.         ##### Destroy curl session #######
  89.         undef $r;
  90. #       delete $r;
  91.         my $r=ClientFunctions->new('download', 'gft', $gft_config);
  92.  
  93.         ###################################
  94.         # Request torrent page
  95.         $eh=$r->get("http://www.thegft.org/download.php?torrent=".$tid);
  96.         return 0 if($eh==0);
  97.  
  98.         ###################################
  99.         # Check for bittorrent header
  100.         my $file_type=$r->curl_get_type();
  101.         if($file_type eq 0 or $file_type ne "application/x-bittorrent"){ $r->err("      Downloaded file is not bittorrent: ".$file_type); }
  102.  
  103.         ###################################
  104.         # Get infohash from downloaded file
  105.         my $down_hash = $r->get_infohash_from_curl;
  106.         if($down_hash eq 0){ $r->err('  Can not continue without infohash, aborting!'); return 0; }
  107.         $r->err('       Downloaded file infohash: '.$down_hash);
  108.         $torrent=$r->{curldat};
  109.         ###################################
  110.         # Stripping html from description, nfo and converting some of it to bbcode
  111.         if($description ne $config->{tuper}->{no_descr_txt}){ $description=$r->strip_description($description); }
  112.         if($nfo ne $config->{tuper}->{no_descr_txt}){ $nfo=$r->strip_description($nfo); }
  113.  
  114.         ###################################
  115.         # Writing files
  116.         # Write NFO file
  117.         my $nfo_file=$config->{paths}->{temp_dir}.$filename.".nfo";
  118.         if($r->write_file('nfo', $nfo_file, $nfo)==0){ return 0; }
  119.         # Wrtie description file
  120.         my $descr_file=$config->{paths}->{temp_dir}.$filename.".txt";
  121.         if($r->write_file('description', $descr_file, $description)==0){ return 0; }
  122.         # Write torrent file
  123.         my $torr_file=$config->{paths}->{temp_dir}.$filename.".torrent";
  124.         if($r->write_file('torrent', $torr_file, $torrent)==0){ return 0; }
  125.  
  126.         my %retvals=(
  127.                 "name" => $name,
  128.                 "descr" => $description,
  129.                 "filename" => $filename,
  130.                 "category" => $category,
  131.                 "nfo_file" => $nfo_file,
  132.                 "descr_file" =>  $descr_file,
  133.                 "torrent_data" => $torrent,
  134.                 "down_hash" => $down_hash,
  135.         );
  136.         return \%retvals;
  137.  
  138. }
  139.  
  140. 1;