1. Li Huan
  2. renren-python-sdk

Commits

Li Huan  committed 1082ea6

add renren touch api.

  • Participants
  • Parent commits d5c584a
  • Branches default

Comments (0)

Files changed (1)

File renren.py

View file
     import simplejson
     _parse_json = lambda s: simplejson.loads(s)
 
+#PC
 RENREN_AUTHORIZATION_URI = "http://graph.renren.com/oauth/authorize"
 RENREN_ACCESS_TOKEN_URI = "http://graph.renren.com/oauth/token"
 RENREN_API_SERVER = "http://api.renren.com/restserver.do"
 
+#WAP and TOUCH
+RENREN_TOUCH_USER_INFO_URI = "http://mapi.renren.com/service/userinfo.do"
+RENREN_TOUCH_BACK_URI = "http://mapi.renren.com/game/backxnbyapp.do"
+
 class RenRenAPI(object):
 
     def __init__(self, access_token=None, api_key=None, secret_key=None):
         params["call_id"] = str(int(time.time() * 1000))
         params["format"] = "json"
         params["v"] = '1.0'
-        sig = self.hash_params(params);
+        sig = self.hash_params(params)
         params["sig"] = sig
 
         post_data = None if params is None else urllib.urlencode(params)
         """
         return isinstance(str, unicode) and str.encode('utf-8') or str
 
+USER_INFO_EXPTION = {
+                    "10001":u"域名及IP认证失败",
+                    "10002":u"密钥认证失败",
+                    "10003":u"用户验证失败",
+                    "50001":u"网络异常"
+                    }
+
+class RenRenTouchAPI(object):
+    def __init__(self, app_id=None, secret_key=None, server_ip=None):
+        self.app_id = app_id
+        self.secret_key = secret_key
+        self.server_ip = server_ip
+
+    def get_user_info(self):
+        auth = self.auth_params()
+        response = urllib.urlopen("%s?pid=%s&appid=%s&auth=%s" % (RENREN_USER_INFO_URL, self.secret_key, self.app_id,
+                                                                  auth))
+        try:
+            user_info = _parse_json(response.read())
+        finally:
+            response.close()
+
+        logging.info(user_info)
+
+        if user_info in USER_INFO_EXPTION.keys():
+            raise RenRenTouchAPIError(user_info, USER_INFO_EXPTION[user_info])
+        else:
+            hostid, name, tinyurl, gender = user_info.split('\n')
+
+    def auth_params(self):
+        return hashlib.md5("%s%s%s" % (self.secret_key, self.app_id, self.server_ip)).hexdigest()
+
+    def get_back_url(self):
+        return "%s?page=home&pid=%s&appid=%s" % (RENREN_TOUCH_BACK_URI, self.secret_key, self.app_id)
+
 class RenRenAPIError(Exception):
     def __init__(self, code, message):
         Exception.__init__(self, message)
         self.code = code
 
+class RenRenTouchAPIError(Exception):
+    def __init__(self, code, message):
+        Exception.__init__(self, message)
+        self.code = code
 
-