2016-02-20 11:29:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-01-01 16:07:23 +01:00
|
|
|
# Copyright 2016-2020 Mike Fährmann
|
2016-02-20 11:29:10 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
"""Extract images from https://www.tumblr.com/"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2018-12-22 14:04:13 +01:00
|
|
|
from .. import text, oauth, extractor, exception
|
2018-04-25 16:13:03 +02:00
|
|
|
from datetime import datetime, timedelta
|
2017-11-18 22:49:55 +01:00
|
|
|
import re
|
2016-02-20 11:29:10 +01:00
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
2018-08-25 18:43:51 +02:00
|
|
|
def _original_inline_image(url):
|
|
|
|
return re.sub(
|
|
|
|
(r"https?://(\d+\.media\.tumblr\.com(?:/[0-9a-f]+)?"
|
|
|
|
r"/tumblr(?:_inline)?_[^_]+)_\d+\.([0-9a-z]+)"),
|
|
|
|
r"https://\1_1280.\2", url
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-11-22 23:09:08 +01:00
|
|
|
def _original_video(url):
|
|
|
|
return re.sub(
|
2018-12-09 09:07:46 +01:00
|
|
|
(r"https?://((?:vt|vtt|ve)(?:\.media)?\.tumblr\.com"
|
2018-09-09 14:06:39 +02:00
|
|
|
r"/tumblr_[^_]+)_\d+\.([0-9a-z]+)"),
|
|
|
|
r"https://\1.\2", url
|
2017-11-22 23:09:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
POST_TYPES = frozenset((
|
|
|
|
"text", "quote", "link", "answer", "video", "audio", "photo", "chat"))
|
|
|
|
|
2018-01-15 18:27:58 +01:00
|
|
|
BASE_PATTERN = (
|
|
|
|
r"(?:tumblr:(?:https?://)?([^/]+)|"
|
|
|
|
r"(?:https?://)?([^.]+\.tumblr\.com))")
|
|
|
|
|
2017-11-22 23:09:08 +01:00
|
|
|
|
2017-11-03 22:16:57 +01:00
|
|
|
class TumblrExtractor(Extractor):
|
|
|
|
"""Base class for tumblr extractors"""
|
2016-02-20 11:29:10 +01:00
|
|
|
category = "tumblr"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "{name}")
|
2018-04-15 18:58:32 +02:00
|
|
|
filename_fmt = "{category}_{blog_name}_{id}_{num:>02}.{extension}"
|
|
|
|
archive_fmt = "{id}_{num}"
|
2020-01-01 16:07:23 +01:00
|
|
|
cookiedomain = None
|
2016-02-20 11:29:10 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
Extractor.__init__(self, match)
|
2018-01-15 18:27:58 +01:00
|
|
|
self.blog = match.group(1) or match.group(2)
|
2017-11-18 22:49:55 +01:00
|
|
|
self.api = TumblrAPI(self)
|
2016-02-20 11:29:10 +01:00
|
|
|
|
2017-11-24 16:51:07 +01:00
|
|
|
self.types = self._setup_posttypes()
|
2018-12-26 14:29:30 +01:00
|
|
|
self.avatar = self.config("avatar", False)
|
2018-12-26 14:35:59 +01:00
|
|
|
self.inline = self.config("inline", True)
|
2018-01-06 15:52:08 +01:00
|
|
|
self.reblogs = self.config("reblogs", True)
|
2017-11-22 23:09:08 +01:00
|
|
|
self.external = self.config("external", False)
|
|
|
|
|
2017-11-24 16:51:07 +01:00
|
|
|
if len(self.types) == 1:
|
2018-01-12 14:56:01 +01:00
|
|
|
self.api.posts_type = next(iter(self.types))
|
2017-11-24 16:51:07 +01:00
|
|
|
elif not self.types:
|
|
|
|
self.log.warning("no valid post types selected")
|
2017-11-22 23:09:08 +01:00
|
|
|
|
2018-09-10 15:40:25 +02:00
|
|
|
if self.reblogs == "same-blog":
|
|
|
|
self._skip_reblog = self._skip_reblog_same_blog
|
|
|
|
|
2019-07-16 23:08:27 +02:00
|
|
|
self.date_min, self.api.before = self._get_date_min_max(0, None)
|
|
|
|
|
2016-02-20 11:29:10 +01:00
|
|
|
def items(self):
|
2018-04-04 14:15:24 +02:00
|
|
|
blog = None
|
2016-02-20 11:29:10 +01:00
|
|
|
yield Message.Version, 1
|
2016-02-20 15:24:30 +01:00
|
|
|
|
2017-11-03 22:16:57 +01:00
|
|
|
for post in self.posts():
|
2019-07-16 23:08:27 +02:00
|
|
|
if self.date_min > post["timestamp"]:
|
|
|
|
return
|
2017-11-22 23:09:08 +01:00
|
|
|
if post["type"] not in self.types:
|
|
|
|
continue
|
2018-04-04 14:15:24 +02:00
|
|
|
if not blog:
|
|
|
|
blog = self.api.info(self.blog)
|
2018-09-10 15:40:25 +02:00
|
|
|
blog["uuid"] = self.blog
|
2018-04-05 13:26:51 +02:00
|
|
|
yield Message.Directory, blog.copy()
|
2017-11-22 23:09:08 +01:00
|
|
|
|
2018-12-26 14:29:30 +01:00
|
|
|
if self.avatar:
|
|
|
|
url = self.api.avatar(self.blog)
|
|
|
|
yield self._prepare_avatar(url, post.copy(), blog)
|
|
|
|
|
2018-01-05 13:00:25 +01:00
|
|
|
reblog = "reblogged_from_id" in post
|
2018-09-07 18:32:45 +02:00
|
|
|
if reblog and self._skip_reblog(post):
|
2018-01-05 13:00:25 +01:00
|
|
|
continue
|
|
|
|
post["reblogged"] = reblog
|
|
|
|
|
2017-11-18 22:49:55 +01:00
|
|
|
post["blog"] = blog
|
2019-04-29 17:30:42 +02:00
|
|
|
post["date"] = text.parse_timestamp(post["timestamp"])
|
2018-04-15 18:58:32 +02:00
|
|
|
post["num"] = 0
|
2017-11-18 22:49:55 +01:00
|
|
|
|
|
|
|
if "trail" in post:
|
|
|
|
del post["trail"]
|
|
|
|
|
2017-11-22 23:09:08 +01:00
|
|
|
if "photos" in post: # type "photo" or "link"
|
2017-11-18 22:49:55 +01:00
|
|
|
photos = post["photos"]
|
|
|
|
del post["photos"]
|
|
|
|
|
2017-11-20 20:51:02 +01:00
|
|
|
for photo in photos:
|
2017-11-22 23:09:08 +01:00
|
|
|
post["photo"] = photo
|
2017-11-18 22:49:55 +01:00
|
|
|
photo.update(photo["original_size"])
|
|
|
|
del photo["original_size"]
|
|
|
|
del photo["alt_sizes"]
|
2018-01-19 23:12:15 +01:00
|
|
|
yield self._prepare_image(photo["url"], post)
|
2017-11-18 22:49:55 +01:00
|
|
|
|
2019-02-19 13:49:55 +01:00
|
|
|
url = post.get("audio_url") # type: "audio"
|
2019-09-07 15:04:53 +02:00
|
|
|
if url and url.startswith("https://a.tumblr.com/"):
|
2019-02-19 13:49:55 +01:00
|
|
|
yield self._prepare(url, post)
|
2017-11-18 22:49:55 +01:00
|
|
|
|
2019-02-19 13:49:55 +01:00
|
|
|
url = post.get("video_url") # type: "video"
|
|
|
|
if url:
|
|
|
|
yield self._prepare(_original_video(url), post)
|
2017-11-18 22:49:55 +01:00
|
|
|
|
2018-12-11 20:02:48 +01:00
|
|
|
if self.inline and "reblog" in post: # inline media
|
|
|
|
# only "chat" posts are missing a "reblog" key in their
|
|
|
|
# API response, but they can't contain images/videos anyway
|
2018-12-11 21:56:12 +01:00
|
|
|
body = post["reblog"]["comment"] + post["reblog"]["tree_html"]
|
2018-12-11 20:02:48 +01:00
|
|
|
for url in re.findall('<img src="([^"]+)"', body):
|
|
|
|
url = _original_inline_image(url)
|
|
|
|
yield self._prepare_image(url, post)
|
|
|
|
for url in re.findall('<source src="([^"]+)"', body):
|
|
|
|
url = _original_video(url)
|
|
|
|
yield self._prepare(url, post)
|
2017-11-18 22:49:55 +01:00
|
|
|
|
2017-11-22 23:09:08 +01:00
|
|
|
if self.external: # external links
|
|
|
|
post["extension"] = None
|
2018-12-22 14:04:13 +01:00
|
|
|
with extractor.blacklist(("tumblr",)):
|
|
|
|
for key in ("permalink_url", "url"):
|
2019-02-19 13:49:55 +01:00
|
|
|
url = post.get(key)
|
|
|
|
if url:
|
|
|
|
yield Message.Queue, url, post
|
|
|
|
break
|
2017-11-03 22:16:57 +01:00
|
|
|
|
|
|
|
def posts(self):
|
|
|
|
"""Return an iterable containing all relevant posts"""
|
|
|
|
|
2017-11-24 16:51:07 +01:00
|
|
|
def _setup_posttypes(self):
|
2018-12-26 14:35:59 +01:00
|
|
|
types = self.config("posts", "all")
|
2017-11-24 16:51:07 +01:00
|
|
|
|
|
|
|
if types == "all":
|
|
|
|
return POST_TYPES
|
|
|
|
|
|
|
|
elif not types:
|
|
|
|
return frozenset()
|
|
|
|
|
|
|
|
else:
|
|
|
|
if isinstance(types, str):
|
|
|
|
types = types.split(",")
|
|
|
|
types = frozenset(types)
|
|
|
|
|
|
|
|
invalid = types - POST_TYPES
|
|
|
|
if invalid:
|
|
|
|
types = types & POST_TYPES
|
2019-12-10 21:30:08 +01:00
|
|
|
self.log.warning("Invalid post types: '%s'",
|
|
|
|
"', '".join(sorted(invalid)))
|
2017-11-24 16:51:07 +01:00
|
|
|
return types
|
|
|
|
|
2017-11-18 22:49:55 +01:00
|
|
|
@staticmethod
|
2017-11-23 16:12:07 +01:00
|
|
|
def _prepare(url, post):
|
2018-04-07 15:28:55 +02:00
|
|
|
text.nameext_from_url(url, post)
|
2018-04-15 18:58:32 +02:00
|
|
|
post["num"] += 1
|
2019-02-14 16:07:17 +01:00
|
|
|
post["hash"] = post["filename"].partition("_")[2]
|
2018-04-07 15:28:55 +02:00
|
|
|
return Message.Url, url, post
|
2017-11-20 20:51:02 +01:00
|
|
|
|
2018-01-19 23:12:15 +01:00
|
|
|
@staticmethod
|
|
|
|
def _prepare_image(url, post):
|
2018-04-07 15:28:55 +02:00
|
|
|
text.nameext_from_url(url, post)
|
2018-04-15 18:58:32 +02:00
|
|
|
post["num"] += 1
|
2018-04-07 15:28:55 +02:00
|
|
|
|
2019-02-14 16:07:17 +01:00
|
|
|
parts = post["filename"].split("_")
|
2018-12-02 19:48:09 +01:00
|
|
|
try:
|
|
|
|
post["hash"] = parts[1] if parts[1] != "inline" else parts[2]
|
|
|
|
except IndexError:
|
2018-12-04 19:15:23 +01:00
|
|
|
# filename doesn't follow the usual pattern (#129)
|
2019-02-14 16:07:17 +01:00
|
|
|
post["hash"] = post["filename"]
|
2018-04-07 15:28:55 +02:00
|
|
|
|
2018-08-14 11:20:25 +02:00
|
|
|
return Message.Url, url, post
|
2018-01-19 23:12:15 +01:00
|
|
|
|
2018-12-26 14:29:30 +01:00
|
|
|
@staticmethod
|
|
|
|
def _prepare_avatar(url, post, blog):
|
|
|
|
text.nameext_from_url(url, post)
|
|
|
|
post["num"] = 1
|
|
|
|
post["blog"] = blog
|
|
|
|
post["reblogged"] = False
|
|
|
|
post["type"] = post["id"] = post["hash"] = "avatar"
|
|
|
|
return Message.Url, url, post
|
|
|
|
|
2018-09-10 15:40:25 +02:00
|
|
|
def _skip_reblog(self, _):
|
|
|
|
return not self.reblogs
|
|
|
|
|
|
|
|
def _skip_reblog_same_blog(self, post):
|
|
|
|
return self.blog != post["reblogged_root_uuid"]
|
2018-09-07 18:32:45 +02:00
|
|
|
|
2017-11-03 22:16:57 +01:00
|
|
|
|
|
|
|
class TumblrUserExtractor(TumblrExtractor):
|
|
|
|
"""Extractor for all images from a tumblr-user"""
|
|
|
|
subcategory = "user"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = BASE_PATTERN + r"(?:/page/\d+|/archive)?/?$"
|
|
|
|
test = (
|
2017-11-22 23:09:08 +01:00
|
|
|
("http://demo.tumblr.com/", {
|
2018-12-27 22:35:02 +01:00
|
|
|
"pattern": r"https://\d+\.media\.tumblr\.com"
|
|
|
|
r"/tumblr_[^/_]+_\d+\.jpg",
|
2017-11-22 23:09:08 +01:00
|
|
|
"count": 1,
|
2018-12-27 22:35:02 +01:00
|
|
|
"options": (("posts", "photo"),),
|
2017-11-22 23:09:08 +01:00
|
|
|
}),
|
|
|
|
("http://demo.tumblr.com/", {
|
|
|
|
"pattern": (r"https?://(?:$|"
|
2018-08-14 11:20:25 +02:00
|
|
|
r"\d+\.media\.tumblr\.com/.+_1280\.jpg|"
|
2019-07-19 17:16:24 +02:00
|
|
|
r"a\.tumblr\.com/tumblr_\w+)"),
|
2017-11-22 23:09:08 +01:00
|
|
|
"count": 3,
|
2018-12-27 22:35:02 +01:00
|
|
|
"options": (("posts", "all"), ("external", True))
|
2017-11-22 23:09:08 +01:00
|
|
|
}),
|
2019-01-02 16:04:34 +01:00
|
|
|
("https://mikf123-hidden.tumblr.com/", { # dashbord-only
|
|
|
|
"count": 2,
|
|
|
|
"keyword": {"tags": ["test", "hidden"]},
|
|
|
|
}),
|
|
|
|
("https://mikf123-private.tumblr.com/", { # password protected
|
|
|
|
"count": 2,
|
|
|
|
"keyword": {"tags": ["test", "private"]},
|
|
|
|
}),
|
|
|
|
("https://mikf123-private-hidden.tumblr.com/", { # both
|
|
|
|
"count": 2,
|
|
|
|
"keyword": {"tags": ["test", "private", "hidden"]},
|
|
|
|
}),
|
2019-07-16 23:08:27 +02:00
|
|
|
("https://mikf123.tumblr.com/", { # date-min/-max/-format (#337)
|
|
|
|
"count": 4,
|
|
|
|
"options": (("date-min", "201804"), ("date-max", "201805"),
|
|
|
|
("date-format", "%Y%m"))
|
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
("https://demo.tumblr.com/page/2"),
|
|
|
|
("https://demo.tumblr.com/archive"),
|
|
|
|
("tumblr:http://www.b-authentique.com/"),
|
|
|
|
("tumblr:www.b-authentique.com"),
|
|
|
|
)
|
2017-11-03 22:16:57 +01:00
|
|
|
|
|
|
|
def posts(self):
|
2018-01-15 18:27:58 +01:00
|
|
|
return self.api.posts(self.blog, {})
|
2017-11-03 22:16:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TumblrPostExtractor(TumblrExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for images from a single post on tumblr"""
|
2016-02-20 15:24:30 +01:00
|
|
|
subcategory = "post"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = BASE_PATTERN + r"/(?:post|image)/(\d+)"
|
|
|
|
test = (
|
2018-02-08 21:20:19 +01:00
|
|
|
("http://demo.tumblr.com/post/459265350", {
|
2018-08-14 11:20:25 +02:00
|
|
|
"pattern": (r"https://\d+\.media\.tumblr\.com"
|
2018-02-08 21:20:19 +01:00
|
|
|
r"/tumblr_[^/_]+_1280.jpg"),
|
|
|
|
"count": 1,
|
|
|
|
}),
|
2018-12-27 22:35:02 +01:00
|
|
|
("https://mikf123.tumblr.com/post/167770226574/text-post", {
|
|
|
|
"count": 2,
|
|
|
|
}),
|
|
|
|
("https://mikf123.tumblr.com/post/181022561719/quote-post", {
|
|
|
|
"count": 1,
|
|
|
|
}),
|
|
|
|
("https://mikf123.tumblr.com/post/167623351559/link-post", {
|
|
|
|
"count": 2,
|
|
|
|
}),
|
|
|
|
("https://muyanna.tumblr.com/post/180692431632/answer-post", {
|
|
|
|
"count": 1,
|
|
|
|
}),
|
|
|
|
("https://mikf123.tumblr.com/post/167633596145/video-post", {
|
|
|
|
"count": 2,
|
|
|
|
}),
|
|
|
|
("https://mikf123.tumblr.com/post/167770026604/audio-post", {
|
|
|
|
"count": 2,
|
|
|
|
}),
|
|
|
|
("https://mikf123.tumblr.com/post/172687798174/photo-post", {
|
|
|
|
"count": 4,
|
|
|
|
}),
|
|
|
|
("https://mikf123.tumblr.com/post/181022380064/chat-post", {
|
|
|
|
"count": 0,
|
|
|
|
}),
|
2019-02-19 13:49:55 +01:00
|
|
|
("http://pinetre-3.tumblr.com/post/181904381470/via", {
|
2019-06-04 13:49:57 +02:00
|
|
|
"count": 0, # audio post with "null" as URL (#165)
|
|
|
|
}),
|
|
|
|
("http://ziemniax.tumblr.com/post/109697912859/", {
|
|
|
|
"exception": exception.NotFoundError, # HTML response (#297)
|
2019-02-19 13:49:55 +01:00
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
("http://demo.tumblr.com/image/459265350"),
|
|
|
|
)
|
2016-02-20 15:24:30 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2017-11-03 22:16:57 +01:00
|
|
|
TumblrExtractor.__init__(self, match)
|
2018-01-15 18:27:58 +01:00
|
|
|
self.post_id = match.group(3)
|
2018-01-05 13:00:25 +01:00
|
|
|
self.reblogs = True
|
2019-07-16 23:08:27 +02:00
|
|
|
self.date_min = 0
|
2016-02-20 15:24:55 +01:00
|
|
|
|
2017-11-03 22:16:57 +01:00
|
|
|
def posts(self):
|
2018-01-15 18:27:58 +01:00
|
|
|
return self.api.posts(self.blog, {"id": self.post_id})
|
2016-02-20 15:24:55 +01:00
|
|
|
|
2017-11-24 16:51:07 +01:00
|
|
|
@staticmethod
|
|
|
|
def _setup_posttypes():
|
|
|
|
return POST_TYPES
|
|
|
|
|
2017-11-03 22:16:57 +01:00
|
|
|
|
|
|
|
class TumblrTagExtractor(TumblrExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for images from a tumblr-user by tag"""
|
2016-02-20 15:24:55 +01:00
|
|
|
subcategory = "tag"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = BASE_PATTERN + r"/tagged/([^/?&#]+)"
|
|
|
|
test = ("http://demo.tumblr.com/tagged/Times%20Square", {
|
2018-08-14 11:20:25 +02:00
|
|
|
"pattern": (r"https://\d+\.media\.tumblr\.com/tumblr_[^/_]+_1280.jpg"),
|
2017-10-08 23:59:27 +02:00
|
|
|
"count": 1,
|
2019-02-08 13:45:40 +01:00
|
|
|
})
|
2016-02-20 15:24:55 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2017-11-03 22:16:57 +01:00
|
|
|
TumblrExtractor.__init__(self, match)
|
2018-01-15 18:27:58 +01:00
|
|
|
self.tag = text.unquote(match.group(3))
|
2017-11-03 22:16:57 +01:00
|
|
|
|
|
|
|
def posts(self):
|
2018-01-15 18:27:58 +01:00
|
|
|
return self.api.posts(self.blog, {"tag": self.tag})
|
2017-11-03 22:16:57 +01:00
|
|
|
|
|
|
|
|
2018-01-12 14:56:01 +01:00
|
|
|
class TumblrLikesExtractor(TumblrExtractor):
|
2018-04-18 18:01:43 +02:00
|
|
|
"""Extractor for images from a tumblr-user's liked posts"""
|
2018-01-12 14:56:01 +01:00
|
|
|
subcategory = "likes"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "{name}", "likes")
|
2018-04-15 18:58:32 +02:00
|
|
|
archive_fmt = "f_{blog[name]}_{id}_{num}"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = BASE_PATTERN + r"/likes"
|
|
|
|
test = ("http://mikf123.tumblr.com/likes", {
|
2018-01-12 14:56:01 +01:00
|
|
|
"count": 1,
|
2019-02-08 13:45:40 +01:00
|
|
|
})
|
2018-01-12 14:56:01 +01:00
|
|
|
|
|
|
|
def posts(self):
|
2018-01-15 18:27:58 +01:00
|
|
|
return self.api.likes(self.blog)
|
2018-01-12 14:56:01 +01:00
|
|
|
|
|
|
|
|
2018-05-10 21:57:45 +02:00
|
|
|
class TumblrAPI(oauth.OAuth1API):
|
2017-11-03 22:16:57 +01:00
|
|
|
"""Minimal interface for the Tumblr API v2"""
|
|
|
|
API_KEY = "O3hU2tMi5e4Qs5t3vezEi6L0qRORJ5y9oUpSGsrWu8iA3UCc3B"
|
2018-01-11 14:11:37 +01:00
|
|
|
API_SECRET = "sFdsK3PDdP2QpYMRAoq0oDnw0sFS24XigXmdfnaeNZpJpqAn03"
|
2018-04-05 13:26:51 +02:00
|
|
|
BLOG_CACHE = {}
|
2017-11-03 22:16:57 +01:00
|
|
|
|
2017-11-18 22:49:55 +01:00
|
|
|
def __init__(self, extractor):
|
2018-05-10 21:57:45 +02:00
|
|
|
oauth.OAuth1API.__init__(self, extractor)
|
2019-07-16 23:08:27 +02:00
|
|
|
self.posts_type = self.before = None
|
2017-11-03 22:16:57 +01:00
|
|
|
|
|
|
|
def info(self, blog):
|
|
|
|
"""Return general information about a blog"""
|
2018-04-05 13:26:51 +02:00
|
|
|
if blog not in self.BLOG_CACHE:
|
|
|
|
self.BLOG_CACHE[blog] = self._call(blog, "info", {})["blog"]
|
|
|
|
return self.BLOG_CACHE[blog]
|
2017-11-03 22:16:57 +01:00
|
|
|
|
2018-12-26 14:29:30 +01:00
|
|
|
def avatar(self, blog, size="512"):
|
|
|
|
"""Retrieve a blog avatar"""
|
2019-03-17 10:50:44 +01:00
|
|
|
if self.api_key:
|
|
|
|
url_fmt = "https://api.tumblr.com/v2/blog/{}/avatar/{}?api_key={}"
|
|
|
|
return url_fmt.format(blog, size, self.api_key)
|
2018-12-26 14:29:30 +01:00
|
|
|
params = {"size": size}
|
|
|
|
data = self._call(blog, "avatar", params, allow_redirects=False)
|
|
|
|
return data["avatar_url"]
|
|
|
|
|
2017-11-03 22:16:57 +01:00
|
|
|
def posts(self, blog, params):
|
|
|
|
"""Retrieve published posts"""
|
2018-01-12 14:56:01 +01:00
|
|
|
params.update({"offset": 0, "limit": 50, "reblog_info": "true"})
|
|
|
|
if self.posts_type:
|
|
|
|
params["type"] = self.posts_type
|
2019-07-16 23:08:27 +02:00
|
|
|
if self.before:
|
|
|
|
params["before"] = self.before
|
2018-01-12 14:56:01 +01:00
|
|
|
while True:
|
|
|
|
data = self._call(blog, "posts", params)
|
2018-04-05 13:26:51 +02:00
|
|
|
self.BLOG_CACHE[blog] = data["blog"]
|
2018-01-12 14:56:01 +01:00
|
|
|
yield from data["posts"]
|
|
|
|
params["offset"] += params["limit"]
|
|
|
|
if params["offset"] >= data["total_posts"]:
|
|
|
|
return
|
|
|
|
|
|
|
|
def likes(self, blog):
|
|
|
|
"""Retrieve liked posts"""
|
2019-07-16 23:08:27 +02:00
|
|
|
params = {"limit": "50", "before": self.before}
|
2018-01-12 14:56:01 +01:00
|
|
|
while True:
|
|
|
|
posts = self._call(blog, "likes", params)["liked_posts"]
|
|
|
|
if not posts:
|
|
|
|
return
|
|
|
|
yield from posts
|
|
|
|
params["before"] = posts[-1]["liked_timestamp"]
|
2017-11-03 22:16:57 +01:00
|
|
|
|
2018-12-26 14:29:30 +01:00
|
|
|
def _call(self, blog, endpoint, params, **kwargs):
|
2018-01-11 14:11:37 +01:00
|
|
|
if self.api_key:
|
|
|
|
params["api_key"] = self.api_key
|
2018-01-15 18:27:58 +01:00
|
|
|
url = "https://api.tumblr.com/v2/blog/{}/{}".format(
|
2017-11-03 22:16:57 +01:00
|
|
|
blog, endpoint)
|
|
|
|
|
2018-12-26 14:29:30 +01:00
|
|
|
response = self.request(url, params=params, **kwargs)
|
2018-01-12 14:56:01 +01:00
|
|
|
|
2019-06-04 13:49:57 +02:00
|
|
|
try:
|
|
|
|
data = response.json()
|
|
|
|
except ValueError:
|
|
|
|
data = response.text
|
|
|
|
status = response.status_code
|
|
|
|
else:
|
|
|
|
status = data["meta"]["status"]
|
|
|
|
if 200 <= status < 400:
|
|
|
|
return data["response"]
|
|
|
|
|
|
|
|
if status == 403:
|
2018-01-12 14:56:01 +01:00
|
|
|
raise exception.AuthorizationError()
|
|
|
|
elif status == 404:
|
2017-11-22 23:09:08 +01:00
|
|
|
raise exception.NotFoundError("user or post")
|
2018-04-12 16:25:20 +02:00
|
|
|
elif status == 429:
|
|
|
|
|
|
|
|
# daily rate limit
|
|
|
|
if response.headers.get("x-ratelimit-perday-remaining") == "0":
|
2018-04-25 16:13:03 +02:00
|
|
|
reset = response.headers.get("x-ratelimit-perday-reset")
|
2020-01-04 23:21:45 +01:00
|
|
|
until = datetime.now() + timedelta(seconds=float(reset))
|
|
|
|
self.log.error("Daily API rate limit exceeded")
|
2019-10-28 16:06:36 +01:00
|
|
|
raise exception.StopExtraction(
|
2020-01-04 23:21:45 +01:00
|
|
|
"Aborting - Rate limit will reset at %s",
|
|
|
|
until.time().isoformat("seconds"))
|
2018-04-12 16:25:20 +02:00
|
|
|
|
|
|
|
# hourly rate limit
|
|
|
|
reset = response.headers.get("x-ratelimit-perhour-reset")
|
|
|
|
if reset:
|
2020-01-04 23:21:45 +01:00
|
|
|
self.log.info("Hourly API rate limit exceeded")
|
|
|
|
self.extractor.wait(seconds=reset, reason="rate limit reset")
|
2018-04-12 16:25:20 +02:00
|
|
|
return self._call(blog, endpoint, params)
|
|
|
|
|
2019-10-28 16:06:36 +01:00
|
|
|
raise exception.StopExtraction(data)
|