Skip to content
Open
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 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CLI tool for downloading subtitles from napiprojekt.pl, fork of [gabrys/napi.py](https://github.com/gabrys/napi.py)

## prerequisites
- Python 3.7 or newer
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it needed to drop support for 3.7 and 3.8?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it needed to drop support for 3.7 and 3.8?

I'm afraid so, because for py7zr version 1.0.0, the minimum required version is Python 3.9, and the maximum is Python 3.13 (planned support for 3.14).

- Python 3.9 or newer

## installation
- `pip install napi-py` for user-wide installation
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
warn_unused_configs = True

[mypy-py7zlib]
[mypy-py7zr]
ignore_missing_imports = True
69 changes: 63 additions & 6 deletions napi/read_7z.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,71 @@
from io import BytesIO
from typing import Optional
from py7zlib import Archive7z, ArchiveError
from typing import Optional, Dict
import threading
import py7zr

NAPI_ARCHIVE_PASSWORD = "iBlm8NTigvru0Jr0"


def un7zip_api_response(content_7z: bytes) -> Optional[bytes]:
class InMemoryIO(py7zr.io.Py7zIO):
def __init__(self, fname: str):
self.fname = fname
self._buf = bytearray()
self._length = 0
self._lock = threading.Lock()

def write(self, data: bytes) -> None:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter complains about returning type None instead of int as required by interface

I think there's a bit more work to make this change working with tests passing etc, i.e.

  • run poetry update and commit updated .lock file
  • make sure tests are passing through the means of make test executed in projects directory
  • add all required changes to this PR

The implementation itself makes sense, so there's just some boilerplate needed around python tooling. I may help you with it or base off of your changes and do it on my own but again, my spare time is super limited so can't promise doing it any time soon

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned when reporting the issue, I am not a Python expert. I admit that I did not perform exhaustive testing. I built a .whl package based on my commits and installed it locally using vevn. I have been using it for a few days instead of the old version, and everything seems to be working fine.
I will run poetry update and add a new poetry.lock. I will also try to run all the tests. I will try to do it myself, and if I have any problems, I will ask you for help. I just don't know when I'll have spare time - it might take me a while.

with self._lock:
self._buf.extend(data)
self._length += len(data)

def read(self, size: Optional[int] = None) -> bytes:
return b""

def seek(self, offset: int, whence: int = 0) -> int:
return offset

def flush(self) -> None:
pass

def size(self) -> int:
return self._length

def getvalue(self) -> bytes:
with self._lock:
return bytes(self._buf)


class InMemoryFactory(py7zr.io.WriterFactory):
def __init__(self, target_filename: Optional[str] = None):
self.products: Dict[str, InMemoryIO] = {}
self.target_filename = target_filename

def create(self, filename: str) -> py7zr.io.Py7zIO:
if self.target_filename is not None and filename != self.target_filename:
product = InMemoryIO(filename)
else:
product = InMemoryIO(filename)
self.products[filename] = product
return product


def un7zip_api_response(content_7z: bytes, target_filename: Optional[str] = None) -> Optional[bytes]:
try:
buffer = BytesIO(content_7z)
archive = Archive7z(buffer, password=NAPI_ARCHIVE_PASSWORD)
return archive.getmember(0).read()
except ArchiveError:
with py7zr.SevenZipFile(buffer, mode="r", password=NAPI_ARCHIVE_PASSWORD) as archive:
factory = InMemoryFactory(target_filename=target_filename)
archive.extractall(factory=factory)

if target_filename:
product = factory.products.get(target_filename)
return product.getvalue() if product else None

if not factory.products:
return None
first_product = next(iter(factory.products.values()))
return first_product.getvalue()

except (py7zr.exceptions.Bad7zFile,
py7zr.exceptions.PasswordRequired,
py7zr.exceptions.UnsupportedCompressionMethodError):
return None
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "napi-py"
version = "1.2.4"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if dropping the support is required, I think it should be 1.3.0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if dropping the support is required, I think it should be 1.3.0

Yes, that makes sense, and you're right.

version = "1.2.5"
description = "CLI tool for downloading subtitles from napiprojekt.pl"
authors = ["emkor93 <emkor93@gmail.com>"]
license = "GPL-3.0 License"
Expand All @@ -11,11 +11,11 @@ keywords = ["napiprojekt", "subs", "subtitles", "movie", "film"]
packages = [{ include = "napi" }]

[tool.poetry.dependencies]
python = "^3.7"
pylzma = "^0.5.0"
python = "^3.9"
py7zr = "^1.0.0"
chardet = "^5.2.0"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
mypy = "^1.4.0"
pytest = "^7.4.4"
black = "^22.3.0"
Expand All @@ -29,4 +29,4 @@ build-backend = "poetry.core.masonry.api"

[tool.black]
line-length = 120
target-version = ['py37', 'py38', 'py39', 'py310']
target-version = ['py39', 'py310']