-
Notifications
You must be signed in to change notification settings - Fork 0
/
trends.py
188 lines (142 loc) · 6.8 KB
/
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import pandas as pd
import csv
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from parsel import Selector
import coloredlogs
import logging
from tqdm import tqdm
import os
import requests
import random
class TrendRequest(object):
def __init__(self, useProxies=False, extensiveLogging=False, geo=''):
self.GLASS_CEILING = 10
self.RATE_LIMIT_ERROR = '(RATE LIMIT ERROR) An error occurred in retrieving trend data: Please try using proxies by switching on the "useProxies" flag.'
self.resultDataFrame = pd.DataFrame()
self.geo = geo
self.useProxies = useProxies
self.extensiveLogging = extensiveLogging
# USE API TO ROTATE PROXIES (TO DO)
self.proxyListData = []
self.proxy = None
if self.extensiveLogging:
self.logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG', logger=self.logger)
if self.useProxies:
proxyListURL = 'https://raw.githubusercontent.com/clarketm/proxy-list/master/proxy-list.txt'
rawProxyListData = requests.get(proxyListURL)
# open('proxyData.txt', 'wb').write(rawProxyListData.content)
self.proxyListData = rawProxyListData.text.split('\n')
self.proxy = self.chooseRandomProxy()
self.chromeDriverPath = '/usr/local/bin/chromedriver'
self.downloadPath = './trendsData'
self.chromeOptions = Options()
self.downloadPreferences = {'download.default_directory' : self.downloadPath,
'download.prompt_for_download' : False,
'profile.default_content_settings.popups' : 0}
self.chromeOptions.add_experimental_option('prefs', self.downloadPreferences)
self.chromeOptions.add_argument('--headless')
if self.useProxies:
self.chromeOptions.add_argument('--proxy-server={}'.format(self.proxy))
# self.chromeOptions.add_argument('--window-size=1920x1080')
def chooseRandomProxy(self):
randProxy = random.choice(self.proxyListData)
proxyVal = None
if randProxy and randProxy[0].isdigit():
randProxyList = randProxy.split()
proxyVal = randProxyList[0]
proxySecuritySettings = randProxyList[1].split('-')
googlePassedValue = randProxyList[2]
if len(proxySecuritySettings) == 3 and proxySecuritySettings[2] == 'S' \
and proxySecuritySettings[1] == 'H' and googlePassedValue == '+':
self.logger.info("Proxy details: {}".format(randProxy))
return proxyVal
else:
return self.chooseRandomProxy()
else:
return self.chooseRandomProxy()
def enableHeadlessDownload(self, browser):
browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
downloadPath = self.downloadPath
parameters = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': downloadPath}}
browser.execute("send_command", parameters)
def _converter(self, originalFileName='trendsData/multiTimeline.csv', finalFileName='trendsData/multiTimelineConverted.csv'):
with open(originalFileName, 'r') as inp, open(finalFileName, 'w') as out:
writer = csv.writer(out)
i = 1
for row in csv.reader(inp):
if i>2:
splitSecondTitleColumn = row[1].split(':')[0]
row[1] = splitSecondTitleColumn
writer.writerow(row)
i += 1
def retrieveTrends(self, keywords, timeFrame='today 3-m', geo='', cat='', sleepTime=20):
browser = webdriver.Chrome(self.chromeDriverPath,options=self.chromeOptions)
timeFrame = timeFrame.replace(' ', '%20')
self.geo = geo
self.enableHeadlessDownload(browser)
if isinstance(keywords, str):
keywords = [keywords]
ctr = 0
emptyResultsSoFar = 0
failedQueries = 0
totalKeywords = len(keywords)
glassCeilingErrorsCheck = 0
pandasIdxInsertionVal = 0
disableTqdm = self.extensiveLogging
for keyIndex in tqdm(range(len(keywords)), disable=disableTqdm):
keyword = keywords[keyIndex]
ctr += 1
if self.extensiveLogging:
self.logger.info("{}/{} - {} empty results - {} failed queries".format(ctr, totalKeywords, emptyResultsSoFar, failedQueries))
if glassCeilingErrorsCheck == self.GLASS_CEILING:
self.logger.error(self.RATE_LIMIT_ERROR)
# If useProxies == True then rotate the proxy, after completion write failedKeywords to csv
# also if useProxies == True then set lower limit for GLASS_CEILING
break
url = 'http://trends.google.com/trends/explore?date={}'.format(timeFrame)
if geo:
url = url + "&geo={}".format(geo)
if cat:
url = url + "&cat={}".format(cat)
url = url + "&q=" + keyword.replace(' ', '%20')
print(url)
try:
browser.get(url)
sleep(5)
sel = Selector(text=browser.page_source)
button = browser.find_element_by_css_selector('button.widget-actions-item.export')
button.click()
sleep(sleepTime)
self._converter()
convertedDataFrame = pd.read_csv('trendsData/multiTimelineConverted.csv', sep=',')
convertedDataFrameValues = list(convertedDataFrame[keyword].values)
if convertedDataFrameValues:
self.resultDataFrame.insert(loc=pandasIdxInsertionVal, column=keyword, value=convertedDataFrameValues)
pandasIdxInsertionVal += 1
else:
emptyResultsSoFar += 1
glassCeilingErrorsCheck = 0
except Exception as e:
self.logger.error(e)
failedQueries += 1
glassCeilingErrorsCheck += 1
try:
os.remove('trendsData/multiTimeline.csv')
except:
self.logger.info("No results for helper file 1")
try:
os.remove('trendsData/multiTimelineConverted.csv')
except:
self.logger.info("No results for helper file 2")
browser.quit()
try:
self.resultDataFrame.to_csv('trendsData/trendsData.csv')
except:
self.logger.error("No final result produced :(")
if emptyResultsSoFar > 0 or failedQueries > 0:
self.logger.error("Errors: {} empty and {} failed results".format(emptyResultsSoFar, failedQueries))
self.logger.info("PROCESS COMPLETE")