forked from rllola/zeronet-torrent-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlertEncoder.py
40 lines (32 loc) · 1.24 KB
/
AlertEncoder.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
import base64
class AlertEncoder(object):
def __init__(self, alert):
self.alert = alert
self.encodeAlert()
if self.response['what'] == 'read_piece':
self.encodeReadPieceAlert()
elif self.response['what'] == 'add_torrent':
print('Add Torrent Alert !')
elif self.response['what'] == 'piece_finished':
self.encodePieceFinishedAlert()
def encodeAlert(self):
self.response = {
'what': self.alert.what(),
'message': self.alert.message(),
'category': self.alert.category()
}
def encodeReadPieceAlert(self):
if hasattr(self.alert, 'error') and self.alert.error.value() != 0:
self.response['error'] = {
'value': self.alert.error.value(),
'message': self.alert.error.message()
}
else :
self.response['pieceIndex'] = self.alert.piece
self.response['size'] = self.alert.size
self.response['buffer'] = self.alert.buffer
def encodePieceFinishedAlert(self):
self.response['handle'] = self.alert.handle
self.response['pieceIndex'] = self.alert.piece_index
def get(self):
return self.response