Add imgur prototype
This commit is contained in:
parent
bfd600cf49
commit
a49bcc881c
2 changed files with 80 additions and 19 deletions
|
@ -1,19 +0,0 @@
|
||||||
# Gets an imgur link from /r/aww
|
|
||||||
from util import hook, http
|
|
||||||
import random
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
|
||||||
def aww(inp):
|
|
||||||
try:
|
|
||||||
data = http.get_json("http://reddit.com/r/aww/.json",
|
|
||||||
user_agent=http.ua_chrome)
|
|
||||||
except Exception as e:
|
|
||||||
return "Error: " + str(e)
|
|
||||||
data = data["data"]["children"]
|
|
||||||
item = None
|
|
||||||
while not item:
|
|
||||||
tempitem = random.choice(data)
|
|
||||||
if tempitem["data"]["domain"] == "i.imgur.com":
|
|
||||||
item = tempitem["data"]
|
|
||||||
return "%s %s" % (item["title"], item["url"])
|
|
80
plugins/imgur.py
Normal file
80
plugins/imgur.py
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
|
||||||
|
from util import hook, http, text, timesince
|
||||||
|
import re
|
||||||
|
import random
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
|
||||||
|
reddit_url = "http://redd.it/{}"
|
||||||
|
base_url = "http://reddit.com/r/{}/.json"
|
||||||
|
imgur_re = re.compile(r'http://(?:i\.)?imgur\.com/(?:(a)(?:/(\w+))?|(\w+\b(?!/))\.?\w?)')
|
||||||
|
|
||||||
|
image_api = "https://api.imgur.com/3/image/{}.json"
|
||||||
|
album_api = "https://api.imgur.com/3/album/{}.json"
|
||||||
|
|
||||||
|
def imgur_query(image_id, is_album=False):
|
||||||
|
if is_album:
|
||||||
|
url = album_api.format(image_id)
|
||||||
|
else:
|
||||||
|
url = image_api.format(image_id)
|
||||||
|
return http.get_json(url)
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid(data):
|
||||||
|
if data["domain"] in ["i.imgur.com", "imgur.com"]:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@hook.command
|
||||||
|
def imgur(inp):
|
||||||
|
try:
|
||||||
|
data = http.get_json(base_url.format(inp.strip()),
|
||||||
|
user_agent=http.ua_chrome)
|
||||||
|
except Exception as e:
|
||||||
|
return "Error: " + str(e)
|
||||||
|
|
||||||
|
data = data["data"]["children"]
|
||||||
|
random.shuffle(data)
|
||||||
|
|
||||||
|
# filter list to only have 10 imgur links
|
||||||
|
filtered_list = [i["data"] for i in data if is_valid(i["data"])][:10]
|
||||||
|
|
||||||
|
if not filtered_list:
|
||||||
|
return "No images found."
|
||||||
|
|
||||||
|
items = []
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"Authorization": "Client-ID b5d127e6941b07a"
|
||||||
|
}
|
||||||
|
|
||||||
|
for item in filtered_list:
|
||||||
|
match = imgur_re.search(item["url"])
|
||||||
|
if match.group(1) == 'a':
|
||||||
|
url = album_api.format(match.group(2))
|
||||||
|
is_album = True
|
||||||
|
elif match.group(3) is not None:
|
||||||
|
url = image_api.format(match.group(3))
|
||||||
|
is_album = False
|
||||||
|
|
||||||
|
result = http.get_json(url, headers=headers)["data"]
|
||||||
|
|
||||||
|
if is_album:
|
||||||
|
items.append(result["id"])
|
||||||
|
else:
|
||||||
|
items.append(result["id"])
|
||||||
|
|
||||||
|
#post_data = {
|
||||||
|
# "ids": items,
|
||||||
|
# "title": "images from /r/{}/".format(inp)
|
||||||
|
#}
|
||||||
|
|
||||||
|
#album = http.get("https://api.imgur.com/3/album/", post_data=post_data, get_method="post", headers=headers)
|
||||||
|
#pprint(album)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return "http://imgur.com/" + ','.join(items)
|
||||||
|
|
Reference in a new issue