Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>At the first glance your app has several issues:</p> <ol> <li>I suppose HOST should be '<a href="http://api.flickr.com" rel="nofollow">http://api.flickr.com</a>'</li> <li>Indentation and formatting (: after if and else) in if-else statement in MainPage class</li> <li>Undefined varibale "photos" in self.response.write(photos)</li> <li>Undefined functions "_prepare_params", "_get_auth_url_suffix", "_get_data" and variable "debug" in _doget function.</li> <li>You should call read() method of urlopen result</li> <li>You should use minidom.parseString</li> <li>You can use run_wsgi_app to run your app</li> </ol> <p>So here is the working example:</p> <pre><code>from google.appengine.ext.webapp.util import run_wsgi_app import webapp2 from urllib import urlencode, urlopen from xml.dom import minidom import hashlib import os HOST = 'http://api.flickr.com' API = '/services/rest' API_KEY = 'my_key' debug = False class MainPage(webapp2.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'text/html' data = _doget('flickr.photos.search', auth=False, text='boston', per_page='2') if data: photos = data.getElementsByTagName("photo") for photo in photos: farm_id = 1 # ??? server_id = photo.attributes['server'].value photo_id = photo.attributes['id'].value secret = photo.attributes['secret'].value photo_url = 'http://farm{farm_id}.staticflickr.com/{server_id}/{photo_id}_{secret}.jpg'.format(farm_id=farm_id, server_id=server_id, photo_id=photo_id, secret=secret) self.response.write('&lt;img src="{0}"&gt;'.format(photo_url)) else: self.response.write('Error') def _doget(method, auth=False, **params): #print "***** do get %s" % method params = params url = '%s%s/?api_key=%s&amp;method=%s&amp;%s'% \ (HOST, API, API_KEY, method, urlencode(params)) #another useful debug print statement if debug: print "_doget", url res = urlopen(url) res = res.read() return minidom.parseString(res) application = webapp2.WSGIApplication([('/', MainPage),], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() </code></pre> <p>To format flickr photo url I used <a href="http://www.flickr.com/services/api/misc.urls.html" rel="nofollow">http://www.flickr.com/services/api/misc.urls.html</a>, but I havent found where to get farm_id parameter, I hope you'll manage it by yourself )</p> <hr> <p>Furthermore, it can be easier to use json format by passing 'format=json' to the API request URL and a bit more properly to use GAE urlfetch service:</p> <pre><code>import json from google.appengine.api import urlfetch # ... class MainPage(webapp2.RequestHandler): def get(self): # ... for photo in data['photos']['photo']: farm_id = 1 # ??? server_id = photo['server'] photo_id = photo['id'] secret = photo['secret'] # ... def _doget(method, auth=False, **params): # ... url = '%s%s/?api_key=%s&amp;method=%s&amp;%s&amp;format=json'% \ (HOST, API, API_KEY, method, urlencode(params)) res = urlfetch.fetch(url).content # Flickr JSON api returns not valid JSON, which wrapped with "jsonFlickrApi(...)", so we get rid of it. if 'jsonFlickrApi(' in res: return res[14:-1] return json.loads(res) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload