Add1E commited on
Commit
b8c802d
·
verified ·
1 Parent(s): 197f22e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -125
app.py CHANGED
@@ -2,15 +2,15 @@ import streamlit as st
2
  import pandas as pd
3
  import xml.etree.ElementTree as ET
4
  import requests
5
- from datetime import datetime
6
- import pytz
 
7
  import hmac
8
  import os
9
  import time
10
  from PIL import Image
11
- from trend_crawl import get_trends
12
 
13
- import re
14
 
15
  TREND_TOPICS = {
16
  1: "Autos and Vehicles",
@@ -34,12 +34,29 @@ TREND_TOPICS = {
34
  19: "Travel and Transportation"
35
  }
36
 
 
 
 
 
 
 
 
37
  def parse_url(url):
38
  response = requests.get(url)
39
 
40
  root = ET.fromstring(response.content)
41
  return root
42
 
 
 
 
 
 
 
 
 
 
 
43
  def convert_into_dict(req_json):
44
  result = {}
45
 
@@ -117,69 +134,15 @@ def find_details2(req_json):
117
  if 'reset' not in st.session_state:
118
  st.session_state.reset = False
119
 
120
- def display_trends_from_yesterday():
121
- checkbox_statuses = {}
122
- urls = []
123
-
124
- timezone = 'Europe/Vienna'
125
- today = datetime.now(pytz.timezone(timezone)).date()
126
- feed = parse_url(feed_url1)
127
- entries = []
128
- ns = {'ht': 'https://trends.google.de/trends/trendingsearches/daily'} # Define namespace
129
- for item in feed.findall('.//item'):
130
- pubDate = datetime.strptime(item.find('pubDate').text, '%a, %d %b %Y %H:%M:%S %z').date()
131
- # Filter: Überspringe, wenn pubDate heute ist
132
- if pubDate == today:
133
- continue
134
- entry = {
135
- 'title': item.find('title').text,
136
- 'pubDate': item.find('pubDate').text,
137
- 'approx_traffic': item.find('ht:approx_traffic', ns).text if item.find('ht:approx_traffic', ns) is not None else None,
138
- 'news_items': []
139
- }
140
- for news_item in item.findall('ht:news_item', ns):
141
- news_details = {
142
- 'title': news_item.find('ht:news_item_title', ns).text,
143
- 'snippet': news_item.find('ht:news_item_snippet', ns).text,
144
- 'url': news_item.find('ht:news_item_url', ns).text,
145
- 'source': news_item.find('ht:news_item_source', ns).text
146
- }
147
- entry['news_items'].append(news_details)
148
- entries.append(entry)
149
-
150
- count = 1
151
- for entry in entries:
152
- with st.expander(f"{count}• {entry['title']} | Generated Traffic: {entry['approx_traffic']}"):
153
- st.write(f"Veröffentlichungsdatum : {entry['pubDate']}")
154
- for count2, link in enumerate(entry['news_items'], start=1):
155
- checkbox_label = f"yesterday_{count}_{count2}"
156
- if st.session_state.reset:
157
- st.session_state[checkbox_label] = False
158
- checkbox_statuses[checkbox_label] = st.session_state.get(checkbox_label, False)
159
- checkbox_statuses[checkbox_label] = st.checkbox(
160
- f"{count2}• {link['title']} | {link['source']} | [Go To →]({link['url']})",
161
- value=checkbox_statuses[checkbox_label],
162
- key=checkbox_label
163
- )
164
- if checkbox_statuses[checkbox_label]:
165
- urls.append(link['url'])
166
-
167
- # Button am Ende des Expanders
168
- base_url = os.getenv("url")
169
- query_params = "&".join([f"article-links[]={url}" for url in urls])
170
- full_url = f"{base_url}{query_params}"
171
- st.link_button("Open All Links" , url= full_url)
172
- count += 1
173
-
174
-
175
 
176
  # Function to display articles for a specific category
177
  def display_articles_for_category(category):
178
  checkbox_statuses = {}
179
  urls = []
180
 
181
- trending_data = st.session_state["real_trending_searches"][selected_country][selected_country][category]
182
  #st.write(trending_data)
 
183
  if st.session_state.get("reset", False):
184
  for idx, (topic, data) in enumerate(trending_data.items()):
185
  for article_index, _ in enumerate(data["articles"]):
@@ -188,8 +151,12 @@ def display_articles_for_category(category):
188
 
189
  for idx, (topic, data) in enumerate(trending_data.items()):
190
 
 
191
 
192
- with st.expander(f"{idx + 1}• {topic} | Generated Traffic: {data['searchQueries']}"):
 
 
 
193
 
194
  for article_index, article in enumerate(data["articles"], start=1):
195
  checkbox_label = f"{category}_{idx}_{article_index}"
@@ -201,8 +168,12 @@ def display_articles_for_category(category):
201
 
202
  disabled = (not current_value) and (sum(checkbox_statuses.values()) >= MAX_CHECKED)
203
 
 
 
 
 
204
  checkbox_statuses[checkbox_label] = st.checkbox(
205
- f"{article_index}• {article['title']} | [Go To →]({article['href']})",
206
  value=current_value,
207
  key=checkbox_label,
208
  disabled=disabled
@@ -216,39 +187,6 @@ def display_articles_for_category(category):
216
  full_url = f"{base_url}{query_params}"
217
  st.link_button("Open All Links", url=full_url)
218
 
219
- # Funktion zum Rendern von Artikeln für heute
220
- def display_articles_for_today(count, index):
221
- checkbox_statuses = {}
222
- urls = []
223
- # Dictionary zur Verwaltung des Status jeder Checkbox
224
- for count2, url in enumerate(index['articles'], start=1):
225
- checkbox_label = f"today_{count}_{count2}"
226
- if st.session_state.reset:
227
- st.session_state[checkbox_label] = False
228
- checkbox_statuses[checkbox_label] = st.session_state.get(checkbox_label, False)
229
-
230
-
231
- with st.expander(f"{count+1}• {index['title']['query']} | Generated Traffic: {index['formattedTraffic']}"):
232
- articles = find_details2(index['articles'])
233
- for count2, url in enumerate(articles, start=1):
234
- checkbox_label = f"today_{count}_{count2}"
235
- disabled = not checkbox_statuses[checkbox_label] and sum(checkbox_statuses.values()) >= MAX_CHECKED
236
- checkbox_statuses[checkbox_label] = st.checkbox(
237
- f"{count2}• {url['articleTitle']} | {url['source']} | [Go To →]({url['url']})",
238
- value=checkbox_statuses[checkbox_label],
239
- key=checkbox_label,
240
- disabled=disabled
241
- )
242
- if checkbox_statuses[checkbox_label]:
243
- urls.append(url['url'])
244
-
245
- # Button am Ende des Expanders
246
- base_url = os.getenv("url")
247
- query_params = "&".join([f"article-links[]={url}" for url in urls])
248
- full_url = f"{base_url}{query_params}"
249
- st.link_button("Open All Links" , url= full_url)
250
-
251
-
252
  country_list = {
253
  "Germamy" : "DE",
254
  "Austria" : "AT"
@@ -259,16 +197,24 @@ if 'base_load_finished' not in st.session_state:
259
  st.session_state["base_data"] = {}
260
  st.session_state["pn"] = "AT"
261
  if 'base_load_finished' not in st.session_state or st.session_state.reset:
 
262
  with st.spinner("Loading Trends"):
263
  st.session_state["today"] = {}
264
  st.session_state["base"] = {}
265
  for country_name, pn_option in country_list.items():
266
  st.session_state["base_data"][pn_option] = {}
267
  st.session_state["real_trending_searches"][pn_option] = {}
268
- print("Load Start")
269
- st.session_state["real_trending_searches"][pn_option] = get_trends(pn_option)
270
-
 
 
 
 
 
 
271
  st.session_state["base_load_finished"]= True
 
272
 
273
 
274
  MAX_CHECKED = 3
@@ -328,42 +274,60 @@ fixed_order = [
328
  if 'selected_option' not in st.session_state:
329
  st.session_state['selected_option'] = "default_value"
330
 
 
331
  img = Image.open(r"heute_tensora.png")
332
  st.sidebar.image(img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
 
334
  # Selectbox to choose a country
335
  selected_country = st.sidebar.selectbox("Choose a Country", ["AT", "DE"])
336
- feed_url1 = f'https://trends.google.de/trends/trendingsearches/daily/rss?geo={selected_country}'
337
 
338
- # Button to trigger actions
339
- if st.sidebar.button("Change Country"):
340
- if selected_country == "AT":
341
- st.session_state["pn"] = selected_country
342
- elif selected_country == "DE":
343
- st.session_state["pn"] = selected_country
344
 
345
- selected_option = st.sidebar.radio("Choose an option", ["Realzeit Anfragen", "Trends von Gestern"])#, "Tagesaktuelle Anfragen"
346
  st.warning("Die aufgelisteten Keywörter für erhöhte Reichweite in den Überschriften verwenden")
347
 
348
- #if selected_option == "Tagesaktuelle Anfragen":
349
- #
350
- # for count, index in enumerate(st.session_state["today"][selected_country], start=0):
351
- # try:
352
- # display_articles_for_today(count, index)
353
- # except Exception as e:
354
- # st.code(e)
355
- # continue
356
- if selected_option == "Realzeit Anfragen":
357
- available_topics = st.session_state["real_trending_searches"][selected_country][selected_country].keys()
358
- filtered_topics = [topic for topic in TREND_TOPICS.values() if topic in available_topics]
359
- all_topics = ["All categories"] + filtered_topics
360
- auswahl = st.selectbox("Select Ressort", all_topics, index=0)
361
-
362
- display_articles_for_category(auswahl)
363
-
364
- elif selected_option == "Trends von Gestern":
365
- st.error("Aktuell nicht Erreichbar")
366
- #display_trends_from_yesterday()
 
 
367
 
368
  if st.session_state.reset:
369
  st.session_state["reset"] = False
 
2
  import pandas as pd
3
  import xml.etree.ElementTree as ET
4
  import requests
5
+ from datetime import datetime, timedelta
6
+ from streamlit_autorefresh import st_autorefresh
7
+ import json
8
  import hmac
9
  import os
10
  import time
11
  from PIL import Image
12
+ from azure.cosmos import CosmosClient
13
 
 
14
 
15
  TREND_TOPICS = {
16
  1: "Autos and Vehicles",
 
34
  19: "Travel and Transportation"
35
  }
36
 
37
+
38
+ client = CosmosClient(os.getenv("COSMOS_ENDPOINT"), os.getenv("COSMOS_KEY"))
39
+ database = client.get_database_client(os.getenv("DATABASE_NAME"))
40
+ container = database.get_container_client(os.getenv("CONTAINER_NAME"))
41
+
42
+
43
+
44
  def parse_url(url):
45
  response = requests.get(url)
46
 
47
  root = ET.fromstring(response.content)
48
  return root
49
 
50
+
51
+ def get_latest_trend(country: str):
52
+ """
53
+ Gibt das neueste Dokument für das angegebene Land zurück,
54
+ basierend auf dem timestamp-Feld.
55
+ """
56
+ query = f"SELECT * FROM c WHERE c.country = '{country}' ORDER BY c.timestamp DESC"
57
+ items = list(container.query_items(query=query, enable_cross_partition_query=True))
58
+ return items[0] if items else None
59
+
60
  def convert_into_dict(req_json):
61
  result = {}
62
 
 
134
  if 'reset' not in st.session_state:
135
  st.session_state.reset = False
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  # Function to display articles for a specific category
139
  def display_articles_for_category(category):
140
  checkbox_statuses = {}
141
  urls = []
142
 
143
+ trending_data = st.session_state["real_trending_searches"][selected_country][category]
144
  #st.write(trending_data)
145
+ active_string = " | :chart_with_upwards_trend: :green[Aktiver Trend]"
146
  if st.session_state.get("reset", False):
147
  for idx, (topic, data) in enumerate(trending_data.items()):
148
  for article_index, _ in enumerate(data["articles"]):
 
151
 
152
  for idx, (topic, data) in enumerate(trending_data.items()):
153
 
154
+ with st.expander(f"{idx + 1}• {topic}" + (active_string if not data["is_trend_finished"] else "")):
155
 
156
+ if data["related_queries"]:
157
+ st.markdown("**Related Keywords:**")
158
+ related_queries_string = f"{', '.join(data['related_queries'])}"
159
+ st.markdown(related_queries_string)
160
 
161
  for article_index, article in enumerate(data["articles"], start=1):
162
  checkbox_label = f"{category}_{idx}_{article_index}"
 
168
 
169
  disabled = (not current_value) and (sum(checkbox_statuses.values()) >= MAX_CHECKED)
170
 
171
+ dt = datetime.fromisoformat(article["time"])
172
+ formatted_dt = dt.strftime("%d.%m.%Y %H:%M")
173
+ st.markdown(f"{article_index}• {article['source']} / {formatted_dt}:")
174
+
175
  checkbox_statuses[checkbox_label] = st.checkbox(
176
+ f"{article['title']} | [Go To →]({article['href']})",
177
  value=current_value,
178
  key=checkbox_label,
179
  disabled=disabled
 
187
  full_url = f"{base_url}{query_params}"
188
  st.link_button("Open All Links", url=full_url)
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  country_list = {
191
  "Germamy" : "DE",
192
  "Austria" : "AT"
 
197
  st.session_state["base_data"] = {}
198
  st.session_state["pn"] = "AT"
199
  if 'base_load_finished' not in st.session_state or st.session_state.reset:
200
+
201
  with st.spinner("Loading Trends"):
202
  st.session_state["today"] = {}
203
  st.session_state["base"] = {}
204
  for country_name, pn_option in country_list.items():
205
  st.session_state["base_data"][pn_option] = {}
206
  st.session_state["real_trending_searches"][pn_option] = {}
207
+ with open(f"data_{pn_option}.json", "r") as file:
208
+ data = json.load(file)
209
+ try:
210
+ st.session_state["real_trending_searches"][pn_option] = get_latest_trend(pn_option).get("trends")
211
+ except:
212
+ st.warning("No data found in the Trendcrawl"
213
+ )
214
+ st.stop()
215
+ st.session_state["_"] = 0
216
  st.session_state["base_load_finished"]= True
217
+ st.session_state["start_time"] = datetime.now()
218
 
219
 
220
  MAX_CHECKED = 3
 
274
  if 'selected_option' not in st.session_state:
275
  st.session_state['selected_option'] = "default_value"
276
 
277
+
278
  img = Image.open(r"heute_tensora.png")
279
  st.sidebar.image(img)
280
+ elapsed_time = datetime.now() - st.session_state["start_time"]
281
+ if elapsed_time > timedelta(minutes=10):
282
+ st.markdown(
283
+ """
284
+ <style>
285
+ /* Zentriert den Button und vergrößert ihn */
286
+ div.stButton > button {
287
+ width: 300px;
288
+ height: 70px;
289
+ font-size: 20px;
290
+ margin: auto;
291
+ display: block;
292
+ }
293
+ </style>
294
+ """,
295
+ unsafe_allow_html=True
296
+ )
297
+
298
+ if st.button(f"Letzte Aktualisierung {st.session_state['start_time'].strftime('%H:%M')}\n- Für Update Hier Klicken"):
299
+ st.session_state["reset"] = True
300
+ st.session_state["base_load_finished"] = False
301
+ st.rerun()
302
+
303
 
304
  # Selectbox to choose a country
305
  selected_country = st.sidebar.selectbox("Choose a Country", ["AT", "DE"])
 
306
 
 
 
 
 
 
 
307
 
 
308
  st.warning("Die aufgelisteten Keywörter für erhöhte Reichweite in den Überschriften verwenden")
309
 
310
+ st.warning("Der Hinweis „Aktiver Trend“ signalisiert, dass zurzeit signifikant mehr Suchanfragen zu diesem Thema erfolgen. Bitte beachten Sie, dass auch andere Themen weiterhin von Relevanz sind.")
311
+
312
+ available_topics = st.session_state["real_trending_searches"][selected_country].keys()
313
+ filtered_topics = [topic for topic in TREND_TOPICS.values() if topic in available_topics]
314
+ all_topics = ["All categories"] + filtered_topics
315
+ auswahl = st.selectbox("Select Ressort", all_topics, index=0)
316
+
317
+ display_articles_for_category(auswahl)
318
+
319
+ if st.session_state["base_load_finished"]:
320
+ st.session_state["_"] = st_autorefresh(interval=600000, limit=5, key=f"autorefresh_{st.session_state['start_time']}")
321
+ st.markdown(
322
+ """
323
+ <style>
324
+ [data-testid="stToolbar"] {
325
+ display: none;
326
+ }
327
+ </style>
328
+ """,
329
+ unsafe_allow_html=True
330
+ )
331
 
332
  if st.session_state.reset:
333
  st.session_state["reset"] = False