2018-11-09 21:49:20 +00:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from .common import InfoExtractor
|
|
|
|
from ..utils import (
|
|
|
|
compat_str,
|
2018-12-01 19:39:22 +00:00
|
|
|
ExtractorError,
|
2020-11-29 01:09:20 +00:00
|
|
|
float_or_none,
|
2018-11-09 21:49:20 +00:00
|
|
|
int_or_none,
|
|
|
|
str_or_none,
|
|
|
|
try_get,
|
|
|
|
url_or_none,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-12-01 19:39:22 +00:00
|
|
|
class TikTokBaseIE(InfoExtractor):
|
2020-11-29 01:09:20 +00:00
|
|
|
def _extract_video(self, data, video_id=None):
|
2022-01-07 01:41:11 +00:00
|
|
|
video = try_get(data, lambda x: x['video'], dict)
|
|
|
|
if not video:
|
|
|
|
return
|
|
|
|
width = int_or_none(video.get('width'))
|
|
|
|
height = int_or_none(video.get('height'))
|
2018-11-09 21:49:20 +00:00
|
|
|
|
2018-12-01 19:39:22 +00:00
|
|
|
format_urls = set()
|
2018-11-09 21:49:20 +00:00
|
|
|
formats = []
|
2020-11-29 01:09:20 +00:00
|
|
|
for format_id in ('download', 'play'):
|
|
|
|
format_url = url_or_none(video.get('%sAddr' % format_id))
|
|
|
|
if not format_url:
|
|
|
|
continue
|
|
|
|
if format_url in format_urls:
|
|
|
|
continue
|
|
|
|
format_urls.add(format_url)
|
|
|
|
formats.append({
|
|
|
|
'url': format_url,
|
|
|
|
'ext': 'mp4',
|
|
|
|
'height': height,
|
|
|
|
'width': width,
|
|
|
|
'http_headers': {
|
|
|
|
'Referer': 'https://www.tiktok.com/',
|
|
|
|
}
|
|
|
|
})
|
2018-11-09 21:49:20 +00:00
|
|
|
self._sort_formats(formats)
|
|
|
|
|
2020-11-29 01:09:20 +00:00
|
|
|
thumbnail = url_or_none(video.get('cover'))
|
|
|
|
duration = float_or_none(video.get('duration'))
|
|
|
|
|
2022-01-07 01:41:11 +00:00
|
|
|
author = data.get('author')
|
|
|
|
if isinstance(author, dict):
|
|
|
|
uploader_id = author.get('id')
|
|
|
|
else:
|
|
|
|
uploader_id = data.get('authorId')
|
|
|
|
author = data
|
|
|
|
uploader = str_or_none(author.get('nickname'))
|
2020-11-29 01:09:20 +00:00
|
|
|
|
|
|
|
timestamp = int_or_none(data.get('createTime'))
|
2018-11-09 21:49:20 +00:00
|
|
|
|
2022-01-07 01:41:11 +00:00
|
|
|
stats = try_get(data, lambda x: x['stats'], dict)
|
|
|
|
view_count, like_count, comment_count, repost_count = [
|
|
|
|
stats and int_or_none(stats.get('%sCount' % key))
|
|
|
|
for key in ('play', 'digg', 'comment', 'share', )]
|
2020-11-29 01:09:20 +00:00
|
|
|
|
|
|
|
aweme_id = data.get('id') or video_id
|
2018-11-09 21:49:20 +00:00
|
|
|
|
|
|
|
return {
|
2018-12-01 19:39:22 +00:00
|
|
|
'id': aweme_id,
|
2022-01-07 01:41:11 +00:00
|
|
|
'display_id': video_id,
|
2018-12-01 19:39:22 +00:00
|
|
|
'title': uploader or aweme_id,
|
2022-01-07 01:41:11 +00:00
|
|
|
'description': str_or_none(data.get('desc')),
|
2018-12-01 19:39:22 +00:00
|
|
|
'thumbnail': thumbnail,
|
2020-11-29 01:09:20 +00:00
|
|
|
'duration': duration,
|
2018-11-09 21:49:20 +00:00
|
|
|
'uploader': uploader,
|
2022-01-07 01:41:11 +00:00
|
|
|
'uploader_id': str_or_none(uploader_id),
|
2018-12-01 19:39:22 +00:00
|
|
|
'timestamp': timestamp,
|
2020-11-29 01:09:20 +00:00
|
|
|
'view_count': view_count,
|
|
|
|
'like_count': like_count,
|
2018-12-01 19:39:22 +00:00
|
|
|
'comment_count': comment_count,
|
|
|
|
'repost_count': repost_count,
|
2018-11-09 21:49:20 +00:00
|
|
|
'formats': formats,
|
|
|
|
}
|
2018-12-01 19:39:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TikTokIE(TikTokBaseIE):
|
2020-11-29 01:09:20 +00:00
|
|
|
_VALID_URL = r'https?://(?:www\.)?tiktok\.com/@[^/]+/video/(?P<id>\d+)'
|
2019-04-07 14:05:50 +00:00
|
|
|
_TESTS = [{
|
2020-11-29 01:09:20 +00:00
|
|
|
'url': 'https://www.tiktok.com/@zureeal/video/6606727368545406213',
|
|
|
|
'md5': '163ceff303bb52de60e6887fe399e6cd',
|
2018-12-01 19:39:22 +00:00
|
|
|
'info_dict': {
|
|
|
|
'id': '6606727368545406213',
|
|
|
|
'ext': 'mp4',
|
2022-01-07 01:41:11 +00:00
|
|
|
'title': 'md5:24acc456b62b938a7e2dd88e978b20d9',
|
2018-12-01 19:39:22 +00:00
|
|
|
'description': '#bowsette#mario#cosplay#uk#lgbt#gaming#asian#bowsettecosplay',
|
2020-11-29 01:09:20 +00:00
|
|
|
'thumbnail': r're:^https?://.*',
|
|
|
|
'duration': 15,
|
2022-01-07 01:41:11 +00:00
|
|
|
'uploader': 'md5:24acc456b62b938a7e2dd88e978b20d9',
|
2020-11-29 01:09:20 +00:00
|
|
|
'uploader_id': '188294915489964032',
|
2018-12-01 19:39:22 +00:00
|
|
|
'timestamp': 1538248586,
|
|
|
|
'upload_date': '20180929',
|
2020-11-29 01:09:20 +00:00
|
|
|
'view_count': int,
|
|
|
|
'like_count': int,
|
2018-12-01 19:39:22 +00:00
|
|
|
'comment_count': int,
|
|
|
|
'repost_count': int,
|
|
|
|
}
|
2019-04-07 14:05:50 +00:00
|
|
|
}]
|
2020-11-29 01:09:20 +00:00
|
|
|
def _real_initialize(self):
|
|
|
|
# Setup session (will set necessary cookies)
|
|
|
|
self._request_webpage(
|
|
|
|
'https://www.tiktok.com/', None, note='Setting up session')
|
|
|
|
|
2018-12-01 19:39:22 +00:00
|
|
|
def _real_extract(self, url):
|
|
|
|
video_id = self._match_id(url)
|
2022-01-07 01:41:11 +00:00
|
|
|
|
2020-11-29 01:09:20 +00:00
|
|
|
webpage = self._download_webpage(url, video_id)
|
2022-01-07 01:41:11 +00:00
|
|
|
|
|
|
|
page_props = self._parse_json(self._search_regex(
|
|
|
|
r'''(?s)<script\s[^>]*?\bid\s*=\s*(?P<q>"|'|\b)sigi-persisted-data(?P=q)[^>]*>[^=]*=\s*(?P<json>{.+?})\s*(?:;[^<]+)?</script''',
|
|
|
|
webpage, 'sigi data', default='{}', group='json'), video_id)
|
|
|
|
data = try_get(page_props, lambda x: x['ItemModule'][video_id]['video'], dict)
|
|
|
|
if data:
|
|
|
|
data = page_props['ItemModule'][video_id]
|
|
|
|
if data.get('privateItem'):
|
|
|
|
raise ExtractorError('This video is private', expected=True)
|
|
|
|
return self._extract_video(data, video_id)
|
|
|
|
|
|
|
|
page_props = self._parse_json(self._search_regex(
|
|
|
|
r'<script[^>]+\bid=["\']__NEXT_DATA__[^>]+>\s*({.+?})\s*</script',
|
|
|
|
webpage, 'data'), video_id)['props']['pageProps']
|
|
|
|
data = try_get(page_props, lambda x: x['itemInfo']['itemStruct'], dict)
|
2021-03-21 08:10:38 +00:00
|
|
|
if not data and page_props.get('statusCode') == 10216:
|
|
|
|
raise ExtractorError('This video is private', expected=True)
|
2020-11-29 01:09:20 +00:00
|
|
|
return self._extract_video(data, video_id)
|
2018-12-01 19:39:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TikTokUserIE(TikTokBaseIE):
|
2020-11-29 01:09:20 +00:00
|
|
|
_VALID_URL = r'https://(?:www\.)?tiktok\.com/@(?P<id>[^/?#&]+)'
|
2019-04-07 14:05:50 +00:00
|
|
|
_TESTS = [{
|
2020-11-29 01:09:20 +00:00
|
|
|
'url': 'https://www.tiktok.com/@zureeal',
|
2018-12-01 19:39:22 +00:00
|
|
|
'info_dict': {
|
|
|
|
'id': '188294915489964032',
|
|
|
|
},
|
|
|
|
'playlist_mincount': 24,
|
2019-04-07 14:05:50 +00:00
|
|
|
}]
|
2020-11-29 01:09:20 +00:00
|
|
|
_WORKING = False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def suitable(cls, url):
|
|
|
|
return False if TikTokIE.suitable(url) else super(TikTokUserIE, cls).suitable(url)
|
2018-12-01 19:39:22 +00:00
|
|
|
|
|
|
|
def _real_extract(self, url):
|
|
|
|
user_id = self._match_id(url)
|
|
|
|
data = self._download_json(
|
|
|
|
'https://m.tiktok.com/h5/share/usr/list/%s/' % user_id, user_id,
|
|
|
|
query={'_signature': '_'})
|
|
|
|
entries = []
|
|
|
|
for aweme in data['aweme_list']:
|
|
|
|
try:
|
2020-11-29 01:09:20 +00:00
|
|
|
entry = self._extract_video(aweme)
|
2018-12-01 19:39:22 +00:00
|
|
|
except ExtractorError:
|
|
|
|
continue
|
|
|
|
entry['extractor_key'] = TikTokIE.ie_key()
|
|
|
|
entries.append(entry)
|
|
|
|
return self.playlist_result(entries, user_id)
|