Add1E commited on
Commit
6dda1aa
·
verified ·
1 Parent(s): c74a283

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pytrends.request import TrendReq
2
+ import streamlit as st
3
+ import pandas as pd
4
+
5
+
6
+
7
+ def convert_into_pd(req_json):
8
+ wanted_keys = ["entityNames", "title"]
9
+
10
+ final_json = [{ key: ts[key] for key in ts.keys() if key in wanted_keys} for ts in req_json ]
11
+
12
+ result_df = pd.DataFrame(final_json)
13
+ return result_df
14
+
15
+ def find_url(req_json, gewünschter_titel):
16
+ gewünschte_urls = []
17
+ for trend_info in req_json:
18
+ if trend_info['title'] == gewünschter_titel:
19
+
20
+ for article in trend_info['articles']:
21
+
22
+ gewünschte_urls.append(article['url'])
23
+ return gewünschte_urls
24
+
25
+ def display_articles_for_category(category):
26
+ for index, row in real_trending_searches[category].iterrows():
27
+ count = index + 1
28
+ with st.expander(f"{count}• {row['title']}"):
29
+ articles = find_url(base_data[category], row['title'])
30
+ for count2, url in enumerate(articles, start=1):
31
+ st.write(f"{count2}• {url}")
32
+
33
+ categories = {
34
+ "Gesundheit": "m",
35
+ "Alle": "all",
36
+ "Business": "b",
37
+ "Headlines": "h",
38
+ "Sport": "s",
39
+ "Entertainment": "e",
40
+ "Technik": "t",
41
+ }
42
+
43
+ pytrend = TrendReq(hl='de-AT', tz=360, timeout=(10,50))
44
+ real_trending_searches = {}
45
+ base_data = {}
46
+
47
+ for category_name, category_code in categories.items():
48
+ base = pytrend.realtime_trending_searches(pn='AT', cat=category_code, count=75)
49
+ base_data[category_name] = base
50
+ real_trending_searches[category_name] = convert_into_pd(base)
51
+
52
+
53
+ choices_list = list(real_trending_searches.keys())
54
+ auswahl = st.selectbox("Select Ressort", choices_list)
55
+
56
+ display_articles_for_category(auswahl)
57
+
58
+ def check_password():
59
+ """Returns `True` if the user had the correct password."""
60
+
61
+ def password_entered():
62
+ """Checks whether a password entered by the user is correct."""
63
+ if hmac.compare_digest(st.session_state["password"], os.environ.get("PASSWORD")):
64
+ st.session_state["password_correct"] = True
65
+ del st.session_state["password"] # Don't store the password.
66
+ else:
67
+ st.session_state["password_correct"] = False
68
+
69
+ # Return True if the password is validated.
70
+ if st.session_state.get("password_correct", False):
71
+ return True
72
+
73
+ # Show input for password.
74
+ st.text_input(
75
+ "Password", type="password", on_change=password_entered, key="password"
76
+ )
77
+ if "password_correct" in st.session_state:
78
+ st.error("😕 Password incorrect")
79
+ return False
80
+
81
+
82
+ if not check_password():
83
+ st.stop() # Do not continue if check_password is not True.