Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from bs4 import BeautifulSoup as soup
|
3 |
+
from urllib.request import urlopen
|
4 |
+
from newspaper import Article
|
5 |
+
import nltk
|
6 |
+
nltk.download('punkt')
|
7 |
+
|
8 |
+
st.set_page_config(page_title='NewsARC by theaimart')
|
9 |
+
|
10 |
+
def fetch_news_search_topic(topic):
|
11 |
+
site = 'https://news.google.com/rss/search?q={}'.format(topic)
|
12 |
+
op = urlopen(site) # Open that site
|
13 |
+
rd = op.read() # read data from site
|
14 |
+
op.close() # close the object
|
15 |
+
sp_page = soup(rd, 'xml') # scrapping data from site
|
16 |
+
news_list = sp_page.find_all('item') # finding news
|
17 |
+
return news_list
|
18 |
+
|
19 |
+
def fetch_top_news():
|
20 |
+
site = 'https://news.google.com/news/rss'
|
21 |
+
op = urlopen(site) # Open that site
|
22 |
+
rd = op.read() # read data from site
|
23 |
+
op.close() # close the object
|
24 |
+
sp_page = soup(rd, 'xml') # scrapping data from site
|
25 |
+
news_list = sp_page.find_all('item') # finding news
|
26 |
+
return news_list
|
27 |
+
|
28 |
+
def fetch_category_news(topic):
|
29 |
+
site = 'https://news.google.com/news/rss/headlines/section/topic/{}'.format(topic)
|
30 |
+
op = urlopen(site) # Open that site
|
31 |
+
rd = op.read() # read data from site
|
32 |
+
op.close() # close the object
|
33 |
+
sp_page = soup(rd, 'xml') # scrapping data from site
|
34 |
+
news_list = sp_page.find_all('item') # finding news
|
35 |
+
return news_list
|
36 |
+
|
37 |
+
def display_news(list_of_news, news_quantity):
|
38 |
+
c = 0
|
39 |
+
for news in list_of_news:
|
40 |
+
c += 1
|
41 |
+
st.write('**({}) {}**'.format(c, news.title.text))
|
42 |
+
news_data = Article(news.link.text)
|
43 |
+
try:
|
44 |
+
news_data.download()
|
45 |
+
news_data.parse()
|
46 |
+
news_data.nlp()
|
47 |
+
except Exception as e:
|
48 |
+
st.error(e)
|
49 |
+
with st.expander(news.title.text):
|
50 |
+
st.markdown(
|
51 |
+
'''<h6 style='text-align: justify;'>{}"</h6>'''.format(news_data.summary),
|
52 |
+
unsafe_allow_html=True)
|
53 |
+
st.markdown("[Read more at {}...]({})".format(news.source.text, news.link.text))
|
54 |
+
st.success("Published Date: " + news.pubDate.text)
|
55 |
+
if c >= news_quantity:
|
56 |
+
break
|
57 |
+
|
58 |
+
def run():
|
59 |
+
st.title("NewsARC: A Summarised News")
|
60 |
+
|
61 |
+
category = ['--Select--', 'Trending News', 'Favourite Topics', 'Search Topic']
|
62 |
+
cat_op = st.selectbox('Select your Category', category)
|
63 |
+
if cat_op == category[0]:
|
64 |
+
st.warning('Please select Type!!')
|
65 |
+
elif cat_op == category[1]:
|
66 |
+
st.subheader("Here is the Trending News for you")
|
67 |
+
no_of_news = st.slider('Number of News:', min_value=5, max_value=25, step=1)
|
68 |
+
news_list = fetch_top_news()
|
69 |
+
display_news(news_list, no_of_news)
|
70 |
+
elif cat_op == category[2]:
|
71 |
+
av_topics = ['Choose Topic', 'WORLD', 'NATION', 'BUSINESS', 'TECHNOLOGY', 'ENTERTAINMENT', 'SPORTS', 'SCIENCE', 'HEALTH']
|
72 |
+
st.subheader("Choose your favourite Topic")
|
73 |
+
chosen_topic = st.selectbox("Choose your favourite Topic", av_topics)
|
74 |
+
if chosen_topic == av_topics[0]:
|
75 |
+
st.warning("Please Choose the Topic")
|
76 |
+
else:
|
77 |
+
no_of_news = st.slider('Number of News:', min_value=5, max_value=100, step=1)
|
78 |
+
news_list = fetch_category_news(chosen_topic)
|
79 |
+
if news_list:
|
80 |
+
st.subheader("Here are the some {} News for you".format(chosen_topic))
|
81 |
+
display_news(news_list, no_of_news)
|
82 |
+
else:
|
83 |
+
st.error("No News found for {}".format(chosen_topic))
|
84 |
+
|
85 |
+
elif cat_op == category[3]:
|
86 |
+
user_topic = st.text_input("Enter your Topic")
|
87 |
+
no_of_news = st.slider('Number of News:', min_value=5, max_value=100, step=1)
|
88 |
+
|
89 |
+
if st.button("Search") and user_topic != '':
|
90 |
+
user_topic_pr = user_topic.replace(' ', '')
|
91 |
+
news_list = fetch_news_search_topic(topic=user_topic_pr)
|
92 |
+
if news_list:
|
93 |
+
st.subheader("Here are the some {} News for you".format(user_topic.capitalize()))
|
94 |
+
display_news(news_list, no_of_news)
|
95 |
+
else:
|
96 |
+
st.error("No News found for {}".format(user_topic))
|
97 |
+
else:
|
98 |
+
st.warning("Please write Topic Name to Search")
|
99 |
+
|
100 |
+
st.write("by theaimart")
|
101 |
+
|
102 |
+
run()
|