[tumblr] check all 4 keys/secrets before using OAuth

it was possible to cause a crash by setting api-key or -secret to null.
(this commit also slightly improves the blog-cache implementation)
This commit is contained in:
Mike Fährmann
2018-04-05 13:26:51 +02:00
parent 4810d446bb
commit 68e9fbee16

View File

@@ -10,7 +10,6 @@
from .common import Extractor, Message from .common import Extractor, Message
from .. import text, util, exception from .. import text, util, exception
from ..cache import memcache
import re import re
@@ -79,7 +78,7 @@ class TumblrExtractor(Extractor):
continue continue
if not blog: if not blog:
blog = self.api.info(self.blog) blog = self.api.info(self.blog)
yield Message.Directory, blog yield Message.Directory, blog.copy()
reblog = "reblogged_from_id" in post reblog = "reblogged_from_id" in post
if reblog and not self.reblogs: if reblog and not self.reblogs:
@@ -245,29 +244,33 @@ class TumblrAPI():
"""Minimal interface for the Tumblr API v2""" """Minimal interface for the Tumblr API v2"""
API_KEY = "O3hU2tMi5e4Qs5t3vezEi6L0qRORJ5y9oUpSGsrWu8iA3UCc3B" API_KEY = "O3hU2tMi5e4Qs5t3vezEi6L0qRORJ5y9oUpSGsrWu8iA3UCc3B"
API_SECRET = "sFdsK3PDdP2QpYMRAoq0oDnw0sFS24XigXmdfnaeNZpJpqAn03" API_SECRET = "sFdsK3PDdP2QpYMRAoq0oDnw0sFS24XigXmdfnaeNZpJpqAn03"
BLOG_CACHE = {}
def __init__(self, extractor): def __init__(self, extractor):
self.api_key = extractor.config("api-key", self.API_KEY) api_key = extractor.config("api-key", self.API_KEY)
api_secret = extractor.config("api-secret", self.API_SECRET) api_secret = extractor.config("api-secret", self.API_SECRET)
token = extractor.config("access-token") token = extractor.config("access-token")
token_secret = extractor.config("access-token-secret") token_secret = extractor.config("access-token-secret")
if token and token_secret:
if api_key and api_secret and token and token_secret:
self.session = util.OAuthSession( self.session = util.OAuthSession(
extractor.session, extractor.session,
self.api_key, api_secret, token, token_secret) api_key, api_secret,
token, token_secret,
)
self.api_key = None self.api_key = None
else: else:
self.session = extractor.session self.session = extractor.session
self.api_key = api_key
self.posts_type = None self.posts_type = None
self.extractor = extractor self.extractor = extractor
self._blogcache = {}
@memcache(keyarg=1)
def info(self, blog): def info(self, blog):
"""Return general information about a blog""" """Return general information about a blog"""
if blog not in self._blogcache: if blog not in self.BLOG_CACHE:
self._blogcache[blog] = self._call(blog, "info", {})["blog"] self.BLOG_CACHE[blog] = self._call(blog, "info", {})["blog"]
return self._blogcache[blog] return self.BLOG_CACHE[blog]
def posts(self, blog, params): def posts(self, blog, params):
"""Retrieve published posts""" """Retrieve published posts"""
@@ -276,7 +279,7 @@ class TumblrAPI():
params["type"] = self.posts_type params["type"] = self.posts_type
while True: while True:
data = self._call(blog, "posts", params) data = self._call(blog, "posts", params)
self._blogcache[blog] = data["blog"] self.BLOG_CACHE[blog] = data["blog"]
yield from data["posts"] yield from data["posts"]
params["offset"] += params["limit"] params["offset"] += params["limit"]
if params["offset"] >= data["total_posts"]: if params["offset"] >= data["total_posts"]: