-
Notifications
You must be signed in to change notification settings - Fork 14
/
getGames
executable file
·292 lines (246 loc) · 9.37 KB
/
getGames
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/perl -w
#
# $Id: getGames 3 2010-03-02 16:56:48Z alex@steamcalculator.com $
#
# SteamCalculator Scripts - http://www.steamcalculator.com
# Copyright (C) 2010 Alexander Kuhrt (alex@steamcalculator.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
require 5.8.0;
use strict;
use DBI;
use LWP::Simple;
use POSIX qw(ceil strftime);
use Time::HiRes;
use IO::Handle;
use Getopt::Long;
binmode(STDOUT, ":utf8");
# Print usage of getGames
sub printUsage()
{
printf STDOUT ("Usage: getGames [OPTIONS]\n");
printf STDOUT ("Grabs all games form Valve's Steam store\n\n");
printf STDOUT ("Options:\n");
printf STDOUT (" \t--clear\t\tClear database\n");
printf STDOUT (" -d\t--debug\t\tCreate debug.log\n");
printf STDOUT (" -h\t--help\t\tShow this help\n");
printf STDOUT (" -o\t--output-file <file> Redirect output to file\n");
printf STDOUT (" -q\t--quiet\t\tSuppress output\n");
printf STDOUT (" \t--version\tShow current version\n");
exit(1);
}
# Print information about getGames
sub printVersion()
{
printf STDOUT ("getGames 0.3.3\n");
printf STDOUT ("Copyright (C) 2010 Free Software Foundation, Inc.\n");
printf STDOUT ("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
printf STDOUT ("This is free software: you are free to change and redistribute it.\n");
printf STDOUT ("There is NO WARRANTY, to the extent permitted by law.\n\n");
printf STDOUT ("Written by Alexander Kuhrt.\n");
exit(1);
}
# Suppress output
sub printQuiet($)
{
my $outputFile = shift;
if($outputFile ne 0)
{
# redirect STDOUT to $outputFile
open(OUTPUTFILE, ">", $outputFile) or die $!;
STDOUT->fdopen(\*OUTPUTFILE, "w") or die $!;
}
else
{
# Linux, Mac
if($^O !~ m/Win32/i)
{
# redirect STDOUT to /dev/null
open(DEVNULL, ">", "/dev/null") or die $!;
STDOUT->fdopen(\*DEVNULL, 'w') or die $!;
}
# Windows
else
{
# TOOD: anyone?
}
}
}
# Declare options
my $clear = 0;
my $debug = 0;
my $help = 0;
my $outputFile = 0;
my $quiet = 0;
my $version = 0;
my @options = (
"clear" => \$clear,
"d|debug" => \$debug,
"h|help|?" => \$help,
"o|output-file=s" => \$outputFile,
"q|quiet" => \$quiet,
"version" => \$version
);
# Grab options
GetOptions(@options) or exit(1);
# Test options
printUsage() if $help;
printVersion() if $version;
printQuiet($outputFile) if $quiet || $outputFile;
open ERROR, '>', "error.txt" or die $!;
STDERR->fdopen( \*ERROR, 'w' ) or die $!;
# Include stuff
require "./include/Simple.pm";
do "./include/functions.plib";
my $configFile = "./include/config.ini";
my @countries = ('at', 'au', 'de', 'no', 'pl', 'uk', 'us');
my $start = [Time::HiRes::gettimeofday()];
my $db_host;
my $db_user;
my $db_pass;
my $db_name;
logging("Staring SteamCalculator's 'getGames' Script...\n");
logging("Reading config file... ");
# read config file
if($configFile && -r $configFile)
{
printf("[OK]\n");
my $conf = ConfigReader::Simple->new($configFile);
$conf->parse();
$db_host = $conf->get("DBHostname");
$db_user = $conf->get("DBUsername");
$db_pass = $conf->get("DBPassword");
$db_name = $conf->get("DBName");
}
else
{
printf("[FAILED]");
die("-- Warning: unable to open configuration file '$configFile'\n");
}
logging("Connecting to MySQL database '$db_name' on '$db_host' as user '$db_user'... ");
my $db = DBI->connect("DBI:mysql:$db_name:$db_host", $db_user, $db_pass) or die("\nCan't connect to MySQL database '$db_name' on '$db_host'\nServer error: $DBI::errstr\n");
printf("connected [OK]\n");
# Clear database befor fillig it again
if($clear)
{
logging("Cleaning MySQL database\n");
my $query = "TRUNCATE TABLE `sc_steamgames`";
my $stmt = $db->prepare($query);
$stmt->execute() or die $!;
$query = "TRUNCATE TABLE `sc_steamgameprices";
$stmt = $db->prepare($query);
$stmt->execute();
}
for(my $c = 0; $c < @countries; $c++)
{
my $country = $countries[$c];
logging("Connecting to '$country' Steam Store... "); # TODO: add timeout!
my $steamStoreURL = "http://store.steampowered.com/search/results?sort_by=Name&sort_order=ASC&category1=998&cc=$country&v5=1&page=1";
my $pageContent = get($steamStoreURL);
my @entries = (cutter($pageContent, "<div class=\"search_pagination_left\">", "</div>") =~ m/^showing\s\d+\s-\s(\d+)\sof\s(\d+)$/);
my $gamesPerPage = $entries[0];
my $totalEntries = $entries[1];
my $totalPages = ceil($totalEntries / $gamesPerPage);
my %game;
printf("found $totalEntries Game Entries on $totalPages Pages [OK]\n\n");
for (my $page = 1; $page < $totalPages + 1; $page++)
#for(my $page = 1; $page < 2; $page++) # use for debugging
{
printf("\x{2554}%s\x{2557}\n", "\x{2550}" x 78);
printf("\x{2551} \x{2551}\n");
printf("\x{2551} Loading '$country', page %02d of %02d \x{2551}\n", $page, $totalPages);
printf("\x{2551} Entries % 4d - % 4d of %d \x{2551}\n", (($page - 1) * $gamesPerPage + 1), ($page * $gamesPerPage), $totalEntries);
printf("\x{2551} \x{2551}\n");
printf("\x{255A}%s\x{255D}\n", "\x{2550}" x 78);
printf("+---------+---------+-------------+--------------------------------------------+\n");
printf("| AppID | Price | Release | Game Title |\n");
printf("+---------+---------+-------------+--------------------------------------------+\n");
my $gameCount = ($page - 1) * $gamesPerPage + 1;
my $tempCursorPos = 0;
my $pageContent = get("http://store.steampowered.com/search/results?sort_by=Name&sort_order=ASC&category1=998&cc=$country&v5=1&page=$page");
my $tempContent = cutter($pageContent, '<!-- List Items -->', '<!-- End List Items -->');
for(my $i = $gameCount; $i < ($gameCount + $gamesPerPage); $i++)
{
# jump to next game
$tempContent = substr($tempContent, $tempCursorPos);
# grab game info
$game{"appID"}[$i] = ($tempContent =~ /store\.steampowered\.com\/app\/(\d+)/)[0];
$game{"price"}[$i] = formPrice(cutter($tempContent, "<div class=\"col search_price\">", "</div>"));
$game{"release"}[$i] = formDate(cutter($tempContent, "<div class=\"col search_released\">", "</div>"), $country);
$game{"title"}[$i] = formTitle(cutter($tempContent, "<h4>", "</h4>"));
# set new cursor position
$tempCursorPos = index($tempContent, "<div style=\"clear: both;\"></div>") + length("<div style=\"clear: both;\"></div>");
# print result
printf("|% 8s |", $game{"appID"}[$i]);
printf("% 8.2f |", $game{"price"}[$i] / 100);
printf("% 12s |", date("%Y-%b-%d", $game{"release"}[$i]));
printf(" %s%".(43 - length(substr($game{"title"}[$i], 0, 43)))."s|\n", substr($game{"title"}[$i], 0, 42), " ");
if($i == $totalEntries)
{
goto BREAK;
}
}
BREAK:
printf("+---------+---------+-------------+--------------------------------------------+\n\n");
}
# add games to database
for(my $i = 1; $i < scalar(@{$game{"appID"}}); $i++)
{
my $query = qq{
INSERT INTO sc_steamgames
(
`appid`,
`title`,
`releasedate`,
`lastupdate`,
`flags`
)
VALUES
(
?,
?,
?,
UNIX_TIMESTAMP(),
1
)
ON DUPLICATE KEY UPDATE lastupdate = UNIX_TIMESTAMP(), flags = flags | 1 & ~ 2
};
my $stmt = $db->prepare($query);
my $res = $stmt->execute($game{"appID"}[$i], $game{"title"}[$i], $game{"release"}[$i]);
$stmt->finish;
$query = qq|
INSERT INTO sc_steamgameprices
(
`appid`,
$country
)
VALUES
(
?,
?
)
ON DUPLICATE KEY UPDATE $country = ?
|;
$stmt = $db->prepare($query);
$res = $stmt->execute($game{"appID"}[$i], $game{"price"}[$i], $game{"price"}[$i]);
$stmt->finish;
}
}
# set new flag for outdated games
my $query = qq|UPDATE sc_steamgames SET flags = 0 WHERE `lastupdate` < (UNIX_TIMESTAMP() - 360*24) |;
my $stmt = $db->prepare($query);
$stmt->execute();
$db->disconnect;
printf("Elapsed time: ".Time::HiRes::tv_interval($start)."seconds\n");