Giter Site home page Giter Site logo

Comments (3)

7sDream avatar 7sDream commented on July 2, 2024 1

parse_torrent_file will use Python's default dict, but it provides a use_ordered_dict argument, to use collections.OrderedDict. This parameter should be used in this scenario. It seems that this test is not rigorous. I will fix it when I have time.

And for now, you can just use the code in the test file(with use_ordered_dict=True) to calculate info hash. For more context, see Issue #13.

from torrent_parser.

7sDream avatar 7sDream commented on July 2, 2024

Oh, I notice the sorted you want to disscuess is not what I think of.

If you are talking about lexicographic order, the corrent implementation do not follow this. There is no mandatory requirement for the dictionary to be in order when parsing, and it will not actively perform sorting operations during encoding.

But this seems do not effect calculation of info hash, as long as the encoding step generated key order of info dict is the same as origin file bytes(by adding the use_ordered_dict=True parameter).

from torrent_parser.

milahu avatar milahu commented on July 2, 2024

There is no mandatory requirement for the dictionary to be in order when parsing

this would be nice to preserve the infohash

it provides a use_ordered_dict argument, to use collections.OrderedDict

this is needed only for python2
and then torrent_parser should use OrderedDict automatically, to preserve the infohash

if sys.version_info[0] == 2:
    from collections import OrderedDict

result = dict()
if sys.version_info[0] == 2:
    result = OrderedDict()

in python3, dict is an OrderedDict

>>> dict(b=2, a=1)
{'b': 2, 'a': 1}

>>> { "b": 2, "a": 1 }
{'b': 2, 'a': 1}

alternative solution: the parser could calculate the infohashes from raw source bytes of the info dict, and store the infohashes in attributes of the result data dict. calculating sha1 and sha256 digests should be cheap enough to make this default for parse_torrent_file. internally, only the raw hashes are stored. the _hex attributes return _raw.hex() (in python3)

torrent = torrent_parser.parse_torrent_file("input.torrent") 

if torrent.has_v1:
    info_hash_v1_raw = torrent.info_hash_v1_raw # -> bytes
    info_hash_v1_hex = torrent.info_hash_v1_hex # -> string

if torrent.has_v2:
    info_hash_v2_raw = torrent.info_hash_v2_raw # -> bytes
    info_hash_v2_hex = torrent.info_hash_v2_hex # -> string

alternatively, we could store the source locations of the info dict, and the user has to read the file again and calculate the digest manually. but IMO, the infohash is always useful when dealing with torrent files

from torrent_parser.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.