These are the Python equivalents of the PHP examples.
Printing a list of items pulled from an App's feed:
#!/usr/bin/env python """ Fetch a list of objects from the Ning Content store as an Atom Feed. """ # Download feedparser from http://feedparser.org/ import cgi, feedparser # xn_auth is in place to avoid Ning's cookie-based authentication url = "http://bandnames.ning.com/xn/atom/1.0/content(type='NingBlogPost')" \ + "?xn_auth=no&order=published@D&from=0&to=10" # feedparser will open the url, retrieve the content, and parse into a list # of feed entries feed = feedparser.parse(url) # Show the feed contents print "<ul>" li = """<li> <a href="http://bandnames.ning.com/?action=post&id=%s">%s</a> </li>""" for entry in feed.entries: print li % (entry.id, cgi.escape(entry.title)) print "</ul>"
Posting an object to an App:
#!/usr/bin/env python """ Upload an object to the Ning Content Store, as Atom. """ import urllib2, base64, sys # app is an app the user owns. Because user and password are in plaintext, # we'll be making the connection (later) using https. app = 'your-app-here.ning.com' user = 'your-username' password = 'your-password' # This is the object to upload, expressed in Atom. Note the 'my' namespace, # which has to reference the app we're uploading to. entry = """<entry xmlns="http://www.w3.org/2005/Atom" xmlns:xn="http://www.ning.com/atom/1.0" xmlns:my="http://%s"> <xn:type>Food</xn:type> <title type="text">Pizza</title> <summary type="text">A disc of baked dough covered with savoury toppings.</summary> <xn:private>false</xn:private> <my:rating type="number">5</my:rating> <my:ingredients type="string"> <xn:value>Dough</xn:value> <xn:value>Tomato Sauce</xn:value> <xn:value>Mozzarella Cheese</xn:value> <xn:value>Sun-dried Tomatoes</xn:value> <xn:value>Goats Cheese</xn:value> </my:ingredients> </entry> """ % app # Construct the request. HTTP Basic Auth is just a matter of using one extra # header, so add that directly instead of installing an extra opener in # urllib2. urllib2 will also pick up the 'https' of the url automatically, # and behave appropriately. Other notes: # - The Content-type is that used for file uploads, not for forms # - urllib2 will see the data present and use POST, which is why the method # isn't specified explicitly. url = "https://%s/xn/atom/1.0/content" % app base64auth = base64.encodestring('%s:%s' % (user, password)).strip() headers = { 'Content-type': 'form-data', 'Authorization': "Basic %s" % base64auth } request = urllib2.Request(url, data=entry, headers=headers) # urllib2 raises an HTTPError if the response code isn't 200... but the # error object is also a valid response object, so we can use that directly. try: response = urllib2.urlopen(request) except urllib2.HTTPError, err: if 200 <= err.code < 300: response = err else: raise err # Show the response. This should be something like: 201 Created print response.code, response.msg print # Show the headers. These won't include the SSL headers, but will show # content length, encoding, and so on. for h, v in response.headers.items(): print "%s: %s" % (h, v) print # Show the response body, which should be the same Atom document as # originally uploaded. print response.fp.read() # Exit with success sys.exit(0)