19. Working With Strings and Search Titles producing error with search_string


#1

Entering the following code, renders the error

Oops, try again.
Your searchTerm parameter doesn't contain the value of search_string.

When a search term is present, and the string is properly generated (as follows):

http://api.npr.org/query?apiKey=API_KEY&numResults=3&action=Or&requiredAssets=audio&format=Podcast&id=1003,1002&searchTerm=Guitar&title=Muzak

Code:

from urllib2 import urlopen
from urllib import quote


key = "API_KEY"
url = 'http://api.npr.org/query?apiKey='
url += key
url +='&numResults=3&action=Or&requiredAssets=audio&format=Podcast'

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:
    raw_input("Hit ENTER to download your podcast")

    if npr_id:
        url += "&id=" + npr_id
        
    if search_string:
        url += "&searchTerm=" + quote(search_string)

    if feed_title:
        url += "&title=" + quote(feed_title)
    
    print url
    
else:
    print "You must enter an NPR ID, search term, or both."`

Adjusting &searchTerm to &searchterm (as used in the demo in lesson 15) does not change the results, and the code is otherwise identical to that found in 15 (minus the bits needed to issue the query). I'm thinking this is a bug, but would be happy to have someone tell me it's not. :slight_smile:


#2

http://www.npr.org/api/inputReference.php > Data Parameters > searchTerm..

This at least clears that up.


#3

I'd actually tried both versions and received the same results (that is, failure). So yes we've cleared up the formatting, but not the reason for the failure?


#4

This is an old course that may no longer match the current API requirements due to upgrades on the NPR end. Unfortunately I'm not well enough versed to begin speculating what the problem may be, so cannot be of much help in reproducing errors.


#5

This response doesn't even address the issue that was reported, which is to see from a moderator. It should be pretty clear that this error:

Oops, try again. Your searchTerm parameter doesn't contain the value of search_string.

is reported by the Codecademy platform, not the npr.org API.

If the age of the course is an excuse for problems with the lessons in it, then Codecademy should either update/remove the course/lessons or incorporate more graceful workarounds. I think the bar on accountability should be a bit higher here, lest you risk subjecting students to beating their heads against the wall troubleshooting their own code, when the issue is with Codecademy's and thus opaque.


#6

Just a quick followup - testing the original poster's code in my own environment produces no issues. Entering a simple, single-word search term produces the error, even though it is verifiable that the resulting searchTerm parameter does contain the value of search_string, invalidating the Codecademy error message.

As an aside, there is no evidence of changes to the NPR API. Executing that code and entering an NPR ID but no search term will give you "Way to go/Start next lesson prompt".


#7

This worked for me

if npr_id or search_string:
raw_input("Hit enter to download your podcast.")

if npr_id: 
    url += "&id=" + npr_id
if search_string: 
    url += "&searchterm=" + quote(search_string)
if feed_title: 
    url += "&title=" + quote(feed_title)