Skip to content

Commit

Permalink
[tiktok] fix extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
soimort committed Oct 25, 2020
1 parent c271363 commit 5d59f76
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/you_get/extractors/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,42 @@
from ..common import *

def tiktok_download(url, output_dir='.', merge=True, info_only=False, **kwargs):
html = get_html(url, faker=True)
referUrl = url.split('?')[0]
headers = fake_headers

# trick or treat
html = get_content(url, headers=headers)
data = r1(r'<script id="__NEXT_DATA__".*?>(.*?)</script>', html)
info = json.loads(data)
videoData = info['props']['pageProps']['videoData']
urls = videoData['itemInfos']['video']['urls']
videoId = videoData['itemInfos']['id']
uniqueId = videoData['authorInfos'].get('uniqueId')
nickName = videoData['authorInfos'].get('nickName')

for i, videoUrl in enumerate(urls):
title = '%s [%s]' % (nickName or uniqueId, videoId)
if len(urls) > 1:
title = '%s [%s]' % (title, i)

mime, ext, size = url_info(videoUrl, headers={'Referer': url})

print_info(site_info, title, mime, size)
if not info_only:
download_urls([videoUrl], title, ext, size, output_dir=output_dir, merge=merge, headers={'Referer': url})
wid = info['props']['initialProps']['$wid']
cookie = 'tt_webid=%s; tt_webid_v2=%s' % (wid, wid)

# here's the cookie
headers['Cookie'] = cookie

# try again
html = get_content(url, headers=headers)
data = r1(r'<script id="__NEXT_DATA__".*?>(.*?)</script>', html)
info = json.loads(data)
wid = info['props']['initialProps']['$wid']
cookie = 'tt_webid=%s; tt_webid_v2=%s' % (wid, wid)

videoData = info['props']['pageProps']['itemInfo']['itemStruct']
videoId = videoData['id']
videoUrl = videoData['video']['downloadAddr']
uniqueId = videoData['author'].get('uniqueId')
nickName = videoData['author'].get('nickname')

title = '%s [%s]' % (nickName or uniqueId, videoId)

# we also need the referer
headers['Referer'] = referUrl

mime, ext, size = url_info(videoUrl, headers=headers)

print_info(site_info, title, mime, size)
if not info_only:
download_urls([videoUrl], title, ext, size, output_dir=output_dir, merge=merge, headers=headers)

site_info = "TikTok.com"
download = tiktok_download
Expand Down

0 comments on commit 5d59f76

Please sign in to comment.