webcrawler / app.py
Add1E's picture
Update app.py
8e84eed verified
raw
history blame
2.64 kB
from pytrends.request import TrendReq
import streamlit as st
import pandas as pd
import hmac
import os
def convert_into_pd(req_json):
wanted_keys = ["entityNames", "title"]
final_json = [{ key: ts[key] for key in ts.keys() if key in wanted_keys} for ts in req_json ]
result_df = pd.DataFrame(final_json)
return result_df
def find_url(req_json, gewünschter_titel):
gewünschte_urls = []
for trend_info in req_json:
if trend_info['title'] == gewünschter_titel:
for article in trend_info['articles']:
gewünschte_urls.append(article['url'])
return gewünschte_urls
def display_articles_for_category(category):
for index, row in real_trending_searches[category].iterrows():
count = index + 1
with st.expander(f"{count}{row['title']}"):
articles = find_url(base_data[category], row['title'])
for count2, url in enumerate(articles, start=1):
st.write(f"{count2}{url}")
categories = {
"Gesundheit": "m",
"Alle": "all",
"Business": "b",
"Headlines": "h",
"Sport": "s",
"Entertainment": "e",
"Technik": "t",
}
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if hmac.compare_digest(st.session_state["password"], os.environ.get("PASSWORD")):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False
# Return True if the password is validated.
if st.session_state.get("password_correct", False):
return True
# Show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
if "password_correct" in st.session_state:
st.error("😕 Password incorrect")
return False
if not check_password():
st.stop() # Do not continue if check_password is not True.
pytrend = TrendReq(hl='de-AT', tz=360, timeout=(10,50))
real_trending_searches = {}
base_data = {}
for category_name, category_code in categories.items():
base = pytrend.realtime_trending_searches(pn='AT', cat=category_code, count=75)
base_data[category_name] = base
real_trending_searches[category_name] = convert_into_pd(base)
choices_list = list(real_trending_searches.keys())
auswahl = st.selectbox("Select Ressort", choices_list)
display_articles_for_category(auswahl)