-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
101 lines (71 loc) · 2.45 KB
/
utils.py
File metadata and controls
101 lines (71 loc) · 2.45 KB
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
from time import localtime
from datetime import datetime
from threading import Thread
from pathlib import Path
import os
import re
import requests
from urllib.parse import urlparse
from urllib.parse import urljoin
def formatTime(time_in_sec):
r_time_in_sec = round(time_in_sec)
hours = r_time_in_sec // 3600
minutes = r_time_in_sec // 60 % 60
seconds = r_time_in_sec % 60
return "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
def getFormattedTime(time_in_sec):
time_struct = localtime(time_in_sec)
hours = time_struct.tm_hour
minutes = time_struct.tm_min
seconds = time_struct.tm_sec
return "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
def getCurrentTime():
return datetime.now()
def getFormattedDateTimeFile(time_in_sec):
time_struct = localtime(time_in_sec)
year = time_struct.tm_year
month = time_struct.tm_mon
day = time_struct.tm_mday
hours = time_struct.tm_hour
minutes = time_struct.tm_min
seconds = time_struct.tm_sec
return "{:02d}{:02d}{:02d}_{:02d}{:02d}{:02d}".format(year, month, day, hours, minutes, seconds)
def getFormattedFileName(filename):
filename = filename.replace("/", " ")
filename = filename.replace("?", "")
filename = filename.replace(":", "-")
filename = filename.replace('"', "'")
filename = filename.replace('\r\n', " ")
filename = filename.replace('\n', " ")
filename = re.sub(' +', ' ', filename)
return filename
def log(text):
print("[{}] {}".format(getCurrentTime(), text))
def savePlainFile(path, content):
Path(os.path.dirname(path)).mkdir(parents=True, exist_ok=True)
file = open(path, "w", encoding='utf-8')
file.write(content)
file.close()
print("Saved to: ", path)
return os.path.basename(path)
def downloadFile(url, path, filename=None):
print("Downloading:", url)
response = requests.get(url)
if filename is None:
filename = os.path.basename(urlparse(url).path)
path = os.path.join(path, filename)
Path(os.path.dirname(path)).mkdir(parents=True, exist_ok=True)
file = open(path, "wb")
file.write(response.content)
file.close()
print("Downloaded To:", path)
return filename
def getFullUrl(base, url):
if url.startswith("http:") or url.startswith("https:"):
return url
else:
return urljoin(base, url)
def getFile(url):
print("Downloading:", url)
response = requests.get(url)
return response.content