-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUoMCABI.py
87 lines (78 loc) · 3.08 KB
/
UoMCABI.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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
__author__ = "Haoyu (Chris) Lin"
__copyright__ = "Copyright 2017"
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "Haoyu (Chris) Lin"
__email__ = "[email protected]"
__description__ = "This script uses `GMail for Python`
(git://github.com/charlierguo/gmail.git) module to read
your UoM account and try to book in automatically when
there is event place available."
"""
import getpass
import json
import base64
import re
import urllib2
import time
import gmail
mode = input('1. type the student email and pwd directly;\n2. use the config file to save the effort of typing everytime (less secure!)\nchoose \'1\' or \'2\' and then press the ENTER key:')
if mode == 1:
stu_login_name = raw_input('\nstudent login name (\"@student.unimelb.edu.au\" not needed):')
password = getpass.getpass('password:')
sleep_time = input('sleep time (sec):')
elif mode == 2:
# read the config
with open('config.json') as json_file:
config = json.load(json_file)
stu_login_name = config[u'stu_login_name']
password = config[u'password']
sleep_time = config[u'sleep_time']
else:
print "please choose \'1\'or \'2\'"
exit(0)
username = stu_login_name+"@student.unimelb.edu.au"
sleep_time = float(sleep_time)
# log in
g = gmail.login(username, password)
if g.logged_in:
print "\nlog in successfully.\n"
while True:
# read possible mals
psb_mails = g.inbox().mail(unread=True,sender="[email protected]")
for psb_mail in psb_mails:
psb_mail.fetch()
if "event places now available for booking" in psb_mail.subject:
content = base64.b64decode(psb_mail.body)
# print content
# use regexp to get the url to book in
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', content)
# print urls
for url in urls:
if "ViewEvent" in url:
event_id = url.split("=")[1]
# print event_id
event_url = "https://careersonline.unimelb.edu.au/ViewEvent.chpx?id="+event_id
position = content.find(event_url)
print content[position:].split(">")[1].split("<")[0]
if "emailbook" in url:
book_url = url
# open the url
response = urllib2.urlopen(url)
result = response.read()
if "You have successfully booked in" in result:
print "You have successfully booked in to this event!"
psb_mail.read()
elif "Invalid Link" in result:
print "You have already booked in to this event."
psb_mail.read()
else:
print "You are late."
print "\nsleeping for " + str(sleep_time) + " second(s)\n"
time.sleep(sleep_time)
# log out
g.logout()
print "\nlogged out."