root/lang/perl/misc/ogame_check.pl

Revision 27165, 1.6 kB (checked in by otsune, 21 months ago)

modify misc

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2use strict;
3use warnings;
4use URI;
5use WWW::Mechanize;
6use Config::Pit;
7use Web::Scraper;
8use DateTime;
9use YAML;
10
11my $uni = shift || 'uni4';
12# get password
13#Config::Pit::switch('ogame');
14my $config = pit_get("$uni.ogame.jp", require => {
15        "username" => "otsune",
16        "password" => "your password on ogame.jp"
17    });
18
19# login
20my $ogame_login = "http://$uni.ogame.jp/game/reg/login2.php";
21my $uri = URI->new($ogame_login);
22$uri->query_form(
23    login => $config->{username},
24    pass  => $config->{password},
25    v    => 2,
26);
27
28# access
29my $mech = WWW::Mechanize->new(cookie_jar => {});
30my $response = $mech->get( $uri );
31
32if (!$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
40my $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
57binmode STDOUT, ":utf8";
58print YAML::Dump $feed;
59
60__END__
61use Data::Dumper;
62sub dump_utf {
63 my $d = Dumper(@_);
64 $d =~ s/\\x{([0-9a-z]+)}/chr(hex($1))/ge;
65 print $d;
66};
Note: See TracBrowser for help on using the browser.