summaryrefslogtreecommitdiff
path: root/tidal_dl_ng/helper
diff options
context:
space:
mode:
authorRobert Honz <exislow+github@gmail.com>2024-11-12 22:15:11 +0100
committerRobert Honz <exislow+github@gmail.com>2024-11-12 22:15:11 +0100
commit78a48348a8986f0044fbce2097ad3e099258d32d (patch)
tree3d24f4fabdd585bc131ecc4cdaa80038c245768b /tidal_dl_ng/helper
parent4cef07b08f16410f56fa53a34884ce3b5dd6b64e (diff)
downloadtidal-dl-ng-78a48348a8986f0044fbce2097ad3e099258d32d.tar.gz
✨ Track segments are downloaded simultaneously, which results in a huge speed increase. Some refactoring work has been done.
Diffstat (limited to 'tidal_dl_ng/helper')
-rw-r--r--tidal_dl_ng/helper/decryption.py7
-rw-r--r--tidal_dl_ng/helper/path.py21
2 files changed, 25 insertions, 3 deletions
diff --git a/tidal_dl_ng/helper/decryption.py b/tidal_dl_ng/helper/decryption.py
index ebb946a..17ea19b 100644
--- a/tidal_dl_ng/helper/decryption.py
+++ b/tidal_dl_ng/helper/decryption.py
@@ -1,4 +1,5 @@
import base64
+import pathlib
from Crypto.Cipher import AES
from Crypto.Util import Counter
@@ -35,7 +36,7 @@ def decrypt_security_token(security_token: str) -> (str, str):
return key, nonce
-def decrypt_file(path_file_encrypted: str, path_file_destination: str, key: str, nonce: str) -> None:
+def decrypt_file(path_file_encrypted: pathlib.Path, path_file_destination: pathlib.Path, key: str, nonce: str) -> None:
"""
Decrypts an encrypted MQA file given the file, key and nonce.
TODO: Is it really only necessary for MQA of for all other formats, too?
@@ -46,9 +47,9 @@ def decrypt_file(path_file_encrypted: str, path_file_destination: str, key: str,
decryptor = AES.new(key, AES.MODE_CTR, counter=counter)
# Open and decrypt
- with open(path_file_encrypted, "rb") as f_src:
+ with path_file_encrypted.open("rb") as f_src:
audio_decrypted = decryptor.decrypt(f_src.read())
# Replace with decrypted file
- with open(path_file_destination, "wb") as f_dst:
+ with path_file_destination.open("wb") as f_dst:
f_dst.write(audio_decrypted)
diff --git a/tidal_dl_ng/helper/path.py b/tidal_dl_ng/helper/path.py
index cc8d5b6..e6f5b14 100644
--- a/tidal_dl_ng/helper/path.py
+++ b/tidal_dl_ng/helper/path.py
@@ -1,8 +1,10 @@
import math
import os
+import posixpath
import re
import sys
from pathlib import Path, PosixPath
+from urllib.parse import unquote, urlsplit
from pathvalidate import sanitize_filename, sanitize_filepath
from pathvalidate.error import ValidationError
@@ -295,3 +297,22 @@ def resource_path(relative_path):
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
+
+
+def url_to_filename(url: str) -> str:
+ """Return basename corresponding to url.
+ >>> print(url_to_filename('http://example.com/path/to/file%C3%80?opt=1'))
+ fileÀ
+ >>> print(url_to_filename('http://example.com/slash%2fname')) # '/' in name
+ Taken from https://gist.github.com/zed/c2168b9c52b032b5fb7d
+ Traceback (most recent call last):
+ ...
+ ValueError
+ """
+ urlpath: str = urlsplit(url).path
+ basename: str = posixpath.basename(unquote(urlpath))
+
+ if os.path.basename(basename) != basename or unquote(posixpath.basename(urlpath)) != basename:
+ raise ValueError # reject '%2f' or 'dir%5Cbasename.ext' on Windows
+
+ return basename