playtypus/playtypus/threads.py

79 lines
2.9 KiB
Python

"""
The :mod:`threads` module contains functions to make working with QThreads easier
"""
import requests
import qtawesome as qta
from PyQt5 import QtCore, QtGui, QtWidgets
class ThreadWorker(QtCore.QObject):
"""
The :class:`~threads.ThreadWorker` class provides a base class for thread workers.
In Qt5, all work is done in workers, rather than actual QThread classes, and this case class provides
some utility signals and slots to make it easier to use threads and thread workers.
"""
quit = QtCore.pyqtSignal()
error = QtCore.pyqtSignal(str, str)
def start(self):
"""
The start method is how the worker runs. Basically, put your code here.
"""
raise NotImplementedError('Your class needs to override this method and run self.quit.emit() when complete')
class AlbumArtWorker(ThreadWorker):
album_item = None
artwork_url = None
def start(self):
if not self.album_item:
self.error.emit('No album QListWidgetItem set', 'The album_item attribute was not set')
self.quit.emit()
return
if not self.artwork_url:
self.error.emit('No artwork URL set', 'The artwork_url attribute was not set')
self.quit.emit()
return
response = requests.get(self.artwork_url)
if response.status_code == 200:
pixmap = QtGui.QPixmap()
pixmap.loadFromData(response.content)
self.album_item.setIcon(QtGui.QIcon(pixmap))
else:
print(response.text)
self.quit.emit()
class AlbumWorker(ThreadWorker):
"""
A thread worker to fetch the album art
"""
ALBUM_TEMPLATE = "{title}\n{artist}\n{year}"
window = None
def start(self):
if not self.window:
self.error.emit('No window set', 'The window attribute was not set')
self.quit.emit()
return
self.window.albumListWidget.clear()
for album in self.window.funkwhale.albums.list()['results']:
details = {
'title': album['title'],
'year': album['release_date'][:4] if album.get('release_date') else '',
'artist': album['artist']['name']
}
album_item = QtWidgets.QListWidgetItem(self.ALBUM_TEMPLATE.format(**details))
self.window.albumListWidget.addItem(album_item)
if album['cover'] and album['cover'].get('urls') and album['cover']['urls'].get('medium_square_crop'):
artwork_worker = AlbumArtWorker()
artwork_worker.album_item = album_item
artwork_worker.artwork_url = album['cover']['urls']['medium_square_crop']
self.window.run_thread(artwork_worker, 'album-{}'.format(album['title']))
else:
album_item.setIcon(qta.icon('mdi.album'))
self.window.update_album_total(self.window.albumListWidget.count())
self.quit.emit()