-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
291 lines (224 loc) · 7.94 KB
/
main.py
File metadata and controls
291 lines (224 loc) · 7.94 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# MarkClarity Backend Scraper
# Developed by Marcos Meyer Hollerweger
# https://github.com/marcosh72
# For MarkClarity Team - Startup Weekend Women Denver - Feb 2018
from flask import Flask, Response, request
from flask_restful import reqparse
from flask_cors import CORS
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
import json
import urllib
import requests
app = Flask(__name__)
CORS(app)
request_parser = reqparse.RequestParser()
request_parser.add_argument('query', type=str)
request_parser.add_argument('state', type=str)
def search_google(searchQuery):
apiURL = 'https://www.googleapis.com/customsearch/v1?'
key = '' # insert google search token
searchID = '' # insert google search id
reqURL = apiURL + "key=" + key + "&cx=" + searchID + "&q=" + searchQuery;
try:
r = requests.get(reqURL)
except Exception as e:
return {"data": {}, "error": repr(e)}
try:
retObj = r.json()
except Exception as e:
return {"data": {}, "error": repr(e)}
else:
return retObj
def search_uspto(searchQuery):
apiUsername = "" # insert markerapi user here
apiPassword = "" # insert markerapi pass here
apiURL = 'https://www.markerapi.com/api/v1/trademark/search/'
reqURL = apiURL + searchQuery + "/username/" + apiUsername + "/password/" + apiPassword
try:
r = requests.get(reqURL)
except Exception as e:
return {"data": {}, "error": repr(e)}
try:
retObj = r.json()
except Exception as e:
return {"data": {}, "error": repr(e)}
else:
return retObj
def search_de(searchQuery, driver):
retObj = {'data': {}, 'error': ""}
error = False
try:
driver.get("https://icis.corp.delaware.gov/ecorp/entitysearch/NameSearch.aspx")
search_input = driver.find_element_by_css_selector('input[name="ctl00$ContentPlaceHolder1$frmEntityName"]')
search_submit = driver.find_element_by_css_selector('input[type="submit"]')
search_input.send_keys(searchQuery)
search_submit.click()
except Exception as e:
retObj['error'] = "Error submitting search"
error = True
if not error:
try:
pageMessages = driver.find_element_by_css_selector("#ctl00_ContentPlaceHolder1_divCountsMsg")
errorMessage = pageMessages.get_property('innerText')
if errorMessage:
retObj['error'] = errorMessage
error = True
except NoSuchElementException as e:
pass
if not error:
data = []
tableRows = driver.find_elements_by_css_selector('#tblResults tr')
for index, row in enumerate(tableRows):
if index is 0:
continue
tableCols = row.find_elements_by_css_selector('td');
thisRow = {
"documentNumber": urllib.quote(tableCols[0].get_property('innerText').strip().encode('utf-8')),
"name": urllib.quote(tableCols[1].get_property('innerText').strip().encode('utf-8')),
"detailsUrl": urllib.quote(tableCols[1].find_element_by_tag_name('a').get_attribute('href').encode('utf-8'))
}
data.append(thisRow)
retObj['data'] = data
return retObj
def search_co(searchQuery, driver):
retObj = {'data': {}, 'error': ""}
error = False
try:
driver.get("http://www.sos.state.co.us/biz/BusinessEntityCriteriaExt.do")
search_input = driver.find_element_by_css_selector('input[name="searchName"]')
search_submit = driver.find_element_by_css_selector('input[type="submit"]')
search_input.send_keys(searchQuery)
search_submit.click()
except Exception as e:
retObj['error'] = "Error submitting search"
error = True
if not error:
try:
pageMessages = driver.find_element_by_css_selector("#pageMessages")
messages = pageMessages.find_elements_by_css_selector('li')
errorMessage = ""
if len(messages):
for message in messages:
errorMessage += message.get_property('innerText').strip().encode('utf-8')
retObj['error'] = errorMessage
error = True
except NoSuchElementException as e:
pass
if not error:
data = []
tableRows = driver.find_elements_by_css_selector('tr.odd')
for row in tableRows:
tableCols = row.find_elements_by_css_selector('td');
thisRow = {
"id": urllib.quote(tableCols[1].get_property('innerText').strip().encode('utf-8')),
"documentNumber": urllib.quote(tableCols[2].get_property('innerText').strip().encode('utf-8')),
"name": urllib.quote(tableCols[3].get_property('innerText').strip().encode('utf-8')),
"event": urllib.quote(tableCols[4].get_property('innerText').strip().encode('utf-8')),
"status": urllib.quote(tableCols[5].get_property('innerText').strip().encode('utf-8')),
"form": urllib.quote(tableCols[6].get_property('innerText').strip().encode('utf-8')),
"formationDate": urllib.quote(tableCols[7].get_property('innerText').strip().encode('utf-8')),
"detailsUrl": urllib.quote(tableCols[1].find_element_by_tag_name('a').get_attribute('href').encode('utf-8'))
}
data.append(thisRow)
retObj['data'] = data
return retObj
def find_homonyms(searchQuery):
apiURL = 'https://api.datamuse.com/words?sl='
reqURL = apiURL + searchQuery;
try:
r = requests.get(reqURL)
except Exception as e:
return {"data": {}, "error": repr(e)}
try:
retObj = r.json()
except Exception as e:
return {"data": {}, "error": repr(e)}
else:
retObj = [a for a in retObj if a['score'] > 90]
# retObj = [a for index, a in enumerate(retObj) if index < 3]
print "length " + str(len(retObj))
return retObj
@app.route('/homonyms', methods=['GET'])
def aksjbda():
args = request_parser.parse_args()
retObj = {'data': {}, 'error': ""}
searchQuery = args['query']
if not searchQuery :
retObj['error'] = "Not enough input data"
else:
retObj = find_homonyms(searchQuery)
return Response(response=json.dumps(retObj),
status=200,
mimetype="application/json")
@app.route('/state_search', methods=['GET'])
def state_search():
args = request_parser.parse_args()
retObj = {'data': {}, 'error': ""}
searchQuery = args['query']
searchState = args['state']
if (not searchQuery) or (not searchState) :
retObj['error'] = "Not enough input data"
else:
driver = start_browser()
if driver:
if searchState == "co":
retObj = search_co(searchQuery, driver)
elif searchState == "de":
retObj = search_de(searchQuery, driver)
else:
retObj['error'] = "Not enough input data"
try:
driver.quit()
driver.close()
except Exception as e:
pass
else:
retObj['error'] = "Server error"
return Response(response=json.dumps(retObj),
status=200,
mimetype="application/json")
@app.route('/federal_search', methods=['GET'])
def federal_search():
args = request_parser.parse_args()
retObj = {'data': {}, 'error': ""}
searchQuery = args['query']
if not searchQuery :
retObj['error'] = "Not enough input data"
else:
retObj = search_uspto(searchQuery)
return Response(response=json.dumps(retObj),
status=200,
mimetype="application/json")
@app.route('/google_search', methods=['GET'])
def google_search():
args = request_parser.parse_args()
retObj = {'data': {}, 'error': ""}
searchQuery = args['query']
if not searchQuery :
retObj['error'] = "Not enough input data"
else:
retObj = search_google(searchQuery)
return Response(response=json.dumps(retObj),
status=200,
mimetype="application/json")
def start_browser():
try:
chrome_options = Options()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--num-raster-threads=3')
chrome_options.add_argument('--window-size=1440,900')
chrome_options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
driver = webdriver.Chrome(chrome_options=chrome_options)
except Exception as e:
return None
return driver
if __name__ == "__main__":
app.run(threaded=True, host='0.0.0.0')
# driver.close()
# driver.quit()