-
Notifications
You must be signed in to change notification settings - Fork 1
/
imgur_wrapper.py
78 lines (67 loc) · 2.25 KB
/
imgur_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import logging
from imgurpython import ImgurClient
from imgurpython.helpers.error import ImgurClientError
class ImgurWrapper:
def __init__(self, client_id, client_secret):
self.__client = ImgurClient(client_id, client_secret)
@staticmethod
def is_imgur(url):
"""
Simple check to see if url is an imgur url
:param url: ParsedUrl of the
:return: boolean if it is a imagr or not
"""
return "imgur.com" in url.hostname
@staticmethod
def is_album(url):
if "/a/" not in url.path:
return False
else:
return True
def get_image_list(self, url):
"""
This call is intended to take the url and return a list of all images associated with it. It will parse
the image without extension or parse all images.
:param url: parsed url object
:return: list of images
"""
image_list = []
if self.is_album(url):
image_list = self.get_album_images(url)
else:
image = self.get_image(url)
if image is not None:
image_list.append(image)
return image_list
def get_image(self, url):
"""
Get a single image from a url
:param url: parsed url
:return: an image or None if exception raised
"""
image_id = url.path[url.path.rfind("/") + 1 :]
try:
image = self.__client.get_image(image_id)
except ImgurClientError as e:
logging.error(
"Status Code: " + e.status_code + " Error: " + e.error_message
)
image = None
return image
def get_album_images(self, url):
"""
Gets all the images in an album as a list of image objects
:param url: parsed url
:return: Either a list of images or an empty list
"""
album_id = url.path[url.path.rfind("/") + 1 :]
image_list = []
try:
images = self.__client.get_album_images(album_id)
except ImgurClientError as e:
logging.error(
"Status Code: " + str(e.status_code) + " Error: " + e.error_message
)
else:
image_list = images
return image_list