Skip to content

Commit

Permalink
Support TikTok profile avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
CasualYT31 committed Dec 23, 2024
1 parent ed0bc8a commit f670845
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/supportedsites.md
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ Consider all listed sites to potentially be NSFW.
<tr>
<td>TikTok</td>
<td>https://www.tiktok.com/</td>
<td>Photos, Videos, Audio, User Profiles<br>**Videos, Audio, and User Profiles require yt-dlp or youtube-dl.** Pass `-o videos` to download photos only, and `-o tiktok-range="a-yt-dlp-range"` to tell yt-dlp to only extract XYZ links when scraping a user profile.</td>
<td>Photos, Videos, Audio, User Profiles, Profile Avatars<br>**Videos, Audio, and User Profiles require yt-dlp or youtube-dl.** Pass `-o videos` to download photos only, and `-o tiktok-range="a-yt-dlp-range"` to tell yt-dlp to only extract XYZ links when scraping a user profile.</td>
<td><a href="https://github.com/mikf/gallery-dl#cookies">Cookies</a></td>
</tr>
<tr>
Expand Down
26 changes: 19 additions & 7 deletions gallery_dl/extractor/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def urls(self):

def items(self):
videos = self.config("videos", True)
# We assume that all of the URLs served by urls() come from the same
# author.
downloaded_avatar = False
for tiktok_url in self.urls():
# If we can recognise that this is a /photo/ link, preemptively
# replace it with /video/ to prevent a needless second request.
Expand Down Expand Up @@ -84,8 +87,23 @@ def items(self):
title = "TikTok photo #{}".format(id)
title = title[:150]
user = post_info["author"]["uniqueId"]
# It's probably obvious but I thought it was worth noting
# because I got stuck on this for a while: make sure to emit
# a Directory message before attempting to download anything
# with yt-dlp! Otherwise you'll run into NoneType, set_filename
# errors since the download job doesn't get initialized.
yield Message.Directory, {"user": user}
if not downloaded_avatar:
avatar = post_info["author"]["avatarLarger"]
yield Message.Url, avatar, {
"title" : "@" + user,
"id" : post_info["author"]["id"],
"index" : "",
"img_id" : "",
"extension" : text.ext_from_url(avatar)
}
downloaded_avatar = True
if "imagePost" in post_info:
yield Message.Directory, {"user": user}
img_list = post_info["imagePost"]["images"]
for i, img in enumerate(img_list):
url = img["imageURL"]["urlList"][0]
Expand All @@ -100,12 +118,6 @@ def items(self):
"height" : img["imageHeight"]
}
elif videos:
# It's probably obvious but I thought it was worth noting
# because I got stuck on this for a while: make sure to emit
# a Directory message before attempting to download anything
# with yt-dlp! Otherwise you'll run into NoneType, set_filename
# errors since the download job doesn't get initialized.
yield Message.Directory, {"user": user}
if len(original_title) == 0:
title = "TikTok video #{}".format(id)
title = title[:150]
Expand Down
8 changes: 4 additions & 4 deletions test/results/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,31 @@
"#comment" : "Video post",
"#category" : ("", "tiktok", "post"),
"#class" : tiktok.TiktokPostExtractor,
"#urls" : "ytdl:https://www.tiktok.com/@memezar/video/7449708266168274208",
"#pattern" : PATTERN_WITH_AUDIO,
"#options" : {"videos": True}
},
{
"#url" : "https://www.tiktok.com/@memezar/photo/7449708266168274208",
"#comment" : "Video post as a /photo/ link",
"#category" : ("", "tiktok", "post"),
"#class" : tiktok.TiktokPostExtractor,
"#urls" : "ytdl:https://www.tiktok.com/@memezar/video/7449708266168274208",
"#pattern" : PATTERN_WITH_AUDIO,
"#options" : {"videos": True}
},
{
"#url" : "https://vm.tiktok.com/ZGdht7cjp/",
"#comment" : "Video post as a VM link",
"#category" : ("", "tiktok", "vmpost"),
"#class" : tiktok.TiktokVmpostExtractor,
"#urls" : "ytdl:https://vm.tiktok.com/ZGdht7cjp/",
"#pattern" : PATTERN_WITH_AUDIO,
"#options" : {"videos": True}
},
{
"#url" : "https://www.tiktok.com/@memezar/video/7449708266168274208",
"#comment" : "Skipping video post",
"#category" : ("", "tiktok", "post"),
"#class" : tiktok.TiktokPostExtractor,
"#urls" : [],
"#pattern" : PATTERN,
"#options" : {"videos": False}
},
{
Expand Down

0 comments on commit f670845

Please sign in to comment.