root/lang/perl/Google-Chart-DBIC/trunk/t/insert.pl @ 17483

Revision 17483, 1.2 kB (checked in by lopnor, 5 years ago)

lang/perl/Google-Chart-DBIC: first commit

Line 
1use strict;
2use warnings;
3use FindBin;
4use charnames ':full';
5use lib "$FindBin::Bin/lib";
6use TestApp::Schema;
7use Web::Scraper;
8use URI;
9use YAML;
10
11my $schema = TestApp::Schema->connect(
12    "dbi:SQLite:test.db"
13);
14my $rs = $schema->resultset('Climate');
15$rs->delete_all;
16
17my $cities = scraper {
18    process '//th[@bgcolor="#add8e6"]', 'data[]' => sub {
19        my $city = shift;
20        (my $cityname = $city->as_text) =~ s/\N{IDEOGRAPHIC SPACE}+//g;
21        my @citydata = $city->findnodes('../following-sibling::tr[1]/td[@align="right"]/font');
22        my @result = ();
23
24        for my $month (0 .. 11) {
25            my @data = ();
26            for ($citydata[$month]->content_refs_list) {
27                next if ref $$_;
28                push @data, $$_;
29            }
30
31            push @result, {
32                place => $cityname,
33                month => $month + 1,
34                average => $data[0] + 0,
35                high => $data[1] + 0,
36                low => $data[2] + 0,
37                rainfall => $data[3] + 0,
38            }
39        }
40        return @result;
41    };
42    result 'data';
43}->scrape(URI->new('http://www.faminet.co.jp/d_guide/d_gnc/gnc1_3.html'));
44
45warn Dump $cities;
46for (@$cities){
47    $rs->create($_);
48};
Note: See TracBrowser for help on using the browser.