From 010394e6388387c1e69c2682749e5fe6edd21267 Mon Sep 17 00:00:00 2001 From: Ryan Lee Date: Thu, 8 Aug 2019 17:08:45 -0700 Subject: [PATCH 1/3] Add release options --- discogs_cli/completions.py | 12 ++++++--- discogs_cli/discogs.py | 55 +++++++++++++++++++++++++++++--------- discogs_cli/main_cli.py | 6 +++-- 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/discogs_cli/completions.py b/discogs_cli/completions.py index 0f3b33d..b203b86 100644 --- a/discogs_cli/completions.py +++ b/discogs_cli/completions.py @@ -18,22 +18,26 @@ }, 'master': { 'args': '1', - 'opts': '', + 'opts': '' }, 'release': { 'args': '1', - 'opts': '', + 'opts': [ + '--exclude (personnel|tracklist|notes)', + '--include (personnel|tracklist|notes)' + ] }, 'search': { 'args': '"query string"', 'opts': [ '--lookup (artist|label|release)', - ], + ] }, } META_LOOKUP = { '1': 'id: int - discogs ID to retrieve', '--lookup': '(artist|label|release)', - '"(artist|label|release)"': 'Type of query to perform', + '--exclude': '(personnel|tracklist|notes)', + '--include': '(personnel|tracklist|notes)' } META_LOOKUP.update(SUBCOMMANDS) diff --git a/discogs_cli/discogs.py b/discogs_cli/discogs.py index 7b84c49..b918116 100644 --- a/discogs_cli/discogs.py +++ b/discogs_cli/discogs.py @@ -276,16 +276,19 @@ class Release(Discogs): :param release_id: A Discogs.com release id. """ - def __init__(self, release_id): + def __init__(self, release_id, exclude="None", include="All"): super(Release, self).__init__() self.release_id = release_id - self.discogs = self.client.release(self.release_id) + self.exclude = exclude + self.include = include + def show(self): out = [] year = self.discogs.year - + self.discogs.images + extraartists = self.discogs.data["extraartists"] out.append('{artists} - {title}'.format(artists=','.join( self._artists(self.discogs.data['artists'])), title=self.discogs.data['title'])) @@ -311,13 +314,41 @@ def show(self): self.discogs.styles))) out.append(self.clabel('Rating:') + ' {rating}/5'.format( rating=self.discogs.data.get('community', {}).get('rating', - {}).get('average'))) - out.append(self._separator('Tracklist')) - for t in self.discogs.data['tracklist']: - duration = ' {0}'.format(t.get('duration')) - out.append('{pos}\t{title} {dur}'.format( - pos=t['position'], title=t['title'], dur=duration)) - - out.append(self._separator('Notes')) - out.append(self.discogs.data.get('notes', 'None.')) + {}).get('average'))) + out = self.show_extra(self.exclude,self.include,out) click.echo('\n'.join(out), color=True) + + def show_extra(self,exclude,include,out): + personnel = True + tracklist = True + notes = True + if "personnel" in exclude: + personnel = False + elif "tracklist" in exclude: + tracklist = False + elif "notes" in exclude: + notes = False + elif "personnel" in include: + tracklist,notes = False,False + elif "tracklist" in include: + personnel,notes = False,False + elif "notes" in include: + personnel,tracklist = False,False + + if personnel is True: + out.append(self._separator('Personnel')) + for t in self.discogs.data['extraartists']: + name = t["name"] + role = t["role"] + out.append(self.clabel('{role}: '.format(role=role)) + ' {name}'.format( + name=name)) + if tracklist is True: + out.append(self._separator('Tracklist')) + for t in self.discogs.data['tracklist']: + duration = ' {0}'.format(t.get('duration')) + out.append('{pos}\t{title} {dur}'.format( + pos=t['position'], title=t['title'], dur=duration)) + if notes is True: + out.append(self._separator('Notes')) + out.append(self.discogs.data.get('notes', 'None.')) + return out \ No newline at end of file diff --git a/discogs_cli/main_cli.py b/discogs_cli/main_cli.py index 129b913..3e04b96 100755 --- a/discogs_cli/main_cli.py +++ b/discogs_cli/main_cli.py @@ -32,9 +32,11 @@ def label(label_id): @cli.command('release') @click.argument('release_id') -def release(release_id): +@click.option('--exclude', default="None") +@click.option('--include', default="All") +def release(release_id,exclude,include): """Retrieve a single release from the discogs database.""" - r = Release(release_id) + r = Release(release_id,exclude=exclude,include=include) try: r.show() From 7cd20a3bd387bf014b2171cb92ad527a52f75d7d Mon Sep 17 00:00:00 2001 From: Ryan Lee Date: Thu, 8 Aug 2019 17:13:38 -0700 Subject: [PATCH 2/3] Clean up discogs.py --- discogs_cli/discogs.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/discogs_cli/discogs.py b/discogs_cli/discogs.py index b918116..88d63dd 100644 --- a/discogs_cli/discogs.py +++ b/discogs_cli/discogs.py @@ -287,7 +287,6 @@ def __init__(self, release_id, exclude="None", include="All"): def show(self): out = [] year = self.discogs.year - self.discogs.images extraartists = self.discogs.data["extraartists"] out.append('{artists} - {title}'.format(artists=','.join( self._artists(self.discogs.data['artists'])), @@ -319,9 +318,7 @@ def show(self): click.echo('\n'.join(out), color=True) def show_extra(self,exclude,include,out): - personnel = True - tracklist = True - notes = True + personnel,tracklist,notes = True,True,True if "personnel" in exclude: personnel = False elif "tracklist" in exclude: From cf0eb27283ea0e41cd61423f161c663df52d90e5 Mon Sep 17 00:00:00 2001 From: Ryan Lee Date: Tue, 20 Aug 2019 00:47:44 -0700 Subject: [PATCH 3/3] Handle TypeError when release has no style info --- discogs_cli/discogs.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/discogs_cli/discogs.py b/discogs_cli/discogs.py index 88d63dd..a1f29c7 100644 --- a/discogs_cli/discogs.py +++ b/discogs_cli/discogs.py @@ -309,8 +309,11 @@ def show(self): out.append(self.clabel('Year:') + ' {year}'.format(year=year)) out.append(self.clabel('Genre:') + ' {genre}'.format(genre=', '.join( self.discogs.genres))) - out.append(self.clabel('Style:') + ' {style}'.format(style=', '.join( - self.discogs.styles))) + try: + out.append(self.clabel('Style:') + ' {style}'.format(style=', '.join( + self.discogs.styles))) + except: + print("Style info not available.") out.append(self.clabel('Rating:') + ' {rating}/5'.format( rating=self.discogs.data.get('community', {}).get('rating', {}).get('average')))