scripts/create-new-torrent

56 lines
1.5 KiB
Plaintext
Raw Normal View History

2021-09-29 08:01:35 +00:00
#!/bin/env python
import os,sys
import subprocess
"""
Creates a new torrent using mktorrent with some sensible defaults:
--no-date
Finds a README file and uses that as the comment
Picks up a list of trackers from ~/.config/trackers.txt
Run as create-new-torrent --any --other-options /path/to/directory
Most common options you might want to overwrite are
--output
--name
"""
2021-09-29 08:10:22 +00:00
# Remove the script name
2021-09-29 08:01:35 +00:00
arguments = sys.argv
del arguments[0]
2022-01-26 15:38:53 +00:00
if len(sys.argv) < 1:
print("Invalid arguments")
print(" create-new-torrent --output file.torrent /path/to/directory")
sys.exit(1)
2021-09-29 08:10:22 +00:00
# Last argument must be the directory or file
2021-09-29 08:01:35 +00:00
directory = sys.argv[-1]
comment = None
2021-09-29 08:10:22 +00:00
# Only try for a comment if we have a directory
2022-01-26 15:38:53 +00:00
if os.path.isdir(directory):
2021-09-29 08:10:22 +00:00
for file in ["README.txt", "readme.txt", "README.md", "readme.md", "README.nfo"]:
file_path = directory + "/" + file
if os.path.exists(file_path):
with open(file_path, 'r') as f:
comment = f.read()
2022-01-26 15:38:53 +00:00
if not comment:
2021-09-29 08:10:22 +00:00
print("Could not find a readme file in %s" % directory)
# Add all trackers
2021-09-29 08:01:35 +00:00
tracker_file = os.path.expanduser('~/.config/trackers.txt')
with open(tracker_file, 'r') as t:
2021-09-29 08:10:22 +00:00
trackers = t.read().split("\n")
list = ["mktorrent"] + ["--announce=%s" % tracker_url for tracker_url in trackers] + ['--no-date']
# Add a comment if we found one
2021-09-29 08:01:35 +00:00
if comment:
2021-09-29 08:10:22 +00:00
list+=["--comment=\"%s\"" % comment]
# Append any extra arguments we got, including the directory
2021-09-29 08:01:35 +00:00
list += arguments
2021-09-29 08:10:22 +00:00
# Run and passthrough stdout (default)
2021-09-29 08:01:35 +00:00
subprocess.run(args=list)