APi Exceptions & standardizing flow
Incorporating ListenBrainzAPIException into the functions. Utilizing similar API call Exception handling for all relevant functions. Signed-off-by: Pratyush Desai <pratyush.desai@liberta.casa>
This commit is contained in:
parent
ff2bbfcc25
commit
dc6e5be41e
40
plugin.py
40
plugin.py
@ -29,6 +29,8 @@
|
||||
###
|
||||
|
||||
import liblistenbrainz
|
||||
from liblistenbrainz.errors import ListenBrainzAPIException
|
||||
|
||||
|
||||
from supybot import utils, plugins, ircutils, callbacks
|
||||
from supybot.commands import *
|
||||
@ -43,8 +45,7 @@ class ListenBrainz(callbacks.Plugin):
|
||||
threaded = True
|
||||
|
||||
def __init__(self, irc):
|
||||
self.__parent = super(ListenBrainz, self)
|
||||
self.__parent.__init__(irc)
|
||||
super().__init__(irc)
|
||||
self.client = liblistenbrainz.ListenBrainz()
|
||||
self.DISPLAY_LIMIT = 3
|
||||
|
||||
@ -54,8 +55,16 @@ class ListenBrainz(callbacks.Plugin):
|
||||
|
||||
Announces the track currently being played by <user>.
|
||||
"""
|
||||
listen = self.client.get_playing_now(user)
|
||||
if listen is None:
|
||||
try:
|
||||
listen = self.client.get_playing_now(user)
|
||||
except ListenBrainzAPIException as e:
|
||||
irc.error(f"ListenBrainz API Error fetching listens for {user}: {e}", Raise=True)
|
||||
return
|
||||
except Exception as e:
|
||||
irc.error(f"An unexpected error occurred: {e}", Raise=True)
|
||||
return
|
||||
|
||||
if not listen:
|
||||
response = f"{user} doen't seem to be listening to anything"
|
||||
else:
|
||||
response = (
|
||||
@ -72,8 +81,16 @@ class ListenBrainz(callbacks.Plugin):
|
||||
|
||||
Announces total number of tracks scrobbled by <user>
|
||||
"""
|
||||
count = self.client.get_user_listen_count(user)
|
||||
if count is None:
|
||||
try:
|
||||
count = self.client.get_user_listen_count(user)
|
||||
except ListenBrainzAPIException as e:
|
||||
irc.error(f"ListenBrainz API Error fetching listens for {user}: {e}", Raise=True)
|
||||
return
|
||||
except Exception as e:
|
||||
irc.error(f"An unexpected error occurred: {e}", Raise=True)
|
||||
return
|
||||
|
||||
if not count:
|
||||
response = "Nothing recorded"
|
||||
else:
|
||||
response = (f"{user} has recorded listening to {count} tracks")
|
||||
@ -89,7 +106,9 @@ class ListenBrainz(callbacks.Plugin):
|
||||
try:
|
||||
data = self.client.get_user_recordings(user)
|
||||
payload = data.get("payload", {})
|
||||
|
||||
except ListenBrainzAPIException as e:
|
||||
irc.error(f"ListenBrainz API Error fetching listens for {user}: {e}", Raise=True)
|
||||
return
|
||||
except Exception as e:
|
||||
irc.error(f"An unexpected error occurred: {e}", Raise=True)
|
||||
return
|
||||
@ -100,7 +119,7 @@ class ListenBrainz(callbacks.Plugin):
|
||||
irc.reply(f"{user} has no recorded tracks.")
|
||||
return
|
||||
|
||||
top_tracks = recordings[:DISPLAY_LIMIT]
|
||||
top_tracks = recordings[:self.DISPLAY_LIMIT]
|
||||
header = f"{user}'s Top {len(top_tracks)} Tracks (Total unique tracks: {total_tracks:,}):"
|
||||
irc.reply(header)
|
||||
for i, track in enumerate(top_tracks):
|
||||
@ -129,6 +148,9 @@ class ListenBrainz(callbacks.Plugin):
|
||||
try:
|
||||
data = self.client.get_user_releases(user)
|
||||
payload = data.get("payload", {})
|
||||
except ListenBrainzAPIException as e:
|
||||
irc.error(f"ListenBrainz API Error fetching listens for {user}: {e}", Raise=True)
|
||||
return
|
||||
except Exception as e:
|
||||
irc.error(f"An unexpected error occurred: {e}", Raise=True)
|
||||
return
|
||||
@ -140,7 +162,7 @@ class ListenBrainz(callbacks.Plugin):
|
||||
irc.reply(f"{user} has no recorded releases.")
|
||||
return
|
||||
|
||||
top_releases = releases[:DISPLAY_LIMIT]
|
||||
top_releases = releases[:self.DISPLAY_LIMIT]
|
||||
|
||||
header = f"{user}'s Top {len(top_releases)} Releases (Total unique releases: {total_albums:,}):"
|
||||
irc.reply(header)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user