-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblink_server.py
More file actions
83 lines (69 loc) · 2.71 KB
/
blink_server.py
File metadata and controls
83 lines (69 loc) · 2.71 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import _thread
import asyncio
import json
import time
from mattermostdriver import Driver
from blink import Blink
from config import Config
from http_server import Server
class BlinkServer(object):
def __init__(self):
self.config = Config()
self.not_read_channels = []
self.blink = Blink()
self.mattermost_driver = Driver({
'url': self.config.get_string('MATTERMOST', 'url'),
'login_id': self.config.get_string('MATTERMOST', 'login_id'),
'password': self.config.get_string('MATTERMOST', 'password'),
'verify': self.config.get_bool('MATTERMOST', 'verify'),
'scheme': self.config.get_string('MATTERMOST', 'scheme'),
'port': self.config.get_int('MATTERMOST', 'port'),
'debug': self.config.get_bool('MATTERMOST', 'debug')
})
self.ignored_channels = self.config.get_string('MATTERMOST', 'ignored_channels').split(',')
self.server = None
self.start()
def start_wbe_server(self, thread_name):
self.server = Server(self.blink)
@asyncio.coroutine
def socket_handler(self, message):
print(message)
self.handle_message(json.loads(message))
def handle_message(self, json_message):
if 'event' not in json_message:
return
if json_message['event'] == "posted":
try:
self.ignored_channels.index(json_message['data']['channel_display_name'])
except ValueError:
self.parse_post(json.loads(json_message['data']['post']))
elif json_message['event'] == "channel_viewed":
self.mark_chanel_as_read(json_message['data']['channel_id'])
def parse_post(self, post_data):
try:
self.not_read_channels.index(post_data['channel_id'])
except ValueError:
self.not_read_channels.append(post_data['channel_id'])
self.blink.start_unread_blink()
def mark_chanel_as_read(self, channel_id):
try:
index = self.not_read_channels.index(channel_id)
del self.not_read_channels[index]
if len(self.not_read_channels) == 0:
self.blink.stop_unread_blinking()
except ValueError:
pass
def start(self):
_thread.start_new_thread(self.start_wbe_server, ('webserver',))
# todo: polaczenie z gitlabem - nowy MR
self.mattermost_driver.login()
while True:
try:
self.mattermost_driver.init_websocket(self.socket_handler)
except Exception:
print('Reconnecting to websocket')
time.sleep(60)
if __name__ == '__main__':
BlinkServer()