1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-18 20:39:36 +02:00

feat: Implement CDN update process with country data download and flag management

This commit is contained in:
Sean Morley 2025-02-04 17:19:58 -05:00
parent a00d2abe0d
commit 5f344cad83
4 changed files with 163 additions and 40 deletions

View file

@ -26,6 +26,9 @@ RUN mkdir -p /var/www/html/data && cp -r /app/data/* /var/www/html/data/
# Copy Nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf
# Copy the index.html file to the Nginx serving directory
COPY index.html /usr/share/nginx/html/index.html
# Expose port 80 for Nginx
EXPOSE 80

View file

@ -1,5 +1,5 @@
services:
geojson:
cdn:
build: .
container_name: adventurelog-cdn
ports:

89
cdn/index.html Normal file
View file

@ -0,0 +1,89 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AdventureLog CDN</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<style>
body {
background: linear-gradient(to right, #1e3c72, #2a5298);
color: white;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
.hero-container {
max-width: 600px;
padding: 2rem;
border-radius: 15px;
background: rgba(0, 0, 0, 0.3);
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
}
.attribution-background {
background: rgba(42, 71, 105, 0.808);
border-radius: 15px;
padding: 1rem;
}
</style>
</head>
<body>
<div class="hero-container">
<div>
<h1 class="fw-bold">Welcome to the AdventureLog CDN</h1>
<p class="fs-5">
This is a content delivery network for the AdventureLog project. You
can browse the content by clicking the button below.
</p>
<a href="/data/" class="btn btn-light btn-lg fw-bold">Browse Content</a>
</div>
<div class="container mt-5">
<h2 class="fw-bold">About AdventureLog</h2>
<p class="fs-5">
AdventureLog is a project that aims to provide a platform for users to
log their adventures and share them with the world. The project is
developed by <a href="https://seanmorley.com">Sean Morley</a> and is
open source. View it on GitHub here:
<a href="https://github.com/seanmorley15/AdventureLog">AdventureLog</a
>.
</p>
</div>
<!-- Data Attributions -->
<div class="container mt-5">
<div class="card shadow-sm border-0 attribution-background">
<div class="card-body">
<h2 class="fw-bold text-primary">Data Attributions</h2>
<p class="fs-5 text-white">
The data provided in this CDN is sourced from the following
repositories:
</p>
<ul class="list-group">
<li class="list-group-item">
<a
href="https://flagpedia.net/"
class="text-decoration-none fw-semibold"
>
🌍 Flagpedia - Flags of the World
</a>
</li>
<li class="list-group-item">
<a
href="https://github.com/dr5hn/countries-states-cities-database/"
class="text-decoration-none fw-semibold"
>
🏙️ dr5hn/countries-states-cities-database - Country, Region,
and City Data
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</body>
</html>

View file

@ -2,55 +2,86 @@ import requests
import json
import os
# The version of the CDN, this should be updated when the CDN data is updated so the client can check if it has the latest version
ADVENTURELOG_CDN_VERSION = 'v0.0.1'
# https://github.com/dr5hn/countries-states-cities-database/tags
COUNTRY_REGION_JSON_VERSION = 'v2.5' # Should match the version stated in the settings.py file of AdventureLog
COUNTRY_REGION_JSON_VERSION = 'v2.5' # Test on past and latest versions to ensure that the data schema is consistent before updating
def makeDataDir():
"""
Creates the data directory if it doesn't exist
"""
path = os.path.join(os.path.dirname(__file__), 'data')
if not os.path.exists(path):
os.makedirs(path)
def saveCdnVersion():
"""
Saves the CDN version to a JSON file so the client can check if it has the latest version
"""
path = os.path.join(os.path.dirname(__file__), 'data', 'version.json')
with open(path, 'w') as f:
json.dump({'version': ADVENTURELOG_CDN_VERSION}, f)
print('CDN Version saved')
def downloadCountriesStateCities():
"""
Downloads the countries, states and cities data from the countries-states-cities-database repository
"""
res = requests.get(f'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/{COUNTRY_REGION_JSON_VERSION}/json/countries%2Bstates%2Bcities.json')
def downloadGeojson(iso_code=None, region_name=None):
path = os.path.join(os.path.dirname(__file__), 'data', f'countries_states_cities_{COUNTRY_REGION_JSON_VERSION}.json')
with open(path, 'w') as f:
f.write(res.text)
print('Countries, states and cities data downloaded successfully')
def saveCountryFlag(country_code, name):
"""
Download geojson data for a specific region using the Overpass API.
:param iso_code: ISO 3166-2 code for the region (e.g. "US-CT")
:param region_name: Name of the region (e.g. "Connecticut")
:return: Geojson data for the region
Downloads the flag of a country and saves it in the data/flags directory
"""
base_url = "https://overpass-api.de/api/interpreter"
# Get the directory where the script is located
script_dir = os.path.dirname(__file__)
# For standards, use the lowercase country_code
country_code = country_code.lower()
# Save the flag in the data/flags directory
flags_dir = os.path.join(os.path.dirname(__file__), 'data', 'flags')
# Ensure the ./data directory exists
data_dir = os.path.join(script_dir, "data")
os.makedirs(data_dir, exist_ok=True)
# Check if the flags directory exists, if not, create it
if not os.path.exists(flags_dir):
os.makedirs(flags_dir)
# Set the path for the geojson file
geojson_path = os.path.join(data_dir, "geojson.json")
# Check if the flag already exists in the media folder
flag_path = os.path.join(flags_dir, f'{country_code}.png')
if os.path.exists(flag_path):
# remove the flag if it already exists
os.remove(flag_path)
print(f'Flag for {country_code} ({name}) removed')
if iso_code:
query = f'[out:json];relation["boundary"="administrative"]["admin_level"="4"]["ISO3166-2"="{iso_code}"];out body;way(r);(._;>;);out geom;'
response = requests.post(base_url, data=query)
if response.ok and response.json().get("elements"):
data = response.json()
with open(geojson_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
return data
if region_name:
query = f'[out:json];relation["boundary"="administrative"]["admin_level"="4"]["name"="{region_name}"];out body;way(r);(._;>;);out geom;'
response = requests.post(base_url, data=query)
if response.ok and response.json().get("elements"):
data = response.json()
with open(geojson_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
return data
res = requests.get(f'https://flagcdn.com/h240/{country_code}.png'.lower())
if res.status_code == 200:
with open(flag_path, 'wb') as f:
f.write(res.content)
print(f'Flag for {country_code} downloaded')
else:
print(f'Error downloading flag for {country_code} ({name})')
return None # No results found
def saveCountryFlags():
"""
Downloads the flags of all countries and saves them in the data/flags directory
"""
# Load the countries data
with open(os.path.join(os.path.dirname(__file__), 'data', f'countries_states_cities_{COUNTRY_REGION_JSON_VERSION}.json')) as f:
data = json.load(f)
# Example usage
data = downloadGeojson(iso_code="US-CT", region_name="Connecticut")
for country in data:
country_code = country['iso2']
name = country['name']
saveCountryFlag(country_code, name)
if data:
print(f"Region data found and saved to {os.path.join(os.path.dirname(__file__), 'data', 'geojson.json')}!")
else:
print("No region data found.")
# Run the functions
print('Starting CDN update')
makeDataDir()
saveCdnVersion()
downloadCountriesStateCities()
saveCountryFlags()
print('CDN update complete')