forked from bo-li/check-field
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_field.py
126 lines (98 loc) · 3.43 KB
/
check_field.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
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
#!/usr/bin/python
# -*- mode: python; c-basic-offset: 4 -*- vim: set sw=4 tw=70 et sta ai:
import requests
import bs4
import weekdays
import re
import smtplib
import sys
from email.mime.text import MIMEText
# offset for text debug
offset = 1
# week increment, 0 for current week
shift = int(sys.argv[1])
# get the date of Saturday and Sunday
sat = weekdays.weekdays(5, shift).get_weekday_date()
sun = weekdays.weekdays(6, shift).get_weekday_date()
# FIXME: hard-coded text for url. Need to be updated the first time when
# the source website updates
root_url = 'http://kalender.soccarena-olympiapark.de/kalender'
court_url = root_url + '/courts/courts.php?kalenderTag='
# Yes so simple, this would be the url for field information of a given
# date!
sat_url = court_url + str(sat)
sun_url = court_url + str(sun)
def get_frei_url (url):
# Get the http response from the url
response = requests.get (url)
# use bs4 to parse the html text
soup = bs4.BeautifulSoup (response.text)
# return the link for "bookable" court
return [a.attrs.get('href') for a in soup.select('td.buchbar a[href^=../anfrage/anfrage.php]')]
def get_buch_url (url):
# set up the valid time range
validrange = 20
# stripped the head double dot
s_url = re.sub ("^\.\.", "", url)
# append the root url
buch_url = root_url + s_url
# search the court number
m = re.search('(?<=court\=)[0-9]', buch_url)
# search the start time
t = re.search('(?<=startZeit\=)\d\d', str)
startTime = int(t.group(0))
if int(m.group(0)) >= 5 and startTime < validrange:
buch_url=""
return buch_url
def send_buch_email (text):
'''
Send the text from the "sender" to "receiver" hard-coded here
FIXME: extremely insecure! DO NOT use any personal email as the sender
'''
sender = '[email protected]'
receivers = ['[email protected]', '[email protected]']
# receivers = ['[email protected]']
username = '[email protected]'
password = 'm6w9kj874'
# message = """From: From Bo Li <[email protected]>
# To: Team members
# Subject: Soccer field booking information test
# This is a e-mail test message
# """
content = "Found the following place(s): \n" + text
msg = MIMEText (content)
msg['Subject']= "Soccer field booking information for " + str(sat) + " and " + str(sun)
msg['From'] = "Bo Li" + "<" + sender + ">"
msg['To'] = ";".join(receivers)
try:
smtpObj = smtplib.SMTP('smtp.gmail.com:587')
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(username, password)
smtpObj.sendmail (sender, receivers, msg.as_string())
smtpObj.quit()
print "Sucessfully sent email"
except SMTPException:
print "Error: unable to send email"
# main function starts
if __name__ == "__main__":
# get the free url for Satday and Sunday
frei_url_sat = get_frei_url(sat_url)
frei_url_sun = get_frei_url(sun_url)
# Lazy work, combine the two url cells
zusam_url = frei_url_sat + frei_url_sun
text = ""
for url_sat in zusam_url:
# put all the urls in an string array
url_text = get_buch_url (url_sat)
if url_text == "":
continue
else:
text = text + url_text + "\n"
if offset:
if text != "":
send_buch_email (text)
else:
print text
# print str(frei_url_sat) + str(frei_url_sun)
# print root_url + str(frei_url_sat) + '\n' + root_url + str(frei_url_sun)