Add1E commited on
Commit
7c3a5b9
·
verified ·
1 Parent(s): d109296

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -5
app.py CHANGED
@@ -1,9 +1,20 @@
1
  from pytrends.request import TrendReq
2
  import streamlit as st
3
  import pandas as pd
 
 
 
 
4
  import hmac
5
  import os
6
 
 
 
 
 
 
 
 
7
 
8
  def convert_into_pd(req_json):
9
  wanted_keys = ["entityNames", "title"]
@@ -16,6 +27,7 @@ def convert_into_pd(req_json):
16
  def find_details(req_json, gewünschter_titel):
17
  gewünschte_details = []
18
  for trend_info in req_json:
 
19
  if trend_info['title'] == gewünschter_titel:
20
 
21
  for article in trend_info['articles']:
@@ -30,6 +42,20 @@ def find_details(req_json, gewünschter_titel):
30
  gewünschte_details.append(article_details)
31
  return gewünschte_details
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
 
35
  def display_articles_for_category(category):
@@ -40,6 +66,13 @@ def display_articles_for_category(category):
40
  for count2, url in enumerate(articles, start=1):
41
  st.markdown(f"{count2}• {url['articleTitle']} [Go To →]({url['url']})")
42
 
 
 
 
 
 
 
 
43
 
44
  categories = {
45
  "Gesundheit": "m",
@@ -51,6 +84,7 @@ categories = {
51
  "Technik": "t",
52
  }
53
 
 
54
  def check_password():
55
  """Returns `True` if the user had the correct password."""
56
 
@@ -82,14 +116,62 @@ pytrend = TrendReq(hl='de-AT', tz=360, timeout=(10,50))
82
  real_trending_searches = {}
83
  base_data = {}
84
 
 
85
  for category_name, category_code in categories.items():
86
  base = pytrend.realtime_trending_searches(pn='AT', cat=category_code, count=75)
87
  base_data[category_name] = base
88
  real_trending_searches[category_name] = convert_into_pd(base)
89
 
90
-
91
- choices_list = list(real_trending_searches.keys())
92
- auswahl = st.selectbox("Select Ressort", choices_list)
93
-
94
- display_articles_for_category(auswahl)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
 
1
  from pytrends.request import TrendReq
2
  import streamlit as st
3
  import pandas as pd
4
+ import xml.etree.ElementTree as ET
5
+ import requests
6
+ from datetime import datetime
7
+ import pytz
8
  import hmac
9
  import os
10
 
11
+ feed_url1 = 'https://trends.google.de/trends/trendingsearches/daily/rss?geo=AT'
12
+
13
+ def parse_url(url):
14
+ response = requests.get(url)
15
+
16
+ root = ET.fromstring(response.content)
17
+ return root
18
 
19
  def convert_into_pd(req_json):
20
  wanted_keys = ["entityNames", "title"]
 
27
  def find_details(req_json, gewünschter_titel):
28
  gewünschte_details = []
29
  for trend_info in req_json:
30
+ st.code(trend_info)
31
  if trend_info['title'] == gewünschter_titel:
32
 
33
  for article in trend_info['articles']:
 
42
  gewünschte_details.append(article_details)
43
  return gewünschte_details
44
 
45
+ def find_details2(req_json):
46
+ gewünschte_details = []
47
+
48
+ for article in req_json:
49
+ article_details = {
50
+ 'url': article['url'],
51
+ 'snippet': article['snippet'],
52
+ 'articleTitle': article['title'],
53
+ #'time': article['time']
54
+ }
55
+
56
+ gewünschte_details.append(article_details)
57
+ return gewünschte_details
58
+
59
 
60
 
61
  def display_articles_for_category(category):
 
66
  for count2, url in enumerate(articles, start=1):
67
  st.markdown(f"{count2}• {url['articleTitle']} [Go To →]({url['url']})")
68
 
69
+ def display_articles_for_today(count, index):
70
+ with st.expander(f"{count+1}• {index['title']['query']} | Generated Traffic: {index['formattedTraffic']}"):
71
+ articles = find_details2(index['articles'])
72
+ for count2, url in enumerate(articles, start=1):
73
+ st.markdown(f"{count2}• {url['articleTitle']} [Go To →]({url['url']})")
74
+ #st.markdown(f"{count}• {index} [Go To →])")
75
+
76
 
77
  categories = {
78
  "Gesundheit": "m",
 
84
  "Technik": "t",
85
  }
86
 
87
+
88
  def check_password():
89
  """Returns `True` if the user had the correct password."""
90
 
 
116
  real_trending_searches = {}
117
  base_data = {}
118
 
119
+
120
  for category_name, category_code in categories.items():
121
  base = pytrend.realtime_trending_searches(pn='AT', cat=category_code, count=75)
122
  base_data[category_name] = base
123
  real_trending_searches[category_name] = convert_into_pd(base)
124
 
125
+ if 'selected_option' not in st.session_state:
126
+ st.session_state['selected_option'] = "default_value" # You can set a default value as needed
127
+
128
+ # Now, you can safely use st.session_state['selected_option']
129
+ selected_option = st.sidebar.radio("Choose an option", ["Realzeit Anfragen", "Tagesaktuelle Anfragen", "Trending Searches Yesterday"])
130
+
131
+ if selected_option == "Tagesaktuelle Anfragen":
132
+ today = pytrend.today_searches(pn="AT")
133
+ #trending_searches = pytrend.trending_searches(pn="austria")
134
+ for count, index in enumerate(today, start=0):
135
+ display_articles_for_today(count, index)
136
+ elif selected_option == "Realzeit Anfragen":
137
+ choices_list = list(real_trending_searches.keys())
138
+ auswahl = st.selectbox("Select Ressort", choices_list)
139
+
140
+ display_articles_for_category(auswahl)
141
+ elif selected_option == "Trending Searches Yesterday":
142
+ # trending_searches = pytrend.trending_searches(pn="austria")
143
+ # st.code(trending_searches)
144
+ timezone = 'Europe/Vienna'
145
+ today = datetime.now(pytz.timezone(timezone)).date()
146
+ feed = parse_url(feed_url1)
147
+ entries = []
148
+ ns = {'ht': 'https://trends.google.de/trends/trendingsearches/daily'} # Define namespace
149
+ for item in feed.findall('.//item'):
150
+ pubDate = datetime.strptime(item.find('pubDate').text, '%a, %d %b %Y %H:%M:%S %z').date()
151
+ # Filter: Überspringe, wenn pubDate heute ist
152
+ if pubDate == today:
153
+ continue
154
+ entry = {
155
+ 'title': item.find('title').text,
156
+ 'pubDate': item.find('pubDate').text,
157
+ 'approx_traffic': item.find('ht:approx_traffic', ns).text if item.find('ht:approx_traffic', ns) is not None else None,
158
+ 'news_items': []
159
+ }
160
+ for news_item in item.findall('ht:news_item', ns):
161
+ news_details = {
162
+ 'title': news_item.find('ht:news_item_title', ns).text,
163
+ 'snippet': news_item.find('ht:news_item_snippet', ns).text,
164
+ 'url': news_item.find('ht:news_item_url', ns).text,
165
+ 'source': news_item.find('ht:news_item_source', ns).text
166
+ }
167
+ entry['news_items'].append(news_details)
168
+ entries.append(entry)
169
+ count = 1
170
+ for entry in entries:
171
+ with st.expander(f"{count}• {entry['title']} | Generated Traffic: {entry['approx_traffic']}"):
172
+ #st.code(entry)
173
+ st.write(f"Veröffentlichungsdatum : {entry['pubDate']}")
174
+ for count2, link in enumerate(entry['news_items'], start=1):
175
+ st.markdown(f"{count2}• {link['title']} [Go To →]({link['url']})")
176
+ count += 1
177