-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_trends.py
75 lines (53 loc) · 2.13 KB
/
google_trends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
64
65
66
67
68
69
70
71
72
73
74
75
from pytrends.request import TrendReq
from graph_output import ascii_colorful_horizontal_bar_chart
import pandas as pd
from pytrends.request import TrendReq
def sort_by_popularity(topics):
def fetch_interest_over_time(topics):
pytrends = TrendReq(hl='en-US', tz=360)
data_frames = []
for topic in topics:
pytrends.build_payload(kw_list=[topic])
interest_over_time_df = pytrends.interest_over_time()
data_frames.append(interest_over_time_df[topic])
return pd.concat(data_frames, axis=1)
# Fetch interest over time data for all topics
interest_over_time_data = fetch_interest_over_time(topics)
# Calculate the average popularity for each topic
average_popularity = interest_over_time_data.mean(axis=0)
# Normalize the popularity scores
normalized_popularity = (average_popularity - average_popularity.min()) / (average_popularity.max() - average_popularity.min())
# Sort the topics by their normalized popularity
sorted_topics = normalized_popularity.sort_values(ascending=False)
# Print or use the sorted topics as needed
return sorted_topics
def get_trend(topic):
# Create a pytrends object
pytrends = TrendReq(hl='en-US', tz=360)
# Build the payload
pytrends.build_payload(kw_list=[topic])
# Get interest over time
interest_over_time_df = pytrends.interest_over_time()
ttime_labels = interest_over_time_df.index
vvalues = interest_over_time_df[topic].tolist()
time_labels = []
values = []
start = False
for i in range(len(vvalues)):
if vvalues[i] != 0:
start = True
if start:
values.append(vvalues[i])
time_labels.append(ttime_labels[i])
N = 10
INTERVAL = len(values) // N
com_values = []
com_time_labels = []
for i in range(0, len(values), INTERVAL):
average = 0
for j in range(i, i + INTERVAL):
average += values[i]
average /= INTERVAL
com_values.append(average)
com_time_labels.append(time_labels[i])
ascii_colorful_horizontal_bar_chart(com_time_labels, com_values)