Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tumblr] add count metadata field #2804

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions gallery_dl/extractor/tumblr.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ def items(self):
del post["trail"]
post["blog"] = blog
post["date"] = text.parse_timestamp(post["timestamp"])
yield Message.Directory, post
post["num"] = 0
posts = []

if "photos" in post: # type "photo" or "link"
photos = post["photos"]
Expand All @@ -125,32 +124,43 @@ def items(self):

del photo["original_size"]
del photo["alt_sizes"]
yield self._prepare_image(photo["url"], post)
posts.append(
self._prepare_image(photo["url"], post.copy()))
del post["photo"]

url = post.get("audio_url") # type "audio"
if url and url.startswith("https://a.tumblr.com/"):
yield self._prepare(url, post)
posts.append(self._prepare(url, post.copy()))

url = post.get("video_url") # type "video"
if url:
yield self._prepare(_original_video(url), post)
posts.append(self._prepare(_original_video(url), post.copy()))

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
body = post["reblog"]["comment"] + post["reblog"]["tree_html"]
for url in re.findall('<img src="([^"]+)"', body):
url = _original_inline_image(url)
yield self._prepare_image(url, post)
posts.append(self._prepare_image(url, post.copy()))
for url in re.findall('<source src="([^"]+)"', body):
url = _original_video(url)
yield self._prepare(url, post)
posts.append(self._prepare(url, post.copy()))

if self.external: # external links
post["extension"] = None
url = post.get("permalink_url") or post.get("url")
if url:
yield Message.Queue, url, post
post["extension"] = None
posts.append((Message.Queue, url, post.copy()))
del post["extension"]

post["count"] = len(posts)
yield Message.Directory, post

for num, (msg, url, post) in enumerate(posts, 1):
post["num"] = num
post["count"] = len(posts)
yield msg, url, post

def posts(self):
"""Return an iterable containing all relevant posts"""
Expand Down Expand Up @@ -179,14 +189,12 @@ def _setup_posttypes(self):
@staticmethod
def _prepare(url, post):
text.nameext_from_url(url, post)
post["num"] += 1
post["hash"] = post["filename"].partition("_")[2]
return Message.Url, url, post

@staticmethod
def _prepare_image(url, post):
text.nameext_from_url(url, post)
post["num"] += 1

parts = post["filename"].split("_")
try:
Expand All @@ -200,7 +208,7 @@ def _prepare_image(url, post):
@staticmethod
def _prepare_avatar(url, post, blog):
text.nameext_from_url(url, post)
post["num"] = 1
post["num"] = post["count"] = 1
post["blog"] = blog
post["reblogged"] = False
post["type"] = post["id"] = post["hash"] = "avatar"
Expand Down