This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
btrename.py
executable file
·82 lines (63 loc) · 2.03 KB
/
btrename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
#
# Replace the suggested filename for the target of a .torrent file
#
# 2013 Chris Johnson
#
# Original written by Henry 'Pi' James
# see LICENSE.txt for license information
import sys
import os
import getopt
from BitTornado.Meta.Info import MetaInfo
VERSION = '20130326'
def rename(fname, newname, verbose=False):
metainfo = MetaInfo.read(fname)
if verbose:
print("{}: {} -> {}".format(fname, metainfo['info']['name'], newname))
metainfo['info']['name'] = newname
metainfo.write(fname)
def main(argv):
prog, _ = os.path.splitext(os.path.basename(argv[0]))
helpmsg = """Usage: {0} [-v] TORRENT NAME
{0} [-m] TORRENT [...]
Change the suggested filename in a .torrent file
--help display this help and exit
--match set suggested filename to match .torrent file name
--verbose print old and new file name
--version print program version
""".format(prog)
try:
opts, args = getopt.getopt(
argv[1:], "hmvV", ["help", "match", "verbose", "version"])
except getopt.error as msg:
print(msg)
return 0
verbose = False
match = False
for opt, _ in opts:
if opt in ('-h', '--help'):
print(helpmsg)
return 0
elif opt in ('-m', '--match'):
match = True
elif opt in ('-v', '--verbose'):
verbose = True
elif opt in ('-V', '--version'):
print(' '.join((prog, VERSION)))
return 0
if match and not args or not match and len(args) != 2:
print(helpmsg)
return 2 # common exit code for syntax error
if match:
for fname in args:
newname, torrent = os.path.splitext(fname)
if torrent == '.torrent':
rename(fname, newname, verbose)
else:
print("{} does not appear to be a .torrent file".format(fname))
return 0
fname, newname = args
rename(fname, newname, verbose)
return 0
sys.exit(main(sys.argv))