-
Notifications
You must be signed in to change notification settings - Fork 32
/
generate-data_js.pl
executable file
·130 lines (92 loc) · 3.13 KB
/
generate-data_js.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl
##########################################################################
# generate-data_js.pl - Generate data.js for dotabuffcp #
# Copyright (C) <2014> Onur Aslan <[email protected]> #
# #
# See COPYING for distribution information. #
##########################################################################
use strict;
use warnings;
use LWP::Simple;
use JSON;
use POSIX qw/strftime/;
my $DEBUG = 0;
my @heroes;
my @heroes_bg;
my @heroes_wr;
my @win_rates;
sub read_data {
my $content = '';
$content .= $_ while (<DATA>);
return $content;
}
sub hero_link {
my $hero = $_[0];
$hero =~ s/'//g;
$hero =~ s/ /-/g;
$hero =~ tr/[A-Z]/[a-z]/;
return $hero;
}
sub hero_id {
my $hero = $_[0];
my $c = 0;
for (@heroes) {
return $c if ($_ eq $hero);
++$c;
}
return -1;
}
sub get_heroes {
$DEBUG and warn "Getting hero list\n";
my $content = get ('http://dotabuff.com/heroes') or die;
(@heroes_bg) = $content =~ /background: url\((.*?)\)/g;
(@heroes) = $content =~ /<div class="name">(.*?)<\/div>/g;
$_ =~ s/'// for (@heroes); # fix name of nature's prophet
$_ =~ s/&.*?;// for (@heroes);
$_ =~ s///\//g for (@heroes_bg);
}
sub get_winrates_of_hero {
my ($hero, $hid) = ($_[0], hero_id ($_[0]));
$DEBUG and warn "Getting winrates of $hero\n";
my $content = get ('http://dotabuff.com/heroes/' .
hero_link ($hero) .
'/matchups') or die;
my (@wr) = $content =~ /<dl><dd><span class="(?:won|lost)">(.*?)%<\/span><\/dd><dt>Win Rate<\/dt><\/dl>/g;
$heroes_wr[$hid] = $wr[0];
my $re = qr|<td class="cell-xlarge"><a class="link-type-hero" href="/heroes/.*?">(.*?)</a></td><td data-value="(.*?)">.*?%<div class="bar bar-default"><div class="segment segment-advantage" style="width: [\d.]+%;"></div></div></td><td data-value="(.*?)">.*?%<div class="bar bar-default"><div class="segment segment-win" style="width: [\d.]+%;"></div></div></td><td data-value="\d+">([\d,]+)<div class="bar bar-default"><div class="segment segment-match" style="width: [\d.]+%;"></div></div></td></tr>|;
my (@heros) = $content =~ /$re/g;
my $c = 0;
my @a;
for (@heros) {
$_ =~ s/&.*?;// if $c == 0;
$_ =~ s/,// if $c == 3;
push @a, $_;
++$c;
if ($c == 4) {
$win_rates[$hid][hero_id ($a[0])] = [ @a[1..3] ];
$c = 0;
@a = ();
}
}
}
sub get_winrates {
get_winrates_of_hero ($_) for (@heroes);
}
sub print_winrates {
my $json = JSON->new;
$DEBUG and warn "Writing win rates to data.js\n";
open my $fh, '>data.js';
print $fh 'var heroes = ', $json->encode ([ @heroes ]);
print $fh ', heroes_bg = ', $json->encode ([ @heroes_bg ]);
print $fh ', heroes_wr = ', $json->encode ([ @heroes_wr ]);
print $fh ', win_rates = ', $json->encode ([ @win_rates ]);
print $fh ', update_time = "',
strftime("%Y-%m-%d", localtime (time ())),
"\";\n";
close $fh;
}
$_ eq '--debug' and $DEBUG++ for @ARGV;
get_heroes ();
get_winrates ();
print_winrates ();
__DATA__