-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (48 loc) · 1.51 KB
/
main.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
from EventParser import *
import sys
from Utils.DBConnection import *
from Bots.TelegramBot import TelegramBot
import schedule
import time
def fetchNewEvents(parser):
oldEventsQuery = DBConnection.Instance().getSession().query(Assignment).all()
# Transform into an id -> Assignment map
oldEvents = {}
for e in oldEventsQuery:
oldEvents[e.id] = e
newEvents = parser.parseContents()
savedEvents = []
for e in newEvents:
try:
_ = oldEvents[e.id]
except:
# Event does not exist. Therefore save it in the DB
savedEvents.append(e)
DBConnection.Instance().save(e)
# Notify any listener of the fact that we have new events
bot.newEventAdded(e)
# For now, this is like this. In the future, this will be taken from the bot or other settings
if len(sys.argv) < 2:
print("Missing params")
exit(0)
BOT_API_KEY = sys.argv[1]
# Init the DB
DBConnection.Instance().createAllTables()
# Get the URLs for the calendars
groups = DBConnection.Instance().executeQuery("SELECT * FROM Groups")
# Instantiate a parser for each of the URLs
parsers = []
if groups is not None:
for g in groups:
parsers.append(EventParser(g.calendarURL))
bot = TelegramBot(BOT_API_KEY)
for p in parsers:
fetchNewEvents(p)
# # Scheduling happens here!
# schedule.every(10).seconds.do(fetchNewEvents)
#
# while True:
# schedule.run_pending()
# time.sleep(1)
# Last command is initializing the bot
bot.initializeBot()