-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannouncements.py
More file actions
126 lines (94 loc) · 3.14 KB
/
Copy pathannouncements.py
File metadata and controls
126 lines (94 loc) · 3.14 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
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
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sigma import Cardinal
from tg_bot.utils import NotificationTypes
from telebot.types import InlineKeyboardMarkup as K, InlineKeyboardButton as B
from locales.localizer import Localizer
from threading import Thread
from logging import getLogger
import requests
import json
import os
import time
logger = getLogger("FPS.announcements")
localizer = Localizer()
_ = localizer.translate
def get_last_tag() -> str | None:
if not os.path.exists("storage/cache/announcement_tag.txt"):
return None
with open("storage/cache/announcement_tag.txt", "r", encoding="UTF-8") as f:
data = f.read()
return data
REQUESTS_DELAY = 600
LAST_TAG = get_last_tag()
def save_last_tag():
global LAST_TAG
if not os.path.exists("storage/cache"):
os.makedirs("storage/cache")
with open("storage/cache/announcement_tag.txt", "w", encoding="UTF-8") as f:
f.write(LAST_TAG)
def get_announcement(ignore_last_tag: bool = False) -> dict | None:
return None
def download_photo(url: str) -> bytes | None:
return None
def get_notification_type(data: dict) -> NotificationTypes:
types = {
0: NotificationTypes.ad,
1: NotificationTypes.announcement,
2: NotificationTypes.important_announcement
}
return types[data.get("type")] if data.get("type") in types else NotificationTypes.critical
def get_photo(data: dict) -> bytes | None:
if not (photo := data.get("ph")):
return None
return download_photo(u"{}".format(photo))
def get_text(data: dict) -> str | None:
if not (text := data.get("text")):
return None
return u"{}".format(text)
def get_pin(data: dict) -> bool:
return bool(data.get("pin"))
def get_keyboard(data: dict) -> K | None:
if not (kb_data := data.get("kb")):
return None
kb = K()
try:
for row in kb_data:
buttons = []
for btn in row:
btn_args = {u"{}".format(i): u"{}".format(btn[i]) for i in btn}
buttons.append(B(**btn_args))
kb.row(*buttons)
except:
return None
return kb
def announcements_loop_iteration(crd: Cardinal, ignore_last_tag: bool = False):
global LAST_TAG
if not (data := get_announcement(ignore_last_tag=ignore_last_tag)):
time.sleep(REQUESTS_DELAY)
return
elif not LAST_TAG:
LAST_TAG = data.get("tag")
save_last_tag()
time.sleep(REQUESTS_DELAY)
return
if not ignore_last_tag:
LAST_TAG = data.get("tag")
save_last_tag()
text = get_text(data)
photo = get_photo(data)
notification_type = get_notification_type(data)
keyboard = get_keyboard(data)
pin = get_pin(data)
if text or photo:
Thread(target=crd.telegram.send_notification,
args=(text,),
kwargs={"photo": photo, 'notification_type': notification_type, 'keyboard': keyboard,
'pin': pin},
daemon=True).start()
def announcements_loop(crd: Cardinal):
return
def main(crd: Cardinal):
pass
BIND_TO_POST_INIT = []