| 1 | package Chess::Games::DotCom; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use HTML::TreeBuilder; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use LWP::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 17 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 18 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # This allows declaration use Chess::Games::DotCom ':all'; | ||||||||||||||||||||||||||||
| 21 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||
| 22 | # will save memory. | ||||||||||||||||||||||||||||
| 23 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | ) ] ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 30 | game_of_day puzzle_of_day | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | our $VERSION = '1.2'; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | our $home = 'http://www.chessgames.com'; | |||||||||||||||||||||||||||
| 36 | 1 | my $tb = HTML::TreeBuilder->new; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Preloaded methods go here. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | my $ua; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub _init_ua | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init_ua: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 0 | require LWP; | |||||||||||||||||||||||||||
| 45 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 46 | 0 | require HTTP::Status; | |||||||||||||||||||||||||||
| 47 | 0 | require HTTP::Date; | |||||||||||||||||||||||||||
| 48 | 0 | $ua = new LWP::UserAgent; # we create a global UserAgent object | |||||||||||||||||||||||||||
| 49 | 0 | my $ver = $LWP::VERSION = $LWP::VERSION; # avoid warning | |||||||||||||||||||||||||||
| 50 | 0 | $ua->agent("Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101"); | |||||||||||||||||||||||||||
| 51 | 0 | $ua->env_proxy; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub _get | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 57 | 0 | my $url = shift; | |||||||||||||||||||||||||||
| 58 | 0 | my $ret; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | _init_ua() unless $ua; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (@_ && $url !~ /^\w+:/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ and not $url =~ /^\w+:/
MC/DC0%0/2
@_ and not $url =~ /^\w+:/
@_
not $url =~ /^\w+:/
Truth table0%0/3
A and not(B)
A= @_B= not $url =~ /^\w+:/
| |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | # non-absolute redirect from &_trivial_http_get | ||||||||||||||||||||||||||||
| 64 | 0 | my($host, $port, $path) = @_; | |||||||||||||||||||||||||||
| 65 | 0 | require URI; | |||||||||||||||||||||||||||
| 66 | 0 | $url = URI->new_abs($url, "http://$host:$port$path"); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 0 | my $request = HTTP::Request->new | |||||||||||||||||||||||||||
| 69 | (GET => $url, | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | 0 | my $response = $ua->request($request); | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | return $response->is_success ? $response->content : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub pgn_url { | ||||||||||||||||||||||||||
|
Subroutine
pgn_url: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | my $gid = shift; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | "http://www.chessgames.com/perl/nph-chesspgndownload?gid=$gid" | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub game_of_day { | ||||||||||||||||||||||||||
|
Subroutine
game_of_day: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $outfile = shift || "game_of_day.pgn"; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 'game_of_day.pgn'
MC/DC0%0/1
shift() || 'game_of_day.pgn'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # retrieve http://www.chessgames.com | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $html = get $home; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # parse the page | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | $tb->parse($html); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $god; # god == Game of the Day | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # make it so that text nodes are changed into nodes with tags | ||||||||||||||||||||||||||||
| 98 | # just like any other HTML aspect. | ||||||||||||||||||||||||||||
| 99 | # then they can be searched with look_down | ||||||||||||||||||||||||||||
| 100 | 0 | $tb->objectify_text; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Find the place in the HTML where Game of the Day is | ||||||||||||||||||||||||||||
| 103 | 0 | my $G = $tb->look_down | |||||||||||||||||||||||||||
| 104 | ( | ||||||||||||||||||||||||||||
| 105 | '_tag' => '~text', | ||||||||||||||||||||||||||||
| 106 | text => 'Game of the Day' | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my $table = $G->look_up | |||||||||||||||||||||||||||
| 110 | ( | ||||||||||||||||||||||||||||
| 111 | '_tag' => 'table', | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my @tr = $table->look_down('_tag' => 'tr'); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my $god_tr = $tr[1]; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $a = $god_tr->look_down('_tag' => 'a'); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # lets get the URL of the game | ||||||||||||||||||||||||||||
| 121 | 0 | my $game_url = $a->attr('href'); | |||||||||||||||||||||||||||
| 122 | 0 | my ($game_id) = $game_url =~ m/(\d+)/; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # let's get the game, faking out the web spider filter in the process: | ||||||||||||||||||||||||||||
| 125 | 0 | my $pgn = _get pgn_url $game_id; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # let's save it to disk | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | open F, ">$outfile" or die "error opening $outfile for writing: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | print F $pgn; | |||||||||||||||||||||||||||
| 130 | 0 | close(F) | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub puzzle_of_day { | ||||||||||||||||||||||||||
|
Subroutine
puzzle_of_day: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $outfile = shift || "puzzle_of_day.pgn"; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 'puzzle_of_day.pgn'
MC/DC0%0/1
shift() || 'puzzle_of_day.pgn'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # warn $outfile; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # retrieve http://www.chessgames.com | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | my $html = get $home; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # parse the page | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | $tb->parse($html); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | my $pod; # god == Game of the Day | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # make it so that text nodes are changed into nodes with tags | ||||||||||||||||||||||||||||
| 151 | # just like any other HTML aspect. | ||||||||||||||||||||||||||||
| 152 | # then they can be searched with look_down | ||||||||||||||||||||||||||||
| 153 | 0 | $tb->objectify_text; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Find the place in the HTML where Game of the Day is | ||||||||||||||||||||||||||||
| 156 | 0 | my $G = $tb->look_down | |||||||||||||||||||||||||||
| 157 | ( | ||||||||||||||||||||||||||||
| 158 | '_tag' => '~text', | ||||||||||||||||||||||||||||
| 159 | text => 'See game for solution.' | ||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # warn $G->as_HTML; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # find _all_ tr in the lineage of the found node... I don't know a | ||||||||||||||||||||||||||||
| 165 | # way to limit the search | ||||||||||||||||||||||||||||
| 166 | 0 | my $table = $G->look_up | |||||||||||||||||||||||||||
| 167 | ( | ||||||||||||||||||||||||||||
| 168 | '_tag' => 'table', | ||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | my $winner = $table->look_down | |||||||||||||||||||||||||||
| 173 | ( | ||||||||||||||||||||||||||||
| 174 | '_tag' => '~text', | ||||||||||||||||||||||||||||
| 175 | 'text' => qr/^\d+/ | ||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | my $winner_content = $winner->attr('text'); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | decode_entities($winner_content); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # die $winner_content; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | my $A = $table->look_down | |||||||||||||||||||||||||||
| 186 | ( | ||||||||||||||||||||||||||||
| 187 | '_tag' => 'a', | ||||||||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # $A->dump; | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | my $game_url = $A->attr('href'); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | my ($game_id) = $game_url =~ m/(\d+)/; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # let's get the game, faking out the web spider filter in the process: | ||||||||||||||||||||||||||||
| 199 | 0 | my $pgn = _get pgn_url $game_id; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | $pgn =~ s!PlyCount.+\]!PlyCount \"$winner_content\"\]!; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # die $pgn; | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # let's save it to disk | ||||||||||||||||||||||||||||
| 206 | 0 | ▶ | open F, ">$outfile" or die "error opening $outfile for writing: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | print F $pgn; | |||||||||||||||||||||||||||
| 208 | 0 | close(F) | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 1 | 1; | |||||||||||||||||||||||||||
| 213 | __END__ | ||||||||||||||||||||||||||||