From 2fd06a4f6795346cceedb783f28225a388bb6dcd Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Wed, 15 Feb 2023 11:56:51 +0800 Subject: [PATCH] feat(bilibili): add support for first option --- src/you_get/extractor.py | 8 ++++++++ src/you_get/extractors/bilibili.py | 28 ++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/you_get/extractor.py b/src/you_get/extractor.py index bd71717e72..27bbf32f3f 100644 --- a/src/you_get/extractor.py +++ b/src/you_get/extractor.py @@ -19,6 +19,9 @@ def __init__(self, *args): self.url = args[0] class VideoExtractor(): + download_count = 0 + skip_count = 0 + def __init__(self, *args): self.url = None self.title = None @@ -177,6 +180,11 @@ def p_playlist(self, stream_id=None): print("videos:") def download(self, **kwargs): + self.__class__.download_count += 1 + if self.__class__.skip_count and self.__class__.download_count < self.__class__.skip_count: + log.i("skipped({}): {}".format(self.__class__.download_count, self.title)) + return + if 'json_output' in kwargs and kwargs['json_output']: json_output.output(self) elif 'info_only' in kwargs and kwargs['info_only']: diff --git a/src/you_get/extractors/bilibili.py b/src/you_get/extractors/bilibili.py index 6335e6dd87..6a1ad44b27 100644 --- a/src/you_get/extractors/bilibili.py +++ b/src/you_get/extractors/bilibili.py @@ -649,6 +649,16 @@ def download_playlist_by_url(self, url, **kwargs): log.e('[Error] Unsupported URL pattern.') exit(1) + args = kwargs.get('args') + first = 0 + if ('first' in args and args.first!= None): + first = int(args.first) + if first < 0: first = 0 + if sort not in ['video', 'bangumi','bangumi_md','audio_menu']: + args.first = None + if first > 0: + self.__class__.skip_count = first + # regular video if sort == 'video': initial_state_text = match1(html_content, r'__INITIAL_STATE__=(.*?);\(function\(\)') # FIXME @@ -658,12 +668,16 @@ def download_playlist_by_url(self, url, **kwargs): if pn == len(initial_state['videoData']['pages']): # non-interative video - for pi in range(1, pn + 1): + for pi in range(first+1, pn + 1): purl = 'https://www.bilibili.com/video/av%s?p=%s' % (aid, pi) self.__class__().download_by_url(purl, **kwargs) else: # interative video + if first > 0: + args.first = None + self.__class__.skip_count = first + search_node_list = [] download_cid_set = set([initial_state['videoData']['cid']]) params = { @@ -720,7 +734,9 @@ def download_playlist_by_url(self, url, **kwargs): initial_state = json.loads(initial_state_text) epn, i = len(initial_state['epList']), 0 for ep in initial_state['epList']: - i += 1; log.w('Extracting %s of %s videos ...' % (i, epn)) + i += 1 + if i <= first: continue + log.w('Extracting %s of %s videos ...' % (i, epn)) ep_id = ep['id'] epurl = 'https://www.bilibili.com/bangumi/play/ep%s/' % ep_id self.__class__().download_by_url(epurl, **kwargs) @@ -730,7 +746,9 @@ def download_playlist_by_url(self, url, **kwargs): initial_state = json.loads(initial_state_text) epn, i = len(initial_state['mediaInfo']['episodes']), 0 for ep in initial_state['mediaInfo']['episodes']: - i += 1; log.w('Extracting %s of %s videos ...' % (i, epn)) + i += 1 + if i <= first: continue + log.w('Extracting %s of %s videos ...' % (i, epn)) ep_id = ep['ep_id'] epurl = 'https://www.bilibili.com/bangumi/play/ep%s/' % ep_id self.__class__().download_by_url(epurl, **kwargs) @@ -843,7 +861,9 @@ def download_playlist_by_url(self, url, **kwargs): menusong_info = json.loads(api_content) epn, i = len(menusong_info['data']['data']), 0 for song in menusong_info['data']['data']: - i += 1; log.w('Extracting %s of %s songs ...' % (i, epn)) + i += 1 + if i <= first: continue + log.w('Extracting %s of %s songs ...' % (i, epn)) url = 'https://www.bilibili.com/audio/au%s' % song['id'] self.__class__().download_by_url(url, **kwargs)