Git Product home page Git Product logo

torrentfile's Introduction

TorrentFile

torrentfile


GitHub repo size GitHub License PyPI - Downloads GitHub Last Commit CI Codacy Badge Codacy Badge DeepSource codecov

๐ŸŒ Overview

A command line interface for creating, reviewing, editing, or verifying bittorrent meta files (.torrent files). torrentfile is open source, and supports all versions of Bittorrent files, including hybrid meta files. The code base is also importable and can easily be used as a library for creating or manipulating torrent files in external projects. Documentation is available at https://alexpdev.github.io/torrentfile.

A GUI frontend for this project can be found at https://github.com/alexpdev/TorrentfileQt

๐Ÿ”Œ Requirements

  • Python 3.6+
  • Tested on Linux, Windows and Mac

๐Ÿ’ป Install

PyPi:

pip install torrentfile

Git:

git clone https://github.com/alexpdev/torrentfile.git
cd torrentfile
pip install .

Download pre-compiled binaries from the release page.

๐Ÿ“š Documentation

torrentfile documentation available at https://alexpdev.github.io/torrentfile

๐Ÿš€ Usage

Basic Usage

Usage examples can be found in the project documentation on the examples page.

๐Ÿ“ License

Apache Software License v2.0 - See LICENSE

๐Ÿ’ก Issues & Requests & PRs

If you encounter any bugs or would like to request a new feature please open a new issue. PRs and other contributions that are meaningful and add value to the project are welcome.


Usage Examples

Creating Bittorrent Files

Creating a basic torrent file is as easy as using the create subcommand with the path to the torrent file.

torrentfile create /path/to/content

You can add one or more trackers by using any one of -a, --announce flags and listing their URL as a space separated list.

torrentfile create /path/to/content -a http://tracker1.com http://tracker2.net

If you intend to distribute the file on a private tracker then you should use one
of -p, --private flags, which tells your Bittorrent clients to disable DHT and
multitracker protocols.

torrentfile create /path/to/content --private

By default torrentfile displays a progress bar indicating how much of the content
has already been processed. To turn off this display you can either use --quiet mode in
as a global flag or you can set the --prog flag to 0.

torrentfile create /path/to/content --prog 0

torrentfile extracts the name of the contents top level file or directory
and saves the torrent file to the current working directory with the extracted title.

For example running the follwing command would create ./content.torrent.

torrentfile create /path/to/content

To specify an alternative path or filename you may use the -o, --out flags
followed by the path to the preferred destination.

torrentfile create /path/to/content -o /some/other/path/torrent.torrent

If the path specified is an existing directory, then the torrent file will be saved to that directory, with same filename as the default top level path name.

For example the following command would create a torrent file at /some/other/path/content.torrent.

torrentfile create /path/to/content -o /some/other/path/

torrentfile creates Bittorrent v1 files by default. To create a V2 or hybrid (v1 and v2) torrent file, use the --meta-version option followed by the preferred version number option. The options include: 1(v1 default), 2(v2), or 3(v1 & v2).

torrentfile create /path/to/content --meta-version 2
torrentfile create /path/to/content --meta-version 3 

torrentfile includes the option to command line flags for the create sub-command from an ini style configuration file, by using the --config and optional --config-path options to specify the path to the configuration file. If --config-path is ommited, then torrentfile will look by default in the current working directory for a file named torrentfile.ini. If the file is not discovered in the current working directory, it will move on to look ~/.torrentfile/torrentfile.ini followed by ~/.config/torrentfile.ini. Please see the documentation for more details on how the configuration file should be formatted.

Check/Recheck Torrent

The recheck subcommand allows you to scan a Bittorrent file and compare it's contents, against a file or directory containing the contents the torrent file was created from. The output provided by this process gives a detailed perspective if any files are missing or have been corrupted in any way. Supports any version of Bittorrent file.

torrentfile recheck /path/to/some.torrent /path/to/content

Edit Torrent

To edit specific fields of the torrent file, there is the edit subcommand. Using this subcommand you can specify the field with one of the available field flags, for example --announce and specify the value you wish to change it to.

torrentfile edit /path/to/content --announce https://new.tracker.url1.com  https://newtracker.url/2

You can use the -h flag for a full list of available fields that can be edited.

torrentfile edit -h

Create Magnet

To create a magnet URI for a pre-existing torrent meta file, use the sub-command
magnet with the path to the torrent file.

torrentfile magnet /path/to/some.torrent

GUI

If you prefer a windowed GUI please check out the official GUI frontend here

torrentfile's People

Contributors

alexpdev avatar deepsource-autofix[bot] avatar deepsourcebot avatar nitronarcosis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

torrentfile's Issues

Make the piece length option more straightforward

. Reduce the optional formats for entering a specific piece length down to 1

For simplicity sake, removing the option to specify the the piece length as a quantity of bytes and as a power of 2 is overkill since both formats represent the same numbers. Removing the ability to specify as a quantity of bytes will make it much simpler, so the only acceptable inputs will be any number between 14-29.

Can't create a torrent from a directory

There are two issues:

  1. The file name contains Chinese. Torrent created cannot be resolved.
  2. If create a torrent from a directory, the torrent file has no file name, like .torrent, and the torrent file located in the target directory instead of current work directory.

Readme gif needs to be updated to latest version

The gif displayed in the readme is from version 0.8.5. It should be updated to the current version and perhaps this time the demonstration shouldn't show the version number since not much has changed since 0.8.5.

torrentfile/README.md

Lines 54 to 56 in b92e6e0

![Basic Usage](https://github.com/alexpdev/torrentfile/blob/master/assets/Torrentfile.gif?raw=True)

Support for Piece-Aligned (optimise alignment, BEP-47) ?

Torrents containing multi files often get stuck at 99.x% if not piece-aligned when creating.
Could u plz support this feature (Based on BT protocol BEP-47) ? thanks.

Padding files and extended file attributes.
https://www.bittorrent.org/beps/bep_0047.html
Padding files are synthetic files inserted into the file list to let the following file start at a piece boundary. That means their length should fill up the remainder of the piece length of the file that is supposed to be padded.

Related Information:
https://forum.qbittorrent.org/viewtopic.php?p=41005#p41005

v0.8.10 generates invalid magnet URLs for v2/hybrid torrents

Additionally, torrentfile magnet fails on torrent files with no trackers.

Installed from latest git master.

$ torrentfile -m --meta-version 2 ytarchive -a udp://explodie.org:6969
ytarchive                                           |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 7/7 MiB
magnet:?xt=urn:btih:DABCEC469398E3D1A20CFFB15CC993B5FF38C3F762714E6278A24B1926A58CA4&dn=ytarchive&tr=udp%3A%2F%2Fexplodie.org%3A6969

Torrent Save Path:  /home/malloc/ytarchive.torrent

v2 hash listed as urn:btih component instead of urn:btmh, additionally v2 magnet hashes have the prefix 1220, see https://multiformats.io/multihash/. (it isn't clear to me if this was ever written down as a specification or if people just decided to do it, but qbt rejects unprefixed v2 hashes)

$ torrentfile -m --meta-version 3 ytarchive -a udp://explodie.org:6969
ytarchive                                           |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 7/7 MiB
magnet:?xt=urn:btih:9827DDA0E0DF01194729C6ADFA4085469E67E4858F359BACF6872984CB01EA45&dn=ytarchive&tr=udp%3A%2F%2Fexplodie.org%3A6969

Torrent Save Path:  /home/malloc/ytarchive.torrent

Same issue as above, but additionally hybrid torrents should have both btih and btmh

libtorrent.org provides two test torrents on the bottom of this page with corresponding magnet URLs: https://blog.libtorrent.org/2020/09/bittorrent-v2/

torrentfile can't parse either of them:

malloc@host:~$ wget https://libtorrent.org/bittorrent-v2-hybrid-test.torrent
malloc@host:~$ torrentfile magnet bittorrent-v2-hybrid-test.torrent
Traceback (most recent call last):
  File "/home/malloc/.local/bin/torrentfile", line 8, in <module>
    sys.exit(main())
  File "/home/malloc/.local/lib/python3.9/site-packages/torrentfile/cli.py", line 655, in main
    execute()
  File "/home/malloc/.local/lib/python3.9/site-packages/torrentfile/cli.py", line 644, in execute
    return args.func(args)
  File "/home/malloc/.local/lib/python3.9/site-packages/torrentfile/commands.py", line 387, in magnet
    announce_args = ["&tr=" + quote_plus(meta["announce"])]
KeyError: 'announce'

qBittorrent 4.5.2 can load both of them and they function as expected. They don't have a tracker specified, which I think is causing the error. Using transmission-create to make a v1 torrent with no trackers specified creates files torrentfile can't parse, with the above error.

Progress Bar glitches

The progress bar shown glitches at the end when using the create command with meta-version 2 or 3 selected. Needs fixing.

Assignees: alexpdev
Labels: bug

Improve error messages

Most of the common errors result in tracebacks which not helpful in diagnosing where the user went wrong.

It would be helpful if the common errors were identified and if unable to mitigate then more issue specific error messages should be posted.

test if output directory is writeable

In it's current and previous versions, the process used to create .torrent files gathers the relative information, calculates the hashes, and writes the information to the disk. This has the unfortunate consequence of only raising permission errors after the most time consuming portion of the process has already taken place.

A test should be implemented prior to calculating the hashes to ensure the output path is writeable so that it can warn the user who can then request the proper permissions, or choose an alternative output directory.

Add configuration file feature

Suggested enhancement to the CLI.

  • Add an option to the CLI --config that looks for a configuration file with prefilled keys and values for other options available on the command line.

For example, the command torrentfile create --config /path/to/content would create a new torrentfile from the content using the information listed in a configuration file in the users current working directory called torrentfile.ini.

An example of the ini file:

[config]
announce = http://tracker1.net/21323announce
                    http://tracker2.net/23423announce
                    http://tracker3.org/349089dannounce
private = false
comment = comment to embed in torrent file metadata
source = Tracker3
piece-length = 21
http-seed = https://webpage.com/path/to/content
meta-version = 2
out = /path/to/newly_created.torrent

The cli can then automatically parse the contents of the config file, use preset defaults for any options that are not listed in the config file or on the command line, and consider options specified on the command line to have a higher priority than those indicated in the config file.

Add a rebuild subcommand

Enhancement Suggestion

As an enhancement, add a rebuild subcommand that can take a directory containing a dis-assembled torrent download and re-assemble it into the correct file and directory structure required by a torrent file for reseeding.

Proof of concept and use case

Many download torrents and disassemble their contents to fit their specific needs, for example if you have a media server that needs all your video files to be side by side in the same directory so the metadata collection can be collected consistently. Then your torrent tracker sends a reseed request for a torrent but you have already dissasembled it. This command could take the directories containing the content and rebuild the torrent to it's original structure and capable of reseeding.

info command does not print anything

I couldn't get the info command to print anything to the console. I took a look into the source code and history and I guess it was broken by c7af6cf:

output = "\n".join(text)
print(output)
return output

output = "\n".join(text)
logger.info(output)
return output

I don't know if the correct action is to re-add the print or to make sure the logger is set up correctly.

Bug in version 2 meta files when contents size is perfect power of 2

Hashing Bug

I discovered a bug that occurs only when the total size of a specific file is a perfect power of 2 and
perfectly alligns with the merkle tree piece length.

What it does:
It currently adds extra padding to the end of the layer hashes that is unnecessary and breaks the hashes.

What it is supposed to do:

It is supposed to take sha256 hashes of individual blocks of torrent content and since it is a perfect
power of 2, no extra padding should be added to the layers. However one extra blocks worth of
padding is added.

Problem with installation using Python 3.11

After using torrentfile (torrentfile -V or any other command) for the first time this error comes up:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Python311\Scripts\torrentfile.exe\__main__.py", line 7, in <module>
  File "C:\Python311\Lib\site-packages\torrentfile\cli.py", line 608, in main
    execute()
  File "C:\Python311\Lib\site-packages\torrentfile\cli.py", line 544, in execute
    rebuild_parser = subparsers.add_parser(
                     ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\argparse.py", line 1188, in add_parser
    raise ArgumentError(
argparse.ArgumentError: argument create, edit, info, magnet, recheck, rebuild
: conflicting subparser alias: build

Maybe it's an easy fix but I have no idea. Is it something with Python 3.11?

Recheck not working on files with perfect power of 2 lengths

I discovered while testing that when using the recheck subcommand on a torrent created with files with sizes that are perfect powers of 2, Using version 2, possibly others as well. The progress bar spins into a max-recursion-depth error.

libtorrent v2 creation broken

Repro:

$ head -c 1048575 </dev/urandom >1mb-1.bin
$ torrentfile -m --meta-version 3 -a udp://tracker.opentrackr.org:1337/announce -o 1mb-1.bin.v3.torrent 1mb-1.bin

Loading this torrent file into qBittorrent (v4.5.3 libtorrent 2.0.9) and hashing against 1mb-1.bin results in 98.4% progress, missing the last piece. (Pieces: 64 x 16.0 KiB (have 63))

$ torrentfile -m --meta-version 2 -a udp://tracker.opentrackr.org:1337/announce -o 1mb-1.bin.v2.torrent 1mb-1.bin

Loading this torrent file into qBittorrent and hashing against 1mb-1.bin results in 0% progress.

$ torrentfile -m --meta-version 1 -a udp://tracker.opentrackr.org:1337/announce -o 1mb-1.bin.v1.torrent 1mb-1.bin

Loading this torrent file into qBittorrent and hashing against 1mb-1.bin results in 100% progress.

Using torrentfile v0.9.0. It occurred on an earlier version but I didn't come up with a repro. I initially thought this was some kind of last-block issue, but as I wrote this report it seems more like a v2 issue.

Could you please help me understand how root hash is calculated?

I'm kinda lost in understanding of torrent.py's merkle root hash creation, I'm trying to write a function in php to calculate root hashes of files, I can create v1 implementations but still struggling with v2, could you please provide me a simple one block python code function to generate root hash from given file so I could inspect it and learn how to write something equal in php, I know it's too much to ask, but I'm just lost in hash functions, thanks.

Progress Bar Displays inaccurate numbers during and after updated.

The progress bar that appears during the recheck process consistently reads impossible results.

For example:
Somefile.bin [|||||||||||||||||||||||||||||||||||||||||||] 128/64 MiB

It should show for 100% matched contents:
Somefile.bin [||||||||||||||||||||||||||||||||||||||||||] 64/64 MiB

I only observed this with the recheck command.

more readable total progress bar

currently progress bar show progress of each file, it's impossible to know the total progress of torrent creation.

I hope there can be a option to show total progress by bytes without each file info

for example: 50%|[xxx...]|30mb/60mb 1mb/s

no seprated progress bar for each file.

Updates needed to documentation.

  • Improve and update information in the github pages documentation.
  • Update the mkdocs yaml file.
  • Improve readability throughout the api and source code sections

Add support for quiet mode

Add support for a command line flag indicating activation of quiet mode.

In quiet mode, any and all terminal output is redirected to a in-memory IOBuffer and nothing is output to the terminal.

Quiet Mode will be activated with the -q or --quiet flags in the global option section.

Magnet flag should include v2 hash in output if torrent is v2/hybrid

Creating v2/hybrid torrents currently only lists a v1 hash in the magnet output.

malloc@host:~$ torrentfile -m --meta-version 2 ytarchive -a udp://explodie.org:6969
ytarchive                                         |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 7/7 MiB
magnet:?xt=urn:btih:F2CDDC9FE7BF1B3BD95AAAF790E64A34A3571B46&dn=ytarchive&tr=udp%3A%2F%2Fexplodie.org%3A6969

Torrent Save Path:  /home/malloc/ytarchive.torrent
malloc@host:~$ torrentfile -m --meta-version 3 ytarchive -a udp://explodie.org:6969
ytarchive                                         |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 7/7 MiB
magnet:?xt=urn:btih:E1E6AA16639DFB81EB5D1A68D10E552CB124B63D&dn=ytarchive&tr=udp%3A%2F%2Fexplodie.org%3A6969

Torrent Save Path:  /home/malloc/ytarchive.torrent
malloc@host:~$ torrentfile magnet ytarchive.torrent

magnet:?xt=urn:btih:E1E6AA16639DFB81EB5D1A68D10E552CB124B63D&dn=ytarchive&tr=udp%3A%2F%2Fexplodie.org%3A6969

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.