This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
103 lines (86 loc) · 2.86 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# additional functions for main program
DIGITS = {
1: '=',
1000: 'K',
1000000: 'M',
1000000000: 'B'
}
def format_number (number, max_len, ending=''):
# 1548984 >> 1 548 K
# 48975646 >> 48 975 K
# 5489754642 >> 5 489 M
c_len = len(str(number))
i = 0
for d in DIGITS:
num = number // d
if not len(str(num)) > max_len:
res = num
size = DIGITS[d]
break
return str(res) + ' ' + size + ending
def date_dict (date, year_first=True, delimiter=None):
# 25042009 >> {'year': 2009, 'month': 4, 'day': 25}
# 08012003 >> {'year': 2003, 'month': 1, 'day': 8}
# 09112001 >> {'year': 2001, 'month': 11, 'day': 9}
if delimiter is not None:
l = date.split(delimiter)
if year_first:
year = l[0]
month = l[1]
day = l[2]
else:
day = l[0]
month = l[1]
year = l[2]
else:
if year_first:
year = date[:4]
month = date[4:6]
day = date[6:]
else:
day = date[:2]
month = date[2:4]
year = date[4:]
return {'year': year, 'month': month, 'day': day}
def format_date (date_d, year_first=False, delim='.'):
# {'year': 2009, 'month': 4, 'day': 25} >> 25.04.2009
# {'year': 2003, 'month': 1, 'day': 8} >> 08.01.2003
# {'year': 2001, 'month': 11, 'day': 9} >> 09.11.2001
if year_first:
fdate = f"{date_d['year']}{delim}{date_d['month']}{delim}{date_d['day']}"
else:
fdate = f"{date_d['day']}{delim}{date_d['month']}{delim}{date_d['year']}"
return fdate
def format_time (seconds):
# 1, 5 >> 01:05
# 10, 10 >> 10:10
# 4, 20 >> 04:20
m = seconds // 60
s = seconds % 60
duration = f'{m:0>2}:{s:0>2}'
# align d_m and d_s to right with zeros to 2 symbols
return duration
def dict_without_keys (dict_, keys):
# {'a': 5, 'b': 6, 'c': 7} ('c') >> {'a': 5, 'b': 6}
return {k:dict_[k] for k in dict_ if not k in keys}
def extract_ydl_params (from_dict, to_dict):
if 'bitrate' in from_dict:
bitrate = from_dict['bitrate']
to_dict['postprocessors'][0]['preferredquality'] = str(bitrate)
if 'temp_directory' in from_dict:
temp_directory = from_dict['temp_directory']
to_dict['outtmpl'] = temp_directory + r'\%(id)s.%(ext)s'
if 'ffmpeg_location' in from_dict:
ffmpeg_location = from_dict['ffmpeg_location']
to_dict['ffmpeg_location'] = ffmpeg_location
def clear_path (path):
path = path.replace('"', '')
path = path.replace('\\', '')
path = path.replace('/', '')
path = path.replace(':', '')
path = path.replace('*', '')
path = path.replace('?', '')
path = path.replace('<', '')
path = path.replace('>', '')
path = path.replace('|', '')
return path