diff --git a/src/you_get/extractors/douyin.py b/src/you_get/extractors/douyin.py index 8067b1b5..6a59b160 100644 --- a/src/you_get/extractors/douyin.py +++ b/src/you_get/extractors/douyin.py @@ -1,8 +1,6 @@ # coding=utf-8 -import re import json -from urllib.parse import unquote from ..common import ( url_size, @@ -11,25 +9,52 @@ from ..common import ( fake_headers, download_urls, playlist_not_supported, + match1, + get_location, ) - __all__ = ['douyin_download_by_url'] +def get_value(source: dict, path): + try: + value = source + for key in path: + if type(key) is str: + if key in value.keys(): + value = value[key] + else: + value = None + break + elif type(key) is int: + if len(value) != 0: + value = value[key] + else: + value = None + break + except: + value = None + return value + + def douyin_download_by_url(url, **kwargs): + # if short link, get the real url + if 'v.douyin.com' in url: + url = get_location(url) + aweme_id = match1(url, r'/(\d+)/?') + # get video info + video_info_api = 'https://www.douyin.com/web/api/v2/aweme/iteminfo/?item_ids={}' + url = video_info_api.format(aweme_id) page_content = get_content(url, headers=fake_headers) - # The video player and video source are rendered client-side, the data - # contains in a