bnghtz

inforadio_magazin_letolto.pl

Jan 10th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.21 KB | None | 0 0
  1. #! /usr/bin/perl
  2.  
  3. ###############################################################################
  4. ## requires wget as an executable program
  5. ## for windows users: to make sure wget is in the %PATH%, download
  6. ## http://users.ugent.be/~bpuype/cgi-bin/fetch.pl?dl=wget/wget.exe
  7. ## to the %windir% folder
  8. ###############################################################################
  9. ## kelleni fog a wget is, mint futtathato file
  10. ## windows felhasznaloknak: hogy biztos elerheto legyen a wget, toltsd le a
  11. ## http://users.ugent.be/~bpuype/cgi-bin/fetch.pl?dl=wget/wget.exe -t
  12. ## a %windir% mappaba
  13. ###############################################################################
  14.  
  15. #####  INCLUDES  ##############################################################
  16. use strict;
  17. use warnings;
  18.  
  19. #####  CONFIGS  ###############################################################
  20. my $MAGAZIN_NUMBER; # hanyas szamu magazinrol van szo?
  21.  
  22. # hordominta = 27
  23. # vilagszam = 13
  24. # tovabbi magazinok itt: http://inforadio.hu/magazinok/
  25.  
  26. my $out = 'links.bat'; # ebbe a bat-ba fogja generalni a letolto scriptet.
  27. my $out2 = 'links_mar_letoltott.bat'; # ebbe a bat-ba fogja generalni a letolto scriptet.
  28.  
  29. #####  GLOBALS  ###############################################################
  30. my $fn = 'index.html@p=';
  31. my $FH;
  32. my $OUT;
  33. my $LAST_PAGE_NUMER = 0;
  34. my @commands = ();
  35. my @commands2 = ();
  36. my $basename = (split('/', $0))[-1];
  37.  
  38. #####  SUB  ###################################################################
  39. sub ekezetKiller($)
  40. {
  41.   my $txt = shift;
  42.  
  43.   $txt =~ s/á/a/g;
  44.   $txt =~ s/Á/A/g;
  45.   $txt =~ s/í/i/g;
  46.   $txt =~ s/Í/I/g;
  47.   $txt =~ s/é/e/g;
  48.   $txt =~ s/É/E/g;
  49.   $txt =~ s/ö|ó|ő/o/g;
  50.   $txt =~ s/Ö|Ő|Ó/O/g;
  51.   $txt =~ s/ü|ú|ű/u/g;
  52.   $txt =~ s/Ű|Ú|Ü/U/g;
  53.  
  54.   return $txt;
  55. }
  56.  
  57. #####  PROGRAM  CODE  #########################################################
  58.  
  59. $MAGAZIN_NUMBER = $ARGV[0];
  60.   die "$basename <Magazin Szam>\n" unless defined($MAGAZIN_NUMBER);
  61.  
  62. #----  search for downloaded archives  ----------------------------------------
  63.  
  64. my @downloaded;
  65. my $DH;
  66. my $dir = '.';
  67. opendir($DH, $dir) || die "can't opendir $dir: $!";
  68.  
  69. while(readdir $DH)
  70. {
  71.   chomp($_);
  72.   next if (!(-f $_) || ($_ !~ m/\.mp3\z/) );
  73.   push(@downloaded, $_);
  74. }
  75. closedir $DH;
  76.  
  77. #----  search for the bigest archive page number ------------------------------
  78.  
  79. `wget -q http://inforadio.hu/magazinarchivum/$MAGAZIN_NUMBER -O MN`;
  80.  
  81. open($FH, '<', 'MN') or die $!;
  82. my @a = <$FH>;
  83. close($FH) or die $!;
  84. undef($FH);
  85. unlink 'MN' or die $!;
  86. chomp(@a);
  87.  
  88. my $flag = 0;
  89. my $max = 0;
  90.  
  91. foreach my $ln (@a)
  92. {
  93.   $flag |= 1 if ($ln =~ m/<select id="lapozo">/);
  94.   $flag |= 2 if ( $flag && ($ln =~ m{</select>}) );
  95.  
  96.   if ($flag == 1)
  97.   {
  98.     if ($ln =~ m{<option value="(\d+)">\d+</option>})
  99.     {
  100.       $max = $1 if $1 > $max;
  101.     }
  102.   }
  103.   elsif ($flag == 3)
  104.   {
  105.     last;
  106.   }
  107. }
  108. $flag = 0;
  109.  
  110. $LAST_PAGE_NUMER = $max;
  111.  
  112. #----  Get magazin archive and Grep the archive .mp3  -------------------------
  113. foreach my $n (1 .. $LAST_PAGE_NUMER)
  114. {
  115.   `wget -q "http://inforadio.hu/magazinarchivum/$MAGAZIN_NUMBER/?p=$n"`;
  116.  
  117.   my $tempFile = $fn.$n;
  118.  
  119.   open($FH, '<', $tempFile) or die "$tempFile :  $!";
  120.   my @cont = <$FH>;
  121.   close($FH) or die $!;
  122.   undef($FH);
  123.   unlink $tempFile or die $!;
  124.  
  125.   chomp(@cont);
  126.   my $title;
  127.  
  128.   foreach my $ln (@cont)
  129.   {
  130.     if ($ln =~ m{<h4.*?>(.*?magazin) (\d+)\.? ?([^\. ]*?) (\d+)-?.*?sa,? (\d+)\.? r.*?</h4>})
  131.     {
  132.       my ($MAG, $y, $m, $d, $r) = ($1, $2, $3, $4, $5);
  133.      
  134.       $MAG = ekezetKiller($MAG);
  135.      
  136.       if ($m =~ m/jan/i)       { $m = '01'; }
  137.       elsif ($m =~ m/rcius/i)  { $m = '02'; }
  138.       elsif ($m =~ m/febru/i)  { $m = '03'; }
  139.       elsif ($m =~ m/prilis/i) { $m = '04'; }
  140.       elsif ($m =~ m/jus/i)    { $m = '05'; }
  141.       elsif ($m =~ m/nius/i)   { $m = '06'; }
  142.       elsif ($m =~ m/lius/i)   { $m = '07'; }
  143.       elsif ($m =~ m/augusz/i) { $m = '08'; }
  144.       elsif ($m =~ m/szepte/i) { $m = '09'; }
  145.       elsif ($m =~ m/okt/i)    { $m = 10; }
  146.       elsif ($m =~ m/novem/i)  { $m = 11; }
  147.       elsif ($m =~ m/decem/i)  { $m = 12; }
  148.      
  149.       $d = "0$d" if length($d) == 1;
  150.      
  151.       $title = "$MAG - ${y}_${m}_${d} - ${r}.mp3";    
  152.       print $title . "\n";
  153.     }
  154.  
  155.     if (($title) && ($ln =~ m{playaudio\('/audio/([0-9a-f]+)/([^']+)'}i))
  156.    {
  157.      my ($num, $name) = ($1, $2);
  158.      my $url = 'http://inforadio.hu/audio/' . $num . '/' . $name;
  159.      
  160.      if (grep {$title eq $_} @downloaded)
  161.      {
  162.        push(@commands2, "wget -c $url -O \"$title\"");
  163.      }
  164.      else
  165.      {
  166.        push(@commands, "wget -c $url -O \"$title\"");
  167.      }
  168.  
  169.      $title = '';
  170.    }
  171.  }
  172. }
  173.  
  174. #----  write out downloader scripts  ------------------------------------------
  175.  
  176. # hianyzo reszek
  177. if (@commands)
  178. {
  179.  @commands = sort {$b cmp $a} @commands;
  180.  my $command = join("\n", @commands);
  181.  
  182.  open($OUT, '>', $out) or die $!;
  183.  print $OUT $command or die $!;
  184.  close($OUT);
  185.  undef($OUT);
  186. }
  187.  
  188. # mar meglevo reszek
  189. if (@commands2  && $ARGV[1])
  190. {
  191.  @commands2 = sort {$b cmp $a} @commands2;
  192.  my $command2 = join("\n", @commands2);
  193.  open($OUT, '>', $out2) or die $!;
  194.  print $OUT $command2 or die $!;
  195.  close($OUT);
  196.  undef($OUT);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment