From 15d59c1ad7d1bd15b4b1885fd921ca3b0f3e1d7b Mon Sep 17 00:00:00 2001
From: Akashdip Mahapatra <81384987+akashdip2001@users.noreply.github.com>
Date: Fri, 4 Oct 2024 02:37:22 +0530
Subject: [PATCH 1/2] Create Global Events Tracker.py
---
web_programming/Global Events Tracker.py | 128 +++++++++++++++++++++++
1 file changed, 128 insertions(+)
create mode 100644 web_programming/Global Events Tracker.py
diff --git a/web_programming/Global Events Tracker.py b/web_programming/Global Events Tracker.py
new file mode 100644
index 000000000000..0ca7b7d47553
--- /dev/null
+++ b/web_programming/Global Events Tracker.py
@@ -0,0 +1,128 @@
+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
+
+
+
+
+
+ {% 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)
From 24df0011df39fb54006941568f13f9f88c210215 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Thu, 3 Oct 2024 21:12:28 +0000
Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
web_programming/Global Events Tracker.py | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/web_programming/Global Events Tracker.py b/web_programming/Global Events Tracker.py
index 0ca7b7d47553..b78cc116d7ad 100644
--- a/web_programming/Global Events Tracker.py
+++ b/web_programming/Global Events Tracker.py
@@ -4,7 +4,9 @@
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
+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
@@ -96,11 +98,13 @@
{% endblock %}
"""
-@app.route('/')
+
+@app.route("/")
def index():
return render_template_string(BASE_TEMPLATE + INDEX_TEMPLATE)
-@app.route('/covid-stats')
+
+@app.route("/covid-stats")
def covid_stats():
# Fetch COVID-19 stats from the public API
try:
@@ -109,20 +113,24 @@ def covid_stats():
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')
+
+@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 []
+ 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__':
+
+if __name__ == "__main__":
app.run(debug=True)