From 68e9fbee16eeba4c75fba4b8210e270a3f025849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 5 Apr 2018 13:26:51 +0200 Subject: [PATCH] [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) --- gallery_dl/extractor/tumblr.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/gallery_dl/extractor/tumblr.py b/gallery_dl/extractor/tumblr.py index c0fdd038..5ade7b53 100644 --- a/gallery_dl/extractor/tumblr.py +++ b/gallery_dl/extractor/tumblr.py @@ -10,7 +10,6 @@ from .common import Extractor, Message from .. import text, util, exception -from ..cache import memcache import re @@ -79,7 +78,7 @@ class TumblrExtractor(Extractor): continue if not blog: blog = self.api.info(self.blog) - yield Message.Directory, blog + yield Message.Directory, blog.copy() reblog = "reblogged_from_id" in post if reblog and not self.reblogs: @@ -245,29 +244,33 @@ class TumblrAPI(): """Minimal interface for the Tumblr API v2""" API_KEY = "O3hU2tMi5e4Qs5t3vezEi6L0qRORJ5y9oUpSGsrWu8iA3UCc3B" API_SECRET = "sFdsK3PDdP2QpYMRAoq0oDnw0sFS24XigXmdfnaeNZpJpqAn03" + BLOG_CACHE = {} 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) token = extractor.config("access-token") 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( extractor.session, - self.api_key, api_secret, token, token_secret) + api_key, api_secret, + token, token_secret, + ) self.api_key = None else: self.session = extractor.session + self.api_key = api_key + self.posts_type = None self.extractor = extractor - self._blogcache = {} - @memcache(keyarg=1) def info(self, blog): """Return general information about a blog""" - if blog not in self._blogcache: - self._blogcache[blog] = self._call(blog, "info", {})["blog"] - return self._blogcache[blog] + if blog not in self.BLOG_CACHE: + self.BLOG_CACHE[blog] = self._call(blog, "info", {})["blog"] + return self.BLOG_CACHE[blog] def posts(self, blog, params): """Retrieve published posts""" @@ -276,7 +279,7 @@ class TumblrAPI(): params["type"] = self.posts_type while True: data = self._call(blog, "posts", params) - self._blogcache[blog] = data["blog"] + self.BLOG_CACHE[blog] = data["blog"] yield from data["posts"] params["offset"] += params["limit"] if params["offset"] >= data["total_posts"]: