emkor / napi-py

CLI tool for downloading subtitles from napiprojekt.pl
GNU General Public License v3.0
7 stars 2 forks source link

(Possibly) redundant usage of 7z #1

Closed lipowskm closed 3 years ago

lipowskm commented 3 years ago

So one thing I noticed is that you could get rid of the necessity of having 7z as an external tool completely.

You can manage it using py7zlib package. Here's a sample code how I'm using it:

from io import BytesIO
import base64
import xml.etree.ElementTree as ET
from py7zlib import Archive7z

response = requests.post(
    url='http://www.napiprojekt.pl/api/api-napiprojekt3.php',
    data=dict(
        downloaded_subtitles_id=movie_hash,
        downloaded_subtitles_lang='PL',
        client='NapiProjekt',
        mode=17,
    )
)
parser = ET.XML(response.content)
content = parser.find('subtitles').find('content').text
decoded_content = base64.b64decode(content)
buffer = BytesIO(decoded_content)
archive = Archive7z(buffer, password='iBlm8NTigvru0Jr0')
encoded_subtitles = archive.getmember(0).read()
subtitles = encoded_subtitles.decode('Windows-1250 or some other encoding')

Don't ask me about the password since I don't know where it came from, I found this piece of code somewhere on Github and did a little refactor, since it was outdated. But it does it's job obviously and doesn't require 7z at all.

lipowskm commented 3 years ago

Ok now I've noticed that you're using that password too, my bad. Have you considered switching to the package instead of using 7z?

emkor commented 3 years ago

Honestly, I have not, but maybe it's a good idea. Two things I worry about:

Subtitles are small files anyway so decompression performance is not that important, I think.

I can work on this but not before weekend, if you @lipowskm have the time and dedication to implement it sooner, you're welcome to open first PR

lipowskm commented 3 years ago

The package itself is called pylzma and it doesn't require any dependencies, so it's a good thing. Also from the package homepage:

obraz

It states that it's platform independent. Hopefully I will have time to implement it before the weekend.

lipowskm commented 3 years ago

PR is now opened.

emkor commented 3 years ago

Thanks for implementation, updated version is now published on pypi as 1.1.2 after merging https://github.com/emkor/napi-py/pull/2