-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGW2_Items_Collector.pl
More file actions
115 lines (97 loc) · 4.5 KB
/
GW2_Items_Collector.pl
File metadata and controls
115 lines (97 loc) · 4.5 KB
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
#!/usr/bin/perl -w
# GW2_Items_Collector.pl
# Author: fookenCode
# Description: Make calls against the GW2 API framework to gather information about the in-game Items.
# Then store this information in Relational Database for cached usage to limit call volume to services.
# Usage: GW2_Items_Collector.pl --startPage (optional) --debug (optional)
use strict;
use warnings;
use Data::Dumper;
use DBI;
use Getopt::Long;
use JSON::XS;
use LWP::UserAgent;
my $DEBUG = 0;
my $startPage = 1;
GetOptions(
"startPage:i" => \$startPage,
"debug!" => \$DEBUG
);
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $dbh = DBI->connect("dbi:mysql:database=<database>;host=<host>;port=<port>", "<user>", "<pw>")
or die "Couldn't connect: $DBI::errstr\n";
my $sth = $dbh->prepare("insert into items (itemId, name, description, itemType, level, rarity, ".
"vendorValue, icon, chatLink, itemSubType, armorWeight, miniPetId, ".
"bagSlotsTotal, itemSellable) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?) on duplicate key update ".
"name = VALUES(name), description = VALUES(description), itemType = VALUES(itemType), ".
"level = VALUES(level), rarity = VALUES(rarity), vendorValue = VALUES(vendorValue), ".
"icon = VALUES(icon), chatLink = VALUES(chatLink), itemSubType = VALUES(itemSubType), ".
"armorWeight = VALUES(armorWeight), miniPetId = VALUES(miniPetId), ".
"bagSlotsTotal = VALUES(bagSlotsTotal), itemSellable = VALUES(itemSellable)");
my $ath = $dbh->prepare("update items set itemSellable = ? where itemId = ?");
my $response = $ua->get("https://api.guildwars2.com/v2/items?page=$startPage&page_size=200");
if ($response->is_success)
{
print Dumper $response if $DEBUG;
my $totalPages = $response->{'_headers'}{'x-page-total'};
parseResponseForFullEntry($response);
# Loop over the number of pages in the initial response header
for (my $iter = $startPage+1; $iter < $totalPages; $iter+=1)
{
$response = $ua->get("https://api.guildwars2.com/v2/items?page=$iter&page_size=200");
if ($response->{'_headers'}{'x-page-total'} != $totalPages)
{
print "[ERROR]: Total number of pages no longer accurate!\n";
print "Original value: $totalPages. New Value: " . $response->{'_headers'}{'x-page-total'}."\n";
# Exit the program to allow for manual restart
exit 1;
}
parseResponseForFullEntry($response);
print "Processed request #$iter of $totalPages. Sleeping 1 seconds...\n";
# Rudimentary sleep to self-throttle requests
sleep 1;
print "Next request in progress.\n" if $DEBUG;
}
}
$ath->finish();
$sth->finish();
$dbh->disconnect();
sub parseResponseForFullEntry {
my ($response) = @_;
foreach my $item (@{decode_json($response->{'_content'})})
{
$sth->bind_param(1, $item->{'id'});
$sth->bind_param(2, $item->{'name'});
$sth->bind_param(3, $item->{'description'});
$sth->bind_param(4, $item->{'type'});
$sth->bind_param(5, $item->{'level'});
$sth->bind_param(6, $item->{'rarity'});
$sth->bind_param(7, $item->{'vendor_value'});
$sth->bind_param(8, $item->{'icon'});
$sth->bind_param(9, $item->{'chat_link'});
$sth->bind_param(10, $item->{'details'}->{'type'});
$sth->bind_param(11, $item->{'details'}->{'weight_class'});
$sth->bind_param(12, $item->{'details'}->{'minipet_id'});
$sth->bind_param(13, $item->{'details'}->{'size'});
# Negate the grep value, multiply by 1 to change to Numeric value to store in the column
$sth->bind_param(14, (!(grep { "NoSell" eq $_ } @{$item->{'flags'}}) * 1));
$sth->execute();
}
}
sub parseResponseForSellableFieldEntry {
my ($response) = @_;
my $canSell = 1;
my $filterValue = "NoSell";
foreach my $item (@{decode_json($response->{'_content'})})
{
if (grep { $filterValue eq $_ } @{$item->{'flags'}})
{
$canSell = 0;
$ath->bind_param(1, $canSell);
$ath->bind_param(2, $item->{'id'});
$ath->execute();
}
}
}