-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon_bot.py
215 lines (176 loc) · 7.06 KB
/
daemon_bot.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'''
author: None.Pavel
email: [email protected]
Description: this script uses for add torrent from telegram private chat. User under working daemon_bot
need right for write in transmission directory
exapmple: $ python3 daemon_bot.py
'''
import requests
import time
import subprocess
import os
import lockfile
import daemon
import logging
import signal
import traceback
import configparser
logging.getLogger("urllib3").setLevel(logging.WARNING)
class MyDaemonBot:
settings = configparser.ConfigParser()
settings.read('settings.ini', encoding='utf-8')
INTERVAL = settings.getint('DEFAULT', 'INTERVAL')
ADMIN_ID = settings.getint('DEFAULT', 'ADMIN_ID')
URL = settings.get('DEFAULT', 'URL')
TOKEN = settings.get('DEFAULT', 'TOKEN')
TRANSMISSION_LOGIN = settings.get('DEFAULT', 'TRANSMISSION_LOGIN')
TRANSMISSION_PASS = settings.get('DEFAULT', 'TRANSMISSION_PASS')
offset = 0 # ID latest received message
MAGNET = 'magnet'
def __init__(self):
self.from_id = 'None'
self.text = 'None'
self.success = 'success' # res = 0
self.duplicate = 'duplicate' # res = 1
self.invalid = 'invalid' # res = 2
self.path = os.path.dirname(os.path.abspath(__file__))
self.log = logging.getLogger('RutackerBot')
self.log.setLevel(logging.DEBUG)
self.fh = logging.FileHandler('ham.log')
self.fh.setLevel(logging.DEBUG)
self.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
self.fh.setFormatter(self.formatter)
self.log.addHandler(self.fh)
self.log.info('START BOT')
def run(self):
while True:
try:
self.check_updates()
time.sleep(5)
except ConnectionError as e:
self.log.error('Connerction Error')
time.sleep(5)
except (OSError, IOError) as e:
bot.log.error(traceback.format_exc())
time.sleep(5)
except Exception as e:
self.log.error('Exception: '.format(e.__class__.__name__))
time.sleep(5)
def reload_bot(self):
self.log.info('Reload bot')
def exit_bot(self):
self.log.info('Exit bot')
def check_updates(self):
data = {'offset': MyDaemonBot.offset + 1, 'limit': 5, 'timeout': 0} # request
request = requests.post(MyDaemonBot.URL + MyDaemonBot.TOKEN + '/getUpdates', data=data) # chek update
if not request.status_code == 200:
data = None
return False
# if response 'OK' that all ok :)
if not request.json()['ok']:
data = None
return False
data = None
for update in request.json()['result']: # Check element list
MyDaemonBot.offset = update['update_id'] # Id messages
# If in update not messages or in messages not test then pass
if not 'message' in update or not 'text' in update['message']:
self.log.error('Unknown update: {0}'.format(update))
continue
from_id = update['message']['chat']['id'] # Check id chat who send messages
name = update['message']['chat']['username'] # Check username who send messages
if from_id != MyDaemonBot.ADMIN_ID: # if not admin
self.send_text(from_id, "You're not authorized to use me!")
self.log.error('not authorized user: '.format(update))
continue
message = update['message']['text'] # Check message
parameters = (MyDaemonBot.offset, name, from_id, message)
self.log.info('Message (id{0}) from {1} (id{2}): "{3}"'.format(*parameters))
self.run_command(*parameters)
def run_command(self, offest, name, from_id, cmd):
if cmd == '/ping': # ping
self.send_text(from_id, 'pong') # Отправка ответа
elif cmd == '/help': # help
self.send_text(from_id, 'No help today. Sorry.') # Ответ
elif MyDaemonBot.MAGNET in cmd: # add torrent
self.send_text(from_id, 'try add torrent from magnet url')
result = self.add_torrent(cmd)
if result == 0:
self.send_text(from_id, 'torrent add success')
elif result == 1:
self.send_text(from_id, 'duplicate torrent')
elif result == 2:
self.send_text(from_id, 'invalid torrent')
elif result == 3:
self.send_text(from_id, 'unknown error')
else:
self.send_text(from_id, 'Got it.') # send answer
def add_torrent(self, text):
# example success add torrent: localhost:9091/transmission/rpc/ responded: "success"
# example duplicate torrent: Error: duplicate torrent
# example invalid or corrupt torrent: Error: invalid or corrupt torrent file
res = -1
self.text = text
try:
p1 = subprocess.Popen(["transmission-remote", "-n",
MyDaemonBot.TRANSMISSION_LOGIN+":"+MyDaemonBot.TRANSMISSION_PASS, "-a",
self.text],
stdout=subprocess.PIPE).communicate()[0]
except:
self.log.error('error transmission remote')
res = 2
return res
output = p1.decode('utf-8')
if self.success in output:
res = 0
return res
elif self.duplicate in output:
res = 1
return res
elif self.invalid in output:
res = 2
return res
else:
res = 3 # res = 3 unknown result
return res
def send_text(self, chat_id, text):
""" send test to chat_id
ToDo: repeat if failed
:param text: type string
:param chat_id: type int id chat"""
#self.chat_id = chat_id
self.text = text
self.log.info('Sending to {0}: {1}'.format(chat_id, text))
data = {'chat_id': chat_id, 'text': text}
request = requests.post(MyDaemonBot.URL + MyDaemonBot.TOKEN + '/sendMessage', data=data)
if not request.status_code == 200:
#data = None
return False
#data = None
return request.json()['ok']
def __str__(self):
return '{}'.format(self.text)
if __name__ == "__main__":
bot = MyDaemonBot()
context = daemon.DaemonContext(
working_directory=bot.path,
umask=0o002,
pidfile=lockfile.FileLock(bot.path + '/telegram_bot.pid'),
)
# exit()
if context.pidfile.is_locked():
exit('daemon is running now!')
# context.signal_map = {
# signal.SIGTSTP: None,
# signal.SIGTTIN: None,
# signal.SIGTTOU: None,
# signal.SIGTERM: 'terminate',
# }
context.files_preserve = [bot.fh.stream] # logging
while True:
try:
with context:
bot.run()
except Exception as e:
bot.log.error(traceback.format_exc())
time.sleep(5)