Indexer¶
The indexer is responsible for indexing the music library on the filesystem and populating the database with the available metadata and information.
The indexer runs in two stages:
Scan music directories - Scan the filesystem for new music files and store them in the database.
Extract album art - Extract and store the album art from newly indexed releases. Generate thumbnails for the frontend.
The following function executes both stages:
-
src.indexer.
run_indexer
()¶ - Return type
None
Code documentation for the indexer implementation is as follows:
Scan music directories¶
Functions:
|
Given a file, enter its information into the database. |
|
Every time we have a batch of tracks, run some logic. |
Read the music directories to be indexed from the configuration and scan them for new files. |
|
|
Scan a given directory for music files and catalog the discovered files. |
-
src.indexer.scanner.
catalog_file
(filepath, conn)¶ Given a file, enter its information into the database. If associated database objects, e.g. artists and albums, don’t exist, they are created with information from the track.
If a track with this file’s sha256 already exists in the database, the filepath of the existing database row will be updated to the new filepath. No metadata updating will happen.
- Parameters
filepath (
str
) – The filepath of the music file.conn (
sqlite3.Connection
) – A connection to the database.
- Return type
-
src.indexer.scanner.
handle_track_batch
(track_batch)¶ Every time we have a batch of tracks, run some logic. This is logic that has a cost–we don’t want to run it once every track, but not to the point where we want to only run it once.
- Return type
None
-
src.indexer.scanner.
scan_directories
()¶ Read the music directories to be indexed from the configuration and scan them for new files.
- Return type
None
-
src.indexer.scanner.
scan_directory
(directory)¶ Scan a given directory for music files and catalog the discovered files.
- Parameters
directory (
pathlib.Path
) – The directory to scan.- Raises
NotADirectoryError – If the directory does not exist.
- Return type
None
Extract album art¶
Functions:
|
If the track has attached cover art, save it to the cover_arts dir with the sha256 of the cover art as the filename. |
For the releases with pending covers-to-save, look at their first track for a cover art to save. |
-
src.indexer.covers.
save_image
(tf, conn)¶ If the track has attached cover art, save it to the cover_arts dir with the sha256 of the cover art as the filename.
Otherwise, look in the directory of the file and the directory above it for a cover or folder file (case insensitive) if embedded art does not exist.
If neither exist, return None.
- Parameters
tf (
tagfiles.TagFile
) – The tagfile whose image we want to save.- Return type
typing.Optional
[src.library.image.T
]- Returns
The filepath of the saved image, if one was saved.
-
src.indexer.covers.
save_pending_covers
()¶ For the releases with pending covers-to-save, look at their first track for a cover art to save. If a cover art is found, save it to the
COVER_ART_DIR
, generate a thumbnail, and update the database with the filename.- Return type
None