-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
91 lines (80 loc) · 2.45 KB
/
utils.py
File metadata and controls
91 lines (80 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
import os
import re
from urllib.parse import unquote
import requests
class ValueUtils:
@staticmethod
def to_bool(string, default: bool) -> bool:
"""
Convert string to bool. String can be None.
"""
if default:
if str(string).lower() in ("false", "0", "no", "n"):
result = False
else:
result = True
else:
if str(string).lower() in ("true", "1", "yes", "y"):
result = True
else:
result = False
return result
@staticmethod
def to_int(string) -> int or ValueError:
try:
return int(string)
except:
return None
def download_file(url, path="./", rename_to=None, overwrite=True):
"""
download file to local path.
:param rename_to:
:param url: str
:param path: save to...
:param overwrite: bool: when file already exist,
False: overwrite (default)
True: rename new file to "file_name_1.*", "file_name_2.*" etc.
:return: file path
"""
# connect
response = requests.get(url)
# get file name
cd = response.headers.get("Content-Disposition")
print(cd)
if cd:
# file_name = re.findall("file[-_]?name\s*=\s*(.+)\s*(?:;.*)?$", cd)[0]
fn = re.findall("filename\*\s*=\s*UTF-8''(.*)[,;\s]?.*?$", cd)
if not fn:
fn = re.findall("filename\s*=\s*(.*)[,;\s]?.*?$", cd)
file_name = unquote(fn[0])
else:
file_name = unquote(url.split("/")[-1])
# create path if not exist
if not path.endswith("/"):
path += "/"
if not os.path.exists(path):
os.makedirs(path)
# rename_to
if "." in file_name:
file_name1 = "." + file_name.split(".")[-1]
file_name0 = file_name[:-len(file_name1)]
else:
file_name0 = file_name
file_name1 = ""
if rename_to:
file_name0 = rename_to
file_name = file_name0 + file_name1
# rename if already exist
if os.path.exists(path + file_name) and not overwrite:
# file_name == file_name0 + file_name1
i = 1
while os.path.exists(path + file_name0 + " " + str(i) + file_name1):
i += 1
file_name = file_name0 + "_" + str(i) + file_name1
# write
file = path + file_name
with open(file, mode='wb+') as f:
print("saving", file)
f.write(response.content)
print("正在下载:" + url)
return file