Created
April 2, 2015 13:47
-
-
Save andrewbolster/bfb7862ce985954c6279 to your computer and use it in GitHub Desktop.
imgur arbitrary URL parser and decomposer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
from time import sleep | |
from imgurpython import ImgurClient | |
from imgurpython.helpers.error import ImgurClientError, ImgurClientRateLimitError | |
def imgurlinker(x): | |
global client | |
try: | |
defrag = urllib2.urlparse.urldefrag(x)[0] # To deal with post-url items eg "#0" | |
splits = defrag.split("/") | |
if defrag.endswith('new'): | |
splits = splits[:-1] | |
if len(splits) == 4: | |
# Single Image | |
img = client.get_image(splits[-1].split('.')[0]) | |
img_links = [img.link] | |
elif len(splits) == 5: | |
#Album or Gallery | |
if splits[-2] == "a": #Album | |
images = client.get_album_images(splits[-1]) | |
elif splits[-2] == "gallery": | |
gallery = client.gallery_item(splits[-1]) | |
if gallery.is_album: | |
images = [client.get_image(i['id']) for i in gallery.images] | |
else: | |
images = [gallery] | |
else: | |
print "ERROR in ==5: {}".format(x) | |
images = [] | |
img_links = [img.link for img in images] | |
else: | |
print "ERROR unknown split: {}".format(x) | |
img_links = [] | |
print splits[-1] | |
return img_links | |
except ImgurClientRateLimitError as e: | |
print("Rate Ex on {} - Sleeping for 30".format(x)) | |
print client.credits | |
sleep(30) | |
client = get_imgur_client() | |
return imgurlinker(x) | |
except ImgurClientError as e: | |
print("Imgur Error on {}:({}){}: Skipping".format( | |
defrag, e.status_code, e.error_message | |
)) | |
return [] | |
except: | |
print "ERROR: {}".format(x) | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment