-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper_crown_store.py
275 lines (229 loc) · 8.45 KB
/
scraper_crown_store.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
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
### IMPORTING
import requests
from bs4 import BeautifulSoup
### GLOBALS
URL_SPECIAL_OFFERS = 'https://www.elderscrollsonline.com/en-us/crownstore/category/36'
URL_FEATURED = 'https://www.elderscrollsonline.com/en-us/crownstore'
URL_ESOP_DEALS = 'https://www.elderscrollsonline.com/en-us/crownstore/eso-plus'
DESIRED_ITEMS = """dlc
Orsinium
Guilds and Glory: The 4-DLC Mega-Pack
Thieves Guild
Dark Brotherhood
Shadows of the Hist
Horns of the Reach
The Clockwork City
Dragon Bones
Wolfhunter
Murkmire
Wrathstone
Summerset
Scalebreaker
Dragonhold
Harrowstorm
Elsweyr
Stonethorn
Markarth
Flames of Ambition
Greymoor
Year Two Mega-Pack: DLC Bundle
Season of the Dragon: DLC Bundle
Waking Flame
Deadlands
Ascending Tide
Blackwood
Year Three Mega-Pack: DLC Bundle
Lost Depths
Firesong
Dark Heart of Skyrim: DLC Bundle
Gates of Oblivion: DLC Bundle
Scribes of Fate
High Isle""".lower().split('\n')
### FUNCTIONS
def get_html(url):
return requests.get(url).content
def scrape(html):
# Opening the soup
soup = BeautifulSoup(html, 'html.parser')
# Get items
crown_items = soup.find_all("crown-item")
item_list = []
for item in crown_items:
# Parse title
title = item.h4.find(class_='crown-title').text.strip()
# Parse time left
if item.h4.find(class_='time-left'):
time_left = item.find(class_='time-left').text.strip().replace('Time Remaining', '')
else:
time_left = None
# Parse link
link = 'https://www.elderscrollsonline.com' + item.a['href']
# Parse cost
if item.find(class_='eso-plus-label'):
# CASE 1: there are either a cost in crowns or a deal for eso+ subscribers
_costs_crowns = item.find(class_='crowns-price').find_all(class_='sr-only')
cost_crowns = _costs_crowns[0].text.strip()
if cost_crowns:
if len(_costs_crowns)>1:
cost_esop_crowns = _costs_crowns[1].text.strip()
else:
cost_esop_crowns = 'EXCLUSIVE!'
else:
cost_esop_crowns = 'FREE!'
cost_gems = None
cost_seals = None
elif item.find(class_='gems-price'):
# CASE 2: there are either cost in gems or seals
cost_crowns = None
cost_esop_crowns = None
cost_gems = item.find(class_='gems-price').find(class_='sr-only').text.strip()
cost_seals = item.find(class_='seals-price').find(class_='sr-only').text.strip()
elif item.find(class_='crowns-price').find(class_='sr-only'):
# CASE 3: only crown cost
cost_crowns = item.find(class_='crowns-price').find(class_='sr-only').text.strip()
cost_esop_crowns = None
cost_gems = None
cost_seals = None
else:
# CASE 4: no cost
cost_crowns = 'FREE!'
cost_esop_crowns = None
cost_gems = None
cost_seals = None
# Packing everything up
item_list += [{
'item_title': title,
'item_link': link,
'item_time_left': time_left,
'item_cost_crowns': cost_crowns,
'item_cost_esop_crowns': cost_esop_crowns,
'item_cost_gems': cost_gems,
'item_cost_seals': cost_seals
}]
return item_list
### CLASSES
class ScrapedCategory():
def __init__(self, category):
# Import category Special offers
if category == 'special_offers':
self.html = get_html(URL_SPECIAL_OFFERS)
self.list = scrape(self.html)
self.header = 'Special offers'
# Import category Featured
elif category == 'featured':
self.html = get_html(URL_FEATURED)
self.list = scrape(self.html)
self.header = 'Featured'
# Import catgeory ESO Plus Deals
elif category == 'esop_deals':
self.html = get_html(URL_ESOP_DEALS)
self.list = scrape(self.html)
self.header = 'ESO+ deals'
# Build bot-selection category
elif category == 'best_deals':
self.header = 'Best deals'
self._special_offers = ScrapedCategory('special_offers')
self._featured = ScrapedCategory('featured')
self._esop_deals = ScrapedCategory('esop_deals')
super_list = self._special_offers.list + self._featured.list + self._esop_deals.list
self.list = []
for i in super_list:
accept_item = False
# Acceptance criteria 1: it's amid the desired items
if i['item_title'].lower() in DESIRED_ITEMS:
accept_item = True
# Acceptance criteria 2: item is on sale
if i['item_cost_crowns']:
if ' Sale Price' in i['item_cost_crowns']:
accept_item = True
# Acceptance criteria 3: item is free
if 'FREE' in i['item_cost_crowns']:
accept_item = True
# Acceptance criteria 4: item is in sale for eso+ owners
if i['item_cost_esop_crowns']:
accept_item = True
if accept_item:
self.list += [i]
# Remove duplicates in the final instance according to a key
self.remove_duplicates()
self.sort_list()
def sort_list(self):
self.list = sorted(self.list, key=lambda d: d['item_title'])
def remove_duplicates(self, key='item_link'):
keys_set = set()
result = []
for _dict in self.list:
_value = _dict[key]
if _value not in keys_set:
keys_set.add(_value)
result.append(_dict)
self.list = result
@property
def title(self):
return f"""**💸 {self.header} ({len(self.list)})**"""
@property
def markdown(self):
markdown = f"""
**💸 {self.header} ({len(self.list)})**{self.markdown_no_title}
"""
return markdown
@property
def markdown_no_title(self):
def apply_markdown_cost(string):
return string.replace(' Crowns', '👑').replace(' Crown Gems', '💎').replace(' Sale Price','👑 Sale Price')
def apply_markdown_exp(string):
return string.replace('(0 day left)', '(expiring today)')
# No items in the category
if self.list == []:
return "\n🤷 No items today, sorry"
# Build row
_list = []
for i in self.list:
# Title
if i['item_title'].lower() in DESIRED_ITEMS:
_row = f"[**{i['item_title']}**](<{i['item_link']}>)" # bold if there are dlcs
else:
_row = f"[{i['item_title']}](<{i['item_link']}>)"
# Cost
if i['item_cost_esop_crowns']:
if i['item_cost_crowns']:
_row += apply_markdown_cost(f" {i['item_cost_crowns']}, 🏆 {i['item_cost_esop_crowns']}")
else: # free items in "eso+ deals"
_row += apply_markdown_cost(f" 🏆 {i['item_cost_esop_crowns']}")
elif i['item_cost_gems']:
_row += apply_markdown_cost(f" {i['item_cost_gems']} or {i['item_cost_seals']}")
else:
_row += apply_markdown_cost(f" {i['item_cost_crowns']}")
# Time left
if i['item_time_left']:
_row += apply_markdown_exp(f" ({i['item_time_left']})")
_list += [_row]
list_str = '\n'.join(_list)
# Build final markup
markdown = f"""
{list_str}
"""
return markdown
def move_from_featured_to_esop(featured: ScrapedCategory, esop_deals: ScrapedCategory):
items_to_remove = []
for i in featured.list:
if i['item_cost_esop_crowns']:
esop_deals.list.append(i)
items_to_remove.append(i)
for item in items_to_remove:
featured.list.remove(item)
esop_deals.remove_duplicates()
esop_deals.sort_list()
return featured, esop_deals
### MAIN
if __name__ == '__main__':
# Special offers
special_offers = ScrapedCategory('special_offers')
# Featured
featured = ScrapedCategory('featured')
# ESO+ deals
esop_deals = ScrapedCategory('esop_deals')
# Move eso+ deals from featured category
featured, esop_deals = move_from_featured_to_esop(featured, esop_deals)
# Best deals
best_deals = ScrapedCategory('best_deals')