Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tiktok] Add new extractor #18135

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions youtube_dl/extractor/extractors.py
Expand Up @@ -1120,6 +1120,7 @@
from .thisav import ThisAVIE
from .thisoldhouse import ThisOldHouseIE
from .threeqsdn import ThreeQSDNIE
from .tiktok import TikTokIE
from .tinypic import TinyPicIE
from .tmz import (
TMZIE,
Expand Down
114 changes: 114 additions & 0 deletions youtube_dl/extractor/tiktok.py
@@ -0,0 +1,114 @@
# coding: utf-8
from __future__ import unicode_literals

from .common import InfoExtractor
from ..utils import (
compat_str,
determine_ext,
int_or_none,
try_get,
url_or_none,
urlhandle_detect_ext
)


class TikTokIE(InfoExtractor):
_VALID_URL = r'https?://(?:m\.)?tiktok\.com/v/(?P<id>[0-9]+)'
_TEST = {
'url': 'https://m.tiktok.com/v/6606727368545406213.html',
'md5': 'd584b572e92fcd48888051f238022420',
'info_dict': {
'id': '6606727368545406213',
'ext': 'mp4',
'title': 'Zureeal|TikTok|Global Video Community',
'thumbnail': 'http://m-p16.akamaized.net/img/tos-maliva-p-0068/5e7a4ec40fb146888fa27aa8d78f86fd~noop.image',
Flat marked this conversation as resolved.
Show resolved Hide resolved
'description': 'Zureeal has just created an awesome short video with ♬ original sound - joogieboy1596',
'uploader': 'Zureeal',
'width': 540,
'height': 960,
}
}

def _real_extract(self, url):
video_id = self._match_id(url)
webpage = self._download_webpage(url, video_id)

data = self._parse_json(
self._search_regex(
r'var data = ({.+?});', webpage, 'json_string', webpage, 'data'
Flat marked this conversation as resolved.
Show resolved Hide resolved
), video_id)

title = self._og_search_title(webpage)
description = self._og_search_description(webpage)

width = int_or_none(try_get(data, lambda x: x['video']['width'], int))
height = int_or_none(try_get(data, lambda x: x['video']['height'], int))
Flat marked this conversation as resolved.
Show resolved Hide resolved

formats = []

for url in data['video']['play_addr']['url_list']:
Flat marked this conversation as resolved.
Show resolved Hide resolved
ext = determine_ext(url)
if ext == 'unknown_video':
urlh = self._request_webpage(
Flat marked this conversation as resolved.
Show resolved Hide resolved
url, video_id, note='Determining extension'
)
ext = urlhandle_detect_ext(urlh)
formats.append({
'url': url,
Flat marked this conversation as resolved.
Show resolved Hide resolved
'ext': ext,
'height': height,
'width': width,
'quality': -2,
'format_note': "Normal quality",
Flat marked this conversation as resolved.
Show resolved Hide resolved
})

for url in data['video']['download_addr']['url_list']:
Flat marked this conversation as resolved.
Show resolved Hide resolved
ext = determine_ext(url)
if ext == 'unknown_video':
urlh = self._request_webpage(
url, video_id, note='Determining extension'
)
ext = urlhandle_detect_ext(urlh)
formats.append({
'url': url,
'ext': ext,
'height': height,
'width': width,
'quality': 1,
'format_note': "Download quality",
})

for url in data['video']['play_addr_lowbr']['url_list']:
Flat marked this conversation as resolved.
Show resolved Hide resolved
ext = determine_ext(url)
if ext == 'unknown_video':
urlh = self._request_webpage(
url, video_id, note='Determining extension'
)
ext = urlhandle_detect_ext(urlh)
formats.append({
'url': url,
'ext': ext,
'height': height,
'width': width,
'quality': -3,
'format_note': "Low bitrate",
})
Flat marked this conversation as resolved.
Show resolved Hide resolved

self._sort_formats(formats)

Flat marked this conversation as resolved.
Show resolved Hide resolved
uploader = try_get(data, lambda x: x['author']['nickname'], compat_str)

thumbnail = url_or_none(
try_get(
data, lambda x: x['video']['cover']['url_list'][0], compat_str))

return {
'id': video_id,
'title': title,
'description': description,
'uploader': uploader,
'formats': formats,
'thumbnail': thumbnail,
'width': width,
'height': height,
}