Setup coloredlogs

This commit is contained in:
Ritiek Malhotra
2020-05-03 09:06:03 +05:30
parent 715a95df1e
commit ec765119fa
13 changed files with 101 additions and 83 deletions

1
.gitignore vendored
View File

@@ -8,6 +8,7 @@ config.yml
Music/
*.txt
*.m3u
.cache-*
.pytest_cache/

View File

@@ -73,5 +73,6 @@ setup(
"Topic :: Multimedia :: Sound/Audio",
"Topic :: Utilities",
],
entry_points={"console_scripts": ["spotdl = spotdl.command_line.__main__:main"]},
# entry_points={"console_scripts": ["spotdl = spotdl.command_line.__main__:main"]},
entry_points={"console_scripts": ["spotdl = spotdl:main"]},
)

View File

@@ -1,4 +1,6 @@
from spotdl.command_line.__main__ import main
from spotdl.version import __version__
from spotdl.command_line import Spotdl
from spotdl.command_line.lib import Spotdl
from spotdl.track import Track

View File

@@ -1,4 +0,0 @@
from spotdl.command_line import lib
from spotdl.command_line.arguments import get_arguments
from spotdl.command_line.lib import Spotdl

View File

@@ -1,16 +1,37 @@
import logzero
import logging
for module in ("urllib3", "spotipy", "pytube",):
logging.getLogger(module).setLevel(logging.CRITICAL)
import coloredlogs
coloredlogs.DEFAULT_FIELD_STYLES = {
"levelname": {"bold": True, "color": "yellow"},
"name": {"color": "blue"},
"lineno": {"color": "magenta"},
}
import sys
from spotdl import command_line
def set_logger(level):
if level == logging.DEBUG:
fmt = "%(levelname)s:%(name)s:%(lineno)d:\n%(message)s"
else:
fmt = "%(levelname)s: %(message)s"
logging.basicConfig(format=fmt, level=level)
logger = logging.getLogger(name=__name__)
coloredlogs.install(level=level, fmt=fmt, logger=logger)
return logger
def main():
arguments = command_line.get_arguments()
spotdl = command_line.Spotdl(arguments.__dict__)
from spotdl.command_line.arguments import get_arguments
arguments = get_arguments()
logger = set_logger(arguments["log_level"])
from spotdl.command_line.lib import Spotdl
spotdl = Spotdl(arguments)
try:
spotdl.match_arguments()
except KeyboardInterrupt as e:
logzero.logger.exception(e)
logger.exception(e)
sys.exit(2)

View File

@@ -1,4 +1,3 @@
from logzero import logger as log
import appdirs
import logging
@@ -284,26 +283,26 @@ def run_errands(parser, parsed, config):
encoder_exists = shutil.which(parsed.encoder)
if not encoder_exists:
# log.warn("Specified encoder () was not found. Will not encode to specified "
# "output format".format(parsed.encoder))
logger.warn("Specified encoder () was not found. Will not encode to specified "
"output format".format(parsed.encoder))
parsed.encoder = "null"
if parsed.output_file == "-" and parsed.no_metadata is False:
# log.warn(
# "Cannot write metadata when target file is STDOUT. Pass "
# "--no-metadata explicitly to hide this warning."
# )
logger.warn(
"Cannot write metadata when target file is STDOUT. Pass "
"--no-metadata explicitly to hide this warning."
)
parsed.no_metadata = True
elif os.path.isdir(parsed.output_file):
adjusted_output_file = os.path.join(
parsed.output_file,
config["output-file"]
)
# log.warn(
# "Specified output file is a directory. Will write the filename as in
# "default file format. Pass --output-file={} to hide this warning".format(
# adjusted_output_file
# )
logger.warn(
"Specified output file is a directory. Will write the filename as in "
"default file format. Pass --output-file={} to hide this warning".format(
adjusted_output_file
))
parsed.output_file = adjusted_output_file
parsed.log_level = log_leveller(parsed.log_level)
@@ -311,5 +310,5 @@ def run_errands(parser, parsed, config):
# We're done dealing with configuration file here and don't need to use it later
del parsed.config
return parsed
return parsed.__dict__

View File

@@ -20,18 +20,11 @@ import spotdl.config
from spotdl.command_line.exceptions import NoYouTubeVideoError
import logzero
import os
import urllib.request
def set_logger(level):
fmt = "%(color)s%(levelname)s:%(end_color)s %(message)s"
formatter = logzero.LogFormatter(fmt=fmt)
logzero.formatter(formatter)
logzero.loglevel(level)
return logzero.logger
import logging
logger = logging.getLogger(name=__name__)
def search_lyrics(query):
provider = Genius()
@@ -116,8 +109,7 @@ def search_metadata(track, search_format="{artist} - {track-name} lyrics", manua
return
if manual:
youtube_video = prompt_for_youtube_search_result(youtube_videos)
else:
youtube_video = youtube_videos.bestmatch()["url"]
else: youtube_video = youtube_videos.bestmatch()["url"]
youtube_metadata = youtube.from_url(youtube_video)
metadata = spotdl.util.merge(
youtube_metadata,
@@ -160,8 +152,6 @@ class Spotdl:
client_id=self.arguments["spotify_client_id"],
client_secret=self.arguments["spotify_client_secret"]
)
logger = set_logger(self.arguments["log_level"])
logger.debug(self.arguments)
# youtube_tools.set_api_key()
@@ -220,7 +210,7 @@ class Spotdl:
metadata,
output_extension=self.arguments["output_ext"],
)
# log.info(log_fmt)
logger.info(log_fmt)
self.download_track_from_metadata(metadata)
def download_track_from_metadata(self, metadata):
@@ -232,7 +222,7 @@ class Spotdl:
quality=self.arguments["quality"],
preftype=self.arguments["input_ext"],
)
# log.info(stream)
logger.info(stream)
Encoder = {
"ffmpeg": EncoderFFmpeg,
@@ -253,7 +243,7 @@ class Spotdl:
)
)
print(filename)
# log.info(filename)
logger.info(filename)
to_skip = self.arguments["dry_run"]
if not to_skip and os.path.isfile(filename):
@@ -282,14 +272,13 @@ class Spotdl:
try:
track.apply_metadata(filename, encoding=output_extension)
except TypeError:
# log.warning("Cannot write metadata to given file")
logger.warning("Cannot write metadata to given file")
pass
def download_tracks_from_file(self, path):
# log.info(
# "Checking and removing any duplicate tracks "
# "in reading {}".format(path)
# )
logger.info(
"Checking and removing any duplicate tracks in {}".format(path)
)
with open(path, "r") as fin:
# Read tracks into a list and remove any duplicates
tracks = fin.read().splitlines()
@@ -310,14 +299,14 @@ class Spotdl:
try:
metadata = search_metadata(track, self.arguments["search_format"])
log_fmt=(str(number) + ". {artist} - {track-name}")
# log.info(log_fmt)
logger.info(log_fmt)
self.download_track_from_metadata(metadata)
except (urllib.request.URLError, TypeError, IOError) as e:
# log.exception(e.args[0])
# log.warning("Failed. Will retry after other songs\n")
logger.exception(e.args[0])
logger.warning("Failed. Will retry after other songs\n")
tracks.append(track)
except NoYouTubeVideoError:
# log.warning("Failed. No YouTube video found.\n")
logger.warning("Failed. No YouTube video found.\n")
pass
else:
if self.arguments["write_successful"]:
@@ -330,10 +319,9 @@ class Spotdl:
def download_tracks_from_file_threaded(self, path):
# FIXME: Can we make this function cleaner?
# log.info(
# "Checking and removing any duplicate tracks "
# "in reading {}".format(path)
# )
logger.info(
"Checking and removing any duplicate tracks in {}".format(path)
)
with open(path, "r") as fin:
# Read tracks into a list and remove any duplicates
tracks = fin.read().splitlines()
@@ -378,15 +366,15 @@ class Spotdl:
metadata["next_track"].start()
log_fmt=(str(current_iteration) + ". {artist} - {track-name}")
# log.info(log_fmt)
logger.info(log_fmt)
if metadata["current_track"] is None:
# log.warning("Something went wrong. Will retry after downloading remaining tracks")
logger.warning("Something went wrong. Will retry after downloading remaining tracks")
pass
print(metadata["current_track"]["name"])
# self.download_track_from_metadata(metadata["current_track"])
except (urllib.request.URLError, TypeError, IOError) as e:
# log.exception(e.args[0])
# log.warning("Failed. Will retry after other songs\n")
logger.exception(e.args[0])
logger.warning("Failed. Will retry after other songs\n")
tracks.append(current_track)
else:
tracks_count -= 1

View File

@@ -3,6 +3,8 @@ import yaml
import os
import spotdl.util
import logging
logger = logging.getLogger(__name__)
DEFAULT_CONFIGURATION = {
"spotify-downloader": {
@@ -56,17 +58,17 @@ def get_config(config_file):
config = DEFAULT_CONFIGURATION
dump_config(config_file, config=DEFAULT_CONFIGURATION)
# log.info("Writing default configuration to {0}:".format(config_file))
logger.info("Writing default configuration to {0}:".format(config_file))
# for line in yaml.dump(
# DEFAULT_CONFIGURATION["spotify-downloader"], default_flow_style=False
# ).split("\n"):
# if line.strip():
# log.info(line.strip())
# log.info(
# "Please note that command line arguments have higher priority "
# "than their equivalents in the configuration file"
# )
for line in yaml.dump(
DEFAULT_CONFIGURATION["spotify-downloader"], default_flow_style=False
).split("\n"):
if line.strip():
log.info(line.strip())
logger.info(
"Please note that command line arguments have higher priority "
"than their equivalents in the configuration file"
)
return config["spotify-downloader"]

View File

@@ -1,6 +1,8 @@
import subprocess
import os
from logzero import logger as log
# from logzero import logger as log
import logging
logger = logging.getLogger(__name__)
from spotdl.encode import EncoderBase
from spotdl.encode.exceptions import EncoderNotFoundError

View File

@@ -1,6 +1,8 @@
import subprocess
import os
from logzero import logger as log
# from logzero import logger as log
import logging
logger = logging.getLogger(__name__)
from spotdl.encode import EncoderBase
from spotdl.encode.exceptions import EncoderNotFoundError

View File

@@ -5,6 +5,9 @@
from spotdl.authorize.services import AuthorizeSpotify
import logging
logger = logging.getLogger(__name__)
class SpotifyHelpers:
def __init__(self, spotify=None):
if spotify is None:
@@ -28,7 +31,7 @@ class SpotifyHelpers:
# in rare cases, playlists may not be found, so playlists['next']
# is None. Skip these. Also see Issue #91.
if playlist["name"] is not None:
# log.info(
logger.info(
# u"{0:>5}. {1:<30} ({2} tracks)".format(
# check, playlist["name"], playlist["tracks"]["total"]
# )
@@ -57,7 +60,7 @@ class SpotifyHelpers:
)
except spotipy.client.SpotifyException:
# log.error("Unable to find playlist")
# log.info("Make sure the playlist is set to publicly visible and then try again")
logger.info("Make sure the playlist is set to publicly visible and then try again")
sys.exit(11)
return results
@@ -125,7 +128,7 @@ class SpotifyHelpers:
write_album(album_base_url + album["id"], text_file=text_file)
def write_tracks(self, tracks, text_file):
# log.info(u"Writing {0} tracks to {1}".format(tracks["total"], text_file))
logger.info(u"Writing {0} tracks to {1}".format(tracks["total"], text_file))
track_urls = []
with open(text_file, "a") as file_out:
while True:

View File

@@ -11,6 +11,9 @@ from spotdl.metadata.exceptions import YouTubeMetadataNotFoundError
import spotdl.util
import logging
logger = logging.getLogger(__name__)
BASE_SEARCH_URL = "https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q={}"
HEADERS = [('Range', 'bytes=0-'),]
@@ -96,7 +99,7 @@ class YouTubeSearch:
def search(self, query, limit=10):
""" Search and scrape YouTube to return a list of matching videos. """
search_url = self.generate_search_url(query)
# log.debug("Opening URL: {0}".format(search_url))
logger.debug("Opening URL: {0}".format(search_url))
html = self._fetch_response_html(search_url)
videos = self._fetch_search_results(html)

View File

@@ -1,11 +1,12 @@
from logzero import logger as log
import os
import sys
import math
import urllib.request
import threading
import logging
logger = logging.getLogger(__name__)
try:
import winreg
@@ -15,8 +16,8 @@ except ImportError:
try:
from slugify import SLUG_OK, slugify
except ImportError:
log.error("Oops! `unicode-slugify` was not found.")
log.info("Please remove any other slugify library and install `unicode-slugify`")
logger.error("Oops! `unicode-slugify` was not found.")
logger.info("Please remove any other slugify library and install `unicode-slugify`")
sys.exit(5)
@@ -53,19 +54,16 @@ def prompt_user_for_selection(items):
""" Let the user input a choice. """
while True:
try:
# log.info("Choose your number:")
print("Choose your number:")
logger.info("Choose your number:")
the_chosen_one = int(input("> "))
if 1 <= the_chosen_one <= len(items):
return items[the_chosen_one - 1]
elif the_chosen_one == 0:
return None
else:
# log.warning("Choose a valid number!")
print("Chose a valid number!")
logger.warning("Choose a valid number!")
except ValueError:
# log.warning("Choose a valid number!")
print("Chose a valid number!")
logger.warning("Choose a valid number!")
def is_spotify(raw_song):