1
|
|
|
import requests |
2
|
|
|
from bs4 import BeautifulSoup |
3
|
|
|
import dateparser |
4
|
|
|
|
5
|
|
|
URL = 'http://sugarloaf.com/the-mountain/trails-and-lifts' |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
def trail_name(trail): |
9
|
|
|
"""Returns a string containing the name of the trail""" |
10
|
|
|
try: |
11
|
|
|
return trail.contents[0] |
12
|
|
|
except AttributeError: |
13
|
|
|
raise AttributeError(trail) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def trail_status(trail): |
17
|
|
|
"""Returns True if the trail is open""" |
18
|
|
|
if 'closed' in trail.attrs['class'] or 'snowmaking-closed' in trail.attrs['class']: |
19
|
|
|
return False |
20
|
|
|
return True |
21
|
|
|
|
22
|
|
|
def trail_snowmaking(trail): |
23
|
|
|
"""Returns true if snowmaking in progress""" |
24
|
|
|
if 'snowmaking-closed' in trail.attrs['class'] or 'snowmaking-open' in trail.attrs['class']: |
25
|
|
|
return True |
26
|
|
|
return False |
27
|
|
|
|
28
|
|
|
dificulty = {'beginner', 'intermediate', 'double-black', 'black', 'terrain-park'} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def trail_difficulty(trail): |
32
|
|
|
"""Returns a string with the difficulty of given trail""" |
33
|
|
|
dif = set(trail.attrs['class']).intersection(dificulty) |
34
|
|
|
try: |
35
|
|
|
return list(dif)[0] |
36
|
|
|
except IndexError: |
37
|
|
|
raise IndexError(trail) |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def trail_groomed(trail): |
41
|
|
|
"""Returns true if the trail has been groomed""" |
42
|
|
|
if 'groomed' in trail.attrs['class']: |
43
|
|
|
return True |
44
|
|
|
return False |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def trail_terrain_park(trail): |
48
|
|
|
"""Returns True if the trail is a terrain park""" |
49
|
|
|
if 'terrain-park' in trail.attrs['class']: |
50
|
|
|
return True |
51
|
|
|
return False |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def trail_area(trail): |
55
|
|
|
"""Returns a string with the area of the mountain the trail is in""" |
56
|
|
|
try: |
57
|
|
|
return trail.find_previous_sibling('h3').contents[0] |
58
|
|
|
except AttributeError: |
59
|
|
|
raise AttributeError(trail) |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def update_trails(soup): |
63
|
|
|
"""Yields dicts with Sugarloaf trails names, current status, and other attributes""" |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
trail_status_div = soup.find('div', {'class': 'trail-status'}) |
67
|
|
|
|
68
|
|
|
all_trail_divs = trail_status_div.find_all('div', {'class', 'trail'}) |
69
|
|
|
|
70
|
|
|
for trail_div in all_trail_divs: |
71
|
|
|
yield { |
72
|
|
|
'name': trail_name(trail_div), |
73
|
|
|
'open': trail_status(trail_div), |
74
|
|
|
'difficulty': trail_difficulty(trail_div), |
75
|
|
|
'groomed': trail_groomed(trail_div), |
76
|
|
|
'terrain-park': trail_terrain_park(trail_div), |
77
|
|
|
'area': trail_area(trail_div), |
78
|
|
|
'snowmaking': trail_snowmaking(trail_div) |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def lift_name(lift): |
83
|
|
|
"""Returns a string with the lifts name""" |
84
|
|
|
return lift.contents[0] |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
statuses = {'open', 'closed', 'scheduled', 'hold'} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def lift_status(lift): |
91
|
|
|
"""Returns the lift status""" |
92
|
|
|
status = set(lift.attrs['class']).intersection(statuses) |
93
|
|
|
try: |
94
|
|
|
return list(status)[0] |
95
|
|
|
except IndexError: |
96
|
|
|
raise IndexError(lift) |
97
|
|
|
|
98
|
|
|
def update_lifts(soup): |
99
|
|
|
"""Yields dicts with Sugarloaf lift names and statuses""" |
100
|
|
|
div_lift_status = soup.find('div', {'class': 'lift-status'}) |
101
|
|
|
lifts_divs = div_lift_status.find_all('div', {'class': 'lift'}) |
102
|
|
|
|
103
|
|
|
for lift_div in lifts_divs: |
104
|
|
|
yield { |
105
|
|
|
'name': lift_name(lift_div), |
106
|
|
|
'status': lift_status(lift_div) |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def update_time(soup): |
111
|
|
|
"""Returns datetime when the lift and trail report was last updated""" |
112
|
|
|
right_content = soup.find('div', {'class': 'content--right'}) |
113
|
|
|
condition_update_string = right_content.find('small').contents[0] |
114
|
|
|
condition_time_string = condition_update_string.strip().split('of')[1] |
115
|
|
|
return dateparser.parse(condition_time_string) |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
def make_soup(): |
119
|
|
|
"""Returns BeautifulSoup for lifts and trails""" |
120
|
|
|
r = requests.get(URL) |
121
|
|
|
return BeautifulSoup(r.content, 'lxml') |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
if __name__ == '__main__': |
125
|
|
|
import json |
126
|
|
|
|
127
|
|
|
soup = make_soup() |
128
|
|
|
|
129
|
|
|
trails = list(update_trails(soup)) |
130
|
|
|
|
131
|
|
|
lifts = list(update_lifts(soup)) |
132
|
|
|
|
133
|
|
|
all_statuses = {'trails': trails, |
134
|
|
|
'lifts': lifts, |
135
|
|
|
'update datetime': update_time(soup).isoformat()} |
136
|
|
|
|
137
|
|
|
with open('sugarloaf.json', 'w') as f: |
138
|
|
|
json.dump(all_statuses, f) |