m4ly

DS_IMG_DOWNLOADER.pl

May 3rd, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.60 KB | None | 0 0
  1. #!/usr/local/bin/perl
  2.  
  3. # Author: Dawid Mocek
  4. # PP Projekt
  5. # For educational purpose only
  6. # All rights reserved
  7.  
  8. use strict;
  9. use warnings;
  10.  
  11. use IO::File;
  12. use File::Path qw( make_path );
  13. use LWP::Simple;
  14. use LWP::Parallel;
  15. use LWP::Parallel::UserAgent qw(:CALLBACK);
  16. use HTTP::Request;
  17. use Data::Dumper;
  18. use Config::IniFiles;
  19. use DBI;
  20.  
  21. sub newdir {
  22.     my $dir = $_[0];
  23.     if( ! -d $dir) {
  24.     make_path($dir, {verbose => 1}) or die("Failed to create directory: " . $dir);
  25.     }
  26. }
  27.  
  28. sub mynewdir {
  29.  
  30.     my $dir = $_[0];
  31.     if( ! -d $dir) {
  32.     print "mkdir: " , $dir , "\n";
  33.     mkdir($dir);
  34.     }
  35.    
  36. }
  37.  
  38. sub prepare_http_req {
  39.     my $req = HTTP::Request->new('GET' =>  $_[0]);
  40.     $req->header('User-Agent' => $_[1]);
  41.     # 'Accept-Encoding' =>  'gzip, deflate');
  42.     return $req;
  43. }
  44.  
  45. # sub handle_http_resp {
  46. #    my($content, $response, $proto, $entry) = @_;
  47. #    print "Handling answer from " . $response->request->url . "\n";
  48. #    if(length($content)) {
  49. #   $response->add_content($content);
  50. #    }
  51. #    else {
  52. #  
  53. #   return C_ENDCON;
  54. #    }
  55. #  
  56. #}
  57.  
  58. sub load_ini {
  59.  
  60.     my $inifile = $_[0];
  61.  
  62.     unless(-e $inifile) {
  63.          print("File: $inifile does not exists\n");
  64.          exit;
  65.          }
  66.  
  67.     my $cfg =  Config::IniFiles->new(-file => $inifile, -fallback => "General");
  68.     return $cfg;
  69. }
  70.  
  71. ### Config ###
  72. my $ini_file = './config.ini';
  73. my $cfg = load_ini($ini_file);
  74.  
  75. ### Database ###
  76. my $dbh;
  77. my $sth;
  78. my $rows_cnt;
  79. my $row;
  80. $dbh = DBI->connect('DBI:mysql:database=' . $cfg->val('db', 'name') . ';host=' . $cfg->val('db', 'host'), $cfg->val('db', 'user'), $cfg->val('db', 'pass'), {mysql_auto_reconnect => 1, mysql_enable_utf8 => 1});
  81. die "Connection error: " . DBI->errstr unless $dbh;
  82.  
  83. ### LWP Parallel ###
  84. my @requests;
  85. my $req;
  86. my $res;
  87. my $pua = LWP::Parallel::UserAgent->new;
  88. $pua->timeout($cfg->val('ua', 'timeout'));
  89. $pua->agent($cfg->val('ua', 'agent'));
  90. $pua->max_req($cfg->val('ua', 'parallel'));
  91.  
  92.  
  93. $sth = $dbh->prepare('SELECT href, ds_id FROM `' . $cfg->val('db', 'tb_href') . '` ORDER BY id ASC');
  94. $sth->execute();
  95.  
  96. while(my @row = $sth->fetchrow_array) {
  97.         my $url = $row[0];
  98.     my $ds_id = $row[1];
  99.     my $path = $cfg->val('prop', 'output_dir') . '/' . $ds_id;
  100.     my $html_file = $path . '/' . $ds_id . '.html';
  101.     newdir($path);
  102.     print 'Registering: ' . $url . "\n";
  103.     $pua->register(prepare_http_req($url, $cfg->val('ua', 'agent')), $html_file);
  104.    
  105. }
  106.  
  107. $sth->finish();
  108. $dbh->disconnect();
  109.  
  110. my $entries = $pua->wait();
  111. foreach(keys %$entries) {
  112.     $res = $entries->{$_}->response;
  113.     print $res->request->url, " ", $res->message, "\n";
  114. }
Add Comment
Please, Sign In to add comment