19. Working With Strings and Search Titles


#1

I am getting the error: "Oops, try again. Did you create a query string parameter for id?", for the following code:

from urllib2 import urlopen
from urllib import quote

key = "API_KEY"
url = 'http://api.npr.org/query?apiKey='
url += key
query_param="&numResults=3&format=Podcast&requiredAssets=audio&action=Or"
url += query_param
npr_id=raw_input("Enter comma-separated NPR IDs or leave blank.")
search_string=raw_input("Enter your search string or leave blank.")
feed_title=raw_input("What's your feed title?")
if npr_id or search_string is True:
raw_input("Hit Enter to download your podcast.")
if npr_id is True:
url=url+"&id="+npr_id
if search_string is True:
url=url+"&searchTerm="+quote(search_string)
if feed_title is True:
url=url+"&title="+feed_title
else:
print "You must enter an NPR ID, a search term, or both."

I'm pretty sure it has something to do with the line where I add the npr_id to url (url=url+"&id="+npr_id) but I'm not sure what exactly is wrong.


#2

You are need use another method of checking your string lenth:
if len(npr_id) != 0:
url = url + "&id=" + npr_id
if len(search_string) != 0:
url = url + "&searchTerm=" + quote(search_string)
if len(feed_title) != 0:
url = url + '&title='
print url