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

feat(bilibili): add support for first option to skip videos #3000

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/you_get/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']:
Expand Down
28 changes: 24 additions & 4 deletions src/you_get/extractors/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down