Return to Snippet

Revision: 18283
at September 26, 2009 03:53 by wearetherock


Initial Code
import cookielib
import urllib2
import urllib
import httplib
import re
import os

###################################
## Simple Ning API
## Create by [email protected]
## Sat Sep 26, 2:34 PM
###################################
class NingApi:
	def __init__(self, username, password):
		cookie_file = "ningcookie.lwp"
		cj = cookielib.LWPCookieJar() 
		if os.path.isfile(cookie_file) :
			cj.load(cookie_file)
			print ">> file not exist"
		if cookielib :
			opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
			urllib2.install_opener(opener)
			print ">> cookielib ok"
		page = self.login(email, password)


	# login to comsciubu.ning.com, please change
	def login(self,email, password):
		login_url = "http://comsciubu.ning.com/main/authorization/doSignIn?target=http%3A%2F%2Fcomsciubu.ning.com%2F"

		body = { "xg_token" : "", 
				"emailAddress" : email,
				"password" : password }

		header = { "Content-type" : "application/x-www-form-urlencoded",
					"User-agent" : "Mozila/4.0"  }

		req = urllib2.Request(login_url, data = urllib.urlencode(body), 
								headers = header)

		handle = urllib2.urlopen(req)
		page = handle.read()
		print handle.info()
		
		"""
		f = open("ning-login.html", "w")
		f.write(page)
		f.close()
		return page
		"""
	
	# post your status message, change comsciubu & wearetherock to your network & your name 
	def post(self, message):
		# get gk_pattern from profile page
		pro_url = "http://comsciubu.ning.com/profiles/profile/show?id=wearetherock&xg_source=activity"	
		req = urllib2.Request(pro_url)
		handle = urllib2.urlopen(req)
		gk_pattern = """name="xg_token" value="(.+?)">"""
		page = handle.read()	
		
		match = re.compile(gk_pattern)
		gk_token = match.findall(page)[0]

		# post message
		post_url = "http://comsciubu.ning.com/profiles/profile/setStatus"
		body = { "gk_token" : gk_token, "status" : message } 
		header = { "Content-type" : "application/x-www-form-urlencoded",
					"User-agent" : "Mozila/4.0" }

		req = urllib2.Request(post_url, data = urllib.urlencode(body),
				headers = header )
		handle = urllib2.urlopen(req) 

if __name__ == "__main__" :
		email = ""
		password = ""
		n = NingApi(email, password)
		n.post("hello, world!")
		n.post("foobar")

Initial URL


Initial Description


Initial Title
Post Ning Status

Initial Tags
python

Initial Language
Python