-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisthedeal_wrapper.py
223 lines (173 loc) · 6.84 KB
/
isthedeal_wrapper.py
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
import urllib3
import json
import logging
urllib3.disable_warnings()
http = urllib3.PoolManager()
regions = {}
__multiple_api_limit = 25
class PriceDeal:
def __init__(self, shop, region, price, cut):
self.shop = shop
self.region = region
self.price = price
self.cut = cut
@staticmethod
def from_deal(deal_j, region):
price = None
if 'price_new' in deal_j:
price = deal_j['price_new']
elif 'price' in deal_j:
price = deal_j['price']
cut = None
if 'price_cut' in deal_j:
cut = deal_j['price_cut']
elif 'cut' in deal_j:
cut = deal_j['cut']
return PriceDeal(deal_j['shop'], region, price, cut)
def __str__(self):
return "{}{} ({}%) on {}".format(self.price, self.region['currency'], self.cut, self.shop['name'])
def to_dict(self):
return {
'shop': self.shop,
'region': self.region,
'price': self.price,
'cut': self.cut
}
@staticmethod
def from_dict(ddata):
if ddata is None:
return None
return PriceDeal(ddata['shop'], ddata['region'], ddata['price'], ddata['cut'])
class Deal:
def __init__(self, game_id, game_plain, current, historical, country, shop=None):
self.game_id = game_id
self.game_plain = game_plain
self.current_j = current
self.historical_j = historical
self.country = country
region = get_region_by_country(country)
self.current = None
if 'list' in current.keys() and len(current['list']) > 0:
if shop is not None:
for d in current['list']:
if d['shop']['id'] == shop:
self.current = PriceDeal.from_deal(d, region)
if self.current is None:
self.current = PriceDeal.from_deal(current['list'][0], region)
self.historical = PriceDeal.from_deal(historical, region)
def __str__(self):
return "{}: {}\nCurrent: {}\nHistorical: {}".format(self.game_id,
self.game_plain,
self.current,
self.historical)
def to_dict(self):
return {
'game_id': self.game_id,
'game_plain': self.game_plain,
'current_j': self.current_j,
'historical_j': self.historical_j,
'country': self.country
}
@staticmethod
def from_dict(ddata):
if ddata is None:
return None
return Deal(ddata['game_id'], ddata['game_plain'], ddata['current_j'], ddata['historical_j'], ddata['country'])
def require_json(url):
request = http.request('GET', url)
if request.status == 200:
return json.loads(request.data.decode('utf-8'))
else:
logging.error("bad request: {}".format(request.status))
return None
def get_region_by_country(country):
if country in regions:
return regions[country]
j = require_json('https://api.isthereanydeal.com/v01/web/regions/')
region = None
if j is not None:
for r in j['data'].keys():
if country in j['data'][r]['countries']:
region = {'region': r, 'currency': j['data'][r]['currency']['sign']}
break
if region is not None:
regions[country] = region
else:
logging.error("invalid country {}".format(country))
return region
def get_game_plain_by_id(apy_key, game_id, shop='steam'):
url = 'https://api.isthereanydeal.com/v02/game/plain/?key={}&shop={}&game_id={}'.format(apy_key, shop, game_id)
j = require_json(url)
if j is not None and 'plain' in j['data']:
return j['data']['plain']
else:
logging.error("plain not found for game {}".format(game_id))
return None
# returns a {game_id: game_plain} dictionary
def get_multiple_plain_by_ids(api_key, id_list, shop='steam'):
if type(id_list) is str:
id_list = id_list.split(',')
out = {}
while len(id_list) > 0:
s_list = ",".join(id_list[:__multiple_api_limit])
id_list = id_list[__multiple_api_limit:]
url = 'https://api.isthereanydeal.com/v01/game/plain/id/?key={}&shop={}&ids={}'.format(api_key, shop, s_list)
j = require_json(url)
if j is not None:
out.update(j['data'])
else:
logging.warning("no plain found for ids: {}".format(s_list))
if len(out) > 0:
return out
return None
def __get_lowest(api_key, plains, country='IT'):
region = get_region_by_country(country)['region']
current_url = 'https://api.isthereanydeal.com/v01/game/prices/{}/?key={}&plains={}&country={}'.format(
region, api_key, plains, country)
historical_url = 'https://api.isthereanydeal.com/v01/game/lowest/{}/?key={}&plains={}'.format(
region, api_key, plains)
current_j = require_json(current_url)
historical_j = require_json(historical_url)
return current_j['data'], historical_j['data']
def get_game_lowest_prices(api_key, game_id, shop='steam', country='IT'):
plain = get_game_plain_by_id(api_key, game_id, shop)
if plain is None:
return None
cur_j, hist_j = __get_lowest(api_key, plain, country)
return Deal(game_id, plain, cur_j[plain], hist_j[plain], country)
def get_steam_price(api_key, game_id, country='IT'):
plain = get_game_plain_by_id(api_key, game_id, 'steam')
if plain is None:
return None
cur_j, hist_j = __get_lowest(api_key, plain, country)
return Deal(game_id, plain, cur_j[plain], hist_j[plain], country, 'steam')
# returns a Deal list
def get_multiple_games_lowest_prices(api_key, id_list, shop='steam', country='IT'):
plains_map = get_multiple_plain_by_ids(api_key, id_list, shop)
if plains_map is None:
return None
plains_list = [p for p in plains_map.values()]
dl = []
while len(plains_list) > 0:
p_list = ",".join(plains_list[:__multiple_api_limit])
plains_list = plains_list[__multiple_api_limit:]
cur_j, hist_j = __get_lowest(api_key, p_list, country)
for gid in id_list:
if gid in plains_map:
pl = plains_map[gid]
if pl in cur_j and pl in hist_j:
dl.append(Deal(gid, pl, cur_j[pl], hist_j[pl], country))
else:
logging.warning("gid {} was not resolved".format(gid))
for i in id_list:
found = False
for d in dl:
if d.game_id == i:
found = True
break
if not found:
if i in plains_map:
logging.warning("{} ({}) not found".format(plains_map[i], i))
else:
logging.warning("{} not found".format(i))
return dl