Merge pull request #1106 from exislow/gui-playlists

[GUI] Show user's playlists and allow to download them.
This commit is contained in:
Yaronzz
2023-10-31 13:34:49 +08:00
committed by GitHub
10 changed files with 254 additions and 159 deletions
+1
View File
@@ -10,3 +10,4 @@ pydub==0.25.1
PyQt5==5.15.7
qt-material==2.12
lxml==4.7.1
tidalapi==0.7.3
+5 -4
View File
@@ -11,9 +11,9 @@
import sys
import getopt
from tidal_dl.events import *
from tidal_dl.settings import *
from tidal_dl.gui import startGui
from events import *
from settings import *
from gui import startGui
def mainCommand():
@@ -22,11 +22,12 @@ def mainCommand():
"hvgl:o:q:r:",
["help", "version", "gui", "link=", "output=", "quality", "resolution"])
except getopt.GetoptError as errmsg:
Printf.err(vars(errmsg)['msg'] + ". Use 'tidal-dl -h' for useage.")
Printf.err(vars(errmsg)['msg'] + ". Use 'tidal-dl -h' for usage.")
return
link = None
showGui = False
for opt, val in opts:
if opt in ('-h', '--help'):
Printf.usage()
+9 -10
View File
@@ -8,16 +8,14 @@
@Contact : yaronhuang@foxmail.com
@Desc :
'''
import aigpy
import logging
from tidal_dl.paths import *
from tidal_dl.printf import *
from tidal_dl.decryption import *
from tidal_dl.tidal import *
from concurrent.futures import ThreadPoolExecutor
from decryption import *
from printf import *
from tidal import *
def __isSkip__(finalpath, url):
if not SETTINGS.checkExist:
return False
@@ -164,7 +162,7 @@ def downloadTrack(track: Track, album=None, playlist=None, userProgress=None, pa
tool.setPartSize(partSize)
check, err = tool.start(SETTINGS.showProgress and not SETTINGS.multiThread)
if not check:
Printf.err(f"DL Track[{track.title}] failed.{str(err)}")
Printf.err(f"DL Track '{track.title}' failed: {str(err)}")
return False, str(err)
# encrypted -> decrypt and remove encrypted file
@@ -187,13 +185,14 @@ def downloadTrack(track: Track, album=None, playlist=None, userProgress=None, pa
__setMetaData__(track, album, path, contributors, lyrics)
Printf.success(track.title)
return True, ''
except Exception as e:
Printf.err(f"DL Track[{track.title}] failed.{str(e)}")
Printf.err(f"DL Track '{track.title}' failed: {str(e)}")
return False, str(e)
def downloadTracks(tracks, album: Album = None, playlist : Playlist=None):
def downloadTracks(tracks, album: Album = None, playlist: Playlist = None):
def __getAlbum__(item: Track):
album = TIDAL_API.getAlbum(item.album.id)
if SETTINGS.saveCovers and not SETTINGS.usePlaylistFolder:
+1 -8
View File
@@ -9,14 +9,7 @@
@Desc :
"""
import aigpy
import time
from tidal_dl.model import *
from tidal_dl.enums import *
from tidal_dl.tidal import *
from tidal_dl.printf import *
from tidal_dl.download import *
from download import *
'''
=================================
+117 -35
View File
@@ -8,15 +8,11 @@
@Contact : yaronhuang@foxmail.com
@Desc :
"""
import sys
import aigpy
import _thread
import importlib
import sys
from tidal_dl.events import *
from tidal_dl.settings import *
from tidal_dl.printf import *
from tidal_dl.enums import *
from events import *
from printf import *
def enableGui():
@@ -33,11 +29,12 @@ if not enableGui():
Printf.err("Not support gui. Please type: `pip3 install PyQt5 qt_material`")
else:
from PyQt5.QtCore import Qt, QObject
from PyQt5.QtGui import QTextCursor
from PyQt5.QtGui import QTextCursor, QKeyEvent
from PyQt5.QtCore import pyqtSignal
from PyQt5 import QtWidgets
from qt_material import apply_stylesheet
class SettingView(QtWidgets.QWidget):
def __init__(self, ) -> None:
super().__init__()
@@ -55,19 +52,21 @@ else:
self.mainGrid.addWidget(self.c_pathTrackFormat)
self.mainGrid.addWidget(self.c_pathVideoFormat)
class EmittingStream(QObject):
textWritten = pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
class MainView(QtWidgets.QWidget):
s_downloadEnd = pyqtSignal(str, bool, str)
def __init__(self, ) -> None:
super().__init__()
self.initView()
self.setMinimumSize(600, 620)
self.setMinimumSize(800, 620)
self.setWindowTitle("Tidal-dl")
def __info__(self, msg):
@@ -111,7 +110,7 @@ else:
self.c_tableInfo.setShowGrid(False)
self.c_tableInfo.verticalHeader().setVisible(False)
self.c_tableInfo.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.c_tableInfo.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.c_tableInfo.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.c_tableInfo.horizontalHeader().setStretchLastSection(True)
self.c_tableInfo.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.ResizeToContents)
self.c_tableInfo.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
@@ -120,10 +119,20 @@ else:
item = QtWidgets.QTableWidgetItem(name)
self.c_tableInfo.setHorizontalHeaderItem(index, item)
# Create Tree View for playlists.
self.tree_playlists = QtWidgets.QTreeWidget()
self.tree_playlists.setAnimated(False)
self.tree_playlists.setIndentation(20)
self.tree_playlists.setSortingEnabled(True)
self.tree_playlists.resize(200, 400)
self.tree_playlists.setColumnCount(2)
self.tree_playlists.setHeaderLabels(("Name", "# Tracks"))
self.tree_playlists.setColumnWidth(0, 200)
# print
self.c_printTextEdit = QtWidgets.QTextEdit()
self.c_printTextEdit.setReadOnly(True)
self.c_printTextEdit.setFixedHeight(150)
self.c_printTextEdit.setFixedHeight(100)
sys.stdout = EmittingStream(textWritten=self.__output__)
sys.stderr = EmittingStream(textWritten=self.__output__)
@@ -148,16 +157,29 @@ else:
self.funcGrid.addWidget(self.c_printTextEdit)
self.mainGrid = QtWidgets.QGridLayout(self)
self.mainGrid.addLayout(self.funcGrid, 0, 0)
self.mainGrid.addWidget(self.tree_playlists, 0, 0, 1, 2)
self.mainGrid.addLayout(self.funcGrid, 0, 2, 1, 3)
self.mainGrid.addWidget(self.c_widgetSetting, 0, 0)
# connect
self.c_btnSearch.clicked.connect(self.search)
self.c_lineSearch.returnPressed.connect(self.search)
self.c_btnDownload.clicked.connect(self.download)
self.s_downloadEnd.connect(self.downloadEnd)
self.c_combTQuality.currentIndexChanged.connect(self.changeTQuality)
self.c_combVQuality.currentIndexChanged.connect(self.changeVQuality)
self.c_btnSetting.clicked.connect(self.showSettings)
self.tree_playlists.itemClicked.connect(self.playlist_display_tracks)
# Connect the contextmenu
self.tree_playlists.setContextMenuPolicy(Qt.CustomContextMenu)
self.tree_playlists.customContextMenuRequested.connect(self.menuContextTree)
def keyPressEvent(self, event: QKeyEvent):
key = event.key()
if event.modifiers() & Qt.MetaModifier and key == Qt.Key_A:
self.c_tableInfo.selectAll()
def addItem(self, rowIdx: int, colIdx: int, text):
if isinstance(text, str):
@@ -166,9 +188,9 @@ else:
def search(self):
self.c_tableInfo.setRowCount(0)
self.s_type = self.c_combType.currentData()
self.s_text = self.c_lineSearch.text()
if self.s_text.startswith('http'):
tmpType, tmpId = TIDAL_API.parseUrl(self.s_text)
if tmpType == Type.Null:
@@ -194,22 +216,27 @@ else:
self.__info__('No result')
return
self.c_tableInfo.setRowCount(len(self.s_array))
for index, item in enumerate(self.s_array):
self.set_table_search_results(self.s_array, self.s_type)
def set_table_search_results(self, s_array, s_type):
self.c_tableInfo.clearSelection()
self.c_tableInfo.setRowCount(len(s_array))
for index, item in enumerate(s_array):
self.addItem(index, 0, str(index + 1))
if self.s_type in [Type.Album, Type.Track]:
if s_type in [Type.Album, Type.Track]:
self.addItem(index, 1, item.title)
self.addItem(index, 2, TIDAL_API.getArtistsName(item.artists))
self.addItem(index, 3, item.audioQuality)
elif self.s_type in [Type.Video]:
elif s_type in [Type.Video]:
self.addItem(index, 1, item.title)
self.addItem(index, 2, TIDAL_API.getArtistsName(item.artists))
self.addItem(index, 3, item.quality)
elif self.s_type in [Type.Playlist]:
elif s_type in [Type.Playlist]:
self.addItem(index, 1, item.title)
self.addItem(index, 2, '')
self.addItem(index, 3, '')
elif self.s_type in [Type.Artist]:
elif s_type in [Type.Artist]:
self.addItem(index, 1, item.name)
self.addItem(index, 2, '')
self.addItem(index, 3, '')
@@ -217,42 +244,58 @@ else:
def download(self):
index = self.c_tableInfo.currentIndex().row()
if index < 0:
selection = self.c_tableInfo.selectionModel()
has_selection = selection.hasSelection()
if has_selection == False:
self.__info__('Please select a row first.')
return
rows = self.c_tableInfo.selectionModel().selectedRows()
for row in rows:
index = row.row()
item = self.s_array[index]
item_type = self.s_type
self.download_item(item, item_type)
def download_item(self, item, item_type):
self.c_btnDownload.setEnabled(False)
item_to_download = ""
if isinstance(self.s_array[index], Artist):
item_to_download = self.s_array[index].name
if isinstance(item, Artist):
item_to_download = item.name
else:
item_to_download = self.s_array[index].title
self.c_btnDownload.setText(f"Downloading [${item_to_download}]...")
item_to_download = item.title
def __thread_download__(model: MainView):
self.c_btnDownload.setText(f"'{item_to_download}' ...")
self.download_(item, item_type)
# Not race condition safe. Needs refactoring.
def download_(self, item, s_type):
downloading_item = ""
try:
type = model.s_type
item = model.s_array[index]
start_type(type, item)
item_type = s_type
start_type(item_type, item)
if isinstance(item, Artist):
downloading_item = item.name
else:
downloading_item = item.title
model.s_downloadEnd.emit(downloading_item, True, '')
except Exception as e:
model.s_downloadEnd.emit(downloading_item, False, str(e))
_thread.start_new_thread(__thread_download__, (self, ))
self.s_downloadEnd.emit(downloading_item, True, '')
except Exception as e:
self.s_downloadEnd.emit(downloading_item, False, str(e))
def downloadEnd(self, title, result, msg):
self.c_btnDownload.setEnabled(True)
self.c_btnDownload.setText(f"Download")
if result:
self.__info__(f'Download [{title}] finish')
Printf.info(f"Download '{title}' finished.")
else:
self.__info__(f'Download [{title}] failed:{msg}')
Printf.err(f"Download '{title}' failed:{msg}")
def checkLogin(self):
if not loginByConfig():
@@ -269,6 +312,45 @@ else:
def showSettings(self):
self.c_widgetSetting.show()
def tree_items_playlists(self):
playlists = TIDAL_API.get_playlists()
for playlist in playlists:
item = QtWidgets.QTreeWidgetItem(self.tree_playlists)
item.setText(0, playlist.name)
item.setText(1, str(playlist.num_tracks))
item.setText(2, playlist.id)
def playlist_display_tracks(self, item, column):
tracks = TIDAL_API.get_playlist_items(item.text(2))
self.s_array = tracks
self.s_type = Type.Track
self.set_table_search_results(tracks, Type.Track)
def menuContextTree(self, point):
# Infos about the node selected.
index = self.tree_playlists.indexAt(point)
if not index.isValid():
return
item = self.tree_playlists.itemAt(point)
playlist = Playlist()
playlist.title = item.text(0)
playlist.uuid = item.text(2)
# We build the menu.
menu = QtWidgets.QMenu()
action = menu.addAction("Dowload Playlist")
menu.exec_(self.tree_playlists.mapToGlobal(point))
self.download_item(playlist, Type.Playlist)
def startGui():
aigpy.cmd.enableColor(False)
@@ -278,10 +360,10 @@ else:
window = MainView()
window.show()
window.checkLogin()
window.tree_items_playlists()
app.exec_()
if __name__ == '__main__':
SETTINGS.read(getProfilePath())
TOKEN.read(getTokenPath())
+22 -22
View File
@@ -9,28 +9,28 @@
@Desc :
'''
from tidal_dl.lang.arabic import LangArabic
from tidal_dl.lang.chinese import LangChinese
from tidal_dl.lang.croatian import LangCroatian
from tidal_dl.lang.czech import LangCzech
from tidal_dl.lang.danish import LangDanish
from tidal_dl.lang.dutch import LangDutch
from tidal_dl.lang.english import LangEnglish
from tidal_dl.lang.filipino import LangFilipino
from tidal_dl.lang.french import LangFrench
from tidal_dl.lang.german import LangGerman
from tidal_dl.lang.hungarian import LangHungarian
from tidal_dl.lang.italian import LangItalian
from tidal_dl.lang.norwegian import LangNorwegian
from tidal_dl.lang.polish import LangPolish
from tidal_dl.lang.portuguese import LangPortuguese
from tidal_dl.lang.russian import LangRussian
from tidal_dl.lang.spanish import LangSpanish
from tidal_dl.lang.turkish import LangTurkish
from tidal_dl.lang.ukrainian import LangUkrainian
from tidal_dl.lang.vietnamese import LangVietnamese
from tidal_dl.lang.korean import LangKorean
from tidal_dl.lang.japanese import LangJapanese
from lang.arabic import LangArabic
from lang.chinese import LangChinese
from lang.croatian import LangCroatian
from lang.czech import LangCzech
from lang.danish import LangDanish
from lang.dutch import LangDutch
from lang.english import LangEnglish
from lang.filipino import LangFilipino
from lang.french import LangFrench
from lang.german import LangGerman
from lang.hungarian import LangHungarian
from lang.italian import LangItalian
from lang.norwegian import LangNorwegian
from lang.polish import LangPolish
from lang.portuguese import LangPortuguese
from lang.russian import LangRussian
from lang.spanish import LangSpanish
from lang.turkish import LangTurkish
from lang.ukrainian import LangUkrainian
from lang.vietnamese import LangVietnamese
from lang.korean import LangKorean
from lang.japanese import LangJapanese
_ALL_LANGUAGE_ = [
['English', LangEnglish()],
+2 -2
View File
@@ -12,8 +12,8 @@ import os
import aigpy
import datetime
from tidal_dl.tidal import *
from tidal_dl.settings import *
from tidal import *
from settings import *
def __fixPath__(name: str):
+5 -5
View File
@@ -14,12 +14,12 @@ import aigpy
import logging
import prettytable
import tidal_dl.apiKey as apiKey
import apiKey as apiKey
from tidal_dl.model import *
from tidal_dl.paths import *
from tidal_dl.settings import *
from tidal_dl.lang.language import *
from model import *
from paths import *
from settings import *
from lang.language import *
VERSION = '2022.10.31.1'
+2 -2
View File
@@ -12,8 +12,8 @@ import json
import aigpy
import base64
from tidal_dl.lang.language import *
from tidal_dl.enums import *
from lang.language import *
from enums import *
class Settings(aigpy.model.ModelBase):
+30 -11
View File
@@ -8,18 +8,17 @@
@Contact : yaronhuang@foxmail.com
@Desc : tidal api
'''
import json
import random
import re
import time
import aigpy
import base64
import requests
from typing import Union, List
from xml.etree import ElementTree
from tidal_dl.model import *
from tidal_dl.enums import *
from tidal_dl.settings import *
import requests
import tidalapi
from model import *
from settings import *
# SSL Warnings | retry number
requests.packages.urllib3.disable_warnings()
@@ -43,7 +42,8 @@ class TidalAPI(object):
if respond.url.find("playbackinfopostpaywall") != -1 and SETTINGS.downloadDelay is not False:
# random sleep between 0.5 and 5 seconds and print it
sleep_time = random.randint(500, 5000) / 1000
print(f"Sleeping for {sleep_time} seconds, to mimic human behaviour and prevent too many requests error")
print(
f"Sleeping for {sleep_time} seconds, to mimic human behaviour and prevent too many requests error")
time.sleep(sleep_time)
if respond.status_code == 429:
@@ -109,7 +109,7 @@ class TidalAPI(object):
def __post__(self, path, data, auth=None, urlpre='https://auth.tidal.com/v1/oauth2'):
for index in range(3):
try:
result = requests.post(urlpre+path, data=data, auth=auth, verify=False).json()
result = requests.post(urlpre + path, data=data, auth=auth, verify=False).json()
return result
except Exception as e:
if index == 2:
@@ -157,8 +157,14 @@ class TidalAPI(object):
def verifyAccessToken(self, accessToken) -> bool:
header = {'authorization': 'Bearer {}'.format(accessToken)}
result = requests.get('https://api.tidal.com/v1/sessions', headers=header).json()
if 'status' in result and result['status'] != 200:
return False
# Set tidalapi session.
self.session = tidalapi.session.Session()
self.session.load_oauth_session("Bearer", accessToken)
return True
def refreshAccessToken(self, refreshToken) -> bool:
@@ -188,11 +194,12 @@ class TidalAPI(object):
if not aigpy.string.isNull(userid):
if str(result['userId']) != str(userid):
raise Exception("User mismatch! Please use your own accesstoken.",)
raise Exception("User mismatch! Please use your own accesstoken.", )
self.key.userId = result['userId']
self.key.countryCode = result['countryCode']
self.key.accessToken = accessToken
return
def getAlbum(self, id) -> Album:
@@ -233,6 +240,7 @@ class TidalAPI(object):
def search(self, text: str, type: Type, offset: int = 0, limit: int = 10) -> SearchResult:
typeStr = type.name.upper() + "S"
if type == Type.Null:
typeStr = "ARTISTS,ALBUMS,TRACKS,VIDEOS,PLAYLISTS"
@@ -373,7 +381,7 @@ class TidalAPI(object):
ret.trackid = resp.trackid
ret.soundQuality = resp.audioQuality
ret.codec = aigpy.string.getSub(xmldata, 'codecs="', '"')
ret.encryptionKey = ""#manifest['keyId'] if 'keyId' in manifest else ""
ret.encryptionKey = "" # manifest['keyId'] if 'keyId' in manifest else ""
ret.urls = self.parse_mpd(xmldata)[0]
if len(ret.urls) > 0:
ret.url = ret.urls[0]
@@ -474,6 +482,17 @@ class TidalAPI(object):
raise Exception("No result.")
def get_playlists(self) -> List[Union["Playlist", "UserPlaylist"]]:
playlists = self.session.user.playlists()
return playlists
def get_playlist_items(self, playlist_id: int) -> Union[tidalapi.Playlist, tidalapi.UserPlaylist]:
# tracks = self.session.playlist(playlist_id).items()
tracks, videos = TIDAL_API.getItems(playlist_id, Type.Playlist)
return tracks
# Singleton
TIDAL_API = TidalAPI()