Update imgur.py
Add auto-imgur for auto linking of imgur and make the linking a bit fancier.
This commit is contained in:
parent
41cc90cb72
commit
266cbbfe1e
1 changed files with 101 additions and 3 deletions
104
modules/imgur.py
104
modules/imgur.py
|
@ -1,7 +1,8 @@
|
||||||
#--require-config imgur-api-key
|
#--require-config imgur-api-key
|
||||||
|
|
||||||
import re
|
import re, json
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils, EventManager, Logging
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
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+)")
|
||||||
|
@ -9,6 +10,9 @@ REGEX_GALLERY = re.compile("https?://imgur.com/gallery/(\w+)")
|
||||||
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"
|
||||||
|
|
||||||
|
@utils.export("channelset", {"setting": "auto-imgur",
|
||||||
|
"help": "Disable/Enable automatically getting info from Imgur URLs",
|
||||||
|
"validate": utils.bool_or_none})
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
def _prefix(self, data):
|
def _prefix(self, data):
|
||||||
text = "%s: " % data["id"]
|
text = "%s: " % data["id"]
|
||||||
|
@ -18,6 +22,97 @@ class Module(ModuleManager.BaseModule):
|
||||||
text += "%s " % data["account_url"]
|
text += "%s " % data["account_url"]
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
@utils.hook("received.message.channel",
|
||||||
|
priority=EventManager.PRIORITY_LOW)
|
||||||
|
def channel_message(self, event):
|
||||||
|
if not event["channel"].get_setting("auto-imgur", False):
|
||||||
|
return
|
||||||
|
|
||||||
|
self._imgur(event)
|
||||||
|
|
||||||
|
def _imgur(self, event):
|
||||||
|
msg = event["message"]
|
||||||
|
reply = ""
|
||||||
|
match_gallery = REGEX_GALLERY.match(msg)
|
||||||
|
match_image = REGEX_IMAGE.match(msg)
|
||||||
|
|
||||||
|
if match_gallery:
|
||||||
|
reply = self._parse_gallery(match_gallery.group(1))
|
||||||
|
|
||||||
|
if match_image and not reply:
|
||||||
|
reply = self._parse_image(match_image.group(1))
|
||||||
|
|
||||||
|
if not reply:
|
||||||
|
return
|
||||||
|
|
||||||
|
event.eat()
|
||||||
|
|
||||||
|
self.events.on("send.stdout").call(target=event[
|
||||||
|
"channel"], module_name="Imgur", server=event["server"],
|
||||||
|
message=reply)
|
||||||
|
return
|
||||||
|
|
||||||
|
def _parse_gallery(self, hash):
|
||||||
|
api_key = self.bot.config["imgur-api-key"]
|
||||||
|
result = utils.http.request(URL_GALLERY % hash,
|
||||||
|
headers={"Authorization": "Client-ID %s" % api_key},
|
||||||
|
json=True)
|
||||||
|
|
||||||
|
if result and result.data["success"]:
|
||||||
|
data = result.data["data"]
|
||||||
|
text = ""
|
||||||
|
|
||||||
|
nsfw = utils.irc.bold("[NSFW] ") if data["nsfw"] else ""
|
||||||
|
title = utils.irc.bold(data["title"]) + " " if data["title"] else ""
|
||||||
|
views = data["views"]
|
||||||
|
time = datetime.utcfromtimestamp(data["datetime"]). \
|
||||||
|
strftime("%e %b, %Y at %H:%M")
|
||||||
|
ups = utils.irc.color(str(data["ups"]) + "▲", utils.consts.GREEN)
|
||||||
|
downs = utils.irc.color("▼" + str(data["downs"]), utils.consts.RED)
|
||||||
|
images = data["images_count"]
|
||||||
|
image_plural = "" if images is 1 else "s"
|
||||||
|
|
||||||
|
bracket_left = "(" 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" % \
|
||||||
|
(nsfw, title, bracket_left, images, image_plural, views,
|
||||||
|
time, ups, downs,
|
||||||
|
bracket_right)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_image(self, hash):
|
||||||
|
api_key = self.bot.config["imgur-api-key"]
|
||||||
|
result = utils.http.request(URL_IMAGE % hash,
|
||||||
|
headers={"Authorization": "Client-ID %s" % api_key},
|
||||||
|
json=True)
|
||||||
|
|
||||||
|
if result and result.data["success"]:
|
||||||
|
# Logging.Log.debug("%s", result.data["data"])
|
||||||
|
|
||||||
|
data = result.data["data"]
|
||||||
|
text = ""
|
||||||
|
|
||||||
|
nsfw = utils.irc.bold("[NSFW] ") if data["nsfw"] else ""
|
||||||
|
title = utils.irc.bold(data["title"]) + " " if data["title"] else ""
|
||||||
|
type = data["type"].split("/")[-1]
|
||||||
|
width = data["width"]
|
||||||
|
height = data["height"]
|
||||||
|
views = data["views"]
|
||||||
|
time = datetime.utcfromtimestamp(data["datetime"]).\
|
||||||
|
strftime("%e %b, %Y at %H:%M")
|
||||||
|
|
||||||
|
#%Y-%m-%d %H:%M:%S+00:00 (UTC)
|
||||||
|
|
||||||
|
bracket_left = "(" 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" % \
|
||||||
|
(nsfw, title, bracket_left, type, width, height, views, time,
|
||||||
|
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"]
|
||||||
result = utils.http.request(URL_IMAGE % hash,
|
result = utils.http.request(URL_IMAGE % hash,
|
||||||
|
@ -59,10 +154,13 @@ 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>
|
||||||
"""
|
"""
|
||||||
match = REGEX_GALLERY.match(event["args_split"][0])
|
msg = event["args_split"][0]
|
||||||
|
|
||||||
|
match = REGEX_GALLERY.match(msg)
|
||||||
if match:
|
if match:
|
||||||
event["stdout"].write(self._gallery_info(match.group(1)))
|
event["stdout"].write(self._gallery_info(match.group(1)))
|
||||||
return
|
return
|
||||||
|
|
||||||
match = REGEX_IMAGE.match(event["args_split"][0])
|
match = REGEX_IMAGE.match(event["args_split"][0])
|
||||||
if match:
|
if match:
|
||||||
event["stdout"].write(self._image_info(match.group(1)))
|
event["stdout"].write(self._image_info(match.group(1)))
|
||||||
|
|
Loading…
Reference in a new issue