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:
https://apidocs.imgur.com
"""
pattern = re.compile(r'https?://(w+\.)?(m\.)?imgur\.com/((?P<domain>a|album|gallery)/)?(?P<hash>[a-zA-Z0-9]+)[^.]+$')
client_id = None
CLIENT_ID = None
pattern = re.compile(r'https?://(w+\.)?(m\.)?imgur\.com/((?P<domain>a|album|gallery)/)?(?P<hash>[a-zA-Z0-9]+)$')
@classmethod
def get_mimetype(cls, url):
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)
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)
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)
r = requests.get(url, headers=headers)
if r.status_code != 200:
# Fallback and use the old page scraper
if domain == 'album':
return ImgurScrapeAlbumMIMEParser.get_mimetype(url)
else:
@@ -242,6 +251,7 @@ class ImgurScrapeAlbumMIMEParser(BaseMIMEParser):
else:
return url, None
class InstagramMIMEParser(OpenGraphMIMEParser):
"""
Instagram uses the Open Graph protocol

View File

@@ -63,8 +63,8 @@ oauth_redirect_port = 65000
; Access permissions that will be requested.
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
; convoluted imgur links and albums so they can be opened with mailcap.
; This is a separate token for the imgur api. It's used to extract images
; from imgur links and albums so they can be opened with mailcap.
; See https://imgur.com/account/settings/apps to generate your own key.
imgur_client_id = 93396265f59dec9

View File

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