File size: 2,637 Bytes
6dda1aa
 
 
49b58f9
8e84eed
6dda1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49b58f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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)