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.

63 lines
1.7 KiB

3 years ago
import calendar
import datetime
import random
import re
import string
import time
import unicodedata
from urllib.error import URLError
from youtube_dl import DownloadError
3 years ago
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:
result = func()
except DownloadError as e:
if not e.exc_info[0] is URLError:
n_retry += 1
3 years ago
except Exception as e:
3 years ago
n_retry += 1
if n_retry == max_retry:
ret_code = -1
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)
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
if simple:
value = re.sub(r'[/.:]', '_', value)
3 years ago
return value
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()]