I have created a python script that allows me to run through a loop in order to process several GET requests. I am then pulling the value for each request for a field called "creative" and then printing the final counts of both "True" and "False" values at the end of the script. Here is what my code looks like:
import requests
import json
false_count = 0
true_count = 0
qa_creatives = open("numbers.txt")
arraylist = []
for line in qa_creatives.readlines():
arraylist.extend(line.split())
qa_creatives.close()
arraylist = map(int, arraylist)
authorization_code = raw_input("please enter your authorization code: ")
creatives = arraylist
print "Creative Check script is now running, please wait."
for creative in creatives:
url = "http://api.wiki123.com/v1.11/creative?id="+str(creative)
header = {"Authorization": authorization_code}
response = requests.get(url, headers=header)
creative_check = json.loads(response.text)
final_creative_status = creative_check["response"]["creative"]["is_expired"]
#print str(creative) + " expired status: " + str(final_creative_status)
if final_creative_status == False:
false_count += 1
else:
true_count += 1
print "Creative Check Complete:"
print str(false_count) + " creatives are still valid"
print str(true_count) + " creatives have expired"
Here is a sample of the JSON data that is returned when making one of these GET requests:
{
'response': {
'count': 1,
'creative': {
'prime_id': 1092343,
'off_audit': None,
'allow_audit': False,
'allow_ssl_audit': False,
'audit_feedback': None,
'audit_status': 'no_audit',
'backup_upload_status': None,
'brand': {
'category_id': 0,
'id': 1,
'name': 'Unknown'
}
}}}
The odd thing is that this script works some of the time but other times, I get the following error: KeyError: 'creative'. I am puzzled by this because my requests should be returning the same thing each time and as such, the key should never change. Can anyone tell what might be going on here? Any recommendations on how I can debug this issue? If a certain "creative" is breaking the loop, what would be the best way to exclude it? Thanks.
UPDATED Code for my loop:
for creative in creatives:
url = "http://api.wiki123.com/v1.11/creative?id="+str(creative)
header = {"Authorization": authorization_code}
api_response = requests.get(url, headers=header)
creative_check = json.loads(api_response.text)
creative_status = creative_check.get("response",
{}).get("creative{}).get("is_expired", None)
if creative_status is None:
pass
elif creative_status == True:
true_count += 1
else :
false_count += 1
print "Creative Check Complete: "
print str(false_count) + " creatives are still valid"
print str(true_count) + " creatives have expired"
Note: Now, I don't receive a key error back, but both of my counts for false_count and true_count are always 0 after my script has finished.
creativein it.KeyErrorand process it accordingly... no?if "creative" in creative_check["response"] : ( ..Your code here.. ) else : Separate_list += [ creative_check["response"] ]