Skip to content

Commit 88e98c8

Browse files
committed
make destinations request via function
github action runners in US cannot access smartraveller site use functions to return updated destinations (syd1)
1 parent 0cae4cf commit 88e98c8

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ Get all available Smartraveller advisories. This is updated every 3 hours.
5555
{
5656
"last_updated": "2024-10-05T04:55:06.260067+00:00",
5757
"advisories": [
58-
...
5958
{
6059
"country": {
6160
"name": "Spain",
@@ -65,7 +64,6 @@ Get all available Smartraveller advisories. This is updated every 3 hours.
6564
"level": 1,
6665
"page_url": "https://www.smartraveller.gov.au/destinations/europe/spain"
6766
},
68-
...
6967
]
7068
}
7169
```
@@ -80,15 +78,13 @@ Get all available Smartraveller destinations. This is updated every 3 hours.
8078
{
8179
"last_updated": "2024-10-05T04:55:06.260067+00:00",
8280
"destinations": {
83-
...
8481
"ES": {
8582
"country": {
8683
"name": "Spain",
8784
"alpha_2": "ES"
8885
},
8986
"page_url": "/destinations/europe/spain"
9087
},
91-
...
9288
}
9389
}
9490
```

api/api.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from flask import Flask, Response, request
44

55
from smartraveller.advisory import get_advisory
6+
from smartraveller.destination import get_destinations
67

78
app = Flask(__name__, static_url_path="")
89

@@ -36,15 +37,21 @@ def advisory():
3637
@app.route("/advisories")
3738
def advisories():
3839
with open("data/advisories.json", "r") as advisories_file:
39-
json_advisories = json.load(advisories_file)
40-
return _return_success(json_advisories)
40+
advisories = json.load(advisories_file)
41+
return _return_success(advisories)
4142

4243

4344
@app.route("/destinations")
4445
def destinations():
45-
with open("data/destinations.json", "r") as destinations_file:
46-
destinations = json.load(destinations_file)
47-
return _return_success(destinations)
46+
refetch = request.args.get("refetch") == "true"
47+
destinations = get_destinations(refetch)
48+
49+
return _return_success(
50+
{
51+
country: destination.model_dump()
52+
for country, destination in sorted(destinations.items())
53+
}
54+
)
4855

4956

5057
def _return_success(data: dict, cache_duration_min: int = 60) -> Response:

data/destinations.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"last_updated": "2024-10-05T06:11:20.824473+00:00",
2+
"last_updated": "2024-10-05T06:40:46.331869+00:00",
33
"destinations": {
44
"AE": {
55
"country": {

smartraveller/aggregate.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@
1212

1313

1414
def _save_destinations():
15-
destinations = {
16-
country: destination.model_dump()
17-
for country, destination in _fetch_destinations().items()
18-
}
19-
20-
with open(f"{OUTPUT_PREFIX}/destinations.json", "w") as outfile:
21-
json.dump(
22-
{
23-
"last_updated": datetime.now(timezone.utc).isoformat(),
24-
"destinations": dict(sorted(destinations.items())),
25-
},
26-
outfile,
27-
indent=4,
28-
)
15+
response = requests.get(f"{BASE_URL}/destinations?refetch=true")
16+
if response.status_code == 200:
17+
with open(f"{OUTPUT_PREFIX}/destinations.json", "w") as outfile:
18+
json.dump(
19+
{
20+
"last_updated": datetime.now(timezone.utc).isoformat(),
21+
"destinations": response.json(),
22+
},
23+
outfile,
24+
indent=4,
25+
)
26+
logging.info("Saved destinations")
2927

3028

3129
def _aggregate_advisories():
@@ -47,6 +45,7 @@ def _aggregate_advisories():
4745
outfile,
4846
indent=4,
4947
)
48+
logging.info("Saved advisories")
5049

5150

5251
if __name__ == "__main__":

smartraveller/destination.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@ class SmartravellerDestination(BaseModel):
3030
}
3131

3232

33-
def get_destinations() -> dict[str, SmartravellerDestination]:
34-
try:
35-
with open("data/destinations.json", "r") as destinations:
36-
destinations = json.load(destinations)["destinations"].items()
37-
return {
38-
country: SmartravellerDestination(**destination)
39-
for country, destination in destinations
40-
}
41-
except FileNotFoundError:
42-
return _fetch_destinations()
43-
44-
45-
def _fetch_destinations() -> list[dict[str, SmartravellerDestination]]:
33+
def get_destinations(refetch: bool = False) -> dict[str, SmartravellerDestination]:
34+
if not refetch:
35+
try:
36+
with open("data/destinations.json", "r") as destinations:
37+
destinations = json.load(destinations)["destinations"]
38+
except FileNotFoundError:
39+
destinations = _fetch_destinations()
40+
else:
41+
destinations = _fetch_destinations()
42+
43+
return {
44+
country: SmartravellerDestination(**destination)
45+
if isinstance(destination, dict)
46+
else destination
47+
for country, destination in destinations.items()
48+
}
49+
50+
51+
def _fetch_destinations() -> dict[str, SmartravellerDestination]:
4652
response = requests.get("https://www.smartraveller.gov.au/api/publishedpages")
4753
if response.status_code != 200:
4854
logging.error("Failed to get destinations")

0 commit comments

Comments
 (0)