style: Run linter and fix code style issues in wanikani_summary.py
This commit is contained in:
parent
256b6f2cda
commit
bfba29dfc1
|
@ -1,16 +1,19 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Iterator
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from typing import Iterator
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from personal_data.data import DeduplicateMode
|
from personal_data.data import DeduplicateMode
|
||||||
from personal_data.fetchers import Scraper
|
from personal_data.fetchers import Scraper
|
||||||
from personal_data.secrets import wanikani_api_key
|
from personal_data.secrets import wanikani_api_key
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class WaniKaniSummaryFetcher(Scraper):
|
class WaniKaniSummaryFetcher(Scraper):
|
||||||
dataset_name: str = "wanikani_summary"
|
dataset_name: str = 'wanikani_summary'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def deduplicate_mode() -> DeduplicateMode:
|
def deduplicate_mode() -> DeduplicateMode:
|
||||||
|
@ -19,28 +22,30 @@ class WaniKaniSummaryFetcher(Scraper):
|
||||||
def scrape(self) -> Iterator[dict]:
|
def scrape(self) -> Iterator[dict]:
|
||||||
token = wanikani_api_key()
|
token = wanikani_api_key()
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {token}",
|
'Authorization': f'Bearer {token}',
|
||||||
"Wanikani-Revision": "20170710",
|
'Wanikani-Revision': '20170710',
|
||||||
}
|
}
|
||||||
url = "https://api.wanikani.com/v2/summary"
|
url = 'https://api.wanikani.com/v2/summary'
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
lessons = data.get("data", {}).get("lessons", [])
|
lessons = data.get('data', {}).get('lessons', [])
|
||||||
total_lessons = sum(len(lesson.get("subject_ids", [])) for lesson in lessons)
|
total_lessons = sum(len(lesson.get('subject_ids', [])) for lesson in lessons)
|
||||||
|
|
||||||
reviews = data.get("data", {}).get("reviews", [])
|
reviews = data.get('data', {}).get('reviews', [])
|
||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
total_reviews = 0
|
total_reviews = 0
|
||||||
for review in reviews:
|
for review in reviews:
|
||||||
available_at_str = review.get("available_at")
|
available_at_str = review.get('available_at')
|
||||||
if available_at_str:
|
if available_at_str:
|
||||||
available_at = datetime.fromisoformat(available_at_str.replace("Z", "+00:00"))
|
available_at = datetime.fromisoformat(
|
||||||
|
available_at_str.replace('Z', '+00:00'),
|
||||||
|
)
|
||||||
if available_at <= now:
|
if available_at <= now:
|
||||||
total_reviews += len(review.get("subject_ids", []))
|
total_reviews += len(review.get('subject_ids', []))
|
||||||
|
|
||||||
yield {
|
yield {
|
||||||
"lessons_available": total_lessons,
|
'lessons_available': total_lessons,
|
||||||
"reviews_available": total_reviews,
|
'reviews_available': total_reviews,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user