Added support for reading albums, other fixes
This commit is contained in:
parent
a49bcc881c
commit
4f2c690b2d
1 changed files with 19 additions and 31 deletions
|
@ -1,23 +1,12 @@
|
||||||
|
from util import hook, http, web
|
||||||
from util import hook, http, text, timesince
|
|
||||||
import re
|
import re
|
||||||
import random
|
import random
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
|
|
||||||
reddit_url = "http://redd.it/{}"
|
|
||||||
base_url = "http://reddit.com/r/{}/.json"
|
base_url = "http://reddit.com/r/{}/.json"
|
||||||
imgur_re = re.compile(r'http://(?:i\.)?imgur\.com/(?:(a)(?:/(\w+))?|(\w+\b(?!/))\.?\w?)')
|
imgur_re = re.compile(r'http://(?:i\.)?imgur\.com/(a/)?(\w+\b(?!/))\.?\w?')
|
||||||
|
|
||||||
image_api = "https://api.imgur.com/3/image/{}.json"
|
album_api = "https://api.imgur.com/3/album/{}/images.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):
|
def is_valid(data):
|
||||||
|
@ -29,6 +18,7 @@ def is_valid(data):
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def imgur(inp):
|
def imgur(inp):
|
||||||
|
"imgur <subreddit> -- Gets the first page of imgur images from <subreddit> and returns a link to them."
|
||||||
try:
|
try:
|
||||||
data = http.get_json(base_url.format(inp.strip()),
|
data = http.get_json(base_url.format(inp.strip()),
|
||||||
user_agent=http.ua_chrome)
|
user_agent=http.ua_chrome)
|
||||||
|
@ -39,9 +29,9 @@ def imgur(inp):
|
||||||
random.shuffle(data)
|
random.shuffle(data)
|
||||||
|
|
||||||
# filter list to only have 10 imgur links
|
# filter list to only have 10 imgur links
|
||||||
filtered_list = [i["data"] for i in data if is_valid(i["data"])][:10]
|
filtered_posts = [i["data"] for i in data if is_valid(i["data"])]
|
||||||
|
|
||||||
if not filtered_list:
|
if not filtered_posts:
|
||||||
return "No images found."
|
return "No images found."
|
||||||
|
|
||||||
items = []
|
items = []
|
||||||
|
@ -50,21 +40,21 @@ def imgur(inp):
|
||||||
"Authorization": "Client-ID b5d127e6941b07a"
|
"Authorization": "Client-ID b5d127e6941b07a"
|
||||||
}
|
}
|
||||||
|
|
||||||
for item in filtered_list:
|
# loop over the list of posts
|
||||||
match = imgur_re.search(item["url"])
|
for post in filtered_posts:
|
||||||
if match.group(1) == 'a':
|
match = imgur_re.search(post["url"])
|
||||||
|
if match.group(1) == 'a/':
|
||||||
|
# post is an album
|
||||||
url = album_api.format(match.group(2))
|
url = album_api.format(match.group(2))
|
||||||
is_album = True
|
images = http.get_json(url, headers=headers)["data"]
|
||||||
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"]
|
# loop over the images in the gallery and add to the list
|
||||||
|
for image in images:
|
||||||
|
items.append(image["id"])
|
||||||
|
|
||||||
if is_album:
|
elif match.group(2) is not None:
|
||||||
items.append(result["id"])
|
# post is an image
|
||||||
else:
|
items.append(match.group(2))
|
||||||
items.append(result["id"])
|
|
||||||
|
|
||||||
#post_data = {
|
#post_data = {
|
||||||
# "ids": items,
|
# "ids": items,
|
||||||
|
@ -75,6 +65,4 @@ def imgur(inp):
|
||||||
#pprint(album)
|
#pprint(album)
|
||||||
|
|
||||||
|
|
||||||
|
return web.isgd("http://imgur.com/" + ','.join(items))
|
||||||
return "http://imgur.com/" + ','.join(items)
|
|
||||||
|
|
Reference in a new issue