forked from doxxx/lodestone-recipe-db-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
589 lines (472 loc) · 18.3 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import argparse
import asyncio
import hashlib
import json
import os
import re
import shutil
import signal
import sys
import time
from typing import Mapping, Sequence
from urllib.parse import urlunparse
import aiohttp
import lxml.html as html
import tqdm
# type definitions
LanguageMapping = Mapping[str,Mapping[str,str]]
# Restore default Ctrl-C handler for faster process shutdown
signal.signal(signal.SIGINT, signal.SIG_DFL)
RECIPE_LIST_URL = "http://na.finalfantasyxiv.com/lodestone/playguide/db/recipe/"
ITEM_LIST_URL = "http://na.finalfantasyxiv.com/lodestone/playguide/db/item/"
CACHE_EXPIRY = 60*60*12 # 12 hours, in seconds
LANG_HOSTS = {
"en": "na.finalfantasyxiv.com",
"ja": "jp.finalfantasyxiv.com",
"fr": "fr.finalfantasyxiv.com",
"de": "de.finalfantasyxiv.com",
}
CLASSES = [
"carpenter",
"blacksmith",
"armorer",
"goldsmith",
"leatherworker",
"weaver",
"alchemist",
"culinarian",
]
# recipe level -> [ 0-star adjustment, 1-star adjustment, ... ]
LEVEL_DIFF = {
# verified against app
50: [ 0, 5, 20, 40, 60 ], # 50, 55, 70, 90, 110
# unconfirmed from desynthesis results
51: [ 69 ], # 120
52: [ 73 ], # 125
53: [ 77 ], # 130
54: [ 79 ], # 133
55: [ 81 ], # 136
56: [ 83 ], # 139
57: [ 85 ], # 142
58: [ 87 ], # 145
59: [ 89 ], # 148
60: [ 90, 100, 120, 150, 190 ], # 150, 160, 180, 210, 250
61: [ 199 ], # 260
62: [ 203 ], # 265
63: [ 207 ], # 270
64: [ 209 ], # 273
65: [ 211 ], # 276
66: [ 213 ], # 279
67: [ 215 ], # 282
68: [ 217 ], # 285
69: [ 219 ], # 288
70: [ 220, 230, 250, 280, 310 ], # 290, 300, 320, 350, 380
71: [ 319 ], # 390
72: [ 323 ], # 395
73: [ 327 ], # 400
74: [ 329 ], # 403
75: [ 331 ], # 406
76: [ 333 ], # 409
77: [ 335 ], # 412
78: [ 337 ], # 415
79: [ 339 ], # 418
80: [ 350, 360, 370, 400, 430, 436 ], # 430, 440, 450, 480, 510, 516
81: [ 436 ], # 517 # Yeah idfk how these numbers were acquired, are they even necessary?
82: [ 438 ], # 520 # okay turns out they are kinda necessary
83: [ 442 ], # 525 # find them by recipe itemlevel - level
84: [ 446 ], # 530 # e.g.: 530 - 84 = 446
85: [ 450 ], # 535 # thanks for wasting considerable amounts of my time by just not documenting shit properly :/
86: [ 454 ], # 540
87: [ 458 ], # 545
88: [ 462 ], # 550
89: [ 468 ], # 555
90: [ 470, 480 ] # 560, 570
}
MAX_LEVEL = 90
LEVEL_RANGES = ["{0}-{1}".format(start, start + 4) for start in range(1, MAX_LEVEL, 5)]
NUM_LEVEL_RANGES = len(LEVEL_RANGES)
NUM_ADDITIONAL_RECIPE_CATEGORIES = 8
RECIPE_LINK_CATEGORIES = ['%d' % (level_range,) for level_range in range(0, NUM_LEVEL_RANGES)] + \
['c%d' % (cat,) for cat in range(1, NUM_ADDITIONAL_RECIPE_CATEGORIES + 1)]
EMBED_CODE_RE = re.compile("\\[db:[a-z]+=([0-9a-f]+)]")
ASPECT_RE = re.compile("Aspect: (.+)")
RECIPE_CRAFTSMANSHIP_RE = re.compile("Craftsmanship (?:Required|Recommended): ([0-9]+)")
RECIPE_CONTROL_RE = re.compile("Control (?:Required|Recommended): ([0-9]+)")
ITEM_CRAFTSMANSHIP_RE = re.compile(r"Craftsmanship \+([0-9]+)% \(Max ([0-9]+)\)")
ITEM_CONTROL_RE = re.compile(r"Control \+([0-9]+)% \(Max ([0-9]+)\)")
ITEM_CP_RE = re.compile(r"CP \+([0-9]+)% \(Max ([0-9]+)\)")
ITEM_CAT_MEDICINE = 44
ITEM_CAT_MEAL = 46
ITEM_CATEGORIES = {
44: "Medicine",
46: "Meal",
}
FETCH_SEMAPHORE: asyncio.Semaphore
def logInfo(msg):
print(msg, end="\n", file=sys.stderr)
def logError(msg):
print(f"\nERROR: {msg}", end="\n", file=sys.stderr)
async def wait_with_progress(coros: list, desc: str = None, unit: str = None):
for f in tqdm.tqdm(asyncio.as_completed(coros), total=len(coros), desc=desc, unit=unit):
yield await f
def get_cache_key(url: str, **kwargs):
m = hashlib.sha1()
m.update(bytes(url, "UTF-8"))
sorted_keys = list(kwargs.keys())
sorted_keys.sort()
for k in sorted_keys:
m.update(bytes(str(k), "UTF-8"))
m.update(b"=")
m.update(bytes(str(kwargs[k]), "UTF-8"))
m.update(b";")
return m.hexdigest()
def get_cached_text(url: str, **kwargs):
key = get_cache_key(url, **kwargs)
os.makedirs(".cache", exist_ok=True)
filename = f".cache/{key}"
try:
s = os.stat(filename)
if s.st_mtime < time.time() - CACHE_EXPIRY:
os.remove(filename)
return None
with open(filename, mode="rt", encoding="UTF-8") as f:
return f.read()
except FileNotFoundError:
return None
def cache_text(text: str, url: str, **kwargs):
key = get_cache_key(url, **kwargs)
os.makedirs(".cache", exist_ok=True)
with open(f".cache/{key}", mode="wt", encoding="UTF-8") as f:
f.write(text)
async def fetch(session: aiohttp.ClientSession, url: str, **kwargs):
# TODO: cache files
cached_text = get_cached_text(url, **kwargs)
if cached_text is not None:
return cached_text
err_count = 0
while err_count < 5:
# noinspection PyBroadException
try:
async with FETCH_SEMAPHORE:
async with session.get(url, **kwargs) as res:
if res.status == 429:
retry_after = int(res.headers["retry-after"] or '5')
await asyncio.sleep(retry_after)
continue
elif res.status != 200:
raise Exception(f"{res.status} {res.reason}")
text = await res.text()
cache_text(text, url, **kwargs)
return text
except SystemExit:
raise
except:
err_count += 1
await asyncio.sleep(5)
pass
logError(f"Could not load page after 5 tries: {url}")
raise SystemExit
def parse_links_page(text: str) -> (Sequence[str], int, int):
tree = html.fromstring(text)
rel_links = tree.xpath("//div/@data-ldst-href")
links = map(str, rel_links)
show_end = int(tree.xpath("//span[@class='show_end']/text()")[0])
total = int(tree.xpath("//span[@class='total']/text()")[0])
return links, show_end, total
async def fetch_recipe_links_page(session: aiohttp.ClientSession, cls: str, category: str, page: int) -> (Sequence[str], int, int):
params = {
"category2": CLASSES.index(cls),
"category3": category,
"page": page,
}
return parse_links_page(await fetch(session, RECIPE_LIST_URL, params=params))
async def fetch_recipe_links_range(session: aiohttp.ClientSession, cls: str, category: str):
links = []
page = 1
while True:
page_links, show_end, total = await fetch_recipe_links_page(session, cls, category, page)
links += page_links
if show_end < total:
page += 1
else:
break
return links
async def fetch_recipe_links(session: aiohttp.ClientSession, cls: str):
results = wait_with_progress(
[fetch_recipe_links_range(session, cls, category) for category in RECIPE_LINK_CATEGORIES],
desc=f"Fetching {cls.capitalize()} links",
unit=""
)
links = []
async for r in results:
links.extend(r)
return links
def make_lang_url(lang: str, rel_link: str):
return urlunparse(("http", LANG_HOSTS[lang], rel_link, "", "", ""))
async def fetch_pages_all_langs(session: aiohttp.ClientSession, rel_link: str):
pages = {}
for lang in LANG_HOSTS:
pages[lang] = html.fromstring(await fetch(session, make_lang_url(lang, rel_link)))
return pages
def extract_db_id(tree) -> str:
embed_code = tree.xpath("//div[@class='embed_code_txt']//div[contains(text(), 'db:')]/text()")[0]
match = EMBED_CODE_RE.match(embed_code)
if match is None:
raise Exception("embed id not found")
return match.group(1)
async def fetch_recipe(session: aiohttp.ClientSession, rel_link: str):
pages = await fetch_pages_all_langs(session, rel_link)
tree = pages["en"]
recipe_id = extract_db_id(tree)
detail_box = tree.xpath("//div[@class='recipe_detail item_detail_box']")[0]
base_level = int(detail_box.xpath("//span[@class='db-view__item__text__level__num']/text()")[0])
stars = None
level_adjustment = 0
if base_level in LEVEL_DIFF:
stars = len(detail_box.xpath("//div[@class='db-view__item__text__level']//span[contains(@class, 'star')]"))
try:
level_adjustment = LEVEL_DIFF[base_level][stars]
except IndexError:
logError(f"Unsupported number of stars ({stars}) for level {base_level}. Item: {max_quality}")
raise SystemExit
level = base_level + level_adjustment
craft_data = tree.xpath("//ul[@class='db-view__recipe__craftdata']")[0]
difficulty = int(craft_data.xpath("li[span='Difficulty']/text()")[0])
durability = int(craft_data.xpath("li[span='Durability']/text()")[0])
max_quality = int(craft_data.xpath("li[span='Maximum Quality']/text()")[0])
# Base level 51 recipes of difficulty 169 or 339 are adjusted to level 115
# instead of the default 120 that other level 51 recipes are adjusted to.
if ((base_level == 51 and (difficulty == 169 or difficulty == 339)) or
(base_level == 61 and (difficulty == 1116 or difficulty == 558))):
level -= 5
if base_level == 60 and stars == 3 and difficulty == 1764:
level += 10
aspect = None
suggested_craftsmanship = None
suggested_control = None
craft_conditions = tree.xpath("//dl[@class='db-view__recipe__crafting_conditions']")[0]
for characteristic in craft_conditions.xpath("dt[text()='Characteristics']/../dd/text()"):
characteristic = characteristic.strip()
match = ASPECT_RE.match(characteristic)
if not match is None:
aspect = match.group(1)
match = RECIPE_CRAFTSMANSHIP_RE.match(characteristic)
if not match is None:
suggested_craftsmanship = int(match.group(1))
match = RECIPE_CONTROL_RE.match(characteristic)
if not match is None:
suggested_control = int(match.group(1))
recipe = {
"id": recipe_id,
"name": {},
"baseLevel": base_level,
"level": level,
"difficulty": difficulty,
"durability": durability,
"maxQuality": max_quality,
}
if stars:
recipe["stars"] = stars
if aspect:
recipe["aspect"] = aspect
if suggested_craftsmanship:
recipe["suggestedCraftsmanship"] = suggested_craftsmanship
if suggested_control:
recipe["suggestedControl"] = suggested_control
for lang in LANG_HOSTS:
tree = pages[lang]
recipe["name"][lang] = str(tree.xpath("//h2[contains(@class,'db-view__item__text__name')]/text()")[0]).strip()
return recipe
async def fetch_recipes(session: aiohttp.ClientSession, cls: str, links: Sequence[str]):
recipes = wait_with_progress(
[fetch_recipe(session, link) for link in links],
desc=f"Fetching {cls.capitalize()} recipes",
unit=""
)
return [r async for r in recipes]
async def fetch_class(session: aiohttp.ClientSession, additional_languages: LanguageMapping, cls: str):
links = await fetch_recipe_links(session, cls)
recipes = await fetch_recipes(session, cls, links)
recipes.sort(key=lambda r: (r['level'], r['name']['en']))
for recipe in recipes:
for lang in additional_languages.keys():
names = additional_languages[lang]
english_name = recipe['name']['en']
recipe['name'][lang] = names.get(english_name) or english_name
return recipes
async def scrape_classes(session: aiohttp.ClientSession, additional_languages: LanguageMapping, classes: Sequence[str]):
for cls in classes:
recipes = await fetch_class(session, additional_languages, cls)
with open(f"out/{cls.capitalize()}.json", mode="wt", encoding="utf-8") as db_file:
json.dump(recipes, db_file, indent=2, sort_keys=True, ensure_ascii=False)
async def fetch_item_links_page(session: aiohttp.ClientSession, category: int, page: int):
params = {
"category2": 5,
"category3": category,
"page": page,
}
return parse_links_page(await fetch(session, ITEM_LIST_URL, params=params))
async def fetch_item_links_range(session: aiohttp.ClientSession, category: int):
links = []
page = 1
while True:
page_links, show_end, total = await fetch_item_links_page(session, category, page)
links += page_links
if show_end < total:
page += 1
else:
break
return links
async def fetch_item_links(session: aiohttp.ClientSession, category: int):
results = wait_with_progress(
[fetch_item_links_range(session, category)],
desc=f"Fetching {ITEM_CATEGORIES[category]} links",
unit=""
)
links = []
async for r in results:
links.extend(r)
return links
def extract_item_attr(text, item):
found_attr = False
for m in ITEM_CRAFTSMANSHIP_RE.finditer(text):
found_attr = True
item["craftsmanship_percent"] = int(m.group(1))
item["craftsmanship_value"] = int(m.group(2))
for m in ITEM_CONTROL_RE.finditer(text):
found_attr = True
item["control_percent"] = int(m.group(1))
item["control_value"] = int(m.group(2))
for m in ITEM_CP_RE.finditer(text):
found_attr = True
item["cp_percent"] = int(m.group(1))
item["cp_value"] = int(m.group(2))
return found_attr
async def fetch_item(session: aiohttp.ClientSession, rel_link: str) -> (dict, dict):
pages = await fetch_pages_all_langs(session, rel_link)
tree = pages["en"]
item_id = extract_db_id(tree)
item_nq = {
"id": item_id,
"name": {},
"hq": False,
}
item_hq = {
"id": item_id,
"name": {},
"hq": True,
}
info_text_div = tree.xpath("//div[@class='db-view__info_text']")[0]
info_text_nq = "".join(info_text_div.xpath("//ul[@class='sys_nq_element']//text()")).strip()
info_text_hq = "".join(info_text_div.xpath("//ul[@class='sys_hq_element']//text()")).strip()
has_nq_attr = extract_item_attr(info_text_nq, item_nq)
has_hq_attr = extract_item_attr(info_text_hq, item_hq)
if not has_nq_attr and not has_hq_attr:
return []
for lang in LANG_HOSTS:
tree = pages[lang]
item_nq["name"][lang] = str(tree.xpath("//h2[contains(@class,'db-view__item__text__name')]/text()")[0]).strip()
item_hq["name"][lang] = str(tree.xpath("//h2[contains(@class,'db-view__item__text__name')]/text()")[0]).strip()
return [item_nq, item_hq]
async def fetch_items(session: aiohttp.ClientSession, links: Sequence[str]):
items = wait_with_progress(
[fetch_item(session, link) for link in links],
desc=f"Fetching items",
unit=""
)
return [item async for nq_hq in items for item in nq_hq]
async def fetch_items_category(session: aiohttp.ClientSession, additional_languages: LanguageMapping, category: int):
links = await fetch_item_links(session, category)
items = await fetch_items(session, links)
items.sort(key=lambda r: r['name']['en'])
for item in items:
for lang in additional_languages.keys():
names = additional_languages[lang]
english_name = item['name']['en']
item['name'][lang] = names.get(english_name) or english_name
return items
async def scrape_buffs(session: aiohttp.ClientSession, additional_languages: LanguageMapping):
for category in ITEM_CATEGORIES.keys():
category_name = ITEM_CATEGORIES[category]
items = await fetch_items_category(session, additional_languages, category)
with open(f"out/{category_name}.json", mode="wt", encoding="utf-8") as db_file:
json.dump(items, db_file, indent=2, sort_keys=True, ensure_ascii=False)
def load_additional_languages(specs: Sequence[str]) -> LanguageMapping:
additional_languages = {}
for s in specs:
lang, path = s.split("=", 2)
with open(path, mode="rt", encoding="utf-8") as fp:
print(f"Loading additional language '{lang}' from: {path}")
additional_languages[lang] = json.load(fp)
return additional_languages
async def async_main(args):
if not args.recipes and not args.buffs:
print("ERROR: One or more of the following options must be provided: --recipes, --buffs", file=sys.stderr)
return
if args.clear_cache:
shutil.rmtree(".cache")
if args.lang_file:
additional_languages = load_additional_languages(args.lang_file)
else:
additional_languages = {}
global FETCH_SEMAPHORE
FETCH_SEMAPHORE = asyncio.Semaphore(args.concurrency)
session = aiohttp.ClientSession()
try:
if args.recipes:
classes = [cls.lower() for cls in args.recipes]
if "all" in classes:
classes = CLASSES
await scrape_classes(session, additional_languages, classes)
if args.buffs:
await scrape_buffs(session, additional_languages)
except KeyboardInterrupt:
pass
finally:
await session.close()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--concurrency",
metavar="N",
help="Max number of concurrent requests to Lodestone servers. [Default: 8]",
default=8,
type=int,
)
parser.add_argument(
"-l",
"--lang-file",
metavar="LANG=FILE",
help="Language code and path to file that defines mappings from English names to another language.",
action="append",
)
parser.add_argument(
"-r",
"--recipes",
metavar="CLASS",
help=f"Scrape recipes for a class. Can be specified more than once to collect multiple classes, or use 'all' to collect all classes. Classes: {', '.join(CLASSES)}",
action="append",
choices=['all'] + CLASSES,
)
parser.add_argument(
"-b",
"--buffs",
help="Scrap buff items.",
action="store_true"
)
parser.add_argument(
"--clear-cache",
help="Clear the download cache.",
action="store_true",
)
return parser.parse_args()
def main():
args = parse_args()
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(async_main(args))
finally:
loop.close()
if __name__ == '__main__':
main()