class API: def __init__(self, _client_id, _client_secret): self.client_id = _client_id self.client_secret = _client_secret self.client = ImgurClient(_client_id, _client_secret) pass def get_album_image_urls(self, _album_id): images = self.client.get_album(_album_id).__dict__['images'] links = [] for image in images: links.append(image['link']) pass return links pass def get_favorites_image_urls(self, username): images = self.client.get_account_favorites(username) links = [] for image in images: links.append(image.__dict__['link']) pass return links pass pass
class SimpleImgurClient: def __init__(self, username=''): self.load_config() self.username = username self.client = ImgurClient(self.client_id, self.client_secret) self.client.set_user_auth(self.access_token, self.refresh_token) self.authorization_url = self.client.get_auth_url('pin') def load_config(self): config = get_config() config.read('auth.ini') self.client_id = config.get('credentials', 'client_id') self.client_secret = config.get('credentials', 'client_secret') self.access_token = config.get('credentials', 'access_token') self.refresh_token = config.get('credentials', 'refresh_token') def authenticate(self): print("Get your access PIN here: %s" % (self.authorization_url,)) pin = get_input("Please enter your PIN: ") self.authorize(pin) def authorize(self, pin): credentials = self.client.authorize(pin, 'pin') self.client.set_user_auth(credentials['access_token'], credentials['refresh_token']) self.access_token = credentials['access_token'] self.refresh_token = credentials['refresh_token'] self.save() def save(self): with open('auth.ini', 'w') as sessionfile: sessionfile.write("[credentials]\n") sessionfile.write("client_id={0}\n".format(self.client_id)) sessionfile.write("client_secret={0}\n".format(self.client_secret)) sessionfile.write("access_token={0}\n".format(self.access_token)) sessionfile.write("refresh_token={0}\n".format(self.refresh_token)) def whoami(self): '''Request account information from IMGUR server''' acc = self.client.get_account('me') # print("Account ID : %s" % (acc.id)) # print("Account URL: %s" % (acc.url)) # print("Account bio: %s" % (acc.bio)) return acc def backup_myfavs(self, max_page_count=1): imgs = [] with codecs.open('myfav.htm', 'w', 'utf-8') as myfav: for page in range(max_page_count): print("Fetching page #%s" % (page,)) myfav.write("<h1>Page #%s</h1>" % (page)) myfav.write("<table>") myfav.write("<tr><td>Title</td><td>Description</td><td>Datetime</td><td>Link</td></tr>") favs = self.client.get_account_favorites('me', page) for img in favs: imgs.append(img) myfav.write("<tr><td>%s</td><td>%s</td><td>%s</td><td><a href='%s'>%s</a><br/></td></tr>\n" % (img.title, img.description, datetime.datetime.fromtimestamp(img.datetime), img.link, img.link)) # myfav.write('<a href="%s">%s</a><br/>\n' % (img.link, img.link)) myfav.write("</table>") return imgs