Created
August 27, 2013 07:20
-
-
Save cnsoft/6350592 to your computer and use it in GitHub Desktop.
weiyouxi.api python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://xingqibawiki.sinaapp.com/index.php/Python-category#test | |
| #test | |
| import web | |
| import api | |
| urls = ( | |
| '/', 'index' | |
| ) | |
| class index: | |
| def GET(self): | |
| data = web.input() | |
| if data.wyx_session_key: | |
| APP_ID = '' | |
| APP_SECRET = '' | |
| weibo_handle = api.Weibo(APP_ID, APP_SECRET) | |
| weibo_handle.set_session_key(data.wyx_session_key) | |
| player_data = weibo_handle.api('/user/show', {"uid" : "2271093597"}) | |
| return player_data | |
| else: | |
| return "empty session key" | |
| if __name__ == "__main__": | |
| app = web.application(urls, globals()) | |
| app.run() | |
| api | |
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| ''' | |
| @author 星期八 174171262 ixqbar@gmail.com | |
| ''' | |
| try: | |
| from urllib.request import urlopen | |
| from urllib.parse import quote | |
| except ImportError: | |
| from urllib2 import urlopen | |
| from urllib import quote | |
| import hashlib | |
| import time | |
| try: | |
| import json | |
| except ImportError: | |
| from simplejson import json | |
| class Weibo(object): | |
| def __init__(self, app_id, app_secret): | |
| if 0 == len(app_id) or 0 == len(app_secret): | |
| raise ValueError('invlalid app_id or app_secret') | |
| self.app_id = app_id | |
| self.app_secret = app_secret | |
| self.mysha_handle = hashlib.sha1() | |
| def set_session_key(self, session_key): | |
| self.session_key = session_key | |
| def encodeurldata(self, dict_params): | |
| return "&".join(k + "=" + quote(str(dict_params[k]), safe='~') for k in sorted(dict_params.keys())) | |
| def hmac_sha1_sig(self, str_params): | |
| self.mysha_handle.update(str_params.encode('utf-8')) | |
| return self.mysha_handle.hexdigest() | |
| def api(self, api, params = None, method='get', return_json = True): | |
| if 0 == len(api) or '/' != api[0]: | |
| raise ValueError('invalid api') | |
| if params: | |
| params['source'] = self.app_id | |
| params['timestamp'] = time.time() | |
| params['session_key'] = self.session_key | |
| else: | |
| params = { | |
| 'source' : self.app_id, | |
| 'timestamp' : time.time(), | |
| 'session_key' : self.session_key, | |
| } | |
| params['signature'] = self.hmac_sha1_sig(self.encodeurldata(params) + self.app_secret); | |
| uri = 'http://api.weibo.com/game/1%s' % api | |
| if method.lower() == 'get': | |
| url = '%s?%s' % (uri, self.encodeurldata(params)) | |
| data = urlopen(url).read() | |
| elif method.lower() == 'post': | |
| data = urlopen(uri, self.encodeurldata(params).encode('utf-8')).read() | |
| else: | |
| raise ValueError('method invalid:%s' % method) | |
| return json.loads(data.decode('utf-8')) if return_json else data | |
| def test(): | |
| APP_ID = '' | |
| APP_SECRET = '' | |
| weibo_handle = Weibo(APP_ID, APP_SECRET) | |
| weibo_handle.set_session_key('') | |
| player_data = weibo_handle.api('/user/show', {"uid" : "2271093597"}) | |
| print(player_data) | |
| friends_data = weibo_handle.api('/user/are_friends', {"uid1" : "2271093597", "uid2" : ""}) | |
| print(friends_data) | |
| if __name__ == '__main__': | |
| test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment