Skip to main content
Annie Mei uses the Spotify Web API to search for anime opening and ending theme songs and provide direct links to them. This enhances the theme song information from MyAnimeList with playable music links.

Authentication

The bot uses the Client Credentials Flow for server-to-server authentication. This requires:
Register your application at the Spotify Developer Dashboard to obtain these credentials.

Client Setup

Source: src/utils/spotify.rs:15-27

Search Functionality

The main function searches for a song using both romaji and kana (Japanese) names:
Source: src/utils/spotify.rs:29-33

Search Strategy

  1. Try Romaji First: Search using the romanized song name
  2. Fallback to Kana: If no results, try the native Japanese name (if available)
  3. Return URL: Return the Spotify track URL from the top result
Source: src/utils/spotify.rs:49-82

Search Request

Searches are formatted as combined track and artist queries:
Source: src/utils/spotify.rs:89-103

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:
Source: src/utils/spotify.rs:105-118

Redis Caching

Search results are cached in Redis to reduce API calls and improve response times:
Source: src/utils/spotify.rs:35-47

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
Source: src/utils/spotify.rs:54 and src/utils/spotify.rs:85

Usage Example

The Spotify integration is called from the MyAnimeList response transformer:
Source: src/models/mal_response.rs:99-108

Error Handling

The search function returns Option<String> for graceful handling:
  • Some(url): Song found on Spotify
  • None: No match found (displays unlinked song name)
Source: src/utils/spotify.rs:83-84

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
The rspotify library handles token management automatically, requesting new tokens as needed.

Dependencies

The Spotify integration uses the rspotify crate:
Key imports:
Source: src/utils/spotify.rs:1-10

Logging

The integration includes detailed logging for debugging:
These logs help track search performance, cache effectiveness, and API response quality.