🛠️ Ruff refactoring.

This commit is contained in:
Robert Honz
2024-01-12 10:21:34 +01:00
parent 69cf0b4707
commit 7af7fd6219
6 changed files with 39 additions and 35 deletions
+2
View File
@@ -123,6 +123,8 @@ ignore = [
"E501",
# DoNotAssignLambda
"E731",
# Do not use bare `except`
"E722"
]
[tool.ruff.per-file-ignores]
+2 -2
View File
@@ -167,7 +167,7 @@ def download(
media_type = MediaType.Video
dl.item(
id=id_item,
id_media=id_item,
media_type=media_type,
path_base=settings.data.download_base_path,
file_template=file_template,
@@ -188,7 +188,7 @@ def download(
file_template = settings.data.format_mix
media_type = MediaType.Mix
dl.list(
dl.items(
id_media=id_item,
media_type=media_type,
path_base=settings.data.download_base_path,
+10 -7
View File
@@ -29,7 +29,10 @@ from tidal_dl_ng.model.gui_data import ProgressBars
# TODO: Set appropriate client string and use it for video download.
# https://github.com/globocom/m3u8#using-different-http-clients
class RequestsClient:
def download(self, uri, timeout=None, headers={}, verify_ssl=True):
def download(self, uri: str, timeout: int = None, headers: dict | None = None, verify_ssl: bool = True):
if not headers:
headers = {}
o = requests.get(uri, timeout=timeout, headers=headers)
return o.text, o.url
@@ -64,7 +67,7 @@ class Download:
with open(path_file, "wb") as f:
for segment in m3u8_playlist.data["segments"]:
url = segment["uri"]
r = requests.get(url)
r = requests.get(url, timeout=REQUESTS_TIMEOUT_SEC)
f.write(r.content)
@@ -76,7 +79,7 @@ class Download:
self,
path_base: str,
fn_logger: Logger | WrapperLogger,
id: str = None,
id_media: str = None,
file_template: str = None,
media: Track | Video = None,
media_type: MediaType = None,
@@ -84,11 +87,11 @@ class Download:
progress_gui: ProgressBars = None,
progress: Progress = None,
) -> (bool, str):
if id:
if id_media:
if media_type == MediaType.Track:
media = Track(self.session, id)
media = Track(self.session, id_media)
elif media_type == MediaType.Video:
media = Video(self.session, id)
media = Video(self.session, id_media)
# If video download is not allowed
if not video_download:
@@ -225,7 +228,7 @@ class Download:
return result
def list(
def items(
self,
path_base: str,
fn_logger: Logger | WrapperLogger,
+13 -16
View File
@@ -14,10 +14,11 @@ import coloredlogs.converter
from rich.progress import Progress
from tidalapi import Album, Mix, Playlist, Quality, Track, UserPlaylist, Video
from tidalapi.session import SearchTypes
from tidal_dl_ng.config import Settings, Tidal
from tidal_dl_ng.constants import QualityVideo
from tidal_dl_ng.download import Download
from tidal_dl_ng.logger import XStream, logger_cli, logger_gui
from tidal_dl_ng.logger import XStream, logger_gui
from tidal_dl_ng.model.gui_data import ProgressBars, ResultSearch
from tidal_dl_ng.ui.main import Ui_MainWindow
from tidal_dl_ng.ui.spinner import QtWaitingSpinner
@@ -46,7 +47,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
# self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("TIDAL Downloader Next Gen!")
# TODO: Fix icons (make them visible).
my_pixmap = QtGui.QPixmap("tidal_dl_ng/ui/icon.png")
# my_pixmap = QtGui.QPixmap("tidal_dl_ng/ui/icon.png")
my_icon = QtGui.QIcon("tidal_dl_ng/ui/icon.png")
self.setWindowIcon(my_icon)
tray = QtWidgets.QSystemTrayIcon()
@@ -124,7 +125,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def _populate_search_types(self, ui_target: QtWidgets.QComboBox, options: SearchTypes):
for item in options:
if item and not item.__name__ == "Artist":
if item and item.__name__ != "Artist":
ui_target.addItem(item.__name__, item)
self.cb_search_type.setCurrentIndex(1)
@@ -213,7 +214,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
# We build the menu.
menu = QtWidgets.QMenu()
action = menu.addAction("Dowloading Playlist", lambda: self.thread_download_list_media(point))
menu.addAction("Dowloading Playlist", lambda: self.thread_download_list_media(point))
menu.exec(self.tr_lists_user.mapToGlobal(point))
@@ -254,10 +255,10 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.tr_results.addTopLevelItem(child)
def search(self, query: str, types_media: SearchTypes) -> [ResultSearch]:
result_search: [dict[str, *SearchTypes]] = self.tidal.session.search(query, models=types_media, limit=999)
result_search: [dict[str, SearchTypes]] = self.tidal.session.search(query, models=types_media, limit=999)
result: [ResultSearch] = []
for media_type, l_media in result_search.items():
for _media_type, l_media in result_search.items():
if isinstance(l_media, list):
result = result + self.search_result_to_model(l_media)
@@ -383,7 +384,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def on_download_results(self):
self.b_download.setEnabled(False)
self.b_download.setText(f"Downloading...")
self.b_download.setText("Downloading...")
items: [QtWidgets.QTreeWidgetItem] = self.tr_results.selectedItems()
@@ -393,9 +394,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
for item in items:
media: Track | Album | Playlist | Video = item.data(5, QtCore.Qt.ItemDataRole.UserRole)
result_dl: bool = self.download_item(media, self.download)
self.download_item(media, self.download)
self.b_download.setText(f"Download")
self.b_download.setText("Download")
self.b_download.setEnabled(True)
def download_item(self, media: Track | Album | Playlist | Video | Mix, dl: Download) -> bool:
@@ -408,7 +409,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
)
progress: Progress = Progress()
if isinstance(media, Track) or isinstance(media, Video):
if isinstance(media, (Track, Video)):
if isinstance(media, Track):
file_template: str = self.settings.data.format_track
elif isinstance(media, Video):
@@ -427,7 +428,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
logger_gui.info(f"Download successful: {download_path_file}")
else:
logger_gui.info(f"Download skipped (file exists): {download_path_file}")
elif isinstance(media, Album) or isinstance(media, Playlist) or isinstance(media, Mix):
elif isinstance(media, (Album, Playlist, Mix)):
file_template: str | bool = False
if isinstance(media, Album):
@@ -444,7 +445,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.progress_list_name(progress_name)
dl.list(
dl.items(
path_base=self.settings.data.download_base_path,
file_template=file_template,
list_media=media,
@@ -461,10 +462,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
# TODO: Refactor to useful return value.
return True
def progress_reset(self):
self.pb_list.setValue(0)
self.pb_item.setValue(0)
# TODO: Comment with Google Docstrings.
def gui_activate(tidal: Tidal | None = None):
+2 -2
View File
@@ -1,8 +1,8 @@
def get_tidal_media_id(url_or_id_media: str) -> str:
id_dirty = url_or_id_media.rsplit("/", 1)[-1]
id = id_dirty.rsplit("?", 1)[0]
id_media = id_dirty.rsplit("?", 1)[0]
return id
return id_media
def get_tidal_media_type(url_media: str) -> str | bool:
+10 -8
View File
@@ -34,13 +34,13 @@ class Metadata:
type_audio: str = "",
album: str = "",
title: str = "",
artists: [str] = [""],
artists: [str] | None = None,
copy_right: str = "",
tracknumber: int = 0,
discnumber: int = 0,
totaltrack: int = 0,
totaldisc: int = 0,
composer: [str] = [""],
composer: [str] | None = None,
isrc: str = "",
albumartist: str = "",
date: str = "",
@@ -114,14 +114,14 @@ class Metadata:
self.m.tags["title"] = self.title
self.m.tags["album"] = self.album
self.m.tags["albumartist"] = self.albumartist
self.m.tags["artist"] = ", ".join(self.artists)
self.m.tags["artist"] = ", ".join(self.artists) if self.artists else ""
self.m.tags["copyright"] = self.copy_right
self.m.tags["tracknumber"] = str(self.tracknumber)
self.m.tags["tracktotal"] = str(self.totaltrack)
self.m.tags["discnumber"] = str(self.discnumber)
self.m.tags["disctotal"] = str(self.totaldisc)
self.m.tags["date"] = self.date
self.m.tags["composer"] = ", ".join(self.composer)
self.m.tags["composer"] = ", ".join(self.composer) if self.composer else ""
self.m.tags["isrc"] = self.isrc
self.m.tags["lyrics"] = self.lyrics
@@ -132,12 +132,12 @@ class Metadata:
self.m.tags.add(TIT2(encoding=3, text=self.title))
self.m.tags.add(TALB(encoding=3, text=self.album))
self.m.tags.add(TOPE(encoding=3, text=self.albumartist))
self.m.tags.add(TPE1(encoding=3, text=", ".join(self.artists)))
self.m.tags.add(TPE1(encoding=3, text=", ".join(self.artists) if self.artists else ""))
self.m.tags.add(TCOP(encoding=3, text=self.copy_right))
self.m.tags.add(TRCK(encoding=3, text=str(self.tracknumber)))
self.m.tags.add(TRCK(encoding=3, text=self.discnumber))
self.m.tags.add(TDRC(encoding=3, text=self.date))
self.m.tags.add(TCOM(encoding=3, text=", ".join(self.composer)))
self.m.tags.add(TCOM(encoding=3, text=", ".join(self.composer) if self.composer else ""))
self.m.tags.add(TSRC(encoding=3, text=self.isrc))
self.m.tags.add(USLT(encoding=3, lang="eng", desc="desc", text=self.lyrics))
@@ -145,13 +145,13 @@ class Metadata:
self.m.tags["\xa9nam"] = self.title
self.m.tags["\xa9alb"] = self.album
self.m.tags["aART"] = self.albumartist
self.m.tags["\xa9ART"] = ", ".join(self.artists)
self.m.tags["\xa9ART"] = ", ".join(self.artists) if self.artists else ""
self.m.tags["cprt"] = self.copy_right
self.m.tags["trkn"] = [[self.tracknumber, self.totaltrack]]
self.m.tags["disk"] = [[self.discnumber, self.totaldisc]]
# self.m.tags['\xa9gen'] = self.genre
self.m.tags["\xa9day"] = self.date
self.m.tags["\xa9wrt"] = ", ".join(self.composer)
self.m.tags["\xa9wrt"] = ", ".join(self.composer) if self.composer else ""
self.m.tags["\xa9lyr"] = self.lyrics
def cover_data(self, url: str = None, path_file: str = None) -> str | bytes:
@@ -161,12 +161,14 @@ class Metadata:
try:
result = requests.get(url, timeout=REQUESTS_TIMEOUT_SEC).content
except:
# TODO: Implement logging.
pass
elif path_file:
try:
with open(path_file, "rb") as f:
result = f.read()
except OSError:
# TODO: Implement logging.
pass
return result