| 1 | #!/usr/bin/perl |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | use URI; |
|---|
| 5 | use WWW::Mechanize; |
|---|
| 6 | use Config::Pit; |
|---|
| 7 | use Web::Scraper; |
|---|
| 8 | use DateTime; |
|---|
| 9 | use YAML; |
|---|
| 10 | |
|---|
| 11 | my $uni = shift || 'uni4'; |
|---|
| 12 | # get password |
|---|
| 13 | #Config::Pit::switch('ogame'); |
|---|
| 14 | my $config = pit_get("$uni.ogame.jp", require => { |
|---|
| 15 | "username" => "otsune", |
|---|
| 16 | "password" => "your password on ogame.jp" |
|---|
| 17 | }); |
|---|
| 18 | |
|---|
| 19 | # login |
|---|
| 20 | my $ogame_login = "http://$uni.ogame.jp/game/reg/login2.php"; |
|---|
| 21 | my $uri = URI->new($ogame_login); |
|---|
| 22 | $uri->query_form( |
|---|
| 23 | login => $config->{username}, |
|---|
| 24 | pass => $config->{password}, |
|---|
| 25 | v => 2, |
|---|
| 26 | ); |
|---|
| 27 | |
|---|
| 28 | # access |
|---|
| 29 | my $mech = WWW::Mechanize->new(cookie_jar => {}); |
|---|
| 30 | my $response = $mech->get( $uri ); |
|---|
| 31 | |
|---|
| 32 | if (!$response->is_success || $response->content =~ /errormessage/){ |
|---|
| 33 | warn $mech->status(); |
|---|
| 34 | return; |
|---|
| 35 | }; |
|---|
| 36 | |
|---|
| 37 | $mech->follow_link(url_regex => qr{index\.php}i); |
|---|
| 38 | |
|---|
| 39 | # scrape |
|---|
| 40 | my $feed = scraper { |
|---|
| 41 | process '//title', 'title' => 'TEXT'; |
|---|
| 42 | process 'tr.flight', 'entry[]' => scraper { |
|---|
| 43 | process 'span.attack', title => 'TEXT', |
|---|
| 44 | process '//a[@class="attack"]/following-sibling::a', body => '@title'; |
|---|
| 45 | process '//div[starts-with(@id, "bxx")]', date => sub { |
|---|
| 46 | my $dt = DateTime->now(time_zone=>'local'); |
|---|
| 47 | $dt->add( seconds=>$_->attr('title') ); |
|---|
| 48 | return $dt->iso8601(); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | }->scrape($mech->content, $mech->uri); |
|---|
| 52 | |
|---|
| 53 | $feed->{link} = $ogame_login; |
|---|
| 54 | $feed->{image} = 'http://board.ogame.jp/ogame_logo/jp.gif'; |
|---|
| 55 | |
|---|
| 56 | # output |
|---|
| 57 | binmode STDOUT, ":utf8"; |
|---|
| 58 | print YAML::Dump $feed; |
|---|
| 59 | |
|---|
| 60 | __END__ |
|---|
| 61 | use Data::Dumper; |
|---|
| 62 | sub dump_utf { |
|---|
| 63 | my $d = Dumper(@_); |
|---|
| 64 | $d =~ s/\\x{([0-9a-z]+)}/chr(hex($1))/ge; |
|---|
| 65 | print $d; |
|---|
| 66 | }; |
|---|