Skip to content

Commit

Permalink
Updated MDL: Added more try-except block
Browse files Browse the repository at this point in the history
  • Loading branch information
TheurgicDuke771 committed Jul 13, 2023
1 parent 52eb742 commit 09d4351
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
32 changes: 24 additions & 8 deletions python_scripts/mdl_psql.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
def get_synopsis(item_url: str) -> str:
details_url = f"https://kuryana.vercel.app/id{item_url}"
try:
response = requests.request("GET", details_url).json()
synopsis_str = str(response["data"]["synopsis"])
synopsis = synopsis_str.replace("'", "''") if synopsis_str != "" else "No synopsis available"
return synopsis
response_raw = requests.request("GET", details_url)
if response_raw.status_code == 200:
response = response_raw.json()
synopsis_str = str(response["data"]["synopsis"])
synopsis = synopsis_str.replace("'", "''") if synopsis_str != "" else "No synopsis available"
return synopsis
else:
raise Exception(f"URL: {details_url}. Response: {response_raw.text}")
except Exception as e:
print(f"{datetime.now()} ERROR: {str(e)}")
return "No synopsis available"
Expand Down Expand Up @@ -65,10 +69,22 @@ def inset_into_db(content_list: list) -> int:
released_at = item["released_at"]
except:
released_at = "2099-12-31"
url = f"https://mydramalist.com{item['url']}"
genres = item["genres"]
thumbnail = item["thumbnail"]
cover = item["cover"]
try:
url = f"https://mydramalist.com{item['url']}"
except:
raise Exception("No URL Found")
try:
genres = item["genres"]
except:
genres = "Dfault"
try:
thumbnail = item["thumbnail"]
except:
thumbnail = "https://i.mydramalist.com/_4t.jpg"
try:
cover = item["cover"]
except:
cover = "https://i.mydramalist.com/_4c.jpg"
try:
rating = item["rating"]
except:
Expand Down
16 changes: 11 additions & 5 deletions python_scripts/mdl_psql_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,29 @@

try:
scraper = cloudscraper.create_scraper()
for year in range(2022, current_year + 1):
for year in range((current_year - 2), (current_year + 1)):
if year == current_year:
for q in range(1, current_quarter + 1):
current_seasonal = scraper.post(
url="https://mydramalist.com/v1/quarter_calendar",
data={"quarter": q, "year": year},
)
temp_list = current_seasonal.json()
mdl_list.extend(temp_list)
if current_seasonal.status_code == 200:
temp_list = current_seasonal.json()
mdl_list.extend(temp_list)
else:
raise Exception(f"URL: {current_seasonal.url}. Response: {current_seasonal.text}")
else:
for q in range(1, 5):
history_seasonal = scraper.post(
url="https://mydramalist.com/v1/quarter_calendar",
data={"quarter": q, "year": year},
)
temp_list = history_seasonal.json()
mdl_list.extend(temp_list)
if history_seasonal.status_code == 200:
temp_list = history_seasonal.json()
mdl_list.extend(temp_list)
else:
raise Exception(f"URL: {history_seasonal.url}. Response: {history_seasonal.text}")
except Exception as e:
print(f"{datetime.now()} ERROR: {str(e)}")
finally:
Expand Down
10 changes: 6 additions & 4 deletions python_scripts/mdl_seasonal_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ def get_seasonal_data() -> list:
current_quarter = ((now.month - 1) // 3) + 1

scraper = cloudscraper.create_scraper()
seasonal = scraper.post(
seasonal_raw = scraper.post(
url="https://mydramalist.com/v1/quarter_calendar",
data={"quarter": current_quarter, "year": current_year},
).json()

return seasonal
)
if seasonal_raw.status_code == 200:
return seasonal_raw.json()
else:
raise Exception(f"URL: {seasonal_raw.url}. Response: {seasonal_raw.text}")
except Exception as e:
print(f"{datetime.now()} ERROR: Got error while fetching API data. {str(e)}")
return []
Expand Down

0 comments on commit 09d4351

Please sign in to comment.