Environment Configuration
Relevant source files
The following files were used as context for generating this wiki page:
The Environment Configuration system provides a centralized mechanism for managing application settings, credentials, and operational parameters. It ensures that the Spotify API Data Collector can securely access external services while maintaining flexibility across different execution environments (development, testing, and production).
The configuration relies on environment variables, typically loaded from a .env file at the project root, to define sensitive credentials and tuning parameters for the data crawler and the SQLite database.
Sources: app/config.py:1-10, README.md:52-65
Centralized Configuration Architecture
The application utilizes a Config class located in app/config.py to aggregate all settings. This class uses python-dotenv to load variables from the environment into Python attributes. This centralization allows all components, such as the SpotifyClient and DataProcessor, to access consistent configuration values through a single interface.
The diagram above illustrates how the .env file serves as the primary source of truth, feeding the Config class which then distributes parameters to specific system components.
Sources: app/config.py:12-25, service/spotify_client.py:28-30
Configuration Parameters
The configuration is divided into three main categories: Authentication, Storage, and Operational Tuning.
Authentication and Storage
The application requires valid Spotify Developer credentials and a database connection string.
| Parameter | Source Variable | Default Value | Description |
|---|---|---|---|
| Spotify Client ID | SPOTIPY_CLIENT_ID | None (Required) | Client ID from Spotify Dashboard |
| Spotify Secret | SPOTIPY_CLIENT_SECRET | None (Required) | Client Secret from Spotify Dashboard |
| Database URL | DATABASE_URL | sqlite:///spotify_data.db | Connection string for SQLite |
Sources: app/config.py:17-21, README.md:55-60
Operational Tuning
These parameters control the volume of data collected and the intensity of API requests to prevent rate limiting.
| Parameter | Source Variable | Default Value | Description |
|---|---|---|---|
| Tracks Per Year | TRACKS_PER_YEAR | 1000 | Number of tracks searched per year |
| Top N Tracks | TOP_N_TRACKS | 200 | Number of most popular tracks kept |
| Rate Limit Delay | API_RATE_LIMIT_DELAY | 0.1 | Seconds to wait between API calls |
| Artist Delay | ARTIST_FETCH_DELAY | 0.05 | Seconds to wait for artist enrichment |
Sources: app/config.py:23-28, README.md:61-65
Validation Logic
The system includes a mandatory validation step executed at application startup. The Config.validate() method checks for the presence of essential Spotify credentials. If these are missing or empty, the application raises a ValueError and terminates the process to prevent partial or failed data collection.
The sequence diagram shows the validation flow during the main execution loop.
Sources: app/config.py:30-38, app/main.py:24-33
Database Environment Initialization
The environment also dictates how the database is initialized. The Database class in app/db.py parses the DATABASE_URL. If the URL follows the sqlite:/// pattern, it extracts the local file path. If the directory containing the database file does not exist, the system creates it automatically.
class Database:
def __init__(self, db_path: str = None):
if db_path is None:
try:
# Extracts path from sqlite:///spotify_data.db
url = Config.DATABASE_URL
if url.startswith('sqlite:///'):
self.db_path = url.replace('sqlite:///', '')
except:
self.db_path = 'spotify_data.db'
# Ensure directory exists
db_dir = os.path.dirname(os.path.abspath(self.db_path))
if db_dir and not os.path.exists(db_dir):
os.makedirs(db_dir)
Sources: app/db.py:21-41, test/test_db.py:127-142
Summary of Environment Configuration
The environment configuration is a critical foundation for the Spotify API Data Collector. By utilizing the Config class and .env files, the system achieves a clean separation between code and configuration. This setup enables strict validation of credentials, customizable rate limiting for respectful API usage, and automated database environment preparation, ensuring a robust startup process for both the crawler and the FastAPI interface.