-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (57 loc) · 2.47 KB
/
main.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
from pytrends.request import TrendReq
import pandas as pd
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import os
import pickle
pytrend = TrendReq()
geo = input('Choose your geolocalization (ex:US): ')
search_terms = input('Choose your 5 max keywords (use space as separator ex:milk tea coffee): ')
kw_list = search_terms.split()
timeframe = input('Choose your timeframe (ex:today 1-m): ')
pytrend.build_payload(kw_list = kw_list, geo = geo, timeframe = timeframe)
related_queries = pytrend.related_queries()
search_terms = [related_queries.get(elem).get('rising') for elem in kw_list]
for i in range(len(search_terms)):
search_terms[i].insert(loc=0, column="theme", value=kw_list[i])
result = pd.concat(search_terms).reset_index()
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
# here enter the id of your google sheet
SAMPLE_SPREADSHEET_ID_INPUT = 'YOUR GOOGLE SHEET ID'
SAMPLE_RANGE_NAME = 'A1:AA1000'
CLIENT_SECRET_SERVICE = 'YOUR CLIENT ID JSON'
def Create_Service(client_secret_file, api_service_name, api_version, scopes):
cred = None
if os.path.exists('token_write.pickle'):
with open('token_write.pickle', 'rb') as token:
cred = pickle.load(token)
if not cred or not cred.valid:
if cred and cred.expired and cred.refresh_token:
cred.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(client_secret_file, scopes)
cred = flow.run_local_server()
with open('token_write.pickle', 'wb') as token:
pickle.dump(cred, token)
try:
service = build(api_service_name, api_version, credentials=cred)
#print(api_service_name, 'service created successfully')
return service
except Exception as e:
print(e)
return None
service = Create_Service(CLIENT_SECRET_SERVICE, 'sheets', 'v4', SCOPES)
def Export_Data_To_Sheets():
service.spreadsheets().values().batchClear(spreadsheetId=SAMPLE_SPREADSHEET_ID_INPUT,
body= { "ranges":[SAMPLE_RANGE_NAME]}).execute()
service.spreadsheets().values().update(
spreadsheetId=SAMPLE_SPREADSHEET_ID_INPUT,
valueInputOption='RAW',
range=SAMPLE_RANGE_NAME,
body=dict(
majorDimension='ROWS',
values=result.T.reset_index().T.values.tolist())
).execute()
print('Sheet successfully Updated')
Export_Data_To_Sheets()