rndusr / torf

Python module to create, parse and edit torrent files and magnet links
GNU General Public License v3.0
180 stars 17 forks source link

support `bytes` in `Torrent.read_stream` #49

Closed Ravencentric closed 1 month ago

Ravencentric commented 1 month ago

Closes https://github.com/rndusr/torf/issues/48

rndusr commented 1 month ago

Thank your for contributing!

  1. Have you considered checking for bytearray?

    if isinstance(stream, (bytes, bytearray)):

  2. Wouldn't it be better to throw an exception if len(stream) > MAX_TORRENT_FILE_SIZE?

    I can't think of a scenario where a giant byte stream starts with valid torrent metadata.

    And if someone encounters torrent metadata that is larger than MAX_TORRENT_FILE_SIZE, your current implementation would probably result in a confusing exception from bdecode().

  3. In the TypeError message, type(stream).name would provide a nicer error message, e.g. "list" instead of "<class 'list'>".

Ravencentric commented 1 month ago

Thanks for the pointers! I'll fix them up.

Ravencentric commented 1 month ago

All three of these work now:

import io
import requests
from torf import Torrent

resp = requests.get("https://releases.ubuntu.com/24.04/ubuntu-24.04.1-desktop-amd64.iso.torrent")

print(Torrent.read_stream(bytes(resp.content)))
print(Torrent.read_stream(bytearray(resp.content)))
print(Torrent.read_stream(io.BytesIO(resp.content)))