-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
174 lines (144 loc) · 6.73 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import datetime, json, re
from google.appengine.api import xmpp
from google.appengine.ext.webapp import xmpp_handlers, template
import webapp2
from webapp2_extras import jinja2
from pibot.rpcclient import RPCClient
from pibot.tpbclient import TPBClient
from pibot.config import AUTHORIZED_USERS
from pibot.futurama_quotes import get_random_quote
RPC_CLIENT = RPCClient()
TPB_CLIENT = TPBClient()
CONVERSATIONS = {}
def bare_jid(sender):
return sender.split('/')[0]
class XmppHandler(xmpp_handlers.CommandHandler):
def __init__(self, *args, **kwargs):
super(XmppHandler, self).__init__(*args, **kwargs)
self.conversations = CONVERSATIONS
"""Handler class for all XMPP activity."""
def message_received(self, message):
sender = bare_jid(message.sender)
if sender in AUTHORIZED_USERS:
xmpp_handlers.CommandHandler.message_received(self, message)
else:
message.reply(get_random_quote())
def unhandled_command(self, message=None):
"""Shows help text for commands which have no handler.
Args:
message: xmpp.Message: The message that was sent by the user.
"""
message.reply("Hi I'm Transmibot !")
def help_command(self, message=None):
message.reply(template.render('templates/help.html', {}))
def addtorrent_command(self, message=None):
if message.arg:
url = message.arg
res = RPC_CLIENT.request("torrent-add", {'filename' : url})
message.reply(json.dumps(res))
else:
message.reply("Please provide a URL. What do I look like ? A guy who's not lazy ?")
def dl_command(self, message=None):
sender = bare_jid(message.sender)
if sender in self.conversations:
index = int(message.arg) - 1
if index < len(self.conversations[sender]):
torrent = self.conversations[sender][index]
res = RPC_CLIENT.request("torrent-add", {'filename' : torrent["magnet"]})
if res["result"] == "success":
message.reply("Added " + torrent["name"])
else:
message.reply("Could not add " + torrent["name"] + ". Result : " + res["result"])
del self.conversations[sender]
else:
message.reply("Index ("+index+") out of bounds")
else:
message.reply("Use search before using the dl command")
def search_command(self, message=None):
if message.arg:
keywords = message.arg
torrents = TPB_CLIENT.request(keywords)
if len(torrents) > 0:
sender = bare_jid(message.sender)
self.conversations[sender] = torrents
message.reply(template.render('templates/tpb_list.html', {'tpb_items' : torrents}))
else:
message.reply("No torrent found ...")
else:
message.reply("I searched the void, nothin' ... Try with keywords maybe ?")
def best_command(self, message=None):
if message.arg:
torrents = TPB_CLIENT.request(message.arg)
if len(torrents) > 0:
torrent = torrents[0]
res = RPC_CLIENT.request("torrent-add", {'filename' : torrent["magnet"]})
if res["result"] == "success":
message.reply("Added " + torrent["name"])
else:
message.reply("Could not add " + torrent["name"] + ". Result : " + res["result"])
else:
message.reply("No torrent found ...")
else:
message.reply("I searched the void, nothin' ... Try with keywords maybe ?")
def stop_command(self, message=None):
# should allow to set a filter, and stop only active torrents that match said filter
'''Stop all torrents
'''
res = RPC_CLIENT.request("torrent-stop", {})
if res["result"] == "success":
message.reply("Stopped all torrents still active.")
else:
message.reply("Something went horribly wrong. Result : " + res["result"])
def start_command(self, message=None):
# idem here : filter
'''Start unfinished torrents
'''
res = RPC_CLIENT.request("torrent-get", {'fields' : ['id', 'percentDone', 'status']})
torrents = res["arguments"]["torrents"]
ids = []
for torrent in torrents:
if torrent["percentDone"] < 1 and torrent["status"] == 0:
ids.append(torrent["id"])
if len(ids) > 0:
res = RPC_CLIENT.request("torrent-start", {'ids' : ids})
if res["result"] == "success":
message.reply("Started all the unfinished torrents ("+str(len(ids))+").")
else:
message.reply("Something went horribly wrong. Result : " + res["result"])
else:
message.reply("All the torrents are already completed. You don't expect me to seed I hope ?")
def status_command(self, message=None):
res = RPC_CLIENT.request("torrent-get", {'fields' : ['name', 'isFinished', 'percentDone', 'status']})
torrents = res["arguments"]["torrents"]
if len(torrents) > 0:
for torrent in torrents:
torrent["percentDone"] = 100 * torrent["percentDone"]
torrent["isActive"] = not torrent["isFinished"] and torrent["status"] != 0
torrent["isNotCompleted"] = torrent["percentDone"] < 100
all = False
if message.arg == "all":
all = True
message.reply(template.render('templates/status.html', {'torrents' : torrents, 'all' : all}))
else:
message.reply("No torrent in the queue")
def text_message(self, message=""):
"""Called when a message not prefixed by a /cmd is sent to the XMPP bot.
Args:
message: xmpp.Message: The message that was sent by the user.
"""
verb = message.arg.split(" ")[0].lower()
if verb + "_command" in dir(self):
post = self.request.POST
post["body"] = "/" + post["body"]
getattr(self, verb + "_command")(xmpp.Message(post))
else:
self.unhandled_command(message)
def quote_command(self, message=None):
"""Called on /quote , will return a random futurama quote.
Args:
message: xmpp.Message: The message that was sent by the user.
"""
message.reply(get_random_quote())
APPLICATION = webapp2.WSGIApplication([
('/_ah/xmpp/message/chat/', XmppHandler),
], debug=True)