Spaces:
Running
Running
Delete trend_crawl.py
Browse files- trend_crawl.py +0 -89
trend_crawl.py
DELETED
@@ -1,89 +0,0 @@
|
|
1 |
-
from trendspy import Trends
|
2 |
-
import streamlit as st
|
3 |
-
from datetime import datetime, timezone, timedelta
|
4 |
-
|
5 |
-
TREND_TOPICS = {
|
6 |
-
1: "Autos and Vehicles",
|
7 |
-
2: "Beauty and Fashion",
|
8 |
-
3: "Business and Finance",
|
9 |
-
20: "Climate",
|
10 |
-
4: "Entertainment",
|
11 |
-
5: "Food and Drink",
|
12 |
-
6: "Games",
|
13 |
-
7: "Health",
|
14 |
-
8: "Hobbies and Leisure",
|
15 |
-
9: "Jobs and Education",
|
16 |
-
10: "Law and Government",
|
17 |
-
11: "Other",
|
18 |
-
13: "Pets and Animals",
|
19 |
-
14: "Politics",
|
20 |
-
15: "Science",
|
21 |
-
16: "Shopping",
|
22 |
-
17: "Sports",
|
23 |
-
18: "Technology",
|
24 |
-
19: "Travel and Transportation"
|
25 |
-
}
|
26 |
-
|
27 |
-
|
28 |
-
def get_trends_base():
|
29 |
-
return Trends()
|
30 |
-
def process_trends_for_country(country_code, trends_list, tr):
|
31 |
-
trends_json = {}
|
32 |
-
if country_code not in trends_json:
|
33 |
-
trends_json[country_code] = {"All categories" : {}}
|
34 |
-
|
35 |
-
for trend in trends_list:
|
36 |
-
|
37 |
-
timestamp = convert_to_datetime(trend.started_timestamp[0])
|
38 |
-
current_time = datetime.now(timezone.utc)
|
39 |
-
if current_time - timestamp > timedelta(days=2):
|
40 |
-
continue
|
41 |
-
|
42 |
-
category = None
|
43 |
-
for topic_id in trend.topics:
|
44 |
-
if topic_id in TREND_TOPICS:
|
45 |
-
category = TREND_TOPICS[topic_id]
|
46 |
-
break
|
47 |
-
if category is None:
|
48 |
-
category = TREND_TOPICS[11]
|
49 |
-
|
50 |
-
if category not in trends_json[country_code]:
|
51 |
-
trends_json[country_code][category] = {}
|
52 |
-
|
53 |
-
|
54 |
-
topic_name = trend.keyword
|
55 |
-
|
56 |
-
try:
|
57 |
-
news = tr.trending_now_news_by_ids(trend.news_tokens, max_news=3)
|
58 |
-
articles = [
|
59 |
-
{"title": article.title, "href": article.url}
|
60 |
-
for article in news
|
61 |
-
]
|
62 |
-
|
63 |
-
except Exception as e:
|
64 |
-
articles=[]
|
65 |
-
trends_json[country_code]["All categories"][topic_name] = {
|
66 |
-
"searchQueries": trend.volume,
|
67 |
-
"articles": articles,
|
68 |
-
"is_trend_finished": trend.is_trend_finished,
|
69 |
-
}
|
70 |
-
trends_json[country_code][category][topic_name] = {
|
71 |
-
"searchQueries": trend.volume,
|
72 |
-
"articles": articles,
|
73 |
-
}
|
74 |
-
return trends_json
|
75 |
-
|
76 |
-
def convert_to_datetime( raw_time):
|
77 |
-
"""Converts time in seconds to a datetime object with UTC timezone, if it exists."""
|
78 |
-
return datetime.fromtimestamp(raw_time, tz=timezone.utc) if raw_time else None
|
79 |
-
|
80 |
-
def get_trends(country):
|
81 |
-
tr = get_trends_base()
|
82 |
-
trends = tr.trending_now(geo=country)
|
83 |
-
|
84 |
-
trends_json = process_trends_for_country(country, trends, tr)
|
85 |
-
all_categories = trends_json[country]["All categories"]
|
86 |
-
print(trends)
|
87 |
-
return trends_json
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|