|
| 1 | +from flask import Flask, render_template_string |
| 2 | +import requests |
| 3 | + |
| 4 | +app = Flask(__name__) |
| 5 | + |
| 6 | +# Public APIs for real-time global data (you can replace or add more as needed) |
| 7 | +COVID_API_URL = "https://disease.sh/v3/covid-19/countries" # Get COVID-19 stats by country |
| 8 | +NEWS_API_URL = "https://gnews.io/api/v4/top-headlines?token=YOUR_API_KEY&lang=en" # Replace with your API key |
| 9 | + |
| 10 | +# Base HTML template |
| 11 | +BASE_TEMPLATE = """ |
| 12 | +<!DOCTYPE html> |
| 13 | +<html lang="en"> |
| 14 | +<head> |
| 15 | + <meta charset="UTF-8"> |
| 16 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 17 | + <title>Global Events Tracker</title> |
| 18 | + <style> |
| 19 | + body { font-family: Arial, sans-serif; margin: 0; padding: 0; } |
| 20 | + header { background-color: #007bff; color: white; padding: 15px; text-align: center; } |
| 21 | + nav a { margin: 0 15px; color: white; text-decoration: none; } |
| 22 | + nav { margin-bottom: 20px; } |
| 23 | + main { padding: 20px; } |
| 24 | + ul { list-style-type: none; padding: 0; } |
| 25 | + li { margin: 15px 0; padding: 10px; border: 1px solid #ddd; background-color: #f9f9f9; } |
| 26 | + .event-title { font-size: 1.2em; font-weight: bold; } |
| 27 | + .event-meta { font-size: 0.9em; color: #555; } |
| 28 | + .description { margin-top: 10px; } |
| 29 | + </style> |
| 30 | +</head> |
| 31 | +<body> |
| 32 | + <header> |
| 33 | + <h1>Global Events Tracker</h1> |
| 34 | + <nav> |
| 35 | + <a href="/">Home</a> |
| 36 | + <a href="/covid-stats">COVID-19 Stats</a> |
| 37 | + <a href="/news">Global News</a> |
| 38 | + </nav> |
| 39 | + </header> |
| 40 | + <main> |
| 41 | + {% block content %}{% endblock %} |
| 42 | + </main> |
| 43 | +</body> |
| 44 | +</html> |
| 45 | +""" |
| 46 | + |
| 47 | +# Index (Home) Template |
| 48 | +INDEX_TEMPLATE = """ |
| 49 | +{% extends "base.html" %} |
| 50 | +{% block content %} |
| 51 | +<h2>Welcome to the Global Events Tracker</h2> |
| 52 | +<p>Track live global events like COVID-19 stats and global news headlines from reliable sources.</p> |
| 53 | +<ul> |
| 54 | + <li><a href="/covid-stats">View COVID-19 Global Stats</a></li> |
| 55 | + <li><a href="/news">View Latest Global News</a></li> |
| 56 | +</ul> |
| 57 | +{% endblock %} |
| 58 | +""" |
| 59 | + |
| 60 | +# COVID-19 Stats Template |
| 61 | +COVID_TEMPLATE = """ |
| 62 | +{% extends "base.html" %} |
| 63 | +{% block content %} |
| 64 | +<h2>Global COVID-19 Stats</h2> |
| 65 | +<p>Real-time data from the disease.sh API</p> |
| 66 | +<ul> |
| 67 | + {% for country in covid_data %} |
| 68 | + <li> |
| 69 | + <div class="event-title">{{ country['country'] }}</div> |
| 70 | + <div class="event-meta">Cases: {{ country['cases'] }} | Deaths: {{ country['deaths'] }} | Recovered: {{ country['recovered'] }}</div> |
| 71 | + </li> |
| 72 | + {% else %} |
| 73 | + <li>No data available</li> |
| 74 | + {% endfor %} |
| 75 | +</ul> |
| 76 | +{% endblock %} |
| 77 | +""" |
| 78 | + |
| 79 | +# News Template |
| 80 | +NEWS_TEMPLATE = """ |
| 81 | +{% extends "base.html" %} |
| 82 | +{% block content %} |
| 83 | +<h2>Latest Global News</h2> |
| 84 | +<p>Real-time news fetched from the GNews API</p> |
| 85 | +<ul> |
| 86 | + {% for article in news_data %} |
| 87 | + <li> |
| 88 | + <div class="event-title"><a href="{{ article['url'] }}" target="_blank">{{ article['title'] }}</a></div> |
| 89 | + <div class="event-meta">Source: {{ article['source']['name'] }} | Published: {{ article['publishedAt'][:10] }}</div> |
| 90 | + <p class="description">{{ article['description'] }}</p> |
| 91 | + </li> |
| 92 | + {% else %} |
| 93 | + <li>No news articles found</li> |
| 94 | + {% endfor %} |
| 95 | +</ul> |
| 96 | +{% endblock %} |
| 97 | +""" |
| 98 | + |
| 99 | +@app.route('/') |
| 100 | +def index(): |
| 101 | + return render_template_string(BASE_TEMPLATE + INDEX_TEMPLATE) |
| 102 | + |
| 103 | +@app.route('/covid-stats') |
| 104 | +def covid_stats(): |
| 105 | + # Fetch COVID-19 stats from the public API |
| 106 | + try: |
| 107 | + response = requests.get(COVID_API_URL) |
| 108 | + covid_data = response.json() if response.status_code == 200 else [] |
| 109 | + except requests.exceptions.RequestException as e: |
| 110 | + covid_data = [] |
| 111 | + print(f"Error fetching COVID data: {e}") |
| 112 | + |
| 113 | + return render_template_string(BASE_TEMPLATE + COVID_TEMPLATE, covid_data=covid_data) |
| 114 | + |
| 115 | +@app.route('/news') |
| 116 | +def global_news(): |
| 117 | + # Fetch global news using GNews API |
| 118 | + try: |
| 119 | + response = requests.get(NEWS_API_URL) |
| 120 | + news_data = response.json().get('articles', []) if response.status_code == 200 else [] |
| 121 | + except requests.exceptions.RequestException as e: |
| 122 | + news_data = [] |
| 123 | + print(f"Error fetching news data: {e}") |
| 124 | + |
| 125 | + return render_template_string(BASE_TEMPLATE + NEWS_TEMPLATE, news_data=news_data) |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + app.run(debug=True) |
0 commit comments