2018-10-31 16:12:28 +00:00
|
|
|
#--require-config imgur-api-key
|
|
|
|
|
2018-10-31 16:16:31 +00:00
|
|
|
import re
|
2018-10-31 16:12:28 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2018-10-31 16:29:24 +00:00
|
|
|
REGEX_IMAGE = re.compile("https?://(?:i\.)?imgur.com/(\w+)")
|
2018-10-31 16:12:28 +00:00
|
|
|
URL_IMAGE = "https://api.imgur.com/3/image/%s"
|
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def _image_info(self, hash):
|
|
|
|
api_key = self.bot.config["imgur-api-key"]
|
|
|
|
result = utils.http.get_url(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)
|
|
|
|
|
|
|
|
if result and result["success"]:
|
|
|
|
data = result["data"]
|
|
|
|
text = "%s: " % data["id"]
|
|
|
|
if data["nsfw"]:
|
|
|
|
text += "[NSFW] "
|
|
|
|
|
|
|
|
text += "(%s %dx%d, %d views) " % (data["type"], data["width"],
|
|
|
|
data["height"], data["views"])
|
|
|
|
if data["title"]:
|
|
|
|
text += " %s" % data["title"]
|
|
|
|
return text
|
|
|
|
else:
|
|
|
|
raise utils.EventsResultsError()
|
|
|
|
|
|
|
|
@utils.hook("received.command.imgur", min_args=1)
|
|
|
|
def imgur(self, event):
|
|
|
|
"""
|
|
|
|
:help: Get information about a given imgur image URL
|
|
|
|
:usage: <url>
|
|
|
|
"""
|
|
|
|
match = REGEX_IMAGE.match(event["args_split"][0])
|
|
|
|
if match:
|
|
|
|
event["stdout"].write(self._image_info(match.group(1)))
|