forked from jannick-schroeer/InfosnowReadAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfosnow_api.py
More file actions
102 lines (79 loc) · 3.99 KB
/
Copy pathinfosnow_api.py
File metadata and controls
102 lines (79 loc) · 3.99 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
95
96
97
98
99
100
101
102
import requests
def remove_html_tags(text: str):
text = text.replace('ä', 'ä').replace('ö', 'ö').replace('ü', 'ü')
text = text.replace('Ä', 'Ä').replace('Ö', 'Ö').replace('Ü', 'Ü')
text = text.replace('è', 'è').replace('È', 'È')
text = text.replace('é', 'é').replace('É', 'É')
text = text.replace('à', 'à').replace('À', 'À')
text = text.replace('ê', 'ê').replace('Ê', 'Ê')
text = text.replace('ç', 'ç').replace('Ç', 'Ç')
return text.strip()
def get_infosnow_data (resort_id, season):
"""
Gets Data from the Infosnow website.
:param resort_id: ID of the resort you want to pull
:param season: Season of the data you want to pull 1: Winter, 2: Summer
:return: Dictionary filled with data on the stuff you requested.
"""
rq = requests.get('https://www.infosnow.ch/~apgmontagne/',
params={'tab': 'web-js', 'lang': 'de', 'xid': resort_id, 'saison': season},
headers={'User-Agent': 'Totally Not a Bot'})
if rq.status_code != 200: # Return None if status code is not 200
print('Request didn\'t return status 200')
return None
html = rq.content.decode("utf-8") # Decodes request to readable text.
data = {} # Dictionary where information is stored
position = html.find('class="isH1"') # Searches for Headers
while position > -1:
# Isolates header
position = html.find('>', position + 1)
position = html.find('>', position + 1)
end = html.find('</div>', position + 1)
header = html[position + 1:end]
if 'offen)' in header: # Checks that the headers contain important information
h_end = header.find(' (')
header = remove_html_tags(header[:h_end])
else:
position = html.find('class="isH1"', position)
continue
entries = [] # List of Entries in the Header
next_entry = html.find('<div class="isRec">', position)
next_data = html.find('<div class="isBreakBox">', position)
if next_data < 0:
next_data = html.find('<img class="isAPG"', position)
while next_entry < next_data: # Gets each entry and adds it's information to data
next_entry = html.find('~apgmontagne/pub/pics/state/3/', next_entry)
next_entry = html.find(' alt="', next_entry)
status = remove_html_tags(html[next_entry + 6:html.find('" title="', next_entry)])
next_entry = html.find('~apgmontagne/pub/pics/svg/', next_entry)
next_entry = html.find(' alt="', next_entry)
category = remove_html_tags(html[next_entry + 6:html.find('" title="', next_entry)])
next_entry = html.find('<div class="isTxt">', next_entry)
text_end = html.find('</div>', next_entry)
span = html.find('<span', next_entry)
if 0 < span < text_end:
name = remove_html_tags(html[next_entry + 19:span])
span = html.find('>', span)
span_end = html.find('</span>', span)
length = remove_html_tags(html[span + 2:span_end - 1])
else:
name = remove_html_tags(html[next_entry + 19:text_end])
length = None
if ' • ' in name:
point = name.find(' • ')
label = name[:point].strip()
name = name[point + 8:]
else:
label = None
doc = {'name': name, 'status': status, 'type': category}
if label is not None:
doc['label'] = label
if length is not None:
doc['length'] = length
entries.append(doc)
next_entry = html.find('<div class="isRec">', next_entry)
if next_entry < 0:
break
position = html.find('class="isH1"', position)
data[header] = entries
return data # Returns Data