Wrapper over youtube-dl for searching and batch downloading bilibili videos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

import calendar
import datetime
import random
import re
import string
import time
import unicodedata
from urllib.error import URLError
from youtube_dl import DownloadError
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.SystemRandom().choice(chars) for _ in range(size))
def retry_wrapper(func, max_retry, timeout=5):
result = None
ret_code = 0
n_retry = 0
while n_retry < max_retry:
try:
result = func()
break
except DownloadError as e:
print(e)
time.sleep(5)
if not e.exc_info[0] is URLError:
n_retry += 1
except Exception as e:
print(e)
time.sleep(1)
n_retry += 1
if n_retry == max_retry:
ret_code = -1
time.sleep(timeout)
return ret_code, result
def slugify(value, allow_unicode=True, simple=True):
"""
Modified from Django
Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
Remove characters that aren't alphanumerics, underscores, or hyphens.
Convert to lowercase. Also strip leading and trailing whitespace.
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
if simple:
value = re.sub(r'[\\.|]', '_', value)
value = value.replace('/', '')
value = value.replace('?', '')
value = value.replace('<', '')
value = value.replace('>', '')
value = value.replace(':', '')
value = value.replace('"', '')
value = value.replace('*', '')
return value
else:
value = re.sub(r'[^\w\s-]', '_', value).strip().lower()
return re.sub(r'[-\s]+', '-', value)
def get_day_of_week(year, month, day):
date = datetime.date(year, month, day)
return calendar.day_name[date.weekday()]