music-kraken-core/src/main.py

128 lines
3.7 KiB
Python
Raw Normal View History

2022-11-06 22:01:03 +00:00
from metadata.database import Database
from metadata.download import MetadataDownloader
import metadata.download
import metadata.search
2022-10-17 12:56:32 +00:00
import download_links
2022-10-17 22:27:30 +00:00
import url_to_path
2022-10-18 16:48:24 +00:00
import download
2022-10-21 19:24:38 +00:00
import logging
2022-10-21 19:24:38 +00:00
import os
2022-11-06 22:01:03 +00:00
import tempfile
2022-11-06 22:01:03 +00:00
logging.basicConfig(level=logging.INFO)
2022-10-24 11:55:34 +00:00
2022-11-06 22:01:03 +00:00
TEMP_FOLDER = "music-downloader"
DATABASE_FILE = "metadata.db"
DATABASE_STRUCTURE_FILE = "database_structure.sql"
2022-11-07 14:04:08 +00:00
DATABASE_STRUCTURE_FALLBACK = "https://raw.githubusercontent.com/HeIIow2/music-downloader/new_metadata/assets/database_structure.sql"
2022-11-06 22:01:03 +00:00
SEARCH_LOGGER = logging.getLogger("mb-cli")
2022-11-06 22:01:03 +00:00
DATABASE_LOGGER = logging.getLogger("database")
METADATA_DOWNLOAD_LOGGER = logging.getLogger("metadata-download")
URL_DOWNLOAD_LOGGER = logging.getLogger("ling-download")
PATH_LOGGER = logging.getLogger("create-paths")
DOWNLOAD_LOGGER = logging.getLogger("download")
2022-10-24 11:11:32 +00:00
NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea"
2022-10-21 19:24:38 +00:00
MUSIC_DIR = os.path.expanduser('~/Music')
TOR = False
2022-10-19 15:47:48 +00:00
2022-11-06 22:01:03 +00:00
temp_dir = os.path.join(tempfile.gettempdir(), TEMP_FOLDER)
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
database = Database(os.path.join(temp_dir, DATABASE_FILE),
2022-11-07 14:04:08 +00:00
os.path.join(temp_dir, DATABASE_STRUCTURE_FILE),
DATABASE_STRUCTURE_FALLBACK,
DATABASE_LOGGER,
reset_anyways=True)
2022-11-06 22:01:03 +00:00
2022-10-24 11:11:32 +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
def search_for_metadata():
search = metadata.search.Search(logger=SEARCH_LOGGER)
while True:
input_ = input(
"q to quit, .. for previous options, int for this element, str to search for query, ok to download\n")
input_.strip()
if input_.lower() == "ok":
break
if input_.lower() == "q":
break
if input_.lower() == "..":
print()
print(search.get_previous_options())
continue
if input_.isdigit():
print()
print(search.choose(int(input_)))
continue
print()
print(search.search_from_query(input_))
return search.current_option
2022-10-24 11:11:32 +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
def cli(start_at: int = 0):
2022-10-26 15:42:11 +00:00
proxies = None
2022-10-19 15:47:48 +00:00
if TOR:
2022-10-26 15:42:11 +00:00
proxies = {
2022-10-19 15:47:48 +00:00
'http': 'socks5h://127.0.0.1:9150',
'https': 'socks5h://127.0.0.1:9150'
}
if start_at <= 2:
2022-10-24 11:11:32 +00:00
genre = get_genre()
logging.info(f"{genre} has been set as genre.")
2022-10-18 16:48:24 +00:00
if start_at <= 0:
search = search_for_metadata()
logging.info("Starting Downloading of metadata")
2022-11-06 22:01:03 +00:00
metadata_downloader = MetadataDownloader(database, METADATA_DOWNLOAD_LOGGER)
metadata_downloader.download(search)
if start_at <= 1:
logging.info("Fetching Download Links")
2022-11-06 22:01:03 +00:00
download_links.Download(database, METADATA_DOWNLOAD_LOGGER, proxies=proxies)
2022-10-17 22:27:30 +00:00
if start_at <= 2:
logging.info("creating Paths")
2022-11-06 22:01:03 +00:00
url_to_path.UrlPath(database, PATH_LOGGER, genre=genre)
2022-10-17 12:56:32 +00:00
if start_at <= 3:
logging.info("starting to download the mp3's")
2022-11-06 22:01:03 +00:00
download.Download(database, DOWNLOAD_LOGGER, proxies=proxies, base_path=MUSIC_DIR)
2022-10-18 16:48:24 +00:00
if __name__ == "__main__":
2022-11-06 17:10:00 +00:00
cli(start_at=0)