Backend for managing a music database with MongoDB Atlas, using FastAPI.
- Search Songs: Flexible filtering based on various fields.
- List Collections: Retrieve a list of all collections in the database.
- Add Songs: Add a new song to a specified collection, with duplicate checks.
TrackFinder/
│
├── main.py # Entry point of the application, runs the FastAPI app
├── models/
│ ├── __init__.py # Initializes the models package
│ └── songModels.py # Defines the SongMetadata model
├── music_folder/ # Contains JSON files with song metadata
│ └── *.json # Example: rock.json contains metadata for rock songs
├── routers/
│ ├── __init__.py # Initializes the routers package
│ └── general_use_router.py # Contains route definitions
├── .env # Environment variables for MongoDB connection
├── .gitignore # Git ignore file
└── seeding_db.py # Script for seeding the database with JSON files
Ensure you have a .env
file with the following content:
MONGO_DB_USER= ...
MONGO_DB_PASSWORD= ...
So the software can get those and use them here:
MONGO_DB_URL=mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority
To seed the database with song metadata from JSON files, run:
python seeding_db.py
This will populate MongoDB Atlas with your song collections.
To run the application:
python main.py
This will host the API on http://127.0.0.1:8000
by default.
- Endpoint:
/search_songs
- Method: POST
- Description: Search for songs based on criteria.
- Parameters:
title
,artist
,album
,release_year
,genre
... (all optional).
- Endpoint:
/collections
- Method: GET
- Description: Retrieve a list of all collections in the database.
- Endpoint:
/add_song
- Method: POST
- Description: Add a new song to a specified collection. If the collection does not exist, it will be created.
- Parameters:
collection_name
: The name of the collection (path parameter).SongMetadata
: The song data (body parameter).
from pydantic import BaseModel
from typing import Optional
class Song_metadata(BaseModel):
name: str
title: str
sub_title: str
bitrate: int
commentaries: str
main_artist: str
collaborators: str
album_artist: str
album: str
year: str
track_number: str
genre: str
duration: int
Access detailed API documentation generated by FastAPI at http://127.0.0.1:8000/docs
after running the application.
I don't like to use spotify, so I rather manually select and download music.
The thing is that now I have at least 900 songs divided in multiple folders. If I want to make a new playlist I do not know anymore if I already placed a song in another playlist before. So I proceeded to:
-
Manually added the metadata for all the songs I downloaded (not funny at all)
-
I used another project of mine called MusicToMetaData to extract the metadata into json files. Like that evey folder became a single json file (rock, salsa, etc) and inside every json file there is the metadata of the songs in the json format.
-
I uploaded all the files to mondoDB atlas, making for evey file a collection and every song, a document.
-
I developed this backend software so I can check if any new song I like in the future has been already added to a collection.