-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref.py
124 lines (97 loc) · 4.45 KB
/
ref.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
import requests
from bs4 import BeautifulSoup
from enum import Enum
from datetime import timedelta, datetime, date
from pytz import timezone
import sync
tz = timezone("America/Los_Angeles")
class Type(Enum):
ref = 'ref'
line = 'line'
play = 'play'
avail = 'avail'
event = 'event'
@classmethod
def from_string(cls, string):
for resp in cls:
if resp.value == string.lower():
return resp
raise Exception("no such game type known " + string.lower())
class Game:
def __init__(self, date, resp, location, league, duration=timedelta(hours=1.25)):
self.date = date
self.location = location
self.league = league
self.duration = duration
self.resp = Type.from_string(resp.lower())
def __str__(self):
return "{} {} {} @ {}".format(self.date, self.resp, self.league, self.location)
def __repr__(self):
return str(self)
def get_summary(self):
return "{} {}".format(self.resp.name.title(), self.league)
def get_games():
def createGames():
games = BeautifulSoup(s.get("http://ihonc-ca.com/members/schedsearch.cgi",
params={'preset': 'future'}).text, 'html.parser')
form = games.find('form')
if not form:
return []
games = []
for row in form.find_all('input', {'type': 'checkbox', 'name': 'import'}):
try:
row = row.find_parent('tr').find_all("td")
row = [x.text for x in row[2:]]
row[1] = row[1].replace('a', 'AM')
row[1] = row[1].replace('p', 'PM')
if Type.from_string(row[3]) not in (Type.ref, Type.line):
continue
row[1] = row[1].replace("12:00n", "12:00PM")
for pattern in ('%b %d %Y %I:%M%p', '%a %b %d %I:%M%p'):
try:
when = datetime.strptime(
'{} {}'.format(*row[:2]), pattern)
break
except ValueError:
continue
print(row[:2], when)
when = when.replace(year=datetime.now().year)
when = tz.localize(when)
if when < tz.localize(datetime.now()):
when = when.replace(year=datetime.now().year + 1)
games.append(Game(when, *(x for x in row[3:] if x)))
except Exception as e:
print("Couldn't create game because", e)
return games
s = requests.Session()
s.post("http://ihonc-ca.com/members/index.cgi",
data={'login_username': sync.config.get('ihonc', 'username'), 'login_password': sync.config.get('ihonc', 'password')})
games = []
for l in createGames():
games.append(l)
for game in games:
start = game.date
end = game.date + game.duration
result = sync.service.events().list(calendarId="primary",
orderBy="startTime",
singleEvents=True,
sharedExtendedProperty="hockeyref=true",
timeMax=(
start + timedelta(seconds=5)).isoformat(),
timeMin=start.isoformat())
results = result.execute()
if(len(results.get('items', [])) > 0):
print("Not creating {} because it already exists".format(game))
else:
# create the event
result = sync.service.events().insert(calendarId="primary", body={'start': {'dateTime': start.isoformat()},
'summary': game.get_summary(),
'location': game.location,
'end': {'dateTime': end.isoformat()},
'reminders': {'useDefault': False},
'colorId': '6',
'extendedProperties': {'shared': {'hockeyref': True}}}).execute()
print("Created {}".format(game))
if __name__ == '__main__':
sync.setup_google()
get_games()