diff --git a/web_programming/Global Events Tracker.py b/web_programming/Global Events Tracker.py new file mode 100644 index 000000000000..b78cc116d7ad --- /dev/null +++ b/web_programming/Global Events Tracker.py @@ -0,0 +1,136 @@ +from flask import Flask, render_template_string +import requests + +app = Flask(__name__) + +# Public APIs for real-time global data (you can replace or add more as needed) +COVID_API_URL = ( + "https://disease.sh/v3/covid-19/countries" # Get COVID-19 stats by country +) +NEWS_API_URL = "https://gnews.io/api/v4/top-headlines?token=YOUR_API_KEY&lang=en" # Replace with your API key + +# Base HTML template +BASE_TEMPLATE = """ + + + + + + Global Events Tracker + + + +
+

Global Events Tracker

+ +
+
+ {% block content %}{% endblock %} +
+ + +""" + +# Index (Home) Template +INDEX_TEMPLATE = """ +{% extends "base.html" %} +{% block content %} +

Welcome to the Global Events Tracker

+

Track live global events like COVID-19 stats and global news headlines from reliable sources.

+ +{% endblock %} +""" + +# COVID-19 Stats Template +COVID_TEMPLATE = """ +{% extends "base.html" %} +{% block content %} +

Global COVID-19 Stats

+

Real-time data from the disease.sh API

+ +{% endblock %} +""" + +# News Template +NEWS_TEMPLATE = """ +{% extends "base.html" %} +{% block content %} +

Latest Global News

+

Real-time news fetched from the GNews API

+ +{% endblock %} +""" + + +@app.route("/") +def index(): + return render_template_string(BASE_TEMPLATE + INDEX_TEMPLATE) + + +@app.route("/covid-stats") +def covid_stats(): + # Fetch COVID-19 stats from the public API + try: + response = requests.get(COVID_API_URL) + covid_data = response.json() if response.status_code == 200 else [] + except requests.exceptions.RequestException as e: + covid_data = [] + print(f"Error fetching COVID data: {e}") + + return render_template_string(BASE_TEMPLATE + COVID_TEMPLATE, covid_data=covid_data) + + +@app.route("/news") +def global_news(): + # Fetch global news using GNews API + try: + response = requests.get(NEWS_API_URL) + news_data = ( + response.json().get("articles", []) if response.status_code == 200 else [] + ) + except requests.exceptions.RequestException as e: + news_data = [] + print(f"Error fetching news data: {e}") + + return render_template_string(BASE_TEMPLATE + NEWS_TEMPLATE, news_data=news_data) + + +if __name__ == "__main__": + app.run(debug=True)