forked from nickcova/transmission-cleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtransmission.py
executable file
·128 lines (110 loc) · 4.59 KB
/
fixtransmission.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Transmission Error Fix
# (fixtransmission.py)
#
# Copyright 2017 Ruslan Rotaru <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import time
import subprocess
def listTorrents():
try:
commandResult = subprocess.check_output(["transmission-remote","--auth", "transmission:transmission", "-l"])
except CalledProcessError:
dateAndTime = time.strftime("%H:%M:%S") + " " + time.strftime("%d/%m/%Y")
print(dateAndTime + " ERROR: something went wrong checking the torrents listing.")
return -1
return commandResult
def getTorrentInfo(item):
try:
commandResult = subprocess.check_output(["transmission-remote","--auth", "transmission:transmission", "-t", item, "-i"])
except CalledProcessError:
dateAndTime = time.strftime("%H:%M:%S") + " " + time.strftime("%d/%m/%Y")
print(dateAndTime + " ERROR: something went wrong checking the torrent info.")
return -1
return commandResult
def removeTorrent(item):
try:
commandResult = subprocess.check_output(["transmission-remote","--auth", "transmission:transmission", "-t", item, "--remove"])
except CalledProcessError:
dateAndTime = time.strftime("%H:%M:%S") + " " + time.strftime("%d/%m/%Y")
print(dateAndTime + " ERROR: something went wrong removing torrent.")
return -1
return commandResult
def addTorrent(magnet):
try:
commandResult = subprocess.check_output(["transmission-remote","--auth", "transmission:transmission", "--add", magnet])
except CalledProcessError:
dateAndTime = time.strftime("%H:%M:%S") + " " + time.strftime("%d/%m/%Y")
print(dateAndTime + " ERROR: something went wrong adding torrent.")
return -1
return commandResult
def sendEmail(msg):
try:
ps = subprocess.Popen(('echo', msg), stdout=subprocess.PIPE)
output = subprocess.check_output(('mail', '-s', 'Torrents Fixed', "[email protected]"), stdin=ps.stdout)
ps.wait()
except OSError:
dateAndTime = time.strftime("%H:%M:%S") + " " + time.strftime("%d/%m/%Y")
print(dateAndTime + " ERROR: something went wrong with 'mail'. " + output)
return -1
def main():
splitResult = listTorrents().split("\n")
# Remove items which are just empty strings
while True:
try:
splitResult.remove("")
except ValueError:
# Insert error message here
break
# Remove first and last items so the list only contains torrent info.
# If the length of the list is smaller than 2, just exit.
if len(splitResult) > 2:
splitResult.pop(0)
splitResult.pop()
else:
return 0
# For the remaining items, check if any of them does not contain '100%',
# if there is an error string add them to a error torrents list.
errorTorrents = []
for item in splitResult:
if not "100%" in item:
torrentId = item.split()[0].rstrip("*")
torrent = getTorrentInfo(torrentId)
if "Error" in torrent:
torrent = torrent.split()
for item in torrent:
if "magnet" in item:
errorTorrents.append((torrentId, item))
# For each torrentId in the Torrents list, save the magnet link
# remove the torrent and readd it from magnet link
emailMessage = "The following torrents were readded: \n"
torrentsFixed = False
for item in errorTorrents:
commandResultRemove = removeTorrent(item[0])
commandAdd = addTorrent(item[1])
emailMessage += commandResultRemove.rstrip() + ", Id = " + item[0] + ", Torrent Name = " + item[1] + "\n"
if "success" in commandResultRemove:
torrentsFixed = True
if torrentsFixed == True:
sendEmail(emailMessage)
return 0
if __name__ == '__main__':
main()