Minor code stylisation in imgur.py

This commit is contained in:
jesopo 2019-02-25 09:59:38 +00:00
parent 10c8b129d5
commit 5eceb5655c

View file

@ -6,6 +6,9 @@ from src import ModuleManager, utils, EventManager
REGEX_IMAGE = re.compile("https?://(?:i\.)?imgur.com/(\w+)") REGEX_IMAGE = re.compile("https?://(?:i\.)?imgur.com/(\w+)")
REGEX_GALLERY = re.compile("https?://imgur.com/gallery/(\w+)") REGEX_GALLERY = re.compile("https?://imgur.com/gallery/(\w+)")
GALLERY_FORMAT = "%s%s%sA gallery with %s image%s, %s views, posted %s (%s%s)%s"
IMAGE_FORMAT = "%s%s%sA %s image, %sx%s, with %s views, posted %s%s"
URL_IMAGE = "https://api.imgur.com/3/image/%s" URL_IMAGE = "https://api.imgur.com/3/image/%s"
URL_GALLERY = "https://api.imgur.com/3/gallery/%s" URL_GALLERY = "https://api.imgur.com/3/gallery/%s"
@ -23,31 +26,24 @@ class Module(ModuleManager.BaseModule):
text += "%s " % data["account_url"] text += "%s " % data["account_url"]
return text return text
@utils.hook("received.message.channel", @utils.hook("received.message.channel", priority=EventManager.PRIORITY_LOW)
priority=EventManager.PRIORITY_LOW)
def channel_message(self, event): def channel_message(self, event):
if not event["channel"].get_setting("auto-imgur", False): if event["channel"].get_setting("auto-imgur", False):
return match_gallery = REGEX_GALLERY.match(event["message"])
match_image = REGEX_IMAGE.match(event["message"])
msg = event["message"]
reply = ""
match_gallery = REGEX_GALLERY.match(msg)
match_image = REGEX_IMAGE.match(msg)
result = None
if match_gallery: if match_gallery:
reply = self._parse_gallery(match_gallery.group(1)) result = self._parse_gallery(match_gallery.group(1))
elif match_image: elif match_image:
reply = self._parse_image(match_image.group(1)) result = self._parse_image(match_image.group(1))
else: else:
return return
event.eat() event.eat()
self.events.on("send.stdout").call(target=event["channel"], module_name="Imgur", server=event["server"], self.events.on("send.stdout").call(target=event["channel"],
message=reply) module_name="Imgur", server=event["server"], message=result)
return
def _parse_gallery(self, hash): def _parse_gallery(self, hash):
api_key = self.bot.config["imgur-api-key"] api_key = self.bot.config["imgur-api-key"]
@ -72,10 +68,8 @@ class Module(ModuleManager.BaseModule):
bracket_left = "(" if title or nsfw else "" bracket_left = "(" if title or nsfw else ""
bracket_right = ")" if title or nsfw else "" bracket_right = ")" if title or nsfw else ""
return "%s%s%sA gallery with %s image%s, %s views, posted %s (%s%s)%s" % \ return GALLERY_FORMAT % (nsfw, title, bracket_left, images,
(nsfw, title, bracket_left, images, image_plural, views, image_plural, views, time, ups, downs, bracket_right)
time, ups, downs,
bracket_right)
def _parse_image(self, hash): def _parse_image(self, hash):
@ -102,9 +96,8 @@ class Module(ModuleManager.BaseModule):
bracket_left = "(" if title or nsfw else "" bracket_left = "(" if title or nsfw else ""
bracket_right = ")" if title or nsfw else "" bracket_right = ")" if title or nsfw else ""
return "%s%s%sA %s image, %sx%s, with %s views, posted %s%s" % \ return IMAGE_FORMAT % (nsfw, title, bracket_left, type, width, height,
(nsfw, title, bracket_left, type, width, height, views, time, views, time, bracket_right)
bracket_right)
def _image_info(self, hash): def _image_info(self, hash):
api_key = self.bot.config["imgur-api-key"] api_key = self.bot.config["imgur-api-key"]
@ -122,7 +115,7 @@ class Module(ModuleManager.BaseModule):
text += " %s" % data["title"] text += " %s" % data["title"]
return text return text
else: else:
raise utils.EventsResultsError() return None
def _gallery_info(self, hash): def _gallery_info(self, hash):
api_key = self.bot.config["imgur-api-key"] api_key = self.bot.config["imgur-api-key"]
@ -139,7 +132,7 @@ class Module(ModuleManager.BaseModule):
text += " %s" % data["title"] text += " %s" % data["title"]
return text return text
else: else:
raise utils.EventsResultsError() return None
@utils.hook("received.command.imgur", min_args=1) @utils.hook("received.command.imgur", min_args=1)
def imgur(self, event): def imgur(self, event):
@ -147,14 +140,17 @@ class Module(ModuleManager.BaseModule):
:help: Get information about a given imgur image URL :help: Get information about a given imgur image URL
:usage: <url> :usage: <url>
""" """
msg = event["args_split"][0] image_match = REGEX_IMAGE.match(event["args_split"][0])
match = REGEX_GALLERY.match(msg) result = None
if match: if image_match:
event["stdout"].write(self._gallery_info(match.group(1))) result = self._image_info(image_match.group(1))
return else:
gallery_match = REGEX_GALLERY.match(event["args_split"][0])
if gallery_match:
result = self._gallery_info(gallery_match.group(1))
match = REGEX_IMAGE.match(event["args_split"][0]) if result:
if match: event["stdout"].write(result)
event["stdout"].write(self._image_info(match.group(1))) else:
return raise utils.EventsResultsError()