Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding back subtitle decrypt modules #18

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,223 changes: 1,208 additions & 1,015 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ requests = "^2.28.2"
pydantic = "^1.10.5"
validators = "^0.20.0"
yt-dlp = "^2023.2.17"
pycryptodome = "^3.19.0"
pysrt = "^1.1.2"
python-dotenv = "^1.0.0"
cryptography = "^41.0.7"

[tool.poetry.dev-dependencies]
datamodel-code-generator = { extras = ["http"], version = "^0.17.1" }
Expand Down
2 changes: 1 addition & 1 deletion src/kisskh_downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def download_subtitles(
for subtitle in subtitles:
logger.info(f"Downloading {subtitle.label} sub...")
extension = os.path.splitext(urlparse(subtitle.src).path)[-1]
response = requests.get(subtitle.src)
response = requests.get(subtitle.src, timeout=60)
output_path = Path(f"{filepath}.{subtitle.land}{extension}")
output_path.write_bytes(response.content)
if decrypter is not None:
Expand Down
Empty file.
31 changes: 31 additions & 0 deletions src/kisskh_downloader/helper/aes_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from base64 import b64decode, b64encode

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes


class AESCipher:
def __init__(self, key, initialization_vector):
self.key = key.encode("utf8")
self.initialization_vector = initialization_vector.encode("utf8")

def __get_cipher(self):
return Cipher(algorithms.AES(self.key), modes.CBC(self.initialization_vector), backend=default_backend())

def decrypt(self, encrypted_text):
cipher = self.__get_cipher()
decryptor = cipher.decryptor()
decrypted_text = decryptor.update(b64decode(encrypted_text)) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
decrypted_text = unpadder.update(decrypted_text) + unpadder.finalize()
decrypted_text = decrypted_text.rstrip(b"\0").decode("utf8")
return decrypted_text

def encrypt(self, text):
cipher = self.__get_cipher()
padder = padding.PKCS7(128).padder()
padded_data = padder.update(text.encode("utf8")) + padder.finalize()
encryptor = cipher.encryptor()
encrypted = encryptor.update(padded_data) + encryptor.finalize()
return b64encode(encrypted).decode("utf8")
17 changes: 17 additions & 0 deletions src/kisskh_downloader/helper/decrypt_subtitle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pysrt

from kisskh_downloader.helper.aes_cipher import AESCipher


class SubtitleDecrypter:
def __init__(self, key, initialization_vector):
self.cipher = AESCipher(key, initialization_vector)

def decrypt_subtitles(self, file_path):
subs = pysrt.open(file_path)

for sub in subs:
decrypted_text = self.cipher.decrypt(sub.text)
sub.text = decrypted_text

return subs
Empty file added tests/helper/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/helper/test_aes_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from kisskh_downloader.helper.aes_cipher import AESCipher


def test_decrypt():
text = "Hello There"
key = "7846286482638268"
initialization_vector = "4628745283719461"

aes_cipher = AESCipher(key=key, initialization_vector=initialization_vector)
encrypted_text = aes_cipher.encrypt(text)
assert aes_cipher.decrypt(encrypted_text) == text
Loading