Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update imgur.py #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions modules/imgur.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
URL_IMAGE = "https://api.imgur.com/3/image/%s"
URL_GALLERY = "https://api.imgur.com/3/gallery/%s"

ARROW_UP = "↑"
ARROW_DOWN = "↓"

NSFW_TEXT = "(NSFW)"

@utils.export("channelset", utils.BoolSetting("auto-imgur",
"Disable/Enable automatically getting info from Imgur URLs"))

@utils.export("channelset",
utils.BoolSetting("auto-imgur",
"Disable/Enable automatically getting info from Imgur URLs"))
class Module(ModuleManager.BaseModule):

def _prefix(self, data):
text = "%s: " % data["id"]
if data["nsfw"]:
Expand Down Expand Up @@ -48,7 +54,9 @@ def _regex_gallery(self, event):
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()
headers={
"Authorization": "Client-ID %s" % api_key
}).json()

if result and result["success"]:
data = result["data"]
Expand All @@ -59,22 +67,31 @@ def _parse_gallery(self, hash):
views = data["views"]
time = datetime.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)
ups = utils.irc.color(str(data["ups"]) + ARROW_UP, utils.consts.GREEN)
downs = utils.irc.color(ARROW_DOWN + str(data["downs"]), utils.consts.RED)
images = data["images_count"]
image_plural = "" if images == 1 else "s"

bracket_left = "(" if title or nsfw else ""
bracket_right = ")" if title or nsfw else ""

return GALLERY_FORMAT % (nsfw, title, bracket_left, images,
image_plural, views, time, ups, downs, bracket_right)

return GALLERY_FORMAT % (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()
headers={
"Authorization": "Client-ID %s" % api_key
}).json()

if result and result["success"]:
data = result["data"]
Expand All @@ -89,25 +106,25 @@ def _parse_image(self, hash):
time = datetime.datetime.utcfromtimestamp(data["datetime"]).\
strftime("%e %b, %Y at %H:%M")

#%Y-%m-%d %H:%M:%S+00:00 (UTC)
#%Y-%m-%d %H:%M:%S+00:00 (UTC)

bracket_left = "(" if title or nsfw else ""
bracket_right = ")" if title or nsfw else ""

return IMAGE_FORMAT % (nsfw, title, bracket_left, type, width, height,
views, time, bracket_right)
return IMAGE_FORMAT % (nsfw, title, bracket_left, type, width, height, views, time, bracket_right)

def _image_info(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()
headers={
"Authorization": "Client-ID %s" % api_key
}).json()

if result and result["success"]:
data = result["data"]
text = self._prefix(data)

text += "(%s %dx%d, %d views)" % (data["type"], data["width"],
data["height"], data["views"])
text += "(%s %dx%d, %d views)" % (data["type"], data["width"], data["height"], data["views"])
if data["title"]:
text += " %s" % data["title"]
return text
Expand All @@ -117,13 +134,14 @@ def _image_info(self, hash):
def _gallery_info(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()
headers={
"Authorization": "Client-ID %s" % api_key
}).json()

if result and result["success"]:
data = result["data"]
text = self._prefix(data)
text += "(%d views, %d▲▼%d)" % (data["views"],
data["ups"], data["downs"])
text += "(%d views, %d??%d)" % (data["views"], data["ups"], data["downs"])
if data["title"]:
text += " %s" % data["title"]
return text
Expand Down