Skip to content

Commit 09d4351

Browse files
Updated MDL: Added more try-except block
1 parent 52eb742 commit 09d4351

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

python_scripts/mdl_psql.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
def get_synopsis(item_url: str) -> str:
1212
details_url = f"https://kuryana.vercel.app/id{item_url}"
1313
try:
14-
response = requests.request("GET", details_url).json()
15-
synopsis_str = str(response["data"]["synopsis"])
16-
synopsis = synopsis_str.replace("'", "''") if synopsis_str != "" else "No synopsis available"
17-
return synopsis
14+
response_raw = requests.request("GET", details_url)
15+
if response_raw.status_code == 200:
16+
response = response_raw.json()
17+
synopsis_str = str(response["data"]["synopsis"])
18+
synopsis = synopsis_str.replace("'", "''") if synopsis_str != "" else "No synopsis available"
19+
return synopsis
20+
else:
21+
raise Exception(f"URL: {details_url}. Response: {response_raw.text}")
1822
except Exception as e:
1923
print(f"{datetime.now()} ERROR: {str(e)}")
2024
return "No synopsis available"
@@ -65,10 +69,22 @@ def inset_into_db(content_list: list) -> int:
6569
released_at = item["released_at"]
6670
except:
6771
released_at = "2099-12-31"
68-
url = f"https://mydramalist.com{item['url']}"
69-
genres = item["genres"]
70-
thumbnail = item["thumbnail"]
71-
cover = item["cover"]
72+
try:
73+
url = f"https://mydramalist.com{item['url']}"
74+
except:
75+
raise Exception("No URL Found")
76+
try:
77+
genres = item["genres"]
78+
except:
79+
genres = "Dfault"
80+
try:
81+
thumbnail = item["thumbnail"]
82+
except:
83+
thumbnail = "https://i.mydramalist.com/_4t.jpg"
84+
try:
85+
cover = item["cover"]
86+
except:
87+
cover = "https://i.mydramalist.com/_4c.jpg"
7288
try:
7389
rating = item["rating"]
7490
except:

python_scripts/mdl_psql_history.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,29 @@
1010

1111
try:
1212
scraper = cloudscraper.create_scraper()
13-
for year in range(2022, current_year + 1):
13+
for year in range((current_year - 2), (current_year + 1)):
1414
if year == current_year:
1515
for q in range(1, current_quarter + 1):
1616
current_seasonal = scraper.post(
1717
url="https://mydramalist.com/v1/quarter_calendar",
1818
data={"quarter": q, "year": year},
1919
)
20-
temp_list = current_seasonal.json()
21-
mdl_list.extend(temp_list)
20+
if current_seasonal.status_code == 200:
21+
temp_list = current_seasonal.json()
22+
mdl_list.extend(temp_list)
23+
else:
24+
raise Exception(f"URL: {current_seasonal.url}. Response: {current_seasonal.text}")
2225
else:
2326
for q in range(1, 5):
2427
history_seasonal = scraper.post(
2528
url="https://mydramalist.com/v1/quarter_calendar",
2629
data={"quarter": q, "year": year},
2730
)
28-
temp_list = history_seasonal.json()
29-
mdl_list.extend(temp_list)
31+
if history_seasonal.status_code == 200:
32+
temp_list = history_seasonal.json()
33+
mdl_list.extend(temp_list)
34+
else:
35+
raise Exception(f"URL: {history_seasonal.url}. Response: {history_seasonal.text}")
3036
except Exception as e:
3137
print(f"{datetime.now()} ERROR: {str(e)}")
3238
finally:

python_scripts/mdl_seasonal_data.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ def get_seasonal_data() -> list:
99
current_quarter = ((now.month - 1) // 3) + 1
1010

1111
scraper = cloudscraper.create_scraper()
12-
seasonal = scraper.post(
12+
seasonal_raw = scraper.post(
1313
url="https://mydramalist.com/v1/quarter_calendar",
1414
data={"quarter": current_quarter, "year": current_year},
15-
).json()
16-
17-
return seasonal
15+
)
16+
if seasonal_raw.status_code == 200:
17+
return seasonal_raw.json()
18+
else:
19+
raise Exception(f"URL: {seasonal_raw.url}. Response: {seasonal_raw.text}")
1820
except Exception as e:
1921
print(f"{datetime.now()} ERROR: Got error while fetching API data. {str(e)}")
2022
return []

0 commit comments

Comments
 (0)