Building Utilities
Crunchyroll Downloader โ Practical Scripting
Course 1 ยท Chapter 4 ยท Building Utilities
๐ง Building Crunchyroll Utilities
Create reusable utility functions for fetching episode data, parsing responses, and handling errors. Build a CrunchyrollClient class that encapsulates all API interactions.
๐ฆ CrunchyrollClient Class
class CrunchyrollClient: """Reusable Crunchyroll API client.""" def __init__(self, config, token: str): self.config = config self.token = token self.base_url = config.api_base self.episodes_fetched = 0 def _get(self, endpoint: str) -> dict: """Make authenticated GET request.""" url = f"{self.base_url}{endpoint}" headers = { "Authorization": f"Bearer {self.token}" } response = requests.get(url, headers=headers, timeout=10) if response.status_code == 200: return response.json() return None def get_episodes(self) -> list: """Fetch all watched episodes.""" episodes = [] try: data = self._get("/api/v2/episodes") if data: episodes = data.get('items', []) self.episodes_fetched = len(episodes) except Exception as e: print(f"Error: {e}") return episodes
๐ป Coding Challenges
Challenge 1: Build CrunchyrollClient
Create class with __init__, _get(), and get_episodes() methods.
Challenge 2: Episode Parsing
Parse episode data to extract show name, episode name, date watched.
Challenge 3: Error Handling
Handle API errors, token expiration, network issues gracefully.