forked from shanealynn/python_batch_geocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsearch.py
65 lines (51 loc) · 2.06 KB
/
websearch.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
import requests
import urllib
import sys
import logging
#----------------- Logger import ---------------------------------
logger = logging.getLogger(__name__)
log_level = logging.DEBUG
#-----------------Search Term using Custom Search API---------------------------
def search_item(search_term,
API_KEY= "AIzaSyD0H_G1JKCgklUtvDFVcdoMtto3ooyalZ8",
CUSTOM_KEY="007585794764902922731:lhcpji7ohgq",
google_url = "https://www.googleapis.com/customsearch/v1?",
log_level = logging.DEBUG
):
logger.setLevel(log_level)
parameters = {
"key": API_KEY,
"cx": CUSTOM_KEY,
"q": search_term
}
google_url += urllib.parse.urlencode(parameters)
logger.debug("Looking up {}".format(google_url))
results = requests.get(google_url)
results = results.json()
logger.debug("Got {} results".format(len(results)))
output={}
output["search_term"]=search_term
output["search_url"]=google_url
if results.get("error") is not None :
output["formattedTotalResults"]=None
output["totalResults"]=None
output["correction"]="None"
output["status"]="{}. {}".format(results.get("error").get("code"),results.get("error").get("message"))
else:
output["formattedTotalResults"]=\
results.get("searchInformation").get("formattedTotalResults")
output["totalResults"]=\
results.get("searchInformation").get("totalResults")
if results.get("spelling"):
output["correction"]=results.get("spelling").get("correctedQuery")
return output
#---------------------------------------------Main-----------------------------------
#Search the parameters or perform basic test
if __name__ == '__main__':
if(len(sys.argv) > 1):
print(search_item(str(sys.argv[1:])))
else:
result = search_item("Quail Rd, Lake Clifton, WA 6215")
logger.debug("Hello")
print(result)
input()