Spaces:
Runtime error
Runtime error
Download services/csv_parser.py from Gillenn/filmatch-api: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/Gillenn/filmatch-api/resolve/main/services/csv_parser.py
- Command line
-
hf download hf://spaces/Gillenn/filmatch-api/services/csv_parser.py
-
curl -L -o csv_parser.py https://huggingface.co/spaces/Gillenn/filmatch-api/resolve/main/services/csv_parser.py
1.71 kB
| import pandas as pd | |
| from io import BytesIO | |
| from datetime import datetime | |
| from typing import List, Dict, Set | |
| def parse_letterboxd_csv(file_content: bytes) -> List[Dict]: | |
| try: | |
| df = pd.read_csv(BytesIO(file_content)) | |
| except Exception as e: | |
| raise ValueError(f"Impossible de lire le CSV : {e}") | |
| required = {"Date", "Name", "Year", "Letterboxd URI", "Rating"} | |
| missing = required - set(df.columns) | |
| if missing: | |
| raise ValueError(f"Colonnes manquantes : {missing}") | |
| records = [] | |
| for _, row in df.iterrows(): | |
| try: | |
| rating_val = row["Rating"] | |
| if pd.isna(rating_val): | |
| continue | |
| rating = float(rating_val) | |
| date_viewed = datetime.strptime(str(row["Date"]).strip(), "%Y-%m-%d") | |
| name = str(row["Name"]).strip() | |
| year = int(row["Year"]) if pd.notna(row["Year"]) else None | |
| key = (name + "_" + str(year)) if year else name | |
| records.append({"key": key, "name": name, "year": year, "rating": rating, "date_viewed": date_viewed}) | |
| except Exception: | |
| continue | |
| return records | |
| def parse_watchlist_csv(file_content: bytes) -> Set[str]: | |
| try: | |
| df = pd.read_csv(BytesIO(file_content)) | |
| except Exception: | |
| return set() | |
| if "Name" not in df.columns: | |
| return set() | |
| keys = set() | |
| for _, row in df.iterrows(): | |
| try: | |
| name = str(row["Name"]).strip() | |
| year = int(row["Year"]) if pd.notna(row.get("Year", float("nan"))) else None | |
| key = (name + "_" + str(year)) if year else name | |
| keys.add(key) | |
| except Exception: | |
| continue | |
| return keys | |