-
Notifications
You must be signed in to change notification settings - Fork 3
/
monitor_base.py
94 lines (79 loc) · 3.73 KB
/
monitor_base.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
import logging
from abc import ABC, abstractmethod
from datetime import datetime
from typing import List, Union
from cqhttp_notifier import CqhttpMessage, CqhttpNotifier
from status_tracker import StatusTracker
from telegram_notifier import TelegramMessage, TelegramNotifier
from twitter_watcher import TwitterWatcher
class MonitorBase(ABC):
def __init__(self, monitor_type: str, username: str, token_config: dict, user_config: dict, cookies_dir: str):
logger_name = '{}-{}'.format(username, monitor_type)
self.logger = logging.getLogger(logger_name)
self.twitter_watcher = TwitterWatcher(token_config.get('twitter_auth_username_list', []), cookies_dir)
self.username = username
self.user_id = self.twitter_watcher.get_id_by_username(username)
if not self.user_id:
raise RuntimeError('Initialization error, please check if username {} exists'.format(username))
self.telegram_chat_id_list = user_config.get('telegram_chat_id_list', None)
self.cqhttp_url_list = user_config.get('cqhttp_url_list', None)
self.message_prefix = '[{}][{}]'.format(username, monitor_type)
self.update_last_watch_time()
def update_last_watch_time(self):
StatusTracker.update_monitor_status(self.monitor_type, self.username)
def get_last_watch_time(self):
return StatusTracker.get_monitor_status(self.monitor_type, self.username)
def send_message(self,
message: str,
photo_url_list: Union[List[str], None] = None,
video_url_list: Union[List[str], None] = None):
StatusTracker.update_last_notify_time()
message = '{} {}'.format(self.message_prefix, message)
self.logger.info('Sending message: {}\n'.format(message))
if photo_url_list:
photo_url_list = [photo_url for photo_url in photo_url_list if photo_url]
if video_url_list:
video_url_list = [video_url for video_url in video_url_list if video_url]
if photo_url_list:
self.logger.info('Photo: {}'.format(', '.join(photo_url_list)))
if video_url_list:
self.logger.info('Video: {}'.format(', '.join(video_url_list)))
if self.telegram_chat_id_list:
TelegramNotifier.put_message_into_queue(
TelegramMessage(chat_id_list=self.telegram_chat_id_list,
text=message,
photo_url_list=photo_url_list,
video_url_list=video_url_list))
if self.cqhttp_url_list:
CqhttpNotifier.put_message_into_queue(
CqhttpMessage(url_list=self.cqhttp_url_list,
text=message,
photo_url_list=photo_url_list,
video_url_list=video_url_list))
@abstractmethod
def watch(self) -> bool:
pass
@abstractmethod
def status(self) -> str:
pass
class MonitorManager():
monitors = None
def __new__(self):
raise Exception('Do not instantiate this class!')
@classmethod
def init(cls, monitors: dict):
cls.monitors = monitors
cls.logger = logging.getLogger('monitor-caller')
@classmethod
def get(cls, monitor_type: str, username: str) -> Union[MonitorBase, None]:
assert cls.monitors is not None
monitors_by_type = cls.monitors.get(monitor_type, None)
assert monitors_by_type is not None
monitor = monitors_by_type.get(username, None)
return monitor
@classmethod
def call(cls, monitor_type: str, username: str) -> bool:
monitor = cls.get(monitor_type, username)
if not monitor:
return True
return monitor.watch()