Authentication
The bot uses the Client Credentials Flow for server-to-server authentication. This requires:Client Setup
Search Functionality
The main function searches for a song using both romaji and kana (Japanese) names:Search Strategy
- Try Romaji First: Search using the romanized song name
- Fallback to Kana: If no results, try the native Japanese name (if available)
- Return URL: Return the Spotify track URL from the top result
Search Request
Searches are formatted as combined track and artist queries:Search Parameters
- Query:
track:{song_name} artist:{artist_name} - Type: Track search only
- Market: United States (for content availability)
- Limit: Top 5 results
Result Parsing
The top search result’s Spotify URL is extracted:Redis Caching
Search results are cached in Redis to reduce API calls and improve response times:Cache Strategy
- Key Format:
{romaji_name}:{kana_name}:{artist_name} - Cached on Success: Stores the Spotify URL
- Cached on Failure: Stores
"None"to avoid repeated failed searches - Cache Duration: Determined by Redis configuration
Usage Example
The Spotify integration is called from the MyAnimeList response transformer:Error Handling
The search function returnsOption<String> for graceful handling:
Some(url): Song found on SpotifyNone: No match found (displays unlinked song name)
Rate Limiting
Spotify enforces rate limits on their API:- Default Limit: 180 requests per minute for non-premium users
- Mitigation: Redis caching significantly reduces API calls
- Best Practice: Implement exponential backoff for 429 errors
rspotify library handles token management automatically, requesting new tokens as needed.
Dependencies
The Spotify integration uses therspotify crate:
