| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use LWP::UserAgent; | ||
| 4 | use HTML::TreeBuilder; | ||
| 5 | use Digest::Crc32; | ||
| 6 | use Data::Dumper; | ||
| 7 | use IO::Uncompress::Unzip; | ||
| 8 | use Text::Iconv; | ||
| 9 | |||
| 10 | |||
| 11 | my $timeout = 603; | ||
| 12 | my $timestep = 3; | ||
| 13 | my $count = 60; | ||
| 14 | my $type = ov2; | ||
| 15 | my $tocode = "ISO-8859-2"; | ||
| 16 | |||
| 17 | my $directory = ""; | ||
| 18 | $ua = LWP::UserAgent->new; | ||
| 19 | $ua->agent("MyApp/0.1 "); | ||
| 20 | |||
| 21 | $oldh = select(STDOUT); | ||
| 22 | $| = 1; | ||
| 23 | select(STDERR); | ||
| 24 | $| = 1; | ||
| 25 | select($oldh); | ||
| 26 | |||
| 27 | Text::Iconv->raise_error(0); | ||
| 28 | |||
| 29 | printf "Grabbing types and list of categories... "; | ||
| 30 | |||
| 31 | my $req = HTTP::Request->new( GET => 'http://www.poi.cz/index.php?poi=sluco1'); | ||
| 32 | #$req->content_type('application/x-www-form-urlencoded'); | ||
| 33 | #$req->content('poi=sluco1'); | ||
| 34 | |||
| 35 | my $res = $ua->request($req); | ||
| 36 | |||
| 37 | my $converter = Text::Iconv->new( "UTF-8", $tocode ); | ||
| 38 | |||
| 39 | # Check the outcome of the response | ||
| 40 | if ($res->is_success) { | ||
| 41 | |||
| 42 | printf "done\n"; | ||
| 43 | |||
| 44 | my ( @types, @ids ); | ||
| 45 | |||
| 46 | my $tree = HTML::TreeBuilder->new(); | ||
| 47 | $tree->parse( $res->content ); | ||
| 48 | |||
| 49 | for my $type ( $tree->look_down( "_tag", "input", "name", "r1" ) ) { | ||
| 50 | push @typed, $type->attr_get_i("value"); | ||
| 51 | #printf "type: %s\n", $type->attr_get_i("value"); | ||
| 52 | } | ||
| 53 | |||
| 54 | for my $id ( $tree->look_down( "_tag", "input", "name", "messageId[]" ) ) { | ||
| 55 | push @ids, $id->attr_get_i("value"); | ||
| 56 | #printf "id: %s\n", $id->attr_get_i("value");; | ||
| 57 | } | ||
| 58 | |||
| 59 | $tree->delete; | ||
| 60 | |||
| 61 | my $crc = new Digest::Crc32(); | ||
| 62 | my $offset = 0; | ||
| 63 | my $errcount; | ||
| 64 | while ( $offset <= $#ids ) { | ||
| 65 | printf "getting from %d to %d...\n", $offset, $offset + $count; | ||
| 66 | my $url = 'http://www.poi.cz/index.php?poi=sluco1&akce=tvor&r1=' . $type . '&' . toURLparams( $offset, $count, @ids ) . '&nazev=CZ-&vse=vse&h1=Vytvo%C5%99it'; | ||
| 67 | printf "URL: %s\n", $url; | ||
| 68 | $req = HTTP::Request->new( GET => $url ); | ||
| 69 | $req->referer("http://www.poi.cz/index.php?poi=sluco1"); | ||
| 70 | $res = $ua->request($req); | ||
| 71 | my $time = time; | ||
| 72 | #printf "time: %s\n", $time; | ||
| 73 | my $data; | ||
| 74 | $$data = $res->content; | ||
| 75 | if ($res->is_success) { | ||
| 76 | my $headers = $res->headers; | ||
| 77 | printf "content: %s\n", $headers->header("content-type"); | ||
| 78 | if ( $headers->header("content-type") =~ m#application/x-zip# ) { | ||
| 79 | my $z = new IO::Uncompress::Unzip $data, MultiStream => 0, Append => 1; | ||
| 80 | my $c = 0; | ||
| 81 | while ( not $z->eof ) { | ||
| 82 | my $b = ""; | ||
| 83 | my $header = $z->getHeaderInfo(); | ||
| 84 | #printf "header: %s\n", Dumper($header); | ||
| 85 | my $crc32 = $$header{CRC32}; | ||
| 86 | my $name = $converter->convert( $$header{Name} ); | ||
| 87 | $name = $$header{Name} unless $converter->retval; | ||
| 88 | $name =~ s/^\s*//; | ||
| 89 | $name =~ s/\s*$//; | ||
| 90 | |||
| 91 | printf "%d: %s... ", $offset + $c++, $name; | ||
| 92 | |||
| 93 | my $status; | ||
| 94 | do { $status = $z->read($b) } while ( $status > 0 ); | ||
| 95 | if ( $status != 0 ) { | ||
| 96 | printf "Error reading %s from %s.\n", $name, $offset; | ||
| 97 | } | ||
| 98 | |||
| 99 | open ZIP, ">$directory$offset-$count.zip"; | ||
| 100 | print ZIP $$data; | ||
| 101 | close ZIP; | ||
| 102 | |||
| 103 | if ( not $name or not $crc32 ) { | ||
| 104 | my $oldname = $name; | ||
| 105 | $name = "scrap" unless $name; | ||
| 106 | my $d = 0; | ||
| 107 | while ( -e "$directory$name" ) { | ||
| 108 | $name = $name . sprintf "%05d", $d++; | ||
| 109 | } | ||
| 110 | printf "bad header (crc: $crc32; name: $oldname) => saving to $directory$name.\n"; | ||
| 111 | open POI, ">$directory$name.zip"; | ||
| 112 | print POI $$data; | ||
| 113 | close POI; | ||
| 114 | } | ||
| 115 | printf "saving to $directory$name... "; | ||
| 116 | my $identical = 0; | ||
| 117 | $identical = ( $crc->filecrc32( "$directory$name" ) == $crc32 ) if ( -e "$directory$name" and $crc32 ); | ||
| 118 | if ( not $identical and ( $crc->strcrc32( $b ) == $crc32 ) ) { | ||
| 119 | open POI, ">$directory$name"; | ||
| 120 | print POI $b; | ||
| 121 | close POI; | ||
| 122 | printf "saved: %d bytes\n", length $b; | ||
| 123 | } else { | ||
| 124 | print "skipped\n"; | ||
| 125 | } | ||
| 126 | printf STDERR "CRC does not match!!! Filename $name" if ( ( $crc->strcrc32( $b ) != $crc32 ) ); | ||
| 127 | $z->nextStream; | ||
| 128 | } | ||
| 129 | printf "offset: %d + %d\n", $offset, $count; | ||
| 130 | $offset = $offset + $count; | ||
| 131 | } else { | ||
| 132 | $errcount++; | ||
| 133 | if ( $headers->header("content-type") =~ m#text# ) { | ||
| 134 | if ( $$data =~ /Tento\s+limit\s+vypr\S+\s+za\s+(\d+)\s+s\./m ) { | ||
| 135 | printf "Sta¾ení za %d sekund...\n", $1; | ||
| 136 | $time = time + $1 - $timeout + 3; | ||
| 137 | } else { | ||
| 138 | printf "%s\n", $$data; | ||
| 139 | } | ||
| 140 | } else { | ||
| 141 | printf "Bad content: %s\n", $headers->header("content-type"); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | while ( $time + $timeout > time ) { | ||
| 146 | printf "%d seconds left...\r", $time + $timeout - time if $ENV{TERM}; | ||
| 147 | sleep $timestep; | ||
| 148 | } | ||
| 149 | print "\n"; | ||
| 150 | |||
| 151 | } else { | ||
| 152 | $errcount++; | ||
| 153 | print $res->status_line, "\n"; | ||
| 154 | } | ||
| 155 | if ( $errcount > $errtrigger ) { | ||
| 156 | $offset = $offset + $count; | ||
| 157 | $errcount=0; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | } else { | ||
| 162 | print $res->status_line, "\n"; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub toURLparams($$@) { | ||
| 166 | my $offset = shift; | ||
| 167 | my $count = shift; | ||
| 168 | my @ids = @_; | ||
| 169 | my $result = ""; | ||
| 170 | |||
| 171 | for ( my $i = $offset; $i < $offset + $count; $i++ ) { | ||
| 172 | $result = $result . sprintf "&%s=%s", "messageId%5B%5D", $ids[$i] if $ids[$i]; | ||
| 173 | } | ||
| 174 | return substr( $result, 1 ); | ||
| 175 | } |