-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-search-commits.py
executable file
·82 lines (71 loc) · 2.47 KB
/
google-search-commits.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
#!/usr/bin/env python
# Script to take a bunch of patches, and query their subject lines on lkml.org
# Requirements: google python API and a registered Search Engine ID and API
# key
import sys
import argparse
import json
import pprint
from googleapiclient.discovery import build
verbose = 0
debug = 0
searchEngine_ID = 'FILL-ME-IN'
API_key = 'FILL-ME-IN'
def read_opt(argv):
global verbose, debug
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--string', nargs='*', required=True)
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-d', '--debug', action='store_true')
args = parser.parse_args(argv)
if args.verbose:
verbose = 1
if args.debug:
debug = 1
if debug:
print('read_opt: {}'.format(args.string))
return ' '.join(args.string)
count = 0
upcount = 0
noupcount = 0
unknown = 0
if __name__ == "__main__":
str = read_opt(sys.argv[1:])
if debug:
print('string: {}'.format(str))
# We expect the follow string format: "<SHA>^ <patch subject line>"
# So remove the SHA for the search
search_str = str.split('^')[0]
if debug:
print('search_str: {}'.format(search_str))
count += 1
query_str = ' '.join([search_str])
#query = urllib.urlencode({ 'q' : query_str })
# Build a service object for interacting with the API. Visit
# the Google APIs Console <http://code.google.com/apis/console>
# to get an API key for your own application.
service = build("customsearch", "v1", developerKey=API_key)
#response = service.cse().list(q=query_str, siteSearch='lkml.org',
response = service.cse().list(q=query_str,
cx=searchEngine_ID).execute()
# cx=searchEngine_ID, num=2).execute()
if debug:
pprint.pprint(response)
if not response['items']:
unknown += 1
print '\tNo response... possibly throttled by Google. Aborting.'
sys.exit(2)
results = response['responseData']['results']
if debug:
print results
if not results:
noupcount += 1
print '\tNo results'
sys.exit(2)
for result in results:
if verbose:
title = result['title']
url = result['url'] # was URL in the original and that threw a name error exception
print ('\t' + title + '; ' + url )
upcount += 1
print ('Total: {}, noup: {}, upcount: {}, unknown: {}'.format(count, noupcount, upcount, unknown))