-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
67 lines (54 loc) · 1.97 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
import os
from pathlib import PurePosixPath
from sanitize_filename import sanitize
from urllib.parse import unquote, urlparse
def url_to_foldername(url):
"""
Chomikuj url
Input:
url_to_foldername("https://chomikuj.pl/biblioholik/test/za*c5*bc*c3*b3*c5*82*c4*87+g*c4*99*c5*9bl*c4*85+ja*c5*ba*c5*84+ZA*c5*bb*c3*93*c5*81*c4*86+G*c4*98*c5*9aL*c4*84+JA*c5*b9*c5*83")
Output:
encoded: "za*c5*bc*c3*b3*c5*82*c4*87+g*c4*99*c5*9bl*c4*85+ja*c5*ba*c5*84+ZA*c5*bb*c3*93*c5*81*c4*86+G*c4*98*c5*9aL*c4*84+JA*c5*b9*c5*83"
decoded -> "zażółć gęślą jaźń ZAŻÓŁĆ GĘŚLĄ JAŹŃ"
TODO: annotate parts and return path relative to username
"""
parts = PurePosixPath(
unquote(
urlparse(
url
).path
)
).parts
folder = parts[-1]
return folder
def decode_foldername(folder_name):
"""
Decode foldername to utf-8
"""
cnum = 0
out = ''
while cnum < len(folder_name):
if folder_name[cnum]=='+': out += '%02x' % ord(' ')
elif folder_name[cnum]==':': out += '%02x' % ord('-')
elif folder_name[cnum]=='?': out += '%02x' % ord('_')
elif folder_name[cnum]=='*':
out += folder_name[cnum+1:cnum+3]
cnum+=2
else: out += '%02x' % ord(folder_name[cnum])
cnum+=1
return bytes.fromhex(out).decode('utf8')
def get_dest_path(url):
enc_foldername = url_to_foldername(url)
folder_name = decode_foldername(enc_foldername)
sanitized_folder = sanitize(folder_name)
dest_path = os.path.join(os.getcwd(), 'downloads', sanitized_folder)
return dest_path
def prepare_dest_folder(dest_path):
"""
Prepare destination folder for download
"""
if not os.path.exists(dest_path):
os.makedirs(dest_path)
print(f"Created folder: {dest_path}")
print(f"Destination folder {dest_path} already exists.")
return