-
Notifications
You must be signed in to change notification settings - Fork 4
/
delugePush.py
131 lines (102 loc) · 4.04 KB
/
delugePush.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
128
129
130
131
#!/usr/bin/env python
import os
import sys
import urllib2
import json
from threading import Timer
try:
from delugePushConfig import *
except ImportError:
print("You do not seem to have a config file. Please download it! Read more: https://github.com/ckcr4lyf/deluge-dc-notif")
sys.exit(1)
def sendMessage(normalText, title, body, footer=""):
msg = ""
if (SEND_TAG == True):
msg += "<@" + DISCORD_ID + ">, "
msg += normalText
values = {
"content": msg,
"embeds": [
{
"title": title,
"description": body,
"footer": {
"text": footer
}
}
]
}
if BOT_USERNAME != "":
values['username'] = BOT_USERNAME
if BOT_AVATAR != "":
values['avatar_url'] = BOT_AVATAR
data = json.dumps(values)
req = urllib2.Request(WEBHOOK_URL, data)
req.add_header('Content-Type', 'application/json')
req.add_header('User-Agent', 'Chrome')
response = urllib2.urlopen(req)
def getSize(lines):
for line in lines:
ratioIndex = line.find('Ratio:')
if (ratioIndex != -1):
sizeString = line[:ratioIndex - 1]
return sizeString.split('/')[1]
return 'Unknown Size'
def getState(torrentHash, torrentName, delayTime=0, delayCall=False):
delugeCommand = 'deluge-console \"connect 127.0.0.1:' + DELUGE_PORT + ' ' + DELUGE_USERNAME + ' ' + DELUGE_PASSWORD + ' ; info '
if (DELUGE_VERSION[0] == '2'):
delugeCommand += '-v '
delugeCommand += torrentHash
delugeCommand += '\"'
delugeConsoleResult = os.popen(delugeCommand).read()
lines = delugeConsoleResult.split('\n')
if (DELUGE_VERSION[0] == '2'):
if "Failed to connect to" in lines[0]:
print(lines[0]) # Prints the error from deluge back to the user
return
tracker = lines[7][9:]
if (lines[2].find('State: Downloading') == 0):
sendMessage("Added torrent!", torrentName, tracker)
else:
ratioIndex = lines[4].find('Share Ratio')
ratioValue = lines[4][ratioIndex+6:]
if (delayCall):
message = 'After {} seconds, ratio update:'.format(str(delayTime))
sendMessage(message, torrentName, tracker, ratioValue)
else:
message = 'Completed torrent!'
sendMessage(message, torrentName, tracker, ratioValue)
if (delayTime > 0):
delay = Timer(delayTime, getState, (torrentHash, torrentName, delayTime, True))
delay.start()
else:
torrentSize = getSize(lines)
trackerIndex1 = delugeConsoleResult.find('\nTracker status:')
trackerIndex2 = delugeConsoleResult.find(':', trackerIndex1 + 17)
tracker = delugeConsoleResult[trackerIndex1+17:trackerIndex2]
torrentNameMessage = torrentName + " ({})".format(torrentSize)
if (lines[3].find('State: Downloading') == 0):
sendMessage('Added torrent!', torrentNameMessage, tracker)
else:
ratioIndex = lines[5].find('Ratio: ')
ratioValue = lines[5][ratioIndex:]
if (delayCall):
message = 'After {} seconds, ratio update:'.format(str(delayTime))
sendMessage(message, torrentNameMessage, tracker, ratioValue)
else:
message = 'Completed torrent!'
sendMessage(message, torrentNameMessage, tracker, ratioValue)
if (delayTime > 0):
delay = Timer(delayTime, getState, (torrentHash, torrentName, delayTime, True))
delay.start()
try:
torrentHash = str(sys.argv[1])
except IndexError:
print("Missing torrent hash! If you are trying to test, run:\npython delugePush.py [hash]\nWhere [hash] is a hash of a torrent you have in Deluge.")
sys.exit(1)
torrentName = "Test"
try:
torrentName = str(sys.argv[2])
except:
pass
getState(torrentHash, torrentName, RATIO_CHECK_DELAY, False)