1
0

style: Run linter and fix code style issues in wanikani_lessons.py

This commit is contained in:
Jon Michael Aanes (aider) 2025-03-16 14:52:13 +01:00
parent 7c087d7c61
commit 4d6bceb418

View File

@ -1,50 +1,55 @@
import dataclasses
import datetime import datetime
import logging import logging
from collections.abc import Iterator, Mapping
import requests import requests
import dataclasses
from typing import Iterator, Mapping
# Import the base Scraper class; adjust the import if your code structure differs. # Import the base Scraper class; adjust the import if your code structure differs.
from personal_data.data import Scraper from personal_data.data import Scraper
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
class WaniKaniLessonsFetcher(Scraper): class WaniKaniLessonsFetcher(Scraper):
api_token: str api_token: str
@staticmethod @staticmethod
def dataset_name() -> str: def dataset_name() -> str:
return "wanikani_lessons" return 'wanikani_lessons'
def scrape(self) -> Iterator[Mapping[str, object]]: def scrape(self) -> Iterator[Mapping[str, object]]:
""" """
Fetch assignments from the WaniKani API and yield a dict for each assignment Fetch assignments from the WaniKani API and yield a dict for each assignment
with a non-null unlocked_at timestamp. with a non-null unlocked_at timestamp.
""" """
url = "https://api.wanikani.com/v2/assignments" url = 'https://api.wanikani.com/v2/assignments'
headers = { headers = {
"Authorization": f"Bearer {self.api_token}", 'Authorization': f'Bearer {self.api_token}',
"Wanikani-Revision": "20170710" 'Wanikani-Revision': '20170710',
} }
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if response.status_code != 200: if response.status_code != 200:
logger.error("Error retrieving assignments: %s", response.text) logger.error('Error retrieving assignments: %s', response.text)
return return
data = response.json() data = response.json()
# Check that 'data' key exists in the JSON response. # Check that 'data' key exists in the JSON response.
assignments = data.get("data", []) assignments = data.get('data', [])
for assignment in assignments: for assignment in assignments:
assignment_data = assignment.get("data", {}) assignment_data = assignment.get('data', {})
# Only yield if unlocked_at is available. # Only yield if unlocked_at is available.
unlocked_at = assignment_data.get("unlocked_at") unlocked_at = assignment_data.get('unlocked_at')
if unlocked_at: if unlocked_at:
# Convert unlocked_at ISO8601 string (assume 'Z' for UTC) to a datetime object. # Convert unlocked_at ISO8601 string (assume 'Z' for UTC) to a datetime object.
try: try:
dt = datetime.datetime.fromisoformat(unlocked_at.replace("Z", "+00:00")) dt = datetime.datetime.fromisoformat(
unlocked_at.replace('Z', '+00:00'),
)
except Exception as e: except Exception as e:
logger.error("Error parsing unlocked_at '%s': %s", unlocked_at, e) logger.error("Error parsing unlocked_at '%s': %s", unlocked_at, e)
continue continue
yield { yield {
"subject_id": assignment_data.get("subject_id"), 'subject_id': assignment_data.get('subject_id'),
"unlocked_at": dt 'unlocked_at': dt,
} }