-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (81 loc) · 3.12 KB
/
main.py
File metadata and controls
94 lines (81 loc) · 3.12 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
import requests
import sched, time
s = sched.scheduler(time.time, time.sleep)
api_url = "https://tilburguniversity.instructure.com/api/v1/"
token = "<YOUR TOKEN>"
headers = {'Authorization': 'Bearer ' + token}
bot_token = "<YOUR TELEGRAM BOT TOKEN>"
bot_chatID = "<YOUR CHAT ID>"
subedGroups = []
courseIDME = 8934
courseIDLA = 8961
courseIDIA = 8953
num = 1
# Function to Sub to a group based on id
def subtogroup(id):
responsetosub = requests.post(api_url + 'groups/' + str(id) + '/memberships', headers=headers, params={'user_id': 'self'})
if responsetosub.status_code == 200:
print('subbed to:')
print(id)
telegram_bot_sendnewsignup(id)
else:
print('Error subbing to:' + str(id))
# Function to check groups signed up to
def checksubedgroups():
global subedGroups
subedGroups = []
responsesubbed = requests.get(api_url + 'users/self/groups', headers=headers)
print('Now subed to:')
for group in responsesubbed.json():
subedGroups.append(group['id'])
print(group['id'])
#Function to inform user on group signup based on id
def telegram_bot_sendnewsignup(id):
groupInfo = requests.get(api_url + 'groups/' + str(id), headers=headers).json()
courseInfo = requests.get(api_url + 'courses/' + str(groupInfo['course_id']), headers=headers).json()
bot_message = 'Signed up to: ' + courseInfo['name'] + '\n' + groupInfo['name']
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
#Looping maintask
def maintask(sc):
print('')
global num
print("LOOP " + str(num))
checksubedgroups()
# Macro-Economie
print('STARTING MACRO ECONOMIE')
# Get al groups from Macro Economics
response = requests.get(api_url + 'courses/' + str(courseIDME) + '/groups', headers=headers)
#For each group, check if not subscribed and if not full, then subscribe
for groupME in response.json():
if groupME['id'] in subedGroups:
continue
elif groupME['max_membership'] == groupME['members_count']:
print(str(groupME['id']) + ' is full, checking again in 10 seconds!')
continue
else:
subtogroup(groupME['id'])
# Lineare algebra
print('')
print('STARTING LINEARE ALGEBRA')
response = requests.get(api_url + 'courses/' + str(courseIDLA) + '/groups', headers=headers)
#For LA also check if couse is EN and if it's second lesson in the week (as I only want to attend that one)
for groupLA in response.json():
if groupLA['id'] in subedGroups:
continue
if not 'EN' in groupLA['name']:
continue
elif not '.2' in groupLA['name']:
continue
elif groupLA['max_membership'] == groupLA['members_count']:
print(str(groupLA['id']) + ' is full, checking again in 10 seconds!')
continue
else:
subtogroup(groupLA['id'])
num += 1
#Repeat every 10 seconds
s.enter(10, 1, maintask, (sc,))
#Start
s.enter(0, 1, maintask, (s,))
s.run()