-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.py
More file actions
174 lines (123 loc) · 4.04 KB
/
Copy pathdaemon.py
File metadata and controls
174 lines (123 loc) · 4.04 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
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
import classes
import json
from datetime import datetime
import time
import requests
from tqdm import tqdm
def getTime():
n = {}
with open("time.json") as f:
n = json.loads(f.read())
return n
def fix(time, current):
time = time.replace(day=current.day, month=current.month, year=current.year)
return time
wasToday = {}
lastReset = 0
def sendToUser(user, title, content):
for x in user.tokens:
pass
#sendFirebase(x, title, content)
def findNextLesson(timetable, _class):
curDate = datetime.now()
times = getTime()
_next = None
for x in range(len(timetable.lessons)):
startTime = times[_class][x].split("-")[0]
startTime = fix(datetime.strptime(startTime, "%H:%M"), curDate)
if startTime >= curDate and _next == None:
if timetable.lessons[x] != "-":
_next = timetable.lessons[x]
return _next
def getTimetableNotification(_class):
times = getTime()
nonDefault = []
for x in times.keys():
if x != "default":
nonDefault.append(x)
allUsers = classes.User.find("", "all")
for i in tqdm(allUsers):
if i.notifParams["10min"] == True and len(i.tokens) != 0:
userClass = "default"
if str(i.classNumber) in nonDefault:
userClass = str(i.classNumber)
if userClass == _class:
userTimetable = classes.Timetable.createTimetable(i, datetime.now())
lesson = findNextLesson(userTimetable, userClass)
if lesson != None:
_title = "🏫 Скоро урок"
_content = "Через 10 минут у вас начинается " + lesson.title
sendToUser(i, _title, _content)
def checkTimes(times):
curDate = datetime.now()
for i in times.keys():
if i not in wasToday:
wasToday[i] = []
for x in times[i]:
if x in wasToday[i]:
continue
startTime = x.split("-")[0]
startTime = fix(datetime.strptime(startTime, "%H:%M"), curDate)
if startTime >= curDate:
diff = (startTime - curDate).total_seconds()
if diff <= 600:
print("Send 10minute push to " + i)
getTimetableNotification(i)
wasToday[i].append(x)
print("Worker end")
times = getTime()
while True:
checkTimes(times) # Send push notification with lesson start text
####### ######## #####
# Reset watchdog lock#
####### ######## #####
currentDate = datetime.now()
if currentDate.hour == 0:
if currentDate.day != lastReset:
wasToday = {}
lastReset = currentDate.day
time.sleep(1)
"""
To import:
import sys
sys.path.append("..")
from config import Config
import classes
import requests
import json
import os
def sendFirebase(token, title, content):
data = {"to": token, "notification": {"title": title, "body": content}}
requests.post(
"https://fcm.googleapis.com/fcm/send",
headers={
"Content-Type": "application/json",
"Authorization": "key=" + Config().googleFCM,
},
data=json.dumps(data),
)
def sendPush(title, content):
db = classes.createMongo().users
db = db.find()
for i in db:
tokens = i["tokens"]
for c in tokens:
sendFirebase(c, title, content)
if os.path.exists("lastNews") is not True:
with open("lastNews", "w") as f:
f.write("")
with open("cdn/news.json") as f:
jsonData = json.loads(f.read())
with open("lastNews", "r") as f:
raw = f.read()
if raw == "":
lastJson = {}
lastJson["content"] = ""
else:
lastJson = json.loads(raw)
if jsonData[0]["content"] != lastJson["content"]:
if jsonData[0]["important"] == True:
sendPush("Срочная новость", jsonData[0]["title"])
with open("lastNews", "w") as f:
f.write(json.dumps(jsonData[0]))
"""