2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
2018-10-31 16:12:28 +00:00
|
|
|
#--require-config imgur-api-key
|
|
|
|
|
2019-02-24 21:13:02 +00:00
|
|
|
import re, datetime
|
2019-02-24 20:37:17 +00:00
|
|
|
from src import ModuleManager, utils, EventManager
|
2018-10-31 16:12:28 +00:00
|
|
|
|
2018-10-31 16:29:24 +00:00
|
|
|
REGEX_IMAGE = re.compile("https?://(?:i\.)?imgur.com/(\w+)")
|
2018-10-31 17:41:12 +00:00
|
|
|
REGEX_GALLERY = re.compile("https?://imgur.com/gallery/(\w+)")
|
2018-10-31 17:44:21 +00:00
|
|
|
|
2019-02-25 09:59:38 +00:00
|
|
|
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"
|
|
|
|
|
2018-10-31 16:12:28 +00:00
|
|
|
URL_IMAGE = "https://api.imgur.com/3/image/%s"
|
2018-10-31 17:44:21 +00:00
|
|
|
URL_GALLERY = "https://api.imgur.com/3/gallery/%s"
|
2018-10-31 16:12:28 +00:00
|
|
|
|
2019-02-24 21:10:25 +00:00
|
|
|
NSFW_TEXT = "(NSFW)"
|
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("channelset", utils.BoolSetting("auto-imgur",
|
|
|
|
"Disable/Enable automatically getting info from Imgur URLs"))
|
2018-10-31 16:12:28 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-10-31 17:41:12 +00:00
|
|
|
def _prefix(self, data):
|
|
|
|
text = "%s: " % data["id"]
|
|
|
|
if data["nsfw"]:
|
|
|
|
text += "[NSFW] "
|
|
|
|
if data["account_url"]:
|
|
|
|
text += "%s " % data["account_url"]
|
2018-10-31 17:54:44 +00:00
|
|
|
return text
|
2018-10-31 17:41:12 +00:00
|
|
|
|
2019-06-26 13:37:26 +00:00
|
|
|
@utils.hook("command.regex")
|
|
|
|
@utils.kwarg("ignore_action", False)
|
|
|
|
@utils.kwarg("command", "imgur")
|
|
|
|
@utils.kwarg("pattern", REGEX_IMAGE)
|
2019-05-18 18:20:09 +00:00
|
|
|
def _regex_image(self, event):
|
2019-05-18 20:33:50 +00:00
|
|
|
if event["target"].get_setting("auto-imgur", False):
|
2019-05-18 18:20:09 +00:00
|
|
|
event["stdout"].write(self._parse_image(event["match"].group(1)))
|
|
|
|
event.eat()
|
2019-06-26 13:37:26 +00:00
|
|
|
|
|
|
|
@utils.hook("command.regex")
|
|
|
|
@utils.kwarg("ignore_action", False)
|
|
|
|
@utils.kwarg("command", "imgur")
|
|
|
|
@utils.kwarg("pattern", REGEX_GALLERY)
|
2019-05-18 18:20:09 +00:00
|
|
|
def _regex_gallery(self, event):
|
2019-05-18 20:33:50 +00:00
|
|
|
if event["target"].get_setting("auto-imgur", False):
|
2019-05-18 18:20:09 +00:00
|
|
|
event["stdout"].write(self._parse_gallery(event["match"].group(1)))
|
2019-02-25 09:59:38 +00:00
|
|
|
event.eat()
|
2019-02-24 20:34:10 +00:00
|
|
|
|
|
|
|
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 = ""
|
|
|
|
|
2019-02-24 21:10:25 +00:00
|
|
|
nsfw = utils.irc.bold(NSFW_TEXT) + " " if data["nsfw"] else ""
|
|
|
|
title = data["title"] + " " if data["title"] else ""
|
2019-02-24 20:34:10 +00:00
|
|
|
views = data["views"]
|
2019-02-24 21:13:02 +00:00
|
|
|
time = datetime.datetime.utcfromtimestamp(data["datetime"]). \
|
2019-02-24 20:34:10 +00:00
|
|
|
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"]
|
2019-02-24 21:10:25 +00:00
|
|
|
image_plural = "" if images == 1 else "s"
|
2019-02-24 20:34:10 +00:00
|
|
|
|
|
|
|
bracket_left = "(" if title or nsfw else ""
|
|
|
|
bracket_right = ")" if title or nsfw else ""
|
|
|
|
|
2019-02-25 09:59:38 +00:00
|
|
|
return GALLERY_FORMAT % (nsfw, title, bracket_left, images,
|
|
|
|
image_plural, views, time, ups, downs, bracket_right)
|
2019-02-24 20:34:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
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"]:
|
|
|
|
data = result.data["data"]
|
|
|
|
text = ""
|
|
|
|
|
2019-02-24 21:10:25 +00:00
|
|
|
nsfw = utils.irc.bold(NSFW_TEXT) + " " if data["nsfw"] else ""
|
|
|
|
title = data["title"] + " " if data["title"] else ""
|
2019-02-24 20:34:10 +00:00
|
|
|
type = data["type"].split("/")[-1]
|
|
|
|
width = data["width"]
|
|
|
|
height = data["height"]
|
|
|
|
views = data["views"]
|
2019-02-24 21:13:02 +00:00
|
|
|
time = datetime.datetime.utcfromtimestamp(data["datetime"]).\
|
2019-02-24 20:34:10 +00:00
|
|
|
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 ""
|
|
|
|
|
2019-02-25 09:59:38 +00:00
|
|
|
return IMAGE_FORMAT % (nsfw, title, bracket_left, type, width, height,
|
|
|
|
views, time, bracket_right)
|
2019-02-24 20:34:10 +00:00
|
|
|
|
2018-10-31 16:12:28 +00:00
|
|
|
def _image_info(self, hash):
|
|
|
|
api_key = self.bot.config["imgur-api-key"]
|
2018-12-11 22:26:38 +00:00
|
|
|
result = utils.http.request(URL_IMAGE % hash,
|
2018-10-31 16:17:22 +00:00
|
|
|
headers={"Authorization": "Client-ID %s" % api_key},
|
2018-10-31 16:12:28 +00:00
|
|
|
json=True)
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
if result and result.data["success"]:
|
|
|
|
data = result.data["data"]
|
2018-10-31 17:41:12 +00:00
|
|
|
text = self._prefix(data)
|
2018-10-31 16:12:28 +00:00
|
|
|
|
2018-10-31 17:54:44 +00:00
|
|
|
text += "(%s %dx%d, %d views)" % (data["type"], data["width"],
|
2018-10-31 16:12:28 +00:00
|
|
|
data["height"], data["views"])
|
|
|
|
if data["title"]:
|
2018-10-31 17:54:44 +00:00
|
|
|
text += " %s" % data["title"]
|
2018-10-31 17:41:12 +00:00
|
|
|
return text
|
|
|
|
else:
|
2019-02-25 09:59:38 +00:00
|
|
|
return None
|
2018-10-31 17:41:12 +00:00
|
|
|
|
|
|
|
def _gallery_info(self, hash):
|
|
|
|
api_key = self.bot.config["imgur-api-key"]
|
2018-12-11 22:26:38 +00:00
|
|
|
result = utils.http.request(URL_GALLERY % hash,
|
2018-10-31 17:41:12 +00:00
|
|
|
headers={"Authorization": "Client-ID %s" % api_key},
|
|
|
|
json=True)
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
if result and result.data["success"]:
|
|
|
|
data = result.data["data"]
|
2018-10-31 17:41:12 +00:00
|
|
|
text = self._prefix(data)
|
2018-10-31 17:54:44 +00:00
|
|
|
text += "(%d views, %d▲▼%d)" % (data["views"],
|
|
|
|
data["ups"], data["downs"])
|
2018-10-31 17:41:12 +00:00
|
|
|
if data["title"]:
|
2018-10-31 17:54:44 +00:00
|
|
|
text += " %s" % data["title"]
|
2018-10-31 16:12:28 +00:00
|
|
|
return text
|
|
|
|
else:
|
2019-02-25 09:59:38 +00:00
|
|
|
return None
|
2018-10-31 16:12:28 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.imgur", min_args=1)
|
|
|
|
def imgur(self, event):
|
|
|
|
"""
|
|
|
|
:help: Get information about a given imgur image URL
|
|
|
|
:usage: <url>
|
|
|
|
"""
|
2019-02-25 09:59:38 +00:00
|
|
|
image_match = REGEX_IMAGE.match(event["args_split"][0])
|
2019-02-24 20:34:10 +00:00
|
|
|
|
2019-02-25 09:59:38 +00:00
|
|
|
result = None
|
|
|
|
if image_match:
|
|
|
|
result = self._image_info(image_match.group(1))
|
|
|
|
else:
|
|
|
|
gallery_match = REGEX_GALLERY.match(event["args_split"][0])
|
|
|
|
if gallery_match:
|
|
|
|
result = self._gallery_info(gallery_match.group(1))
|
2019-02-24 20:34:10 +00:00
|
|
|
|
2019-02-25 09:59:38 +00:00
|
|
|
if result:
|
|
|
|
event["stdout"].write(result)
|
|
|
|
else:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|