From 578f03259b0ba428d55c8c79196090733f4925f0 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Sun, 23 Jul 2017 02:13:24 -0400 Subject: [PATCH] Adding some comments --- rtv/mime_parsers.py | 22 ++++++++++++++++------ rtv/templates/rtv.cfg | 4 ++-- rtv/terminal.py | 5 +++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/rtv/mime_parsers.py b/rtv/mime_parsers.py index 2747747..4965845 100644 --- a/rtv/mime_parsers.py +++ b/rtv/mime_parsers.py @@ -141,28 +141,37 @@ class ImgurApiMIMEParser(BaseMIMEParser): Reference: https://apidocs.imgur.com """ - - pattern = re.compile(r'https?://(w+\.)?(m\.)?imgur\.com/((?Pa|album|gallery)/)?(?P[a-zA-Z0-9]+)[^.]+$') - client_id = None + CLIENT_ID = None + pattern = re.compile(r'https?://(w+\.)?(m\.)?imgur\.com/((?Pa|album|gallery)/)?(?P[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 diff --git a/rtv/templates/rtv.cfg b/rtv/templates/rtv.cfg index 2fec8ed..84e96b7 100644 --- a/rtv/templates/rtv.cfg +++ b/rtv/templates/rtv.cfg @@ -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 diff --git a/rtv/terminal.py b/rtv/terminal.py index ecf73e3..e0125ba 100644 --- a/rtv/terminal.py +++ b/rtv/terminal.py @@ -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):