-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstardew_scraper.py
293 lines (231 loc) · 7.65 KB
/
stardew_scraper.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from pywikiapi import *
import os
import re
import sys
# Constants
FIRST_BUNDLE_INDEX = 2
LAST_BUNDLE_INDEX = 6
SECTION_STR = "=="
TABLE_START = "{|"
TABLE_END = "|}"
SLOT_IMAGE = "File:Bundle Slot.png"
ITEM_START = "{{"
ITEM_DELIM = "|"
ITEM_END = "}}"
ROW = "|-"
REDIRECT = "#REDIRECT"
LINK_START = "[["
LINK_END = "]]"
class Room:
def __init__(self, name, bundles):
self.name = name
self.bundles = bundles
def print_csv(self, file):
for b in self.bundles:
for i in b.item_counts:
item_amount = b.item_counts[i]
write_items = [self.name, b.name, '', str(b.num_needed), '', i.get_name(), str(item_amount),
str(int(i.spring)), str(int(i.summer)), str(int(i.fall)), str(int(i.winter)),
i.get_clean_description()
]
file.write("\"" + "\",\"".join(write_items) + "\"\n")
def __str__(self):
string = self.name + "\n"
for b in self.bundles:
string += str(b)
return string
class Bundle:
def __init__(self, name, num_needed, item_counts):
self.name = name
self.num_needed = num_needed
self.item_counts = item_counts
def __str__(self):
string = self.name + ": " + str(self.num_needed) + "\n"
for i in self.items:
string += str(i) + "\n"
return string
class Item:
def __init__(self, name, spring, summer, fall, winter, description):
self.attributes = ""
self.name = name
self.spring = spring
self.summer = summer
self.fall = fall
self.winter = winter
self.description = description
def set_attributes(self, attributes):
self.attributes = attributes
def get_name(self):
return self.attributes + self.name
def get_clean_description(self):
split_links = re.split('\[\[|\]\]|\'\'\'|\{\{|\|class=inline|\}\}', self.description)
for i in range(len(split_links)):
split_attributes = split_links[i].split('|')
split_links[i] = split_attributes[-1] \
+ ("g" if "price" in split_attributes[0].lower() \
and len(split_attributes) > 1 else "")
clean_description = ''.join(split_links)
clean_description = clean_description.replace("\"", "'")
return clean_description
def get_rooms(wikitext, site):
rooms = []
for i in range(FIRST_BUNDLE_INDEX, LAST_BUNDLE_INDEX + 1):
rooms.append(create_room(i, wikitext, site))
return rooms
def create_room(num, wikitext, site):
# Get section name
str_index = 0
for i in range(0, num - 1):
for i in range(2):
str_index = wikitext.index(SECTION_STR, str_index) + 2
start_index = wikitext.index(SECTION_STR, str_index) + 2
end_index = wikitext.index(SECTION_STR, start_index)
section_name = wikitext[start_index:end_index]
if print_flag: print("Creating Room: " + section_name)
# Get section text
section_end = wikitext.index(SECTION_STR, end_index + 2)
section_text = wikitext[end_index+2:section_end]
bundle_list = read_bundles(section_text, site)
return Room(section_name, bundle_list)
def read_bundles(section_text, site):
bundle_list = []
table_index = -1
while True:
# Get table text
try:
table_index = section_text.index(TABLE_START, table_index + 1)
except:
break
end_index = section_text.index(TABLE_END, table_index) + 2
table_text = section_text[table_index:end_index]
# Get table name
try:
id_index = table_text.index("id=") + 4
except:
continue
id_end = table_text.index("\"", id_index)
id_text = table_text[id_index:id_end]
if print_flag: print("Creating Bundle: " + id_text)
# Get number of slots
num_slots = table_text.count(SLOT_IMAGE)
# Get items
item_dict = read_items(table_text, site)
bundle_list.append(Bundle(id_text, num_slots, item_dict))
return bundle_list
def read_items(table_text, site):
item_dict = {}
start_row = table_text.index(ROW)
while True:
try:
end_row = table_text.index(ROW, start_row + 1)
name_start = table_text.index(ITEM_START, start_row, end_row)
except:
break
name_start = table_text.index(ITEM_DELIM, name_start, end_row) + 1
name_end = table_text.index(ITEM_END, name_start, end_row)
gold_quality = "gold" in table_text[name_start:name_end]
try:
name_end = table_text.index(ITEM_DELIM, name_start, name_end)
attribute_start = name_end + 1
attribute_end = table_text.index(ITEM_END, attribute_start, end_row)
except:
pass
item_name = table_text[name_start:name_end]
amount = 1
if "<td>" in table_text[start_row:end_row]:
attribute_start = table_text.index("(", start_row, end_row) + 1
attribute_end = table_text.index(")", start_row, end_row)
try:
amount = int(table_text[attribute_start:attribute_end])
attribute_start=-1
attribute_start=-1
except:
pass
if print_flag: print("Creating item: " + item_name)
item = get_item_info(item_name, site)
if gold_quality:
item.set_attributes("Gold Quality ")
item_dict[item] = amount
start_row = end_row
return item_dict
def get_item_info(item_name, site):
page_name = item_name
while True:
if print_flag: print("Querying: " + page_name)
response = site(action="parse", page=page_name, prop=["wikitext"])
wikitext = response["parse"]["wikitext"]
if not wikitext.startswith(REDIRECT):
break
if print_flag: print(page_name + " " + wikitext)
start_index = wikitext.index("[[") + 2
end_index = wikitext.index("]]")
page_name = wikitext[start_index:end_index]
if download_files:
fname = "wiki_pages\\" + item_name + ".txt"
with open(fname, "w", encoding="utf-8") as f:
f.write(wikitext)
info_end = get_info_box_end(wikitext)
spring = summer = fall = winter = False
try:
season_index = wikitext.index("season", 0, info_end)
season_index = wikitext.index("=", season_index) + 2
season_end = wikitext.index("\n", season_index)
season_name = wikitext[season_index:season_end].lower()
spring = season_name.find("spring") >= 0
summer = season_name.find("summer") >= 0
fall = season_name.find("fall") >= 0
winter = season_name.find("winter") >= 0
except:
pass
if ((not spring) and (not summer) and (not fall) and (not winter)):
spring = summer = fall = winter = True
description_start = wikitext.index("\n", info_end) + 1
description_end = wikitext.index("\n", description_start)
return Item(item_name, spring, summer, fall, winter, wikitext[description_start:description_end])
def get_info_box_end(wikitext):
opening = -2
closing = 0
num_opened = 0
num_closed = 0
while (num_opened != num_closed or num_closed == 0):
closing = wikitext.index("}}", closing + 2)
num_closed += 1
while True:
try:
opening = wikitext.index("{{", opening + 2, closing)
num_opened += 1
except:
break
return closing
def set_print_flag(bool_val):
global print_flag
print_flag = bool_val
def set_download_files(bool_val):
global download_files
download_files = bool_val
def get_site():
return Site("https://stardewvalleywiki.com/mediawiki/api.php")
def main():
set_print_flag(("-p") in sys.argv)
set_download_files(("-f") in sys.argv)
site = get_site()
response = site(action="parse", page="Bundles", prop=["wikitext"])
if print_flag: print("Querying Bundles")
if not os.path.exists("wiki_pages"):
os.makedirs("wiki_pages")
# Uncomment to save xml to a file
if download_files:
fname = "wiki_pages\\Bundles.txt"
with open(fname, "w", encoding="utf-8") as f:
f.write(response["parse"]["wikitext"])
room_list = get_rooms(response["parse"]["wikitext"], site)
if print_flag: print("Generating csv...")
stardew_csv = open("community_center.csv", "w")
header_items = ["Room", "Bundle", "Completed", "Amount Needed", "Have Item", "Item", "Amount", "Spring", "Summer", "Fall", "Winter", "Description"]
stardew_csv.write("\"" + "\",\"".join(header_items) + "\"\n")
for room in room_list:
room.print_csv(stardew_csv)
stardew_csv.close()
if __name__ == "__main__":
main()
print("Finished!")