-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemasearch.py
64 lines (58 loc) · 1.78 KB
/
schemasearch.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
# This module uses a custom Google Search Engine to search for sites that contain structured data.
#
# The search engine type (cx) and apiKey are defined in 'config.py'
import config
import requests, json, time
class schemaSearch():
def __init__(self):
self.pagemapIgnore = ['cse_thumbnail','metatags','cse_image']
self.reportData = []
self.APIdisconnect = False
self.totalErrors = 0
self.pages = 0
def findSchema(self,search):
search = search.strip()
if search != '':
startIndex = 1
while True:
if not self.APIdisconnect:
searchData = self.apiRequest(search,startIndex)
if searchData != None:
self.pages = self.pages + 1
startIndex = searchData
else:
break
else:
break
def apiRequest(self,search,startIndex = 1):
apiEndpoint = 'https://www.googleapis.com/customsearch/v1'
returnData = []
params = {
'cx':config.apiConfig['schemasearch']['cx'],
'key':config.apiConfig['schemasearch']['apiKey'],
'start':startIndex,
'q':'search'
}
try:
HTTPrequest = requests.get(apiEndpoint,params = params)
schemaData = json.loads(HTTPrequest.text)
for thisResult in schemaData['items']:
urlData = {}
urlData['url'] = thisResult['link']
urlData['title'] = thisResult['title']
urlData['description'] = thisResult['snippet']
urlSchemas = []
for thisSchema,items in thisResult['pagemap'].items():
if thisSchema not in self.pagemapIgnore:
urlSchemas.append(thisSchema)
urlData['schemas'] = urlSchemas
self.reportData.append(urlData)
if schemaData['queries']['nextPage'] != []:
return schemaData['queries']['nextPage'][0]['startIndex']
else:
return None
except:
self.totalErrors = self.totalErrors + 1
if(self.totalErrors > 50):
self.APIdisconnect = True
return None