Gabesantos1007 commited on
Commit
dc9d537
·
1 Parent(s): 210a212

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +133 -0
App.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from bs4 import BeautifulSoup as soup
4
+ from urllib.request import urlopen
5
+ from newspaper import Article
6
+ import io
7
+ import nltk
8
+ nltk.download('punkt')
9
+
10
+ st.set_page_config(page_title='NewsAgora🇧🇷: Um Portal de Notícias Resumidas📰', page_icon='./Meta/newspaper.ico')
11
+
12
+
13
+ def fetch_news_search_topic(topic):
14
+ site = 'https://news.google.com/rss?hl=pt-BR&gl=BR&ceid=BR:pt'.format(topic)
15
+ op = urlopen(site) # Open that site
16
+ rd = op.read() # read data from site
17
+ op.close() # close the object
18
+ sp_page = soup(rd, 'xml') # scrapping data from site
19
+ news_list = sp_page.find_all('item') # finding news
20
+ return news_list
21
+
22
+
23
+ def fetch_top_news():
24
+ site = 'https://news.google.com/rss?hl=pt-BR&gl=BR&ceid=BR:pt'
25
+ op = urlopen(site) # Open that site
26
+ rd = op.read() # read data from site
27
+ op.close() # close the object
28
+ sp_page = soup(rd, 'xml') # scrapping data from site
29
+ news_list = sp_page.find_all('item') # finding news
30
+ return news_list
31
+
32
+
33
+ def fetch_category_news(topic):
34
+ site = 'https://news.google.com/news/rss/headlines/section/topic/{}?hl=pt-BR&gl=BR&ceid=BR:pt'.format(topic)
35
+ op = urlopen(site) # Open that site
36
+ rd = op.read() # read data from site
37
+ op.close() # close the object
38
+ sp_page = soup(rd, 'xml') # scrapping data from site
39
+ news_list = sp_page.find_all('item') # finding news
40
+ return news_list
41
+
42
+
43
+ def fetch_news_poster(poster_link):
44
+ try:
45
+ u = urlopen(poster_link)
46
+ raw_data = u.read()
47
+ image = Image.open(io.BytesIO(raw_data))
48
+ st.image(image, use_column_width=True)
49
+ except:
50
+ image = Image.open('./Meta/no_image.jpg')
51
+ st.image(image, use_column_width=True)
52
+
53
+
54
+ def display_news(list_of_news, news_quantity):
55
+ c = 0
56
+ for news in list_of_news:
57
+ c += 1
58
+ # st.markdown(f"({c})[ {news.title.text}]({news.link.text})")
59
+ st.write('**({}) {}**'.format(c, news.title.text))
60
+ news_data = Article(news.link.text)
61
+ try:
62
+ news_data.download()
63
+ news_data.parse()
64
+ news_data.nlp()
65
+ except Exception as e:
66
+ st.error(e)
67
+ fetch_news_poster(news_data.top_image)
68
+ with st.expander(news.title.text):
69
+ st.markdown(
70
+ '''<h6 style='text-align: justify;'>{}"</h6>'''.format(news_data.summary),
71
+ unsafe_allow_html=True)
72
+ st.markdown("[Read more at {}...]({})".format(news.source.text, news.link.text))
73
+ st.success("Published Date: " + news.pubDate.text)
74
+ if c >= news_quantity:
75
+ break
76
+
77
+
78
+ def run():
79
+ st.title("NewsAgora🇧🇷: Um Portal de Notícias Resumidas📰")
80
+ image = Image.open('./Meta/newspaper.png')
81
+
82
+ col1, col2, col3 = st.columns([3, 5, 3])
83
+
84
+ with col1:
85
+ st.write("")
86
+
87
+ with col2:
88
+ st.image(image, use_column_width=False)
89
+
90
+ with col3:
91
+ st.write("")
92
+ category = ['--Selecione--', 'Notícias em Alta🔥', 'Tópicos Favoritos💙', 'Buscar Tópico🔍']
93
+ cat_op = st.selectbox('Selecione a categoria', category)
94
+ if cat_op == category[0]:
95
+ st.warning('Por favor, selecione uma opção!')
96
+ elif cat_op == category[1]:
97
+ st.subheader("✅ Aqui estão as notícias em alta para você")
98
+ no_of_news = st.slider('Número de notícias:', min_value=5, max_value=15, step=1)
99
+ news_list = fetch_top_news()
100
+ display_news(news_list, no_of_news)
101
+ elif cat_op == category[2]:
102
+ av_topics = ['Choose Topic', 'WORLD', 'NATION', 'BUSINESS', 'TECHNOLOGY', 'ENTERTAINMENT', 'SPORTS', 'SCIENCE',
103
+ 'HEALTH']
104
+ st.subheader("Escolha seu tópico favorito")
105
+ chosen_topic = st.selectbox("Escolha seu tópico favorito", av_topics)
106
+ if chosen_topic == av_topics[0]:
107
+ st.warning("Por favor, escolha um tópico")
108
+ else:
109
+ no_of_news = st.slider('Número de notícias:', min_value=5, max_value=15, step=1)
110
+ news_list = fetch_category_news(chosen_topic)
111
+ if news_list:
112
+ st.subheader("✅ Aqui estão algumas notícias de {} para você".format(chosen_topic))
113
+ display_news(news_list, no_of_news)
114
+ else:
115
+ st.error("Nenhuma notícia encontrada para {}".format(chosen_topic))
116
+
117
+ elif cat_op == category[3]:
118
+ user_topic = st.text_input("Digite seu tópico🔍")
119
+ no_of_news = st.slider('Número de notícias:', min_value=5, max_value=15, step=1)
120
+
121
+ if st.button("Search") and user_topic != '':
122
+ user_topic_pr = user_topic.replace(' ', '')
123
+ news_list = fetch_news_search_topic(topic=user_topic_pr)
124
+ if news_list:
125
+ st.subheader("✅ Aqui estão algumas notícias de {} para você".format(user_topic.capitalize()))
126
+ display_news(news_list, no_of_news)
127
+ else:
128
+ st.error("Nenhuma notícia encontrada para {}".format(user_topic))
129
+ else:
130
+ st.warning("Por favor, escreva o nome do tópico para pesquisar🔍")
131
+
132
+
133
+ run()