2018-11-09 14:49:20 -07:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
2019-10-25 02:05:54 -06:00
|
|
|
from datetime import datetime
|
2018-11-09 14:49:20 -07:00
|
|
|
|
|
|
|
from .common import InfoExtractor
|
|
|
|
from ..utils import (
|
2018-12-01 12:39:22 -07:00
|
|
|
ExtractorError,
|
2018-11-09 14:49:20 -07:00
|
|
|
int_or_none,
|
|
|
|
str_or_none,
|
2019-10-25 02:05:54 -06:00
|
|
|
try_get
|
2018-11-09 14:49:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-23 06:45:01 -06:00
|
|
|
class TikTokIE(InfoExtractor):
|
2020-02-15 00:00:12 -07:00
|
|
|
_VALID_URL = r'https?://www\.tiktok\.com/@[\w\._]+/video/(?P<id>\d+)'
|
2019-10-25 02:05:54 -06:00
|
|
|
|
2019-04-07 08:05:50 -06:00
|
|
|
_TESTS = [{
|
2019-10-25 02:05:54 -06:00
|
|
|
'url': 'https://www.tiktok.com/@leenabhushan/video/6748451240264420610',
|
|
|
|
'md5': '34a7543afd5a151b0840ba6736fb633b',
|
2018-12-01 12:39:22 -07:00
|
|
|
'info_dict': {
|
2021-08-23 06:45:01 -06:00
|
|
|
'id': '6748451240264420610',
|
2020-02-14 23:39:53 -07:00
|
|
|
'ext': 'mp4',
|
2021-08-23 06:45:01 -06:00
|
|
|
'title': '#jassmanak #lehanga #leenabhushan',
|
|
|
|
'description': '#jassmanak #lehanga #leenabhushan',
|
|
|
|
'duration': 13,
|
2019-10-25 02:05:54 -06:00
|
|
|
'height': 1280,
|
2021-08-23 06:45:01 -06:00
|
|
|
'width': 720,
|
2020-02-14 23:39:53 -07:00
|
|
|
'uploader': 'leenabhushan',
|
2019-10-25 02:05:54 -06:00
|
|
|
'uploader_id': '6691488002098119685',
|
2021-08-23 06:45:01 -06:00
|
|
|
'uploader_url': 'https://www.tiktok.com/@leenabhushan',
|
|
|
|
'creator': 'facestoriesbyleenabh',
|
|
|
|
'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
|
|
|
|
'upload_date': '20191016',
|
|
|
|
'timestamp': 1571246252,
|
|
|
|
'view_count': int,
|
|
|
|
'like_count': int,
|
|
|
|
'repost_count': int,
|
|
|
|
'comment_count': int,
|
2018-12-01 12:39:22 -07:00
|
|
|
}
|
2019-04-07 08:05:50 -06:00
|
|
|
}, {
|
2019-10-25 02:05:54 -06:00
|
|
|
'url': 'https://www.tiktok.com/@patroxofficial/video/6742501081818877190?langCountry=en',
|
|
|
|
'md5': '06b9800d47d5fe51a19e322dd86e61c9',
|
|
|
|
'info_dict': {
|
2021-08-23 06:45:01 -06:00
|
|
|
'id': '6742501081818877190',
|
|
|
|
'ext': 'mp4',
|
|
|
|
'title': 'md5:5e2a23877420bb85ce6521dbee39ba94',
|
2019-10-25 02:05:54 -06:00
|
|
|
'description': 'md5:5e2a23877420bb85ce6521dbee39ba94',
|
|
|
|
'duration': 27,
|
|
|
|
'height': 960,
|
2021-08-23 06:45:01 -06:00
|
|
|
'width': 540,
|
|
|
|
'uploader': 'patrox',
|
|
|
|
'uploader_id': '18702747',
|
|
|
|
'uploader_url': 'https://www.tiktok.com/@patrox',
|
|
|
|
'creator': 'patroX',
|
2019-10-25 02:05:54 -06:00
|
|
|
'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
|
|
|
|
'upload_date': '20190930',
|
2021-08-23 06:45:01 -06:00
|
|
|
'timestamp': 1569860870,
|
|
|
|
'view_count': int,
|
|
|
|
'like_count': int,
|
|
|
|
'repost_count': int,
|
|
|
|
'comment_count': int,
|
2019-10-25 02:05:54 -06:00
|
|
|
}
|
2019-04-07 08:05:50 -06:00
|
|
|
}]
|
2018-12-01 12:39:22 -07:00
|
|
|
|
2021-08-23 06:45:01 -06:00
|
|
|
def _extract_aweme(self, props_data, webpage, url):
|
|
|
|
video_info = try_get(
|
|
|
|
props_data, lambda x: x['pageProps']['itemInfo']['itemStruct'], dict)
|
|
|
|
author_info = try_get(
|
|
|
|
props_data, lambda x: x['pageProps']['itemInfo']['itemStruct']['author'], dict) or {}
|
|
|
|
stats_info = try_get(props_data, lambda x: x['pageProps']['itemInfo']['itemStruct']['stats'], dict) or {}
|
|
|
|
|
|
|
|
user_id = str_or_none(author_info.get('uniqueId'))
|
|
|
|
download_url = try_get(video_info, (lambda x: x['video']['playAddr'],
|
|
|
|
lambda x: x['video']['downloadAddr']))
|
|
|
|
height = try_get(video_info, lambda x: x['video']['height'], int)
|
|
|
|
width = try_get(video_info, lambda x: x['video']['width'], int)
|
|
|
|
thumbnails = [{
|
|
|
|
'url': video_info.get('thumbnail') or self._og_search_thumbnail(webpage),
|
|
|
|
'width': width,
|
|
|
|
'height': height
|
|
|
|
}]
|
|
|
|
tracker = try_get(props_data, lambda x: x['initialProps']['$wid'])
|
|
|
|
|
|
|
|
return {
|
|
|
|
'id': str_or_none(video_info.get('id')),
|
|
|
|
'url': download_url,
|
|
|
|
'ext': 'mp4',
|
|
|
|
'height': height,
|
|
|
|
'width': width,
|
|
|
|
'title': video_info.get('desc') or self._og_search_title(webpage),
|
|
|
|
'duration': try_get(video_info, lambda x: x['video']['duration'], int),
|
|
|
|
'view_count': int_or_none(stats_info.get('playCount')),
|
|
|
|
'like_count': int_or_none(stats_info.get('diggCount')),
|
|
|
|
'repost_count': int_or_none(stats_info.get('shareCount')),
|
|
|
|
'comment_count': int_or_none(stats_info.get('commentCount')),
|
|
|
|
'timestamp': try_get(video_info, lambda x: int(x['createTime']), int),
|
|
|
|
'creator': str_or_none(author_info.get('nickname')),
|
|
|
|
'uploader': user_id,
|
|
|
|
'uploader_id': str_or_none(author_info.get('id')),
|
|
|
|
'uploader_url': f'https://www.tiktok.com/@{user_id}',
|
|
|
|
'thumbnails': thumbnails,
|
|
|
|
'description': str_or_none(video_info.get('desc')),
|
|
|
|
'webpage_url': self._og_search_url(webpage),
|
|
|
|
'http_headers': {
|
|
|
|
'Referer': url,
|
|
|
|
'Cookie': 'tt_webid=%s; tt_webid_v2=%s' % (tracker, tracker),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 12:39:22 -07:00
|
|
|
def _real_extract(self, url):
|
|
|
|
video_id = self._match_id(url)
|
|
|
|
|
2020-10-26 06:38:25 -06:00
|
|
|
# If we only call once, we get a 403 when downlaoding the video.
|
2020-10-26 20:20:18 -06:00
|
|
|
self._download_webpage(url, video_id)
|
2020-02-14 23:39:53 -07:00
|
|
|
webpage = self._download_webpage(url, video_id, note='Downloading video webpage')
|
2019-10-25 02:05:54 -06:00
|
|
|
json_string = self._search_regex(
|
2020-02-15 00:00:12 -07:00
|
|
|
r'id=\"__NEXT_DATA__\"\s+type=\"application\/json\"\s*[^>]+>\s*(?P<json_string_ld>[^<]+)',
|
|
|
|
webpage, 'json_string', group='json_string_ld')
|
2019-10-25 02:05:54 -06:00
|
|
|
json_data = self._parse_json(json_string, video_id)
|
2020-10-25 19:15:46 -06:00
|
|
|
props_data = try_get(json_data, lambda x: x['props'], expected_type=dict)
|
2018-12-01 12:39:22 -07:00
|
|
|
|
2019-10-25 02:05:54 -06:00
|
|
|
# Chech statusCode for success
|
2021-03-24 16:32:37 -06:00
|
|
|
status = props_data.get('pageProps').get('statusCode')
|
|
|
|
if status == 0:
|
2020-10-25 19:15:46 -06:00
|
|
|
return self._extract_aweme(props_data, webpage, url)
|
2021-03-24 16:32:37 -06:00
|
|
|
elif status == 10216:
|
|
|
|
raise ExtractorError('This video is private', expected=True)
|
2019-10-25 02:05:54 -06:00
|
|
|
|
2020-02-14 23:39:53 -07:00
|
|
|
raise ExtractorError('Video not available', video_id=video_id)
|