forked from atlasacademy/fgo-game-data-api
-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_rayshift_quest_list.py
97 lines (82 loc) · 3.11 KB
/
load_rayshift_quest_list.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
import argparse
import time
import httpx
from app.db.load import (
get_all_missing_query_ids,
get_missing_query_ids,
load_rayshift_quest_details,
load_rayshift_quest_list,
)
from app.rayshift.quest import get_all_quest_lists, get_multiple_quests
from app.schemas.common import Region
def main(
quest_ids: list[int],
load_all: bool = False,
no_load: bool = False,
selected_region: str | None = None,
) -> None:
client = httpx.Client(follow_redirects=True, timeout=60)
for region in [Region.NA, Region.JP]:
if selected_region and region != selected_region:
continue
print(f"Loading {region} rayshift data cache …")
start_loading_time = time.perf_counter()
try:
quest_list = get_all_quest_lists(client, region)
except httpx.TimeoutException:
print("TimeoutException: Failed to get quest list")
return
if not quest_list:
print(f"No quest list found for {region}")
continue
print(f"Inserting {region} rayshift data cache into db …")
load_rayshift_quest_list(region, quest_list)
if no_load:
continue
if load_all:
query_ids = get_all_missing_query_ids(region, quest_ids)
else:
query_ids = get_missing_query_ids(region, quest_ids)
print(f"Loading {len(query_ids)} query IDs")
if query_ids:
QUERY_IDS_PER_REQUEST = 25
for i in range(0, len(query_ids), QUERY_IDS_PER_REQUEST):
start_req_time = time.perf_counter()
request_query_ids = query_ids[i : i + QUERY_IDS_PER_REQUEST]
quest_details = get_multiple_quests(client, region, request_query_ids)
if quest_details:
load_rayshift_quest_details(region, quest_details)
load_req_time = round((time.perf_counter() - start_req_time) * 1000)
print(
f"Loaded {min(i + QUERY_IDS_PER_REQUEST, len(query_ids))} query IDs in {load_req_time}ms"
)
else:
print(f"Failed to fetch query IDs: {request_query_ids}")
rayshift_load_time = time.perf_counter() - start_loading_time
print(f"Loaded {region} rayshift in {rayshift_load_time:.2f}s.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Load Rayshift quest data into the db."
)
parser.add_argument(
"--all",
"-a",
help="Load all missing quest details instead of just the latest ones",
action="store_true",
)
parser.add_argument(
"--no-load", "-n", help="Don't load rayshift quest data", action="store_true"
)
parser.add_argument(
"--quest-id",
"-q",
help="quest_id to load rayshift quest data",
type=int,
action="append",
required=False,
)
parser.add_argument(
"--region", "-r", help="Region", type=str, required=False, default=None
)
args = parser.parse_args()
main(args.quest_id, args.all, args.no_load, args.region)