Fetch the data and host the JSON-Server website. This project contains two parts:
- Python part: To fetch the latest astros data
- Node JS part: To host the JSON-Server website
Create a new Bing Resource instance. Take the first & secondary key and save them in .env file (See .env.example
for example).
Prerequisites: Node & Python 3.x
Install required packages
pip install -r requirements.txt
Run the fetcher
py fetcher.py
npm install
Then
npm start
flowchart TD
subgraph "External API"
A(OpenNotifyAPI) & B(Bing API)
end
B <--> C
A <--> C
subgraph fetcher.py
C[[Fetch latest astros data]] --> D[(db.json)] & E[(log.json)] --> F(Commit & push)
end
F -->|Railway build triggered| G[Deployed to Railway]
db.json contains the actual astronauts' database. log.json will store the date & time of the fetcher run.
The fetcher.py is scheduled to run automatically via GitHub action. The frequency is as defined in fetcher.yml script.
Hosted on Railway.
Update 20/10/2024: I'm no longer hosting the API in Railway to save cost. Just use the db.json from GitHub directly.
https://raw.githubusercontent.com/iqfareez/astros-api/refs/heads/master/db.json
Basic example to retrieve the data using Dart
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() async {
final response = await http.get(Uri.parse(
'https://raw.githubusercontent.com/iqfareez/astros-api/refs/heads/master/db.json'));
if (response.statusCode != 200) {
throw Exception('Failed to load astros. StatusCode ${response.statusCode}');
}
final result = jsonDecode(response.body)["data"];
final totalPeopleInSpace = result['number'];
final peoples = result['people'];
print('Total people in space is $totalPeopleInSpace:\n');
for (final people in peoples) {
print(people['name']);
}
}