-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_request.py
102 lines (85 loc) · 3.74 KB
/
api_request.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
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
import pycountry
from pytrends.request import TrendReq
import gtab
import matplotlib.pyplot as plt
import pandas as pd
class GoogleRequests:
def __init__(self, keywords_arr, cat, timeframes, country, gprop, anchor_time):
self.keywords_arr = keywords_arr
self.cat = cat
self.timeframes = timeframes
self.country = country
self.gprop = gprop
self.anchor_time = anchor_time
self.querry_arr = []
self.trends_arr = []
self.country_code = self.country_to_code(self.country)
self.underscored_countryname = country.replace(" ", "_")
self.pytrends = TrendReq(hl='en-US', tz=360, timeout=(10,25), retries=2, backoff_factor=0.1)
self.t = gtab.GTAB()
self.t.set_options(pytrends_config={"geo": self.country_code, "timeframe": self.anchor_time})
#self.t.create_anchorbank(verbose=True)
self.df = pd.DataFrame()
def country_to_code(self, country):
return pycountry.countries.get(name=str(country)).alpha_2
def interest_over_time(self, word_list):
self.pytrends.build_payload(word_list, self.cat, self.timeframes[0], self.country_code, self.gprop)
data = self.pytrends.interest_over_time()
print(data)
return data
def interest_per_region(self, word_list):
self.pytrends.build_payload(word_list, self.cat, self.timeframes[0], self.country_code, self.gprop)
data = self.pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=True)
#data = data.sort_values(by=self.keywords_arr, ascending=False)
print(data)
return data
def related_queries(self):
self.pytrends.build_payload(self.keywords_arr, self.cat, self.timeframes[0], self.country_code, self.gprop)
data = self.pytrends.related_queries()
for kw in self.keywords_arr:
print(kw + ' top queries: ')
if data[kw]['top'] is None:
print('There is not enough data')
else:
print(data[kw]['top'].head(9))
for index, row in data[kw]['top'].iterrows():
self.querry_arr.append(row['query'])
print(kw + ' rising queries: ')
if data[kw]['rising'] is None:
print('There is not enough data')
else:
print(data[kw]['rising'].head(9))
for index, row in data[kw]['rising'].iterrows():
self.querry_arr.append(row['query'])
#print('___________')
def country_trends(self):
data = self.pytrends.trending_searches(self.underscored_countryname)
for index, row in data.iterrows():
self.trends_arr.append(row.values[0])
def search_array(self):
self.related_queries()
self.country_trends()
return [*self.keywords_arr, *self.querry_arr, *self.trends_arr]
def arrange_data(self, word_list):
#ax = plt.subplot2grid((1, 1), (0, 0), rowspan=1, colspan=1)
for kw in word_list:
kw_query = self.t.new_query(kw)
try:
self.df[kw] = kw_query.max_ratio
#ax.plot(kw_query.max_ratio, label=kw)
#ax.legend()
except AttributeError:
pass
return self.df
def request_window(self):
request_arr = []
search_arr = self.search_array()
#plt.figure()
for i in range(len(search_arr)):
request_arr.append(search_arr[i])
if i % 5 == 0:
self.arrange_data(request_arr)
#self.interest_over_time(request_arr)
#self.interest_per_region(request_arr)
request_arr = []
return self.df