| 1 | # | ||||||||||
| 2 | # This file is part of WWW-DaysOfWonder-Memoir44 | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is copyright (c) 2009 by Jerome Quelin. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 8 | # | ||||||||||
| 9 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | package WWW::DaysOfWonder::Memoir44::App::Command::update; | ||||||||||
| 14 | # ABSTRACT: update db from dow website | ||||||||||
| 15 | 1 | $WWW::DaysOfWonder::Memoir44::App::Command::update::VERSION = '3.000'; | |||||||||
| 16 | 1 | ▶ | use HTML::TreeBuilder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use Term::ProgressBar::Quiet; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Term::Twiddle::Quiet; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use Text::Trim; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | use WWW::DaysOfWonder::Memoir44::App -command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | ▶ | use WWW::DaysOfWonder::Memoir44::Scenario; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | ▶ | use WWW::DaysOfWonder::Memoir44::DB::Scenarios; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 1 | ▶ | use WWW::DaysOfWonder::Memoir44::Url; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | # -- public methods | ||||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | sub description { | ||||||||
|
Subroutine
description: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 31 | 0 | 'Update the database after the list of scenarios from days of wonder | |||||||||
| 32 | website. This will *not* download the scenarios themselves - see the | ||||||||||
| 33 | download command for this action.'; | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub opt_spec { | ||||||||
|
Subroutine
opt_spec: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 37 | 0 | my $self = shift; | |||||||||
| 38 | 0 | return; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 42 | 0 | my $self = shift; | |||||||||
| 43 | 0 | my $twiddle = Term::Twiddle::Quiet->new; | |||||||||
| 44 | 0 | my $db = WWW::DaysOfWonder::Memoir44::DB::Scenarios->new; | |||||||||
| 45 | |||||||||||
| 46 | # the user agent will be reused | ||||||||||
| 47 | 0 | my $ua = LWP::UserAgent->new; | |||||||||
| 48 | 0 | $ua->agent('WWW::DaysOfWonder::Memoir44'); | |||||||||
| 49 | 0 | $ua->env_proxy; | |||||||||
| 50 | |||||||||||
| 51 | # remove all existing scenarios from db | ||||||||||
| 52 | 0 | $db->clear; | |||||||||
| 53 | |||||||||||
| 54 | 0 | foreach my $source ( qw{ game approved classified public } ) { | |||||||||
| 55 | # create the source url | ||||||||||
| 56 | 0 | my $url = WWW::DaysOfWonder::Memoir44::Url->new({source=>$source}); | |||||||||
| 57 | 0 | say "* updating $source scenarios"; | |||||||||
| 58 | 0 | say "- url: $url"; | |||||||||
| 59 | |||||||||||
| 60 | # downloading url listing the scenarios | ||||||||||
| 61 | 0 | print "- downloading url: "; | |||||||||
| 62 | 0 | $twiddle->start; | |||||||||
| 63 | 0 | my $response = $ua->get("$url"); | |||||||||
| 64 | 0 | $twiddle->stop; | |||||||||
| 65 | 0 | ▶ | die $response->status_line unless $response->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | say "done"; | |||||||||
| 67 | |||||||||||
| 68 | # parse html to find list of scenarios | ||||||||||
| 69 | 0 | print "- parsing: "; | |||||||||
| 70 | 0 | $twiddle->start; | |||||||||
| 71 | 0 | my $tree = HTML::TreeBuilder->new_from_content( $response->content ); | |||||||||
| 72 | 0 | my $table = $tree->find_by_tag_name( 'table' ); | |||||||||
| 73 | #die $table->dump; | ||||||||||
| 74 | 0 | my $depth = $table->depth; | |||||||||
| 75 | # find rows, but only one level deep. otherwise, it would also | ||||||||||
| 76 | # find rows of tables embedded in some cells. | ||||||||||
| 77 | my @rows = $table->look_down( | ||||||||||
| 78 | '_tag', 'tr', | ||||||||||
| 79 | 0 | ▶ | sub { $_[0]->depth == $depth+1 }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 80 | 0 | ); | |||||||||
| 81 | 0 | shift @rows; # trim title line | |||||||||
| 82 | 0 | $twiddle->stop; | |||||||||
| 83 | 0 | say "found ", scalar(@rows), " scenarios"; | |||||||||
| 84 | |||||||||||
| 85 | # extracting scenarios from table rows | ||||||||||
| 86 | 0 | my $prefix = "- extracting scenarios"; | |||||||||
| 87 | 0 | my $progress = Term::ProgressBar::Quiet->new( { | |||||||||
| 88 | count => scalar(@rows), | ||||||||||
| 89 | bar_width => 50, | ||||||||||
| 90 | remove => 1, | ||||||||||
| 91 | name => $prefix, | ||||||||||
| 92 | } ); | ||||||||||
| 93 | 0 | $progress->minor(0); # don't know why this doesn't work as constructor param | |||||||||
| 94 | 0 | foreach my $row ( @rows ) { | |||||||||
| 95 | # extract scenario data from row | ||||||||||
| 96 | 0 | my %data = _scenario_data_from_html_row($row); | |||||||||
| 97 | 0 | $data{source} = $source; | |||||||||
| 98 | # create a scenario object and store it in the database | ||||||||||
| 99 | 0 | my $scenario = WWW::DaysOfWonder::Memoir44::Scenario->new(%data); | |||||||||
| 100 | 0 | $db->add( $scenario ); | |||||||||
| 101 | 0 | $progress->update; | |||||||||
| 102 | } | ||||||||||
| 103 | 0 | say "${prefix}: done"; | |||||||||
| 104 | |||||||||||
| 105 | # source complete | ||||||||||
| 106 | 0 | print "\n"; | |||||||||
| 107 | } | ||||||||||
| 108 | 0 | $db->write; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | # -- private methods | ||||||||||
| 113 | |||||||||||
| 114 | # | ||||||||||
| 115 | # my %data = _scenario_data_from_html_row($row); | ||||||||||
| 116 | # | ||||||||||
| 117 | # given a $row of the table fetched from dow website, parse it and | ||||||||||
| 118 | # return a hash with all scenario properties extracted. the hash | ||||||||||
| 119 | # keys are: | ||||||||||
| 120 | # - id: id of the scenario | ||||||||||
| 121 | # - name: scenario name | ||||||||||
| 122 | # - operation: operation the scenario is part of | ||||||||||
| 123 | # - front: west, east, mediterranean, etc. | ||||||||||
| 124 | # - author: who wrote the scenario | ||||||||||
| 125 | # - rating: average scenario rating (1, 2 or 3) | ||||||||||
| 126 | # - updated: date of last scenario update | ||||||||||
| 127 | # - format: standard, overlord, breakthru | ||||||||||
| 128 | # - board: country, beach, winter, desert | ||||||||||
| 129 | # - need_tp: whether terrain pack is needed | ||||||||||
| 130 | # - need_ef: whether eastern front is needed | ||||||||||
| 131 | # - need_mt: whether mediterranean theater is needed | ||||||||||
| 132 | # - need_pt: whether pacific theater is needed | ||||||||||
| 133 | # - need_ap: whether air pack is needed | ||||||||||
| 134 | # - need_bm: whether battle maps is needed | ||||||||||
| 135 | # - need_cb: whether campaign book is needed | ||||||||||
| 136 | # | ||||||||||
| 137 | 0 | ▶ | sub _scenario_data_from_html_row { | ||||||||
|
Subroutine
_scenario_data_from_html_row: not called CC=2 SCAR=17.9
| |||||||||||
| 138 | 0 | my $row = shift; | |||||||||
| 139 | 0 | my %data; | |||||||||
| 140 | |||||||||||
| 141 | # split row in cells | ||||||||||
| 142 | 0 | my $depth = $row->depth; | |||||||||
| 143 | my @cells = $row->look_down( | ||||||||||
| 144 | '_tag', 'td', | ||||||||||
| 145 | 0 | ▶ | sub { $_[0]->depth == $depth+1 }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 146 | 0 | ); | |||||||||
| 147 | |||||||||||
| 148 | # extract values and fill in the hash | ||||||||||
| 149 | 0 | my $link = $cells[0]->find_by_tag_name('a')->attr('href'); | |||||||||
| 150 | 0 | ($data{id}) = ($link =~ /id=(\d+)/); | |||||||||
| 151 | 0 | $data{name} = trim($cells[0]->as_text); | |||||||||
| 152 | 0 | $data{operation} = trim($cells[2]->as_text); | |||||||||
| 153 | 0 | $data{front} = trim($cells[3]->as_text); | |||||||||
| 154 | 0 | $data{author} = trim($cells[4]->as_text); | |||||||||
| 155 | 0 | $data{rating} = substr $cells[6]->find_by_tag_name('img')->attr('alt'), 0, 1; | |||||||||
| 156 | |||||||||||
| 157 | 0 | my $updated = trim($cells[5]->as_text); # dd/mm/yyyy | |||||||||
| 158 | 0 | $data{updated} = join '-', reverse split /\//, $updated; # yyyy-mm-dd | |||||||||
| 159 | |||||||||||
| 160 | # fill in langs, board & booleans | ||||||||||
| 161 | # - langs | ||||||||||
| 162 | 0 | my @subcells = $cells[8]->find_by_tag_name('td'); | |||||||||
| 163 | 0 | my @langs = map { $_->attr('alt') } $subcells[1]->find_by_tag_name('img'); | |||||||||
| 164 | 0 | $data{languages} = \@langs; | |||||||||
| 165 | # - board types | ||||||||||
| 166 | 0 | my $boardimg = $subcells[2]->find_by_tag_name('img')->attr('src'); | |||||||||
| 167 | 0 | ▶ | $boardimg =~ /mm_board_([^_]+)_([^.]+)\.gif/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | or die "unknwon board image: $boardimg"; | ||||||||||
| 169 | 0 | $data{format} = $1; | |||||||||
| 170 | 0 | $data{board} = $2; | |||||||||
| 171 | # - booleans | ||||||||||
| 172 | my @imgs = | ||||||||||
| 173 | 0 | map { $_->attr('src') } | |||||||||
| 174 | $subcells[3]->find_by_tag_name('img'); | ||||||||||
| 175 | 0 | $data{need_tp} = grep { /pack_terrain/ } @imgs; | |||||||||
| 176 | 0 | $data{need_ef} = grep { /pack_eastern/ } @imgs; | |||||||||
| 177 | 0 | $data{need_pt} = grep { /pack_pacific/ } @imgs; | |||||||||
| 178 | 0 | $data{need_mt} = grep { /pack_mediterranean/ } @imgs; | |||||||||
| 179 | 0 | $data{need_ap} = grep { /pack_air/ } @imgs; | |||||||||
| 180 | 0 | $data{need_bm} = 0; # grep { /pack_/ } @imgs; | |||||||||
| 181 | 0 | $data{need_cb} = 0; # grep { /pack_/ } @imgs; | |||||||||
| 182 | |||||||||||
| 183 | 0 | return %data; | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | |||||||||||
| 187 | 1 | 1; | |||||||||
| 188 | |||||||||||
| 189 | __END__ | ||||||||||