This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeojobs.py
50 lines (38 loc) · 1.68 KB
/
geojobs.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
import pathlib
import modal
import subprocess
import urllib.request
from os import environ
import re
import hooks
stub = modal.Stub("geospatial-jobs")
urlwatch_image = modal.Image.from_dockerfile("Dockerfile")
volume = modal.SharedVolume().persist("geospatial-jobs-urlwatch-cache")
URLWATCH_DIR = "/urlwatch"
DB_PATH = pathlib.Path(URLWATCH_DIR, "geojobs.db")
URL_REMOTE_PATH = "https://raw.githubusercontent.com/dchirst/geospatialjobs/main/urls.yaml"
CONFIG_REMOTE_PATH = "https://raw.githubusercontent.com/dchirst/geospatialjobs/main/config.yaml"
URL_PATH = pathlib.Path(URLWATCH_DIR, "urls.yaml")
CONFIG_PATH = pathlib.Path(URLWATCH_DIR, "config.yaml")
@stub.function(schedule=modal.Cron("0 7 * * 2,5"),
image=urlwatch_image,
shared_volumes={URLWATCH_DIR: volume},
secret=modal.Secret.from_name("gmail"))
def run_geospatial_urlwatch():
with urllib.request.urlopen(urllib.request.Request(URL_REMOTE_PATH)) as src:
with open(URL_PATH, "w+") as dst:
dst.write(src.read().decode("utf-8"))
with urllib.request.urlopen(urllib.request.Request(CONFIG_REMOTE_PATH)) as src:
with open(CONFIG_PATH, "w+") as dst:
config_data = re.sub(r'\$\{password\}',
environ.get("EMAIL_PASSWORD"),
src.read().decode("utf-8"))
config_data = re.sub(r'\$\{to_email\}',
environ.get("TO_EMAIL"),
config_data)
print(config_data)
dst.write(config_data)
subprocess.run(["webchanges", "--urls", URL_PATH, "--config", CONFIG_PATH, "--hooks", "hooks.py", "--cache", DB_PATH])
if __name__ == '__main__':
with stub.run():
run_geospatial_urlwatch.call()