-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.py
executable file
·217 lines (147 loc) · 6.04 KB
/
notify.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
import functools
import json
import re
import time
import random
from collections import namedtuple
from datetime import datetime
from typing import Optional
import requests
from pathlib import Path
from bs4 import BeautifulSoup
@functools.lru_cache(maxsize=None)
def translate_to_english(scientific_name: str) -> Optional[str]:
translations_file = Path.cwd() / "data/translations.html"
soup = BeautifulSoup(translations_file.read_bytes(), "html.parser")
table = soup.find_all("table", id="species")[0]
latin_column = 0
english_column = 1
first = True
for row in table.find_all("tr"):
if first:
first = False
continue
tds = row.find_all("td")
if tds[latin_column].text == scientific_name:
return tds[english_column].text
return None
def get_species(observation):
species_in_observation = []
for species in observation.children:
try:
specie = species.find_all("span", class_="sci_name")[0].parent.text
except IndexError:
specie = species.find_all("i")[1].text + " (TODO: this is a strange observation, getting species only?)"
scientific_name = get_scientific_name(specie)
english_name = translate_to_english(scientific_name)
if english_name is not None:
species_in_observation.append(specie + " " + english_name)
else:
species_in_observation.append(specie)
return species_in_observation
def get_comarca_abbr(location: str) -> Optional[str]:
"""Get the comarca from the location name.
Return the comarca abbreviation."""
comarca_abbr = re.search(r"\(([A-Z]{3})\)", location)
if comarca_abbr:
return comarca_abbr.group(1)
else:
return None
def get_scientific_name(sighting: str) -> Optional[str]:
scientific_name = re.search(r"\((.*)\)", sighting)
if scientific_name:
return scientific_name.group(1)
else:
return None
def debug_request(url: str, page):
dir = Path.cwd() / "data/dumps"
dir.mkdir(exist_ok=True)
page_content = url + "\n\n" + page.text
filename = datetime.now().strftime("%Y%m%d-%H%M%S.%f.html")
# print("Requested page:", url)
# print("Dump:", filename)
Path(dir / filename).write_text(page_content)
def get_raw_sighting():
for page_number in range(1, 150):
url = f"https://www.ornitho.cat/index.php?m_id=4&sp_DOffset=1&mp_item_per_page=60&mp_current_page={page_number}"
headers = {"User-Agent": "Estem provant un script per tenir unes notificacions, [email protected]"}
page = requests.get(url, headers=headers)
debug_request(url, page)
soup = BeautifulSoup(page.content, "html.parser")
dates = soup.find_all("div", class_="listTop")
if len(dates) == 0:
# Finish iterating
return
date = dates[0].text
list_locations = soup.find_all("div", class_="listSubmenu")
list_species = soup.find_all("div", class_="listObservation")
for location, species in zip(list_locations, list_species):
species_list = get_species(species)
yield {"location": location.text, "species": species_list, "date": date}
time.sleep(random.randint(1, 5))
def get_all_sightings() -> dict:
raw_sighting_generator = get_raw_sighting()
sightings = {}
for sighting in raw_sighting_generator:
SightingKey = namedtuple("SightingKey", "location date")
key = SightingKey(sighting["location"], sighting["date"])
if key in sightings:
sightings[key] += sighting["species"]
else:
sightings[key] = sighting["species"]
return sightings
def get_next_sighting() -> dict:
raw_sighting_generator = get_raw_sighting()
previous = next(raw_sighting_generator)
if previous is None:
# Early in the morning there isn't the date?
return {}
for current_sighting in raw_sighting_generator:
if current_sighting is None:
break
if previous["location"] == current_sighting["location"]:
previous["species"] += current_sighting["species"]
else:
yield previous
previous = current_sighting
def save_sightings(sighting: list[dict]):
data = Path.cwd() / "data"
data.mkdir(exist_ok=True)
file = data / "sent.json"
with file.open("w") as fp:
json.dump(sighting, fp, indent=2)
def load_sightings() -> list[dict]:
file = Path.cwd() / "data/sent.json"
if not file.exists():
return []
with file.open("r") as fp:
return json.load(fp)
def format_sighting(sighting: dict) -> str:
formatted = ""
formatted += f"Location: {sighting['location']}\n"
formatted += f"Comarca: {get_comarca_abbr(sighting['location'])}\n"
for species in sighting["species"]:
formatted += f" {species}\n"
return formatted
def send_sighting(sighting: dict) -> None:
print(format_sighting(sighting))
def get_sights_for_location_date(location: str, date: str, sightings: list[dict]) -> list[str]:
for sighting in sightings:
if sighting["location"] == location and sighting["date"] == date:
return sighting["species"]
return []
def print_new_sightings():
sightings_previously_sent = load_sightings()
all_current_sightings = get_all_sightings()
all_sightings = []
for location_date, species in all_current_sightings.items():
species_previously_sent_for_location = get_sights_for_location_date(location_date.location, location_date.date, sightings_previously_sent)
new_species = sorted(set(species) - set(species_previously_sent_for_location))
sight_to_send = {"location": location_date.location, "date": location_date.date, "species": new_species}
if len(new_species) > 0:
send_sighting(sight_to_send)
all_sightings.append({"location": location_date.location, "date": location_date.date, "species": species})
save_sightings(all_sightings)
if __name__ == "__main__":
print_new_sightings()