2022-11-24 21:10:22 +00:00
|
|
|
import gc
|
|
|
|
|
2022-11-24 13:34:36 +00:00
|
|
|
from typing import List
|
2022-11-23 07:24:05 +00:00
|
|
|
import musicbrainzngs
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2022-11-24 13:34:36 +00:00
|
|
|
from . import (
|
|
|
|
database,
|
2023-02-01 15:19:17 +00:00
|
|
|
not_used_anymore,
|
|
|
|
target
|
2022-11-24 13:34:36 +00:00
|
|
|
)
|
2023-02-06 08:16:28 +00:00
|
|
|
|
2022-11-24 13:34:36 +00:00
|
|
|
|
2022-11-23 07:24:05 +00:00
|
|
|
from .utils.shared import (
|
|
|
|
MUSIC_DIR,
|
|
|
|
NOT_A_GENRE
|
|
|
|
)
|
2022-11-24 13:56:51 +00:00
|
|
|
|
2022-12-06 16:55:07 +00:00
|
|
|
# from .lyrics import lyrics
|
2022-11-16 09:37:41 +00:00
|
|
|
|
2022-11-28 16:18:26 +00:00
|
|
|
|
2022-11-24 21:10:22 +00:00
|
|
|
"""
|
|
|
|
At the start I modify the garbage collector to run a bit fewer times.
|
|
|
|
This should increase speed:
|
|
|
|
https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/
|
|
|
|
"""
|
|
|
|
# Clean up what might be garbage so far.
|
|
|
|
gc.collect(2)
|
|
|
|
|
|
|
|
allocs, gen1, gen2 = gc.get_threshold()
|
|
|
|
allocs = 50_000 # Start the GC sequence every 50K not 700 allocations.
|
|
|
|
gen1 = gen1 * 2
|
|
|
|
gen2 = gen2 * 2
|
|
|
|
gc.set_threshold(allocs, gen1, gen2)
|
|
|
|
|
2022-11-22 13:53:29 +00:00
|
|
|
logging.getLogger("musicbrainzngs").setLevel(logging.WARNING)
|
|
|
|
musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeIIow2/music-downloader")
|
2022-11-15 12:04:44 +00:00
|
|
|
|
2022-11-24 13:34:36 +00:00
|
|
|
# define the most important values and function for import in the __init__ file
|
2022-11-24 17:25:49 +00:00
|
|
|
Song = database.Song
|
2022-11-30 15:15:38 +00:00
|
|
|
Artist = database.Artist
|
2022-12-06 16:55:07 +00:00
|
|
|
Source = database.Source
|
2023-01-20 22:05:15 +00:00
|
|
|
SourceTypes = database.SourceTypes
|
|
|
|
SourcePages = database.SourcePages
|
2022-12-06 16:55:07 +00:00
|
|
|
Target = database.Target
|
|
|
|
Lyrics = database.Lyrics
|
2022-12-07 14:51:38 +00:00
|
|
|
Album = database.Album
|
2023-01-30 22:54:21 +00:00
|
|
|
|
2023-01-14 13:41:40 +00:00
|
|
|
ID3Timestamp = database.ID3Timestamp
|
2023-01-30 22:54:21 +00:00
|
|
|
|
|
|
|
cache = database.cache
|
|
|
|
Database = database.Database
|
2022-11-24 17:25:49 +00:00
|
|
|
|
2022-11-24 14:42:50 +00:00
|
|
|
|
|
|
|
|
2022-11-24 13:56:51 +00:00
|
|
|
def set_targets(genre: str):
|
|
|
|
target.set_target.UrlPath(genre=genre)
|
|
|
|
|
2022-11-24 17:25:49 +00:00
|
|
|
|
2022-11-24 13:34:36 +00:00
|
|
|
def fetch_sources(songs: List[Song], skip_existing_files: bool = True):
|
2023-02-01 15:19:17 +00:00
|
|
|
not_used_anymore.fetch_sources(songs=songs, skip_existing_files=skip_existing_files)
|
2022-11-24 13:34:36 +00:00
|
|
|
|
2022-11-24 17:25:49 +00:00
|
|
|
|
2022-11-24 13:34:36 +00:00
|
|
|
def fetch_audios(songs: List[Song], override_existing: bool = False):
|
2023-02-01 15:19:17 +00:00
|
|
|
not_used_anymore.fetch_audios(songs=songs, override_existing=override_existing)
|
2022-11-24 13:34:36 +00:00
|
|
|
|
2022-11-22 13:25:01 +00:00
|
|
|
|
2022-11-29 10:47:46 +00:00
|
|
|
def clear_cache():
|
|
|
|
cache.init_db(reset_anyways=True)
|
|
|
|
|
2022-11-15 12:04:44 +00:00
|
|
|
def get_existing_genre():
|
|
|
|
valid_directories = []
|
|
|
|
for elem in os.listdir(MUSIC_DIR):
|
|
|
|
if elem not in NOT_A_GENRE:
|
|
|
|
valid_directories.append(elem)
|
|
|
|
|
|
|
|
return valid_directories
|
|
|
|
|
2022-11-23 18:15:55 +00:00
|
|
|
|
2022-11-23 07:10:22 +00:00
|
|
|
def help_search_metadata():
|
|
|
|
msg = """
|
2022-11-23 22:44:25 +00:00
|
|
|
- - - Available Options - - -
|
2022-11-22 08:24:25 +00:00
|
|
|
.. - Previous Options
|
|
|
|
(query_string) - Search for songs, albums, bands...
|
|
|
|
(int) - Select an item from the search results
|
|
|
|
d - Start the download
|
|
|
|
h - Help
|
|
|
|
q - Quit / Exit
|
2022-11-23 22:44:25 +00:00
|
|
|
|
|
|
|
- - - How the Query works (examples) - - -
|
|
|
|
> #a <any artist>
|
|
|
|
searches for the artist <any artist>
|
|
|
|
|
|
|
|
> #a <any artist> #r <any releas>
|
|
|
|
searches for the release (album) <any release> by the artist <any artist>
|
|
|
|
|
|
|
|
> #r <any release> Me #t <any track>
|
|
|
|
searches for the track <any track> from the release <any relaese>
|
2022-11-23 07:10:22 +00:00
|
|
|
"""
|
|
|
|
print(msg)
|
2022-11-22 08:24:25 +00:00
|
|
|
|
2022-11-23 07:10:22 +00:00
|
|
|
|
2022-11-15 12:04:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_genre():
|
|
|
|
existing_genres = get_existing_genre()
|
|
|
|
print("printing available genres:")
|
|
|
|
for i, genre_option in enumerate(existing_genres):
|
|
|
|
print(f"{i}: {genre_option}")
|
|
|
|
|
|
|
|
genre = input("Input the ID for an existing genre or text for a new one: ")
|
|
|
|
|
|
|
|
if genre.isdigit():
|
|
|
|
genre_id = int(genre)
|
|
|
|
if genre_id >= len(existing_genres):
|
|
|
|
logging.warning("An invalid genre id has been given")
|
|
|
|
return get_genre()
|
|
|
|
return existing_genres[genre_id]
|
|
|
|
|
|
|
|
return genre
|