Skip to content

Commit 6ab9486

Browse files
committed
Improve GeoIP, imgur, quote error handling, remove un-needed var in youtube,
1 parent 3bf49ad commit 6ab9486

4 files changed

Lines changed: 13 additions & 11 deletions

File tree

plugins/geoip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def update_db():
4242
else:
4343
try:
4444
return geoip2.database.Reader(PATH)
45-
except Exception:
45+
except geoip2.errors.GeoIP2Error:
4646
# issue loading, geo
4747
fetch_db()
4848
return geoip2.database.Reader(PATH)

plugins/imgur.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import random
33

44
from imgurpython import ImgurClient
5+
from contextlib import suppress
56

67
from cloudbot import hook
78
from cloudbot.util import web
@@ -85,11 +86,9 @@ def imgur(text):
8586
title = item.title
8687

8788
# if it's an imgur meme, add the meme name
88-
try:
89+
# if not, AttributeError will trigger and code will carry on
90+
with suppress(AttributeError):
8991
title = "\x02{}\x02 - {}".format(item.meme_metadata["meme_name"].lower(), title)
90-
except Exception:
91-
# this is a super un-important thing, so if it fails we don't care, carry on
92-
pass
9392

9493
# if the item has a tag, show that
9594
if item.section:

plugins/quote.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
from cloudbot import hook
66
from cloudbot.util import botvars
7+
8+
from sqlalchemy import select
79
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint
810
from sqlalchemy.types import REAL
9-
from sqlalchemy import select
11+
from sqlalchemy.exc import IntegrityError
12+
1013

1114
qtable = Table(
1215
'quote',
@@ -40,7 +43,7 @@ def add_quote(db, chan, target, sender, message):
4043
)
4144
db.execute(query)
4245
db.commit()
43-
except Exception:
46+
except IntegrityError:
4447
return "Message already stored, doing nothing."
4548
return "Quote added."
4649

plugins/youtube.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
err_no_api = "The YouTube API is off in the Google Developers Console."
2020

2121

22-
def get_video_description(video_id, key):
23-
json = requests.get(api_url.format(video_id, key)).json()
22+
def get_video_description(video_id):
23+
json = requests.get(api_url.format(video_id, dev_key)).json()
2424

2525
if json.get('error'):
2626
if json['error']['code'] == 403:
@@ -75,7 +75,7 @@ def load_key(bot):
7575

7676
@hook.regex(youtube_re)
7777
def youtube_url(match):
78-
return get_video_description(match.group(1), dev_key)
78+
return get_video_description(match.group(1))
7979

8080

8181
@hook.command("youtube", "you", "yt", "y")
@@ -97,7 +97,7 @@ def youtube(text):
9797

9898
video_id = json['items'][0]['id']['videoId']
9999

100-
return get_video_description(video_id, dev_key) + " - " + video_url % video_id
100+
return get_video_description(video_id) + " - " + video_url % video_id
101101

102102

103103
@hook.command("youtime", "ytime")

0 commit comments

Comments
 (0)