Skip to content

Commit

Permalink
Support user profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
CasualYT31 committed Dec 23, 2024
1 parent 73932ac commit afdac67
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 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</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><a href="https://github.com/mikf/gallery-dl#cookies">Cookies</a></td>
</tr>
<tr>
Expand Down
33 changes: 31 additions & 2 deletions gallery_dl/extractor/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""Extractors for https://www.tiktok.com/"""

from .common import Extractor, Message
from .. import exception, text, util
from .. import exception, text, util, ytdl
from re import compile, escape, IGNORECASE

BASE_PATTERN = r"(?:https?://)?(?:www\.)?tiktok\.com"
Expand Down Expand Up @@ -138,4 +138,33 @@ class TiktokVmpostExtractor(TiktokExtractor):
example = "https://vm.tiktok.com/ZGdh4WUhr/"


# TODO: Write profile extractor.
class TiktokUserExtractor(TiktokExtractor):
"""Extract a TikTok user's profile"""

subcategory = "user"
pattern = USER_PATTERN
example = "https://www.tiktok.com/@chillezy"

def urls(self):
"""Attempt to use yt-dlp/youtube-dl to extract links from a
user's page"""

try:
module = ytdl.import_module(self.config("module"))
except (ImportError, SyntaxError) as exc:
self.log.error("Cannot import module '%s'",
getattr(exc, "name", ""))
self.log.debug("", exc_info=exc)
raise exception.ExtractionError("yt-dlp or youtube-dl is required "
"for this feature!")
with ytdl.construct_YoutubeDL(
module=module,
obj=self,
user_opts={
"cookiefile": self.cookies_file,
"playlist_items": str(self.config("tiktok-range", ""))
}
) as ydl:
info = ydl.extract_info(self.url, download=False)
# This should include video and photo posts in /video/ URL form.
return [video["webpage_url"] for video in info["entries"]]
16 changes: 16 additions & 0 deletions test/results/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,20 @@
"#pattern" : PATTERN_WITH_AUDIO,
"#options" : {"videos": True}
},
{
"#url" : "https://www.tiktok.com/@chillezy",
"#comment" : "User profile",
"#category" : ("", "tiktok", "user"),
"#class" : tiktok.TiktokUserExtractor,
"#pattern" : PATTERN_WITH_AUDIO,
"#options" : {"videos": True, "tiktok-range": "1-10"}
},
{
"#url" : "https://www.tiktok.com/@chillezy",
"#comment" : "User profile without audio or videos",
"#category" : ("", "tiktok", "user"),
"#class" : tiktok.TiktokUserExtractor,
"#pattern" : PATTERN,
"#options" : {"videos": False, "tiktok-range": "1-10"}
},
)

0 comments on commit afdac67

Please sign in to comment.