Adding some comments

This commit is contained in:
Michael Lazar
2017-07-23 02:13:24 -04:00
parent e388591b54
commit 578f03259b
3 changed files with 21 additions and 10 deletions

View File

@@ -141,28 +141,37 @@ class ImgurApiMIMEParser(BaseMIMEParser):
Reference: Reference:
https://apidocs.imgur.com https://apidocs.imgur.com
""" """
CLIENT_ID = None
pattern = re.compile(r'https?://(w+\.)?(m\.)?imgur\.com/((?P<domain>a|album|gallery)/)?(?P<hash>[a-zA-Z0-9]+)[^.]+$') pattern = re.compile(r'https?://(w+\.)?(m\.)?imgur\.com/((?P<domain>a|album|gallery)/)?(?P<hash>[a-zA-Z0-9]+)$')
client_id = None
@classmethod @classmethod
def get_mimetype(cls, url): def get_mimetype(cls, url):
endpoint = 'https://api.imgur.com/3/{domain}/{page_hash}' endpoint = 'https://api.imgur.com/3/{domain}/{page_hash}'
headers = {'authorization': 'Client-ID {0}'.format(cls.client_id)} headers = {'authorization': 'Client-ID {0}'.format(cls.CLIENT_ID)}
m = cls.pattern.match(url) m = cls.pattern.match(url)
page_hash = m.group('hash') page_hash = m.group('hash')
domain = 'album' if m.group('domain') in ('a', 'album') else 'gallery'
if m.group('domain') in ('a', 'album'):
domain = 'album'
else:
# This could be a gallery or a single image, but there doesn't
# seem to be a way to reliably distinguish between the two just
# from the URL. So we assume a gallery, which appear to be more
# common, and fallback to an image request upon failure.
domain = 'gallery'
url = endpoint.format(domain=domain, page_hash=page_hash) url = endpoint.format(domain=domain, page_hash=page_hash)
r = requests.get(url, headers=headers) r = requests.get(url, headers=headers)
if r.status_code != 200: if r.status_code != 200 and domain == 'gallery':
# Fallback and try to download using the image endpoint
url = endpoint.format(domain='image', page_hash=page_hash) url = endpoint.format(domain='image', page_hash=page_hash)
r = requests.get(url, headers=headers) r = requests.get(url, headers=headers)
if r.status_code != 200: if r.status_code != 200:
# Fallback and use the old page scraper
if domain == 'album': if domain == 'album':
return ImgurScrapeAlbumMIMEParser.get_mimetype(url) return ImgurScrapeAlbumMIMEParser.get_mimetype(url)
else: else:
@@ -242,6 +251,7 @@ class ImgurScrapeAlbumMIMEParser(BaseMIMEParser):
else: else:
return url, None return url, None
class InstagramMIMEParser(OpenGraphMIMEParser): class InstagramMIMEParser(OpenGraphMIMEParser):
""" """
Instagram uses the Open Graph protocol Instagram uses the Open Graph protocol

View File

@@ -63,8 +63,8 @@ oauth_redirect_port = 65000
; Access permissions that will be requested. ; Access permissions that will be requested.
oauth_scope = edit,history,identity,mysubreddits,privatemessages,read,report,save,submit,subscribe,vote oauth_scope = edit,history,identity,mysubreddits,privatemessages,read,report,save,submit,subscribe,vote
; This is a separate token for the imgur api. It is used to extract images from ; This is a separate token for the imgur api. It's used to extract images
; convoluted imgur links and albums so they can be opened with mailcap. ; from imgur links and albums so they can be opened with mailcap.
; See https://imgur.com/account/settings/apps to generate your own key. ; See https://imgur.com/account/settings/apps to generate your own key.
imgur_client_id = 93396265f59dec9 imgur_client_id = 93396265f59dec9

View File

@@ -61,8 +61,9 @@ class Terminal(object):
self._mailcap_dict = mailcap.getcaps() self._mailcap_dict = mailcap.getcaps()
self._term = os.environ['TERM'] self._term = os.environ['TERM']
# Hack to allow setting the Imgur OAuth cred in the config file # This is a hack, the MIME parsers should be stateless
mime_parsers.ImgurApiMIMEParser.client_id = config['imgur_client_id'] # but we need to load the imgur credentials from the config
mime_parsers.ImgurApiMIMEParser.CLIENT_ID = config['imgur_client_id']
@property @property
def up_arrow(self): def up_arrow(self):