My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Links

Py51: 51平台API的Python封装

Py51是一个对51.com平台API的Python封装,基于PyFacebook,使用和风格同PyFacebook保持一致。Py51可以独立使用,同时支持Django,并且兼容Google App Engine。

快速使用帮助

和Django结合使用

# 下载Py51代码,把fiveone整个目录放到Django项目的根下 # 编辑settings.py

MIDDLEWARE_CLASSES = (
    ...
    'fiveone.djangofo.FiveOneMiddleware',
    ...
)

FIVEONE_API_KEY = 'your_api_key'
FIVEONE_SECRET_KEY = 'your_api_secret'
FIVEONE_APP_NAME = "app_name" #51应用的名字
FIVEONE_CALLBACK_PATH = "/path/" #相对于服务器根目录的callback_path

# 然后就可以在view函数里这样调用Py51

import fiveone.djangofo as fo
from django.utils import simplejson

@fo.require_add()
def foo(request):
    fo = request.fiveone
    #make api calls now
    logging.debug('Testing users.getInfo')
    info = fo.users.getInfo(uids=[fo.user],
                    fields=["username", "sex", "facebig", "facesmall", "face","nickname"])
    logging.debug('Got user info: %s' % info)
    #返回的nickname是base64编码的,需要解码
    logging.debug('nickname: %s' % base64.standard_b64decode(info[0]['nickname']))
    
    logging.debug('Testing friends.get')
    info = fo.friends.get(uid=fo.user)
    logging.debug('Got result: %s' % info)
    
    logging.debug('Testing friends.areFriends')
    info = fo.friends.areFriends(uids1=[fo.user], uids2=['user2'])
    logging.debug('Got result: %s' % info)
    
    logging.debug('Testing friends.getAppUsers')
    info = fo.friends.getAppUsers()
    logging.debug('Got result: %s' % info)
    
    logging.debug('Testing homes.getInfo')
    info = fo.homes.getInfo(uids=[fo.user])
    logging.debug('Got result: %s' % info)
    
    logging.debug('Testing photos.getHome')
    info = fo.photos.getHome(fo.user)
    logging.debug('Got result: %s' % info)
    
    logging.debug('Testing photos.getAlbums')
    info = fo.photos.getAlbums(uid=fo.user)
    logging.debug('Got result: %s' % info)
    
    logging.debug('Testing photos.get')
    info = fo.photos.get(uid=fo.user, aid='album_id')
    logging.debug('Got result: %s' % info)
    
    logging.debug('Testing feed.publishTemplatizedAction')
    #需要把中文编码成GBK再做base64编码
    info = fo.feed.publishTemplatizedAction(
           template_id='1',
           body_data=simplejson.dumps({
                         base64.standard_b64encode((u'你好!').encode('GBK')),
                     }),
           uids=['user1','user2'],
           )
    logging.debug('Got result: %s' % info)

    logging.debug('Testing users.invite')
    info = fo.users.invite(invitees=['user1','user2'],
                           reason=u'很好很强大!',
                           )
    logging.debug('Got result: %s' % info)

    logging.debug('Testing profile.set51ML')
    info = fo.profile.set51ML(profile=u'你好')
    logging.debug('Got result: %s' % info)

    return HttpResponse('Hello!') 

独立于任何web framework在python环境中使用

# 下载Py51代码,确保fiveone目录在PYTHONPATH # 这样初始化一个FiveOne实例:

...
fo = FiveOne(api_key="your_api_key", secret_key="your_secret_key", app_name="your_app_name", callback_path="app_callback_path", internal=True)
if not fo.check_session(request):
    return fo.redirect(fo.get_add_url())
#make api calls now
friends = fo.friends.get(uid=fo.user)
...

2008年10月19日

v0.1版

  • 支持51平台至今所有正式API
  • 已测试Google App Engine的部署
  • 需要更多的测试,欢迎报告问题
Powered by Google Project Hosting