Fix for IndexError and date parsing in deemon and deezer utils#121
Fix for IndexError and date parsing in deemon and deezer utils#121Np3ir wants to merge 2 commits intodigitalec:mainfrom
Conversation
Change Summary: Improved str_to_datetime_obj() for safer date parsing Previous behavior: The function only handled the specific case "0000-00-00" by replacing it with "1980-01-01". It did not handle other malformed or missing inputs and could raise exceptions without logging. New behavior: The updated version introduces input validation and exception handling: Handles None, empty strings, and any value starting with "0000" Logs a warning when an invalid date is found and defaults to "1980-01-01" Catches parsing errors and logs them as errors Returns None on failure Benefits: Prevents crashes from malformed or missing date strings Adds meaningful logs for debugging Improves reliability when consuming date data from external APIs
Hi Np3ir, Each time i run deemon refresh i have those errors, is it releated to the changes you suggest? :: Scanning release data for new releases... 62% |
|
When changing the dates.py with yours, i have this errors : PS C:\Users\tangs> deemon refresh |
|
I fixed the syntax erreor like this:
Thank you really much for this fix! |
🔧 Summary of Changes
This pull request includes two key improvements to enhance stability when handling edge cases in both date parsing and Deezer track metadata.
🗓️ 1. Improved Date Parsing in str_to_datetime_obj()
Before:
def str_to_datetime_obj(d: str) -> datetime:
if d == "0000-00-00":
d = "1980-01-01"
return datetime.strptime(d, "%Y-%m-%d")
After:
def str_to_datetime_obj(d: str) -> datetime | None:
try:
if not d or d.strip() == "" or d.startswith("0000"):
logger.warning(f"[dates] Invalid date detected ('{d}'), defaulting to 1980-01-01.")
return datetime.strptime("1980-01-01", "%Y-%m-%d")
return datetime.strptime(d, "%Y-%m-%d")
except Exception as e:
logger.error(f"[dates] Error parsing date '{d}': {e}")
return None
Benefits:
Note: Although the fix was applied in the deezer/utils.py dependency folder, it is required to prevent crashes caused by invalid API data. If this module is bundled in this repo, the patch ensures stability."
🎵 2. Safe Access to track['MEDIA'] in Deezer Track Mapping
Before:
result['preview'] = track['MEDIA'][0]['HREF']
After:
if 'MEDIA' in track and isinstance(track['MEDIA'], list) and len(track['MEDIA']) > 0:
result['preview'] = track['MEDIA'][0].get('HREF')
else:
result['preview'] = None
Benefits:
📌 These changes improve fault tolerance and make the tool more reliable when interacting with Deezer.