对yupoo api的python封装

对yupoo api的python封装,2007的时候写的了,
那时候写了用来在nokia s60手机上上传照片到yupoo,
也是第一次接触xmlrpc,rest方式的调用。

yupoo.py,对yupoo api的封装


#--------------------------------------------------------------------
# -*- coding: utf-8 -*-
# Name:        yupoo api for python
# Purpose:     yupoo api for python
#
# Author:      chenyc
# Created:     22-06-2007
# Copyright:   (c) chenyc 2007
# Licence:     GNU
#--------------------------------------------------------------------

import yupoo_helper,urllib,httplib,os
config=yupoo_helper.config
def login(username,password):
    opener=yupoo_helper.bulid_opener()
    login_url='http://www.yupoo.com/account/auth/'
    postdata=urllib.urlencode({'j_username':username,'j_password':password,'j_remember_me':'1'})
    opener.open(login_url,postdata)
    return opener

def user_auth(username,password,perms,frob):
    opener=login(username,password)
    auth_url='http://www.yupoo.com/services/auth/?'
    params={'api_key':config['api_key'],'frob':frob,'perms':perms}
    api_sig=yupoo_helper.sig(yupoo_helper.join_params(params))
    auth_url=auth_url+'api_key='+config['api_key']+'&frob='+frob+'&perms='+perms+'&api_sig='+api_sig
    opener.open(auth_url)

def auth_set_perms(username,password,perms):
    a=Auth()
    frob=a.auth_getFrob()['frob']
    user_auth(username,password,perms,frob)
    auth_info=a.auth_getToken(frob)
    config['auth_token']=auth_info['token']
    yupoo_helper.set_config(config)
    return auth_info

def auth_get_FullToken(mini_token):
    a=Auth()
    auth_info=a.auth_getFullToken(mini_token)
    config['auth_token']=auth_info['token']
    yupoo_helper.set_config(config)
    return auth_info

def auth_checkToken(auth_token):
    a=Auth()
    auth_info=a.auth_checkToken(auth_token)
    return auth_info

class Auth:
    def auth_getFrob(self):
        method_name='yupoo.auth.getFrob'
        params={}
        return  yupoo_helper.call_method(method_name,params,True)

    def auth_getToken(self,frob):
        method_name='yupoo.auth.getToken'
        params={'frob':frob}
        return  yupoo_helper.call_method(method_name,params,True)

    def auth_getFullToken(self,mini_token):
        method_name='yupoo.auth.getFullToken'
        params={'mini_token':mini_token}
        return yupoo_helper.call_method(method_name,params,True)

    def auth_checkToken(self,auth_token):
        method_name='yupoo.auth.checkToken'
        params={'auth_token':auth_token}
        return  yupoo_helper.call_method(method_name,params,True)

class YupooAPI:
    def __init__(self,api_key,shared_secret,username='',password='',perms='delete'):
        if config['api_key']== api_key and config['shared_secret']==shared_secret \
        and config['perms']==perms and config['auth_token']<>'?':
            auth_info=auth_checkToken(config['auth_token'])
        else:
            self.set_api_info(api_key,shared_secret,perms)
            auth_info=auth_set_perms(username,password,perms)
        self.auth_token=auth_info['token']
        self.user_id=auth_info['user']['id']
        self.callmethod=yupoo_helper.call_method

    def blogs_getList(self):
        method_name='yupoo.blogs.getList'
        params={}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def blogs_postPhoto(self,blog_id,photo_id,title,description,blog_password=''):
        method_name='yupoo.blogs.postPhoto'
        params={'blog_id':blog_id,'photo_id':photo_id,'title':title,
                'description':description}
        if blog_password<>'':
            paramsp['blog_password']=blog_password

        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def contacts_getList(self,filter=''):
        method_name='yupoo.contacts.getList'
        params={}
        if filter<>'':
            params={'filter':filter}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def contacts_getPublicList(self,user_id=''):
        if user_id=='':
            user_id=self.user_id
        method_name='yupoo.contacts.getPublicList'
        params={'user_id':user_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def favorites_add(self,photo_id):
        method_name='yupoo.favorites.add'
        params={'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def favorites_getList(self,user_id='',per_page='100',page='1'):
        if user_id=='':
            user_id=self.user_id
        method_name='yupoo.favorites.getList'
        params={'per_page':per_page,'page':page}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def favorites_remove(self,photo_id):
        method_name='yupoo.favorites.remove'
        params={'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def groups_getList(self):
        method_name='yupoo.groups.getList'
        params={}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def groups_pools_getContext(self,photo_id,group_id):
        method_name='yupoo.groups.pools.getContext'
        params={'photo_id':photo_id,'group_id':group_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def groups_pools_getPhotos(self,group_id,tags='',user_id='',per_page='100',page='1'):
        method_name='yupoo.groups.pools.getPhotos'
        params={'group_id':group_id,'per_page':per_page,'page':page}
        if user_id<>'':
            params['user_id']=user_id
        if user_id<>'':
            params['tags']=tags
        return  yupoo_helper.call_method(method_name,params)

    def people_findByEmail(self,email):
        method_name='yupoo.people.findByEmail'
        params={'email':email}
        return  yupoo_helper.call_method(method_name,params)

    def people_findByUsername(self,username):
        method_name='yupoo.people.findByUsername'
        params={'username':username}
        return  yupoo_helper.call_method(method_name,params)

    def people_getUploadStatus(self):
        method_name='yupoo.people.getUploadStatus'
        params={}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_addTags(self,photo_id,tags):
        method_name='yupoo.photos.addTags'
        params={'photo_id':photo_id,'tags':tags}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_delete(self,photo_id):
        method_name='yupoo.photos.delete'
        parmas={'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_getContext(self,photo_id):
        method_name='yupoo.photos.getContext'
        parmas={'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params)

    def photos_getArchives(self,user_id='',atype='posted'):
        method_name='yupoo.photos.getArchives'
        if user_id=='':
            user_id=self.user_id
        params={'user_id':user_id,'type':atype}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_getInfo(self,photo_id):
        method_name='yupoo.photos.getInfo'
        params={'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params)

    def photos_getPerms(self,photo_id):
        method_name='yupoo.photos.getPerms'
        params={'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_removeTag(self,photo_id,tag):
        method_name='yupoo.photos.removeTag'
        params={'photo_id':photo_id,'tag':tag}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_search(self,user_id='',tags='',text='',per_page='100',page='1'):
        method_name='yupoo.photos.search'
        if user_id=='':
            user_id=self.user_id
        params={'user_id':user_id,'tags':tags,'text':text,'per_page':per_page,'page':page}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_setMeta(self,photo_id,title,description=''):
        method_name='yupoo.photos.setMeta'
        params={'photo_id':photo_id,'title':title,'description':description}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_setPerms(self,photo_id,view,comment,addmeta,blogthis,viewexif ):
        method_name='yupoo.photos.setPerms'
        params={'photo_id':photo_id,'view':view,'comment':comment,
                'addmeta':addmeta,'blogthis':blogthis,'viewexif':viewexif}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_setTags(self,photo_id,tags):
        method_name='yupoo.photos.setTags'
        params={'photo_id':photo_id,'tags':tags}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_licenses_getInfo(self):
        method_name='yupoo.photos.licenses.getInfo'
        params={}
        return  yupoo_helper.call_method(method_name,params)

    def photos_licenses_setLicense(self,photo_id,license_id):
        method_name='yupoo.photos.licenses.setLicense'
        params={'photo_id':photo_id,'license_id':license_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_notes_add(self,photo_id,note_x,note_y,note_w,note_h,note_text):
        method_name='yupoo.photos.notes.add'
        params={'photo_id':photo_id,'note_x':note_x,'note_y':note_y,
                'note_w':note_w,'note_h':note_h,'note_text':note_text}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_notes_delete(self,note_id):
        method_name='yupoo.photos.notes.delete'
        params={'note_id':note_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_notes_edit(self,note_id,note_x,note_y,note_w,note_h,note_text):
        method_name='yupoo.photos.notes.edit'
        params={'note_id':note_id,'note_x':note_x,'note_y':note_y,
                'note_w':note_w,'note_h':note_h,'note_text':note_text}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def photos_notes_getList(self,photo_id):
        method_name='yupoo.photos.notes.getList'
        params={'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params)

    def albums_addPhoto(self,album_id,photo_id):
        method_name='yupoo.albums.addPhoto'
        params={'album_id':album_id,'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def albums_create(self,title,description='',cover_id=''):
        method_name='yupoo.albums.create'
        params={'title':title,'description':description}
        if cover_id<>'':
            params['cover_id']=cover_id
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def albums_delete(self,album_id):
        method_name='yupoo.albums.delete'
        params={'album_id':album_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def albums_editMeta(self,album_id,title,description=''):
        method_name='yupoo.albums.editMeta'
        params={'album_id':album_id,'title':title,'description':description}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def albums_editPhotos(self,album_id,photo_ids,cover_id=''):
        method_name='yupoo.albums.editPhotos'
        params={'album_id':album_id,'photo_ids':photo_ids}
        if cover_id<>'':
           params['cover_id']=cover_id
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def albums_getContext(self,album_id,photo_id):
        method_name='yupoo.albums.getContext'
        params={'album_id':album_id,'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params)

    def albums_getList(self,user_id=''):
        if user_id=='':
            user_id=self.user_id
        method_name='yupoo.albums.getList'
        params={'user_id':user_id}
        return  yupoo_helper.call_method(method_name,params)

    def albums_getPhotos(self,album_id):
        method_name='yupoo.albums.getPhotos'
        params={'album_id':album_id}
        return  yupoo_helper.call_method(method_name,params)

    def albums_order(self,album_ids):
        method_name='yupoo.albums.order'
        params={'album_ids':album_ids}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def albums_removePhoto(self,album_id,photo_id):
        method_name='yupoo.albums.removePhoto'
        params={'album_id':album_id,'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params,True,self.auth_token)

    def tags_getListPhoto(self,photo_id):
        method_name='yupoo.tags.getListPhoto'
        params={'photo_id':photo_id}
        return  yupoo_helper.call_method(method_name,params)

    def tags_getListUser(self,user_id=''):
        if user_id=='':
            user_id=self.user_id
        method_name='yupoo.tags.getListUser'
        params={'user_id':user_id}
        return  yupoo_helper.call_method(method_name,params)

    def tags_getListUserPopular(self,user_id='',count='10'):
        if user_id=='':
            user_id=self.user_id
        method_name='yupoo.tags.getListUserPopular'
        params={'user_id':user_id,'count':count}
        return  yupoo_helper.call_method(method_name,params)

    def tags_getRelated(self,tag):
        method_name='yupoo.tags.getRelated'
        params={'tag':tag}
        return  yupoo_helper.call_method(method_name,params)

    def set_api_info(self,api_key,shared_secret,perms=''):
        if api_key<>'':
            config['api_key']=api_key
        if shared_secret<>'':
            config['shared_secret']=shared_secret
        if perms<>'':
            config['perms']=perms
        yupoo_helper.set_config(config)

    def bulid_photo_url(self,host,adir,filename,size_type='',secret='',originalformat=''):
        if originalformat=='':
            url='http://photo'+host+'.yupoo.com/'+adir+'/'+filename+size_type+'.jpg'
        else:
            url='http://photo'+host+'.yupoo.com/'+adir+'/'+filename+'_'+secret+'.'+originalformat
        return url

    def upload_photo(self,photo,title='',description='',tags='',album_id='',group_id='',
                 is_public='1',is_contact='1',is_friend='1',is_family='1'):

        upload_url='http://www.yupoo.com/api/upload/'
        auth_token=config['auth_token']
        params={'api_key':config['api_key'],'title':title,'description':description,'tags':tags,
               'is_public':is_public,'is_contact':is_contact,
                'is_friend':is_friend,'is_family':is_family,'auth_token':auth_token}
        if album_id<>'':
            params['album_id']=album_id
        if group_id<>'':
            params['group_id']=group_id

        api_sig=yupoo_helper.sig(yupoo_helper.join_params(params))
        params['api_sig']=api_sig
        params['photo']=photo
        opener=yupoo_helper.build_Multipart_opener()
        return opener.open(upload_url,params).read()

if __name__ == "__main__":

    pass
    #p=people()
    #photo=pho.search( p.findByUsername('chenyangcun')['id'])
    #print bulid_photo_url(photo['host'],photo['dir'],photo['filename'])
    #p=people()
    #print p.getUploadStatus()

   # f=open('c:\\chenyc.jpg','rb')
   # print upload_photo(f)

yupoo_helper.py,读取用户相关的配置文件,返回xml的解析成dict处理

#----------------------------------------------------------------------
# -*- coding: utf-8 -*-
# Name:        yupoo_helper
# Purpose:
#
# Author:      chenyc
# Created:     22-06-2007
# Copyright:   (c) chenyc 2007
# Licence:     GNU
#-----------------------------------------------------------------------
import md5,urllib,httplib,os
import urllib2,MultipartPostHandler,cookielib
try:
    import xml.etree.ElementTree as ET  #python2.5
except ImportError:
    import elementtree.ElementTree as ET

def read_config():
    config={}
    keys=[u'api_key','shared_secret','mini_token','auth_token','perms']
    try:
        f=file('yupoo.xml','r')
        root=ET.parse(f)
        f.close()
    except:
        for key in keys:
            config[key]='?'
        root=set_config(config)
    db=bulid_dic(root)
    for key in keys:
        config[key]=db[key]
    return config

def set_config(config):
    root=ET.Element('XML')
    for key,value in config.items():
        key_node=ET.SubElement(root,key)
        key_node.text=value
    tree=ET.ElementTree(root)
    f=file('yupoo.xml','w')
    tree.write(f)
    f.close()
    return root

#解析yupoo返回的xml,并生成字典
def bulid_dic(root):
    dic={}
    iter = root.getiterator()
    for element in iter:
        if element.keys():
            key_dic={}
            for name,value in element.items():
                key_dic[name]=value
            if dic.has_key(element.tag):
                if type(dic[element.tag]) is list:
                    dic[element.tag].append(key_dic)
                else:
                    ll=[dic[element.tag],key_dic]
                    dic[element.tag]=ll
            else:
                dic[element.tag]=key_dic;
        else:
            dic[element.tag]=element.text
    return dic

#生成api_sgi
def sig(string):
    return md5.new(string).hexdigest()

def call_method(method_name,params,need_sig=False,auth_token=''):
    params['api_key']=config['api_key']
    if auth_token <>'':
        params['auth_token']=auth_token
    if need_sig:
        api_sig=sig(join_params(params,method_name))
        params['api_sig']=api_sig
    params['method']=method_name
    rest_url='http://www.yupoo.com/api/rest/?'
    for key,value in params.items():
        rest_url=rest_url+key+'='+value+'&'
        xmldata=urllib.urlopen(rest_url).read()
    if xmldata<>'':
        root=ET.fromstring(xmldata)
        return bulid_dic(root)
    else:
	return {}

def join_params(params,method_name=''):
    string=config['shared_secret']
    if method_name<>'':
        params['method']=method_name
    keys=params.keys()
    keys.sort()
    for k in keys:
        string=string+k+params[k]
    return string

#common method for yupoo api
#创建opener,用来登录验证
def bulid_opener():
    cj=cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    opener.addheaders =[
        ("User-agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031107 Debian/1.5-3"),
        ("Accept", "text/html, image/jpeg, image/png, text/*, image/*, */*")]
    return opener

#创建Multipart opener,用来上传文件
def build_Multipart_opener():
    cookies = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
                                MultipartPostHandler.MultipartPostHandler)
    return opener

config=read_config()

MultipartPostHandler.py 作者:Will Holcomb,用来处理照片等附件的上传,不过这个在nokia s60的手机上不能调用成功,还需要修改一下。

#!/usr/bin/python

####
# 02/2006 Will Holcomb <wholcomb@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
"""
Usage:
  Enables the use of multipart/form-data for posting forms

Inspirations:
  Upload files in python:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

  urllib2_file:
    Fabien Seisen: <fabien@seisen.org>

Example:
  import MultipartPostHandler, urllib2, cookielib

  cookies = cookielib.CookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
                                MultipartPostHandler.MultipartPostHandler)
  params = { "username" : "bob", "password" : "riviera",
             "file" : open("filename", "rb") }
  opener.open("http://wwww.bobsite.com/upload/", params)

Further Example:
  The main function of this file is a sample which downloads a page and
  then uploads it to the W3C validator.
"""

import urllib
import urllib2
import mimetools, mimetypes
import os, stat

class Callable:
    def __init__(self, anycallable):
        self.__call__ = anycallable

# Controls how sequences are uncoded. If true, elements may be given multiple values by
#  assigning a sequence.
doseq = 1

class MultipartPostHandler(urllib2.BaseHandler):
    handler_order = urllib2.HTTPHandler.handler_order - 10 # needs to run first

    def http_request(self, request):
        data = request.get_data()
        if data is not None and type(data) != str:
            v_files = []
            v_vars = []
            try:
                 for(key, value) in data.items():
                     if type(value) == file:
                         v_files.append((key, value))
                     else:
                         v_vars.append((key, value))
            except TypeError:
                systype, value, traceback = sys.exc_info()
                raise TypeError, "not a valid non-string sequence or mapping object", traceback

            if len(v_files) == 0:
                data = urllib.urlencode(v_vars, doseq)
            else:
                boundary, data = self.multipart_encode(v_vars, v_files)
                contenttype = 'multipart/form-data; boundary=%s' % boundary
                if(request.has_header('Content-Type')
                   and request.get_header('Content-Type').find('multipart/form-data') != 0):
                    print "Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data')
                request.add_unredirected_header('Content-Type', contenttype)

            request.add_data(data)
        return request

    def multipart_encode(vars, files, boundary = None, buffer = None):
        if boundary is None:
            boundary = mimetools.choose_boundary()
        if buffer is None:
            buffer = ''
        for(key, value) in vars:
            buffer += '--%s\r\n' % boundary
            buffer += 'Content-Disposition: form-data; name="%s"' % key
            buffer += '\r\n\r\n' + value + '\r\n'
        for(key, fd) in files:
            file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
            filename = fd.name.split('/')[-1]
            contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
            buffer += '--%s\r\n' % boundary
            buffer += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename)
            buffer += 'Content-Type: %s\r\n' % contenttype
            # buffer += 'Content-Length: %s\r\n' % file_size
            fd.seek(0)
            buffer += '\r\n' + fd.read() + '\r\n'
        buffer += '--%s--\r\n\r\n' % boundary
        return boundary, buffer
    multipart_encode = Callable(multipart_encode)

    https_request = http_request

def main():
    import tempfile, sys

    validatorURL = "http://validator.w3.org/check"
    opener = urllib2.build_opener(MultipartPostHandler)

    def validateFile(url):
        temp = tempfile.mkstemp(suffix=".html")
        os.write(temp[0], opener.open(url).read())
        params = { "ss" : "0",            # show source
                   "doctype" : "Inline",
                   "uploaded_file" : open(temp[1], "rb") }
        print opener.open(validatorURL, params).read()
        os.remove(temp[1])

    if len(sys.argv[1:]) > 0:
        for arg in sys.argv[1:]:
            validateFile(arg)
    else:
        validateFile("http://www.google.com")

if __name__=="__main__":
    main()

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>