Posted By

wearetherock on 09/26/09


Tagged

python status ning


Versions (?)

Post Ning Status


 / Published in: Python
 

  1. import cookielib
  2. import urllib2
  3. import urllib
  4. import httplib
  5. import re
  6. import os
  7.  
  8. ###################################
  9. ## Simple Ning API
  10. ## Create by [email protected]
  11. ## Sat Sep 26, 2:34 PM
  12. ###################################
  13. class NingApi:
  14. def __init__(self, username, password):
  15. cookie_file = "ningcookie.lwp"
  16. cj = cookielib.LWPCookieJar()
  17. if os.path.isfile(cookie_file) :
  18. cj.load(cookie_file)
  19. print ">> file not exist"
  20. if cookielib :
  21. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  22. urllib2.install_opener(opener)
  23. print ">> cookielib ok"
  24. page = self.login(email, password)
  25.  
  26.  
  27. # login to comsciubu.ning.com, please change
  28. def login(self,email, password):
  29. login_url = "http://comsciubu.ning.com/main/authorization/doSignIn?target=http%3A%2F%2Fcomsciubu.ning.com%2F"
  30.  
  31. body = { "xg_token" : "",
  32. "emailAddress" : email,
  33. "password" : password }
  34.  
  35. header = { "Content-type" : "application/x-www-form-urlencoded",
  36. "User-agent" : "Mozila/4.0" }
  37.  
  38. req = urllib2.Request(login_url, data = urllib.urlencode(body),
  39. headers = header)
  40.  
  41. handle = urllib2.urlopen(req)
  42. page = handle.read()
  43. print handle.info()
  44.  
  45. """
  46. f = open("ning-login.html", "w")
  47. f.write(page)
  48. f.close()
  49. return page
  50. """
  51.  
  52. # post your status message, change comsciubu & wearetherock to your network & your name
  53. def post(self, message):
  54. # get gk_pattern from profile page
  55. pro_url = "http://comsciubu.ning.com/profiles/profile/show?id=wearetherock&xg_source=activity"
  56. req = urllib2.Request(pro_url)
  57. handle = urllib2.urlopen(req)
  58. gk_pattern = """name="xg_token" value="(.+?)">"""
  59. page = handle.read()
  60.  
  61. match = re.compile(gk_pattern)
  62. gk_token = match.findall(page)[0]
  63.  
  64. # post message
  65. post_url = "http://comsciubu.ning.com/profiles/profile/setStatus"
  66. body = { "gk_token" : gk_token, "status" : message }
  67. header = { "Content-type" : "application/x-www-form-urlencoded",
  68. "User-agent" : "Mozila/4.0" }
  69.  
  70. req = urllib2.Request(post_url, data = urllib.urlencode(body),
  71. headers = header )
  72. handle = urllib2.urlopen(req)
  73.  
  74. if __name__ == "__main__" :
  75. email = ""
  76. password = ""
  77. n = NingApi(email, password)
  78. n.post("hello, world!")
  79. n.post("foobar")

Report this snippet  

You need to login to post a comment.