Created
September 12, 2015 04:28
-
-
Save dmil/5703e6af81d7847b9a91 to your computer and use it in GitHub Desktop.
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
""" | |
Stub for script to query API | |
""" | |
from urllib2 import Request, urlopen, URLError | |
import json | |
import logging | |
from time import sleep | |
URL_STUB = "http://website.com/api?q=" | |
API_KEY = "" | |
# set logging level to DEBUG | |
logging.basicConfig(level=logging.DEBUG) | |
def build_url(query_words, rows, start): | |
""" | |
""" | |
url = URL_STUB | |
url += "%20".join(query_words) | |
url += "&start=" + str(start) | |
url += "&rows=" + str(rows) | |
url += "&output=json" | |
logging.debug("Built URL: %s", url) | |
return url | |
def query_api(query_url): | |
request = Request(query_url) | |
try: | |
response = urlopen(request) | |
response_json = json.loads(response.read()) | |
return response_json | |
except URLError, e: | |
print 'Got an error code:', e | |
# Main logic of module | |
query = ['word1','word2'] | |
num_rows = 50 | |
start = 0 | |
while True: | |
url = build_url(query, num_rows, start) | |
results = query_api(url) | |
if results == []: | |
break | |
else: | |
for result in results: | |
# Do something with each result. | |
pass | |
start += len(result) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment