开发者

upload picture through external website to gravatar profile

开发者 https://www.devze.com 2023-02-06 11:32 出处:网络
I was wondering if it is possible to create an upload function to u开发者_如何学Gopload picture through my own site to the gravatar site?Yes, this is possible. See http://en.gravatar.com/site/implemen

I was wondering if it is possible to create an upload function to u开发者_如何学Gopload picture through my own site to the gravatar site?


Yes, this is possible. See http://en.gravatar.com/site/implement/xmlrpc/ , specifically the grav.saveData or grav.SaveUrl calls.


Yes it's possible!

import base64

from xmlrpclib import ServerProxy, Fault
from hashlib import md5


class GravatarXMLRPC(object):
    API_URI = 'https://secure.gravatar.com/xmlrpc?user={0}'

    def __init__(self, request, password=''):
        self.request = request
        self.password = password
        self.email = sanitize_email(request.user.email)
        self.email_hash = md5_hash(self.email)
        self._server = ServerProxy(
            self.API_URI.format(self.email_hash))

    def saveData(self, image):
        """ Save binary image data as a userimage for this account """           
        params = { 'data': base64_encode(image.read()), 'rating': 0, }
        return self._call('saveData', params)
        #return self.useUserimage(image)

    def _call(self, method, params={}):
        """ Call a method from the API, gets 'grav.' prepended to it. """
        args = { 'password': self.password, }
        args.update(params)

        try:
            return getattr(self._server, 'grav.' + method, None)(args)
        except Fault as error:
            error_msg = "Server error: {1} (error code: {0})"
            print error_msg.format(error.faultCode, error.faultString)


def base64_encode(obj):
    return base64.b64encode(obj)

def sanitize_email(email):
    return email.lower().strip()

def md5_hash(string):
    return md5(string.encode('utf-8')).hexdigest()

Just call the class in your view :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号