Skip to content

Commit

Permalink
cleanup some discogs stuff (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
aw-was-here committed Jun 5, 2023
1 parent a02699e commit 1e2f788
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ nowplaying/version.py
NowPlaying.egg-info
nowplaying/qtrc.py
.DS_Store
nowplaying.sublime-project
nowplaying.sublime-workspace
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import tempfile

import pytest
from PySide6.QtCore import QCoreApplication, QSettings, QStandardPaths # pylint: disable=no-name-in-module
from PySide6.QtCore import QCoreApplication, QSettings, QStandardPaths # pylint: disable=import-error, no-name-in-module

import nowplaying.bootstrap
import nowplaying.config
Expand Down
55 changes: 33 additions & 22 deletions nowplaying/artistextras/discogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import requests.exceptions
import urllib3.exceptions
import nowplaying.vendor.discogs_client
from nowplaying.vendor.discogs_client import models

import nowplaying.config
from nowplaying.artistextras import ArtistExtrasPlugin


Expand All @@ -25,7 +25,30 @@ def __init__(self, config=None, qsettings=None):
self.version = config.version
self.there = re.compile('(?i)^the ')

def _find_discogs_releaselist(self, metadata):
def _get_apikey(self):
apikey = self.config.cparser.value('discogs/apikey')
if not apikey or not self.config.cparser.value('discogs/enabled', type=bool):
return None
return apikey

def _setup_client(self):
''' setup the discogs client '''
if apikey := self._get_apikey():
delay = self.calculate_delay()
self.client = nowplaying.vendor.discogs_client.Client(
f'whatsnowplaying/{self.config.version}', user_token=apikey)
self.client.set_timeout(connect=delay, read=delay)
return True
return False

def _find_discogs_artist_releaselist(self, metadata):

if not self.client and not self._setup_client():
return None

if not self.client:
return None

try:
logging.debug('Fetching %s - %s', metadata['artist'], metadata['album'])
resultlist = self.client.search(metadata['album'],
Expand All @@ -43,39 +66,33 @@ def _find_discogs_releaselist(self, metadata):
return None

return next(
(result.artists[0] for result in resultlist
if isinstance(result, nowplaying.vendor.discogs_client.models.Release)),
(result.artists[0] for result in resultlist if isinstance(result, models.Release)),
None,
)

def download(self, metadata=None, imagecache=None): # pylint: disable=too-many-branches, too-many-return-statements
''' download content '''

apikey = self.config.cparser.value('discogs/apikey')

if not apikey or not self.config.cparser.value('discogs/enabled', type=bool):
return None

# discogs basically works by search for a combination of
# artist and album so we need both
if not metadata or not metadata.get('artist') or not metadata.get('album'):
logging.debug('artist or album is empty, skipping')
return None

oldartist = metadata['artist']
if not self.client and not self._setup_client():
logging.error('No discogs apikey or client setup failed.')
return None

if not self.client:
delay = self.calculate_delay()
self.client = nowplaying.vendor.discogs_client.Client(f'whatsnowplaying/{self.version}',
user_token=apikey)
self.client.set_timeout(connect=delay, read=delay)
return None

artistresultlist = self._find_discogs_releaselist(metadata)
oldartist = metadata['artist']
artistresultlist = self._find_discogs_artist_releaselist(metadata)

if not artistresultlist and self.there.match(metadata['artist']):
logging.debug('Trying without a leading \'The\'')
metadata['artist'] = self.there.sub('', metadata['artist'])
artistresultlist = self._find_discogs_releaselist(metadata)
artistresultlist = self._find_discogs_artist_releaselist(metadata)

if not artistresultlist:
logging.debug('discogs did not find it')
Expand Down Expand Up @@ -115,9 +132,6 @@ def providerinfo(self): # pylint: disable=no-self-use
''' return list of what is provided by this plug-in '''
return ['artistlongbio', 'artistthumbraw', 'discogs-artistfanarturls', 'artistwebsites']

def connect_settingsui(self, qwidget, uihelp):
''' pass '''

def load_settingsui(self, qwidget):
''' draw the plugin's settings page '''
if self.config.cparser.value('discogs/enabled', type=bool):
Expand All @@ -130,9 +144,6 @@ def load_settingsui(self, qwidget):
func = getattr(qwidget, f'{field}_checkbox')
func.setChecked(self.config.cparser.value(f'discogs/{field}', type=bool))

def verify_settingsui(self, qwidget):
''' pass '''

def save_settingsui(self, qwidget):
''' take the settings page and save it '''

Expand Down

0 comments on commit 1e2f788

Please sign in to comment.