Spaces:
Running
Running
Upload 2 files
Browse files- app.py +369 -369
- trend_crawl.py +9 -1
app.py
CHANGED
@@ -1,370 +1,370 @@
|
|
1 |
-
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",
|
17 |
-
2: "Beauty and Fashion",
|
18 |
-
3: "Business and Finance",
|
19 |
-
20: "Climate",
|
20 |
-
4: "Entertainment",
|
21 |
-
5: "Food and Drink",
|
22 |
-
6: "Games",
|
23 |
-
7: "Health",
|
24 |
-
8: "Hobbies and Leisure",
|
25 |
-
9: "Jobs and Education",
|
26 |
-
10: "Law and Government",
|
27 |
-
11: "Other",
|
28 |
-
13: "Pets and Animals",
|
29 |
-
14: "Politics",
|
30 |
-
15: "Science",
|
31 |
-
16: "Shopping",
|
32 |
-
17: "Sports",
|
33 |
-
18: "Technology",
|
34 |
-
19: "Travel and Transportation"
|
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 |
-
if
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
"
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
articles
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
"
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
'
|
94 |
-
'
|
95 |
-
'
|
96 |
-
'
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
'
|
109 |
-
'
|
110 |
-
'
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
'
|
137 |
-
'
|
138 |
-
'
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
'
|
144 |
-
'
|
145 |
-
'
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
checkbox_statuses[checkbox_label] = st.
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
checkbox_statuses[checkbox_label]
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
"
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
st.session_state["
|
260 |
-
st.session_state["
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
st.session_state["
|
266 |
-
|
267 |
-
|
268 |
-
st.session_state["
|
269 |
-
st.session_state["real_trending_searches"][pn_option] =
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
if
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
"
|
308 |
-
"
|
309 |
-
"
|
310 |
-
"
|
311 |
-
"
|
312 |
-
"
|
313 |
-
"
|
314 |
-
"
|
315 |
-
"
|
316 |
-
"
|
317 |
-
"
|
318 |
-
"
|
319 |
-
"
|
320 |
-
"
|
321 |
-
"
|
322 |
-
"
|
323 |
-
"
|
324 |
-
"
|
325 |
-
"
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
st.
|
347 |
-
|
348 |
-
|
349 |
-
#
|
350 |
-
#
|
351 |
-
#
|
352 |
-
#
|
353 |
-
#
|
354 |
-
#
|
355 |
-
#
|
356 |
-
|
357 |
-
if selected_option == "Realzeit Anfragen":
|
358 |
-
available_topics = st.session_state["real_trending_searches"][selected_country][selected_country].keys()
|
359 |
-
filtered_topics = [topic for topic in TREND_TOPICS.values() if topic in available_topics]
|
360 |
-
all_topics = ["All categories"] + filtered_topics
|
361 |
-
auswahl = st.selectbox("Select Ressort", all_topics, index=0)
|
362 |
-
|
363 |
-
display_articles_for_category(auswahl)
|
364 |
-
|
365 |
-
elif selected_option == "Trends von Gestern":
|
366 |
-
st.error("Aktuell nicht Erreichbar")
|
367 |
-
#display_trends_from_yesterday()
|
368 |
-
|
369 |
-
if st.session_state.reset:
|
370 |
st.session_state["reset"] = False
|
|
|
1 |
+
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",
|
17 |
+
2: "Beauty and Fashion",
|
18 |
+
3: "Business and Finance",
|
19 |
+
20: "Climate",
|
20 |
+
4: "Entertainment",
|
21 |
+
5: "Food and Drink",
|
22 |
+
6: "Games",
|
23 |
+
7: "Health",
|
24 |
+
8: "Hobbies and Leisure",
|
25 |
+
9: "Jobs and Education",
|
26 |
+
10: "Law and Government",
|
27 |
+
11: "Other",
|
28 |
+
13: "Pets and Animals",
|
29 |
+
14: "Politics",
|
30 |
+
15: "Science",
|
31 |
+
16: "Shopping",
|
32 |
+
17: "Sports",
|
33 |
+
18: "Technology",
|
34 |
+
19: "Travel and Transportation"
|
35 |
+
}
|
36 |
+
os.environ["PASSWORD"] = "heute_123"
|
37 |
+
|
38 |
+
def parse_url(url):
|
39 |
+
response = requests.get(url)
|
40 |
+
|
41 |
+
root = ET.fromstring(response.content)
|
42 |
+
return root
|
43 |
+
|
44 |
+
def convert_into_dict(req_json):
|
45 |
+
result = {}
|
46 |
+
|
47 |
+
# Iterate over each category in the JSON data
|
48 |
+
for category, entries in req_json.items():
|
49 |
+
# Initialize the category if not already in result
|
50 |
+
if category not in result:
|
51 |
+
result[category] = {}
|
52 |
+
|
53 |
+
for entry in entries:
|
54 |
+
# Extract 'entityName' and 'searchQueries' from 'static_data'
|
55 |
+
static_data = entry.get("static_data", [])
|
56 |
+
if static_data and len(static_data[0]) >= 4:
|
57 |
+
entity_name = static_data[0][0] # First element
|
58 |
+
search_queries = static_data[0][3] # Fourth element
|
59 |
+
else:
|
60 |
+
entity_name = None
|
61 |
+
search_queries = None
|
62 |
+
|
63 |
+
# Initialize the entity under the category if not already present
|
64 |
+
if entity_name:
|
65 |
+
if entity_name not in result[category]:
|
66 |
+
result[category][entity_name] = {
|
67 |
+
"searchQueries": search_queries,
|
68 |
+
"articles": []
|
69 |
+
}
|
70 |
+
|
71 |
+
# Extract articles from 'dynamic_data'
|
72 |
+
articles = entry.get("dynamic_data", {}).get("article", [])
|
73 |
+
for article in articles:
|
74 |
+
href = article.get("href")
|
75 |
+
article_title = article.get("title")
|
76 |
+
|
77 |
+
# Append the article information to the corresponding entity's article list
|
78 |
+
result[category][entity_name]["articles"].append({
|
79 |
+
"href": href,
|
80 |
+
"title": article_title
|
81 |
+
})
|
82 |
+
|
83 |
+
return result
|
84 |
+
|
85 |
+
|
86 |
+
def find_details(req_json, gewünschter_titel):
|
87 |
+
gewünschte_details = []
|
88 |
+
for trend_info in req_json:
|
89 |
+
if trend_info['title'] == gewünschter_titel:
|
90 |
+
|
91 |
+
for article in trend_info['articles']:
|
92 |
+
article_details = {
|
93 |
+
'url': article['url'],
|
94 |
+
'snippet': article['snippet'],
|
95 |
+
'articleTitle': article['articleTitle'],
|
96 |
+
'time': article['time'],
|
97 |
+
'source' : article['source']
|
98 |
+
}
|
99 |
+
|
100 |
+
gewünschte_details.append(article_details)
|
101 |
+
return gewünschte_details
|
102 |
+
|
103 |
+
def find_details2(req_json):
|
104 |
+
gewünschte_details = []
|
105 |
+
|
106 |
+
for article in req_json:
|
107 |
+
article_details = {
|
108 |
+
'url': article['url'],
|
109 |
+
'snippet': article['snippet'],
|
110 |
+
'articleTitle': article['title'],
|
111 |
+
'source' : article['source']
|
112 |
+
|
113 |
+
}
|
114 |
+
|
115 |
+
gewünschte_details.append(article_details)
|
116 |
+
return gewünschte_details
|
117 |
+
|
118 |
+
if 'reset' not in st.session_state:
|
119 |
+
st.session_state.reset = False
|
120 |
+
|
121 |
+
def display_trends_from_yesterday():
|
122 |
+
checkbox_statuses = {}
|
123 |
+
urls = []
|
124 |
+
|
125 |
+
timezone = 'Europe/Vienna'
|
126 |
+
today = datetime.now(pytz.timezone(timezone)).date()
|
127 |
+
feed = parse_url(feed_url1)
|
128 |
+
entries = []
|
129 |
+
ns = {'ht': 'https://trends.google.de/trends/trendingsearches/daily'} # Define namespace
|
130 |
+
for item in feed.findall('.//item'):
|
131 |
+
pubDate = datetime.strptime(item.find('pubDate').text, '%a, %d %b %Y %H:%M:%S %z').date()
|
132 |
+
# Filter: Überspringe, wenn pubDate heute ist
|
133 |
+
if pubDate == today:
|
134 |
+
continue
|
135 |
+
entry = {
|
136 |
+
'title': item.find('title').text,
|
137 |
+
'pubDate': item.find('pubDate').text,
|
138 |
+
'approx_traffic': item.find('ht:approx_traffic', ns).text if item.find('ht:approx_traffic', ns) is not None else None,
|
139 |
+
'news_items': []
|
140 |
+
}
|
141 |
+
for news_item in item.findall('ht:news_item', ns):
|
142 |
+
news_details = {
|
143 |
+
'title': news_item.find('ht:news_item_title', ns).text,
|
144 |
+
'snippet': news_item.find('ht:news_item_snippet', ns).text,
|
145 |
+
'url': news_item.find('ht:news_item_url', ns).text,
|
146 |
+
'source': news_item.find('ht:news_item_source', ns).text
|
147 |
+
}
|
148 |
+
entry['news_items'].append(news_details)
|
149 |
+
entries.append(entry)
|
150 |
+
|
151 |
+
count = 1
|
152 |
+
for entry in entries:
|
153 |
+
with st.expander(f"{count}• {entry['title']} | Generated Traffic: {entry['approx_traffic']}"):
|
154 |
+
st.write(f"Veröffentlichungsdatum : {entry['pubDate']}")
|
155 |
+
for count2, link in enumerate(entry['news_items'], start=1):
|
156 |
+
checkbox_label = f"yesterday_{count}_{count2}"
|
157 |
+
if st.session_state.reset:
|
158 |
+
st.session_state[checkbox_label] = False
|
159 |
+
checkbox_statuses[checkbox_label] = st.session_state.get(checkbox_label, False)
|
160 |
+
checkbox_statuses[checkbox_label] = st.checkbox(
|
161 |
+
f"{count2}• {link['title']} | {link['source']} | [Go To →]({link['url']})",
|
162 |
+
value=checkbox_statuses[checkbox_label],
|
163 |
+
key=checkbox_label
|
164 |
+
)
|
165 |
+
if checkbox_statuses[checkbox_label]:
|
166 |
+
urls.append(link['url'])
|
167 |
+
|
168 |
+
# Button am Ende des Expanders
|
169 |
+
base_url = os.getenv("url")
|
170 |
+
query_params = "&".join([f"article-links[]={url}" for url in urls])
|
171 |
+
full_url = f"{base_url}{query_params}"
|
172 |
+
st.link_button("Open All Links" , url= full_url)
|
173 |
+
count += 1
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
# Function to display articles for a specific category
|
178 |
+
def display_articles_for_category(category):
|
179 |
+
checkbox_statuses = {}
|
180 |
+
urls = []
|
181 |
+
|
182 |
+
trending_data = st.session_state["real_trending_searches"][selected_country][selected_country][category]
|
183 |
+
#st.write(trending_data)
|
184 |
+
if st.session_state.get("reset", False):
|
185 |
+
for idx, (topic, data) in enumerate(trending_data.items()):
|
186 |
+
for article_index, _ in enumerate(data["articles"]):
|
187 |
+
checkbox_label = f"{category}_{idx}_{article_index + 1}"
|
188 |
+
st.session_state[checkbox_label] = False
|
189 |
+
|
190 |
+
for idx, (topic, data) in enumerate(trending_data.items()):
|
191 |
+
|
192 |
+
|
193 |
+
with st.expander(f"{idx + 1}• {topic} | Generated Traffic: {data['searchQueries']}"):
|
194 |
+
|
195 |
+
for article_index, article in enumerate(data["articles"], start=1):
|
196 |
+
checkbox_label = f"{category}_{idx}_{article_index}"
|
197 |
+
|
198 |
+
|
199 |
+
current_value = st.session_state.get(checkbox_label, False)
|
200 |
+
checkbox_statuses[checkbox_label] = current_value
|
201 |
+
|
202 |
+
|
203 |
+
disabled = (not current_value) and (sum(checkbox_statuses.values()) >= MAX_CHECKED)
|
204 |
+
|
205 |
+
checkbox_statuses[checkbox_label] = st.checkbox(
|
206 |
+
f"{article_index}• {article['title']} | [Go To →]({article['href']})",
|
207 |
+
value=current_value,
|
208 |
+
key=checkbox_label,
|
209 |
+
disabled=disabled
|
210 |
+
)
|
211 |
+
|
212 |
+
if checkbox_statuses[checkbox_label]:
|
213 |
+
urls.append(article["href"])
|
214 |
+
|
215 |
+
base_url = os.getenv("url", "https://example.com/?")
|
216 |
+
query_params = "&".join([f"article-links[]={u}" for u in urls])
|
217 |
+
full_url = f"{base_url}{query_params}"
|
218 |
+
st.link_button("Open All Links", url=full_url)
|
219 |
+
|
220 |
+
# Funktion zum Rendern von Artikeln für heute
|
221 |
+
def display_articles_for_today(count, index):
|
222 |
+
checkbox_statuses = {}
|
223 |
+
urls = []
|
224 |
+
# Dictionary zur Verwaltung des Status jeder Checkbox
|
225 |
+
for count2, url in enumerate(index['articles'], start=1):
|
226 |
+
checkbox_label = f"today_{count}_{count2}"
|
227 |
+
if st.session_state.reset:
|
228 |
+
st.session_state[checkbox_label] = False
|
229 |
+
checkbox_statuses[checkbox_label] = st.session_state.get(checkbox_label, False)
|
230 |
+
|
231 |
+
|
232 |
+
with st.expander(f"{count+1}• {index['title']['query']} | Generated Traffic: {index['formattedTraffic']}"):
|
233 |
+
articles = find_details2(index['articles'])
|
234 |
+
for count2, url in enumerate(articles, start=1):
|
235 |
+
checkbox_label = f"today_{count}_{count2}"
|
236 |
+
disabled = not checkbox_statuses[checkbox_label] and sum(checkbox_statuses.values()) >= MAX_CHECKED
|
237 |
+
checkbox_statuses[checkbox_label] = st.checkbox(
|
238 |
+
f"{count2}• {url['articleTitle']} | {url['source']} | [Go To →]({url['url']})",
|
239 |
+
value=checkbox_statuses[checkbox_label],
|
240 |
+
key=checkbox_label,
|
241 |
+
disabled=disabled
|
242 |
+
)
|
243 |
+
if checkbox_statuses[checkbox_label]:
|
244 |
+
urls.append(url['url'])
|
245 |
+
|
246 |
+
# Button am Ende des Expanders
|
247 |
+
base_url = os.getenv("url")
|
248 |
+
query_params = "&".join([f"article-links[]={url}" for url in urls])
|
249 |
+
full_url = f"{base_url}{query_params}"
|
250 |
+
st.link_button("Open All Links" , url= full_url)
|
251 |
+
|
252 |
+
|
253 |
+
country_list = {
|
254 |
+
"Germamy" : "DE",
|
255 |
+
"Austria" : "AT"
|
256 |
+
}
|
257 |
+
|
258 |
+
if 'base_load_finished' not in st.session_state:
|
259 |
+
st.session_state["real_trending_searches"] = {}
|
260 |
+
st.session_state["base_data"] = {}
|
261 |
+
st.session_state["pn"] = "AT"
|
262 |
+
print(st.session_state.reset)
|
263 |
+
if 'base_load_finished' not in st.session_state or st.session_state.reset:
|
264 |
+
with st.spinner("Loading Trends"):
|
265 |
+
st.session_state["today"] = {}
|
266 |
+
st.session_state["base"] = {}
|
267 |
+
for country_name, pn_option in country_list.items():
|
268 |
+
st.session_state["base_data"][pn_option] = {}
|
269 |
+
st.session_state["real_trending_searches"][pn_option] = {}
|
270 |
+
st.session_state["real_trending_searches"][pn_option] = get_trends([pn_option])
|
271 |
+
|
272 |
+
st.session_state["base_load_finished"]= True
|
273 |
+
|
274 |
+
|
275 |
+
MAX_CHECKED = 3
|
276 |
+
|
277 |
+
def check_password():
|
278 |
+
"""Returns `True` if the user had the correct password."""
|
279 |
+
|
280 |
+
def password_entered():
|
281 |
+
"""Checks whether a password entered by the user is correct."""
|
282 |
+
if hmac.compare_digest(st.session_state["password"], os.environ.get("PASSWORD")):
|
283 |
+
st.session_state["password_correct"] = True
|
284 |
+
|
285 |
+
del st.session_state["password"] # Don't store the password.
|
286 |
+
else:
|
287 |
+
st.session_state["password_correct"] = False
|
288 |
+
|
289 |
+
|
290 |
+
# Return True if the password is validated.
|
291 |
+
if st.session_state.get("password_correct", False):
|
292 |
+
return True
|
293 |
+
|
294 |
+
# Show input for password.
|
295 |
+
st.text_input(
|
296 |
+
"Password", type="password", on_change=password_entered, key="password"
|
297 |
+
)
|
298 |
+
if "password_correct" in st.session_state:
|
299 |
+
st.error("😕 Password incorrect")
|
300 |
+
return False
|
301 |
+
|
302 |
+
|
303 |
+
if not check_password():
|
304 |
+
st.stop() # Do not continue if check_password is not True.
|
305 |
+
|
306 |
+
fixed_order = [
|
307 |
+
"All categories",
|
308 |
+
"Autos and Vehicles",
|
309 |
+
"Beauty and Fashion",
|
310 |
+
"Business and Finance",
|
311 |
+
"Climate",
|
312 |
+
"Entertainment",
|
313 |
+
"Food and Drink",
|
314 |
+
"Games",
|
315 |
+
"Health",
|
316 |
+
"Hobbies and Leisure",
|
317 |
+
"Jobs and Education",
|
318 |
+
"Law and Government",
|
319 |
+
"Other",
|
320 |
+
"Pets and Animals",
|
321 |
+
"Politics",
|
322 |
+
"Science",
|
323 |
+
"Shopping",
|
324 |
+
"Sports",
|
325 |
+
"Technology",
|
326 |
+
"Travel and Transportation",
|
327 |
+
]
|
328 |
+
|
329 |
+
if 'selected_option' not in st.session_state:
|
330 |
+
st.session_state['selected_option'] = "default_value"
|
331 |
+
|
332 |
+
img = Image.open(r"heute_tensora.png")
|
333 |
+
st.sidebar.image(img)
|
334 |
+
|
335 |
+
# Selectbox to choose a country
|
336 |
+
selected_country = st.sidebar.selectbox("Choose a Country", ["AT", "DE"])
|
337 |
+
feed_url1 = f'https://trends.google.de/trends/trendingsearches/daily/rss?geo={selected_country}'
|
338 |
+
|
339 |
+
# Button to trigger actions
|
340 |
+
if st.sidebar.button("Change Country"):
|
341 |
+
if selected_country == "AT":
|
342 |
+
st.session_state["pn"] = selected_country
|
343 |
+
elif selected_country == "DE":
|
344 |
+
st.session_state["pn"] = selected_country
|
345 |
+
|
346 |
+
selected_option = st.sidebar.radio("Choose an option", ["Realzeit Anfragen", "Trends von Gestern"])#, "Tagesaktuelle Anfragen"
|
347 |
+
st.warning("Die aufgelisteten Keywörter für erhöhte Reichweite in den Überschriften verwenden")
|
348 |
+
|
349 |
+
#if selected_option == "Tagesaktuelle Anfragen":
|
350 |
+
#
|
351 |
+
# for count, index in enumerate(st.session_state["today"][selected_country], start=0):
|
352 |
+
# try:
|
353 |
+
# display_articles_for_today(count, index)
|
354 |
+
# except Exception as e:
|
355 |
+
# st.code(e)
|
356 |
+
# continue
|
357 |
+
if selected_option == "Realzeit Anfragen":
|
358 |
+
available_topics = st.session_state["real_trending_searches"][selected_country][selected_country].keys()
|
359 |
+
filtered_topics = [topic for topic in TREND_TOPICS.values() if topic in available_topics]
|
360 |
+
all_topics = ["All categories"] + filtered_topics
|
361 |
+
auswahl = st.selectbox("Select Ressort", all_topics, index=0)
|
362 |
+
|
363 |
+
display_articles_for_category(auswahl)
|
364 |
+
|
365 |
+
elif selected_option == "Trends von Gestern":
|
366 |
+
st.error("Aktuell nicht Erreichbar")
|
367 |
+
#display_trends_from_yesterday()
|
368 |
+
|
369 |
+
if st.session_state.reset:
|
370 |
st.session_state["reset"] = False
|
trend_crawl.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
from trendspy import Trends
|
|
|
|
|
2 |
tr = Trends()
|
3 |
|
4 |
TREND_TOPICS = {
|
@@ -47,20 +49,27 @@ def process_trends_for_country(country_code, trends_list):
|
|
47 |
{"title": article.title, "href": article.url}
|
48 |
for article in news
|
49 |
]
|
|
|
50 |
except Exception as e:
|
51 |
articles=[]
|
52 |
trends_json[country_code]["All categories"][topic_name] = {
|
53 |
"searchQueries": trend.volume,
|
54 |
"articles": articles,
|
|
|
55 |
}
|
56 |
trends_json[country_code][category][topic_name] = {
|
57 |
"searchQueries": trend.volume,
|
58 |
"articles": articles,
|
59 |
}
|
60 |
|
|
|
|
|
|
|
|
|
61 |
def get_trends(countries: list):
|
62 |
for country in countries:
|
63 |
trends = tr.trending_now(geo=country)
|
|
|
64 |
process_trends_for_country(country, trends)
|
65 |
all_categories = trends_json[country]["All categories"]
|
66 |
sorted_all_categories = dict(
|
@@ -69,4 +78,3 @@ def get_trends(countries: list):
|
|
69 |
trends_json[country]["All categories"] = sorted_all_categories
|
70 |
return trends_json
|
71 |
|
72 |
-
|
|
|
1 |
from trendspy import Trends
|
2 |
+
import streamlit as st
|
3 |
+
from datetime import datetime, timezone
|
4 |
tr = Trends()
|
5 |
|
6 |
TREND_TOPICS = {
|
|
|
49 |
{"title": article.title, "href": article.url}
|
50 |
for article in news
|
51 |
]
|
52 |
+
|
53 |
except Exception as e:
|
54 |
articles=[]
|
55 |
trends_json[country_code]["All categories"][topic_name] = {
|
56 |
"searchQueries": trend.volume,
|
57 |
"articles": articles,
|
58 |
+
"ended_timestamp": convert_to_datetime(trend.started_timestamp[0]),
|
59 |
}
|
60 |
trends_json[country_code][category][topic_name] = {
|
61 |
"searchQueries": trend.volume,
|
62 |
"articles": articles,
|
63 |
}
|
64 |
|
65 |
+
def convert_to_datetime( raw_time):
|
66 |
+
"""Converts time in seconds to a datetime object with UTC timezone, if it exists."""
|
67 |
+
return datetime.fromtimestamp(raw_time, tz=timezone.utc) if raw_time else None
|
68 |
+
|
69 |
def get_trends(countries: list):
|
70 |
for country in countries:
|
71 |
trends = tr.trending_now(geo=country)
|
72 |
+
|
73 |
process_trends_for_country(country, trends)
|
74 |
all_categories = trends_json[country]["All categories"]
|
75 |
sorted_all_categories = dict(
|
|
|
78 |
trends_json[country]["All categories"] = sorted_all_categories
|
79 |
return trends_json
|
80 |
|
|