-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLib.py
63 lines (38 loc) · 1.69 KB
/
Lib.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
import threading
from pathlib import Path
from requests import get
from threading import Thread
from typing import List, Dict, Literal
def getThumbnail( url: str, root: Path, name: str = 'thumbnail.jpg') -> Path:
path = Path(root).joinpath(name)
response = get(url)
if response.status_code == 200:
with open(path, 'wb') as file:
file.write(response.content)
return path
def existsThread(name: str) -> bool:
# Itera sobre todos los hilos activos
for th in threading.enumerate():
if th.name == name: return True
return False
def convertToMinutes(duration: int = 0) -> str:
hour = duration // 3600
minutes = (duration % 3600) // 60
seconds_res = duration % 60
time = {'s': seconds_res, 'm': minutes, 'h': hour}
return '{h:02d}:{m:02d}:{s:02d}'.format(**time) if hour > 0 else '{m:02d}:{s:02d}'.format(**time)
def getItags(aData, vData, aud: str = 'Audio', vid: str = 'Video') -> Dict[Literal['aud', 'vid'], int]:
itags = {}
def setAud() -> str:
for itag, aBr in aData.items():
if aud == aBr: return itag
itags['aud'] = int(list(aData.keys())[-1] if 'Audio' == aud else setAud())
itags['vid'] = int(list(vData.values())[-1] if 'Video' == vid else vData[vid])
return itags
def getVideoStream(audio: Dict, video: Dict, itags: Dict, format: str, fileType: List[str]) -> Dict[Literal['audio', 'video', 'format'], str]:
videoStream = {}
videoStream['format'] = '.mp3' if format == '.format' else format
videoStream['audio'] = audio.get_by_itag(itags['aud'])
if format in fileType:
videoStream['video'] = video.get_by_itag(itags['vid'])
return videoStream