Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- main.py +28 -0
- requirements.txt +3 -0
- scraper.py +25 -0
- static/style.css +113 -0
- templates/index.html +72 -0
main.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import feedparser
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
NEWS_FEEDS = {
|
| 7 |
+
'BBC': 'http://feeds.bbci.co.uk/news/rss.xml',
|
| 8 |
+
'CNN': 'http://rss.cnn.com/rss/edition.rss',
|
| 9 |
+
'Times of India': 'https://timesofindia.indiatimes.com/rssfeedstopstories.cms'
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 13 |
+
def index():
|
| 14 |
+
selected_source = None
|
| 15 |
+
articles = []
|
| 16 |
+
|
| 17 |
+
if request.method == 'POST':
|
| 18 |
+
selected_source = request.form.get('source')
|
| 19 |
+
if selected_source in NEWS_FEEDS:
|
| 20 |
+
feed = feedparser.parse(NEWS_FEEDS[selected_source])
|
| 21 |
+
# take top 5 news items
|
| 22 |
+
articles = feed.entries[:5]
|
| 23 |
+
|
| 24 |
+
return render_template('index.html', sources=NEWS_FEEDS.keys(),
|
| 25 |
+
selected_source=selected_source, articles=articles)
|
| 26 |
+
|
| 27 |
+
if __name__ == '__main__':
|
| 28 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
requests
|
| 3 |
+
beautifulsoup4
|
scraper.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
|
| 4 |
+
def get_headings(url):
|
| 5 |
+
response = requests.get(url)
|
| 6 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 7 |
+
headings = [h.get_text(strip=True) for h in soup.find_all(['h2', 'h3'])[:10]]
|
| 8 |
+
return headings
|
| 9 |
+
|
| 10 |
+
def scrape_news(url, heading=None):
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 13 |
+
articles = soup.find_all('article')[:6]
|
| 14 |
+
news = []
|
| 15 |
+
for art in articles:
|
| 16 |
+
title = art.get_text(strip=True)
|
| 17 |
+
link = art.find('a')['href'] if art.find('a') else url
|
| 18 |
+
image = art.find('img')['src'] if art.find('img') else ''
|
| 19 |
+
news.append({
|
| 20 |
+
'title': title,
|
| 21 |
+
'summary': f'Brief summary about {title[:40]}...',
|
| 22 |
+
'link': link,
|
| 23 |
+
'image': image
|
| 24 |
+
})
|
| 25 |
+
return news
|
static/style.css
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
body {
|
| 2 |
+
font-family: 'Poppins', sans-serif;
|
| 3 |
+
background: radial-gradient(circle at top left, #0f2027, #203a43, #2c5364);
|
| 4 |
+
color: #fff;
|
| 5 |
+
margin: 0;
|
| 6 |
+
padding: 0;
|
| 7 |
+
min-height: 100vh;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
header {
|
| 11 |
+
display: flex;
|
| 12 |
+
align-items: center;
|
| 13 |
+
justify-content: center;
|
| 14 |
+
padding: 1rem;
|
| 15 |
+
background: rgba(0, 0, 0, 0.5);
|
| 16 |
+
box-shadow: 0 4px 10px rgba(0, 255, 255, 0.3);
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
.logo {
|
| 20 |
+
width: 50px;
|
| 21 |
+
margin-right: 10px;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
.container {
|
| 25 |
+
max-width: 900px;
|
| 26 |
+
margin: 2rem auto;
|
| 27 |
+
padding: 1rem;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
.input-card {
|
| 31 |
+
background: rgba(255, 255, 255, 0.1);
|
| 32 |
+
border-radius: 15px;
|
| 33 |
+
padding: 1rem;
|
| 34 |
+
backdrop-filter: blur(10px);
|
| 35 |
+
box-shadow: 0 4px 15px rgba(0, 255, 255, 0.2);
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
select,
|
| 39 |
+
button {
|
| 40 |
+
width: 100%;
|
| 41 |
+
padding: 10px;
|
| 42 |
+
margin: 10px 0;
|
| 43 |
+
border: none;
|
| 44 |
+
border-radius: 10px;
|
| 45 |
+
font-size: 1rem;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
button {
|
| 49 |
+
background: cyan;
|
| 50 |
+
color: black;
|
| 51 |
+
cursor: pointer;
|
| 52 |
+
transition: all 0.3s;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
button:hover {
|
| 56 |
+
background: #00ffcc;
|
| 57 |
+
transform: scale(1.05);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
.news-results {
|
| 61 |
+
display: grid;
|
| 62 |
+
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
| 63 |
+
gap: 1rem;
|
| 64 |
+
margin-top: 2rem;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
.news-card {
|
| 68 |
+
background: rgba(255, 255, 255, 0.1);
|
| 69 |
+
border-radius: 15px;
|
| 70 |
+
padding: 1rem;
|
| 71 |
+
cursor: pointer;
|
| 72 |
+
transition: transform 0.3s;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
.news-card:hover {
|
| 76 |
+
transform: scale(1.05);
|
| 77 |
+
box-shadow: 0 0 15px cyan;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.popup {
|
| 81 |
+
position: fixed;
|
| 82 |
+
inset: 0;
|
| 83 |
+
background: rgba(0, 0, 0, 0.8);
|
| 84 |
+
display: flex;
|
| 85 |
+
align-items: center;
|
| 86 |
+
justify-content: center;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
.popup.hidden {
|
| 90 |
+
display: none;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.popup-content {
|
| 94 |
+
background: #111;
|
| 95 |
+
border-radius: 15px;
|
| 96 |
+
padding: 2rem;
|
| 97 |
+
max-width: 600px;
|
| 98 |
+
text-align: center;
|
| 99 |
+
color: white;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
.popup-content img {
|
| 103 |
+
width: 100%;
|
| 104 |
+
border-radius: 10px;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
#closePopup {
|
| 108 |
+
position: absolute;
|
| 109 |
+
top: 15px;
|
| 110 |
+
right: 25px;
|
| 111 |
+
font-size: 1.5rem;
|
| 112 |
+
cursor: pointer;
|
| 113 |
+
}
|
templates/index.html
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
|
| 4 |
+
<head>
|
| 5 |
+
<title>News Agent</title>
|
| 6 |
+
<style>
|
| 7 |
+
body {
|
| 8 |
+
font-family: Arial, sans-serif;
|
| 9 |
+
background-color: #f8f9fa;
|
| 10 |
+
text-align: center;
|
| 11 |
+
padding: 30px;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
select,
|
| 15 |
+
button {
|
| 16 |
+
padding: 10px;
|
| 17 |
+
margin: 10px;
|
| 18 |
+
font-size: 16px;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
.news-container {
|
| 22 |
+
margin-top: 30px;
|
| 23 |
+
display: flex;
|
| 24 |
+
flex-direction: column;
|
| 25 |
+
align-items: center;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
.news-card {
|
| 29 |
+
background-color: white;
|
| 30 |
+
border-radius: 10px;
|
| 31 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 32 |
+
margin: 10px;
|
| 33 |
+
padding: 20px;
|
| 34 |
+
width: 60%;
|
| 35 |
+
text-align: left;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
a {
|
| 39 |
+
text-decoration: none;
|
| 40 |
+
color: #007bff;
|
| 41 |
+
font-weight: bold;
|
| 42 |
+
}
|
| 43 |
+
</style>
|
| 44 |
+
</head>
|
| 45 |
+
|
| 46 |
+
<body>
|
| 47 |
+
<h1>🗞 News Agent</h1>
|
| 48 |
+
|
| 49 |
+
<form method="POST">
|
| 50 |
+
<label for="source">Select News Source:</label>
|
| 51 |
+
<select name="source" id="source">
|
| 52 |
+
{% for source in sources %}
|
| 53 |
+
<option value="{{ source }}" {% if selected_source == source %}selected{% endif %}>{{ source }}</option>
|
| 54 |
+
{% endfor %}
|
| 55 |
+
</select>
|
| 56 |
+
<button type="submit">Show News</button>
|
| 57 |
+
</form>
|
| 58 |
+
|
| 59 |
+
<div class="news-container">
|
| 60 |
+
{% if articles %} {% for article in articles %}
|
| 61 |
+
<div class="news-card">
|
| 62 |
+
<h2>{{ article.title }}</h2>
|
| 63 |
+
<p>{{ article.summary | safe }}</p>
|
| 64 |
+
<a href="{{ article.link }}" target="_blank">Read more</a>
|
| 65 |
+
</div>
|
| 66 |
+
{% endfor %} {% elif selected_source %}
|
| 67 |
+
<p>No news found for {{ selected_source }} 😢</p>
|
| 68 |
+
{% endif %}
|
| 69 |
+
</div>
|
| 70 |
+
</body>
|
| 71 |
+
|
| 72 |
+
</html>
|