Created
July 31, 2016 06:35
-
-
Save chechedotmx/e74b7cf4dc9b5812ed3fb7f9e231ef36 to your computer and use it in GitHub Desktop.
Zapier CSV Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from googleapiclient.discovery import build | |
import pprint | |
import csv | |
my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
my_cse_id = "xxxxxxxxxxxxxxx:xxxxxxxxxxxxx" | |
def google_search(search_term, api_key, cse_id, **kwargs): | |
service = build("customsearch", "v1", developerKey=api_key) | |
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute() | |
return res['items'] | |
toSearch = [] | |
csvArray = [] | |
c = 1 | |
with open('zapier.csv', 'rb') as csvfile: | |
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') | |
for row in spamreader: | |
if c != 1: | |
toSearch.append(row[0]) | |
csvArray.append(row) | |
c += 1 | |
for i in toSearch: | |
results = google_search(i, my_api_key, my_cse_id, num=1) | |
csvArray[toSearch.index(i)+1][1] = results[0]['link'] | |
with open('zapier.csv', 'wb') as csvfile: | |
spamwriter = csv.writer(csvfile, delimiter=',', | |
quotechar='|', quoting=csv.QUOTE_MINIMAL) | |
for row in csvArray: | |
spamwriter.writerow(row); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment