Spaces:
Running
Running
Update trend_crawl.py
Browse files- trend_crawl.py +7 -5
trend_crawl.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from trendspy import Trends
|
2 |
import streamlit as st
|
3 |
from datetime import datetime, timezone
|
|
|
4 |
|
5 |
TREND_TOPICS = {
|
6 |
1: "Autos and Vehicles",
|
@@ -23,9 +24,7 @@ TREND_TOPICS = {
|
|
23 |
18: "Technology",
|
24 |
19: "Travel and Transportation"
|
25 |
}
|
26 |
-
|
27 |
trends_json = {}
|
28 |
-
|
29 |
def process_trends_for_country(country_code, trends_list):
|
30 |
if country_code not in trends_json:
|
31 |
trends_json[country_code] = {"All categories" : {}}
|
@@ -56,24 +55,27 @@ def process_trends_for_country(country_code, trends_list):
|
|
56 |
trends_json[country_code]["All categories"][topic_name] = {
|
57 |
"searchQueries": trend.volume,
|
58 |
"articles": articles,
|
59 |
-
"
|
60 |
}
|
61 |
trends_json[country_code][category][topic_name] = {
|
62 |
"searchQueries": trend.volume,
|
63 |
"articles": articles,
|
64 |
}
|
65 |
-
return trends_json
|
66 |
|
67 |
def convert_to_datetime( raw_time):
|
68 |
"""Converts time in seconds to a datetime object with UTC timezone, if it exists."""
|
69 |
return datetime.fromtimestamp(raw_time, tz=timezone.utc) if raw_time else None
|
70 |
|
71 |
def get_trends(countries: list):
|
72 |
-
tr = Trends()
|
73 |
for country in countries:
|
74 |
trends = tr.trending_now(geo=country)
|
75 |
|
76 |
process_trends_for_country(country, trends)
|
77 |
all_categories = trends_json[country]["All categories"]
|
|
|
|
|
|
|
|
|
78 |
return trends_json
|
79 |
|
|
|
|
1 |
from trendspy import Trends
|
2 |
import streamlit as st
|
3 |
from datetime import datetime, timezone
|
4 |
+
tr = Trends()
|
5 |
|
6 |
TREND_TOPICS = {
|
7 |
1: "Autos and Vehicles",
|
|
|
24 |
18: "Technology",
|
25 |
19: "Travel and Transportation"
|
26 |
}
|
|
|
27 |
trends_json = {}
|
|
|
28 |
def process_trends_for_country(country_code, trends_list):
|
29 |
if country_code not in trends_json:
|
30 |
trends_json[country_code] = {"All categories" : {}}
|
|
|
55 |
trends_json[country_code]["All categories"][topic_name] = {
|
56 |
"searchQueries": trend.volume,
|
57 |
"articles": articles,
|
58 |
+
"is_trend_finished": trend.is_trend_finished,
|
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(
|
76 |
+
# sorted(all_categories.items(), key=lambda x: x[1]["searchQueries"], reverse=True)
|
77 |
+
# )
|
78 |
+
#trends_json[country]["All categories"] = sorted_all_categories
|
79 |
return trends_json
|
80 |
|
81 |
+
|