Python. Доступ по api к mutagen

← разместить еще код

Леплено на коленке вторпях, не pythonic )))

Расшаренный код:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xmlrpclib
from time import sleep

####################################################################
class CookieTransport(xmlrpclib.Transport):
    cookies = []

    #def send_cookies(self, connection):
        #if self.cookies:
            #for cookie in self.cookies:
                #connection.putheader("Cookie", cookie)
                
    def send_cookies(self, connection):
        if self.cookies:
            cookie_str =  ''
            for cookie in self.cookies:
                cookie_str = cookie_str + cookie + ';'
            connection.putheader("Cookie", cookie_str[:-1])    

    def request(self, host, handler, request_body, verbose=0):
        self.verbose = verbose
        h = self.make_connection(host)

        self.send_request(h, handler, request_body)
        self.send_host(h, host)
        print 'cookies:', self.cookies
        self.send_cookies(h)
        self.send_user_agent(h)
        self.send_content(h, request_body)

        response = h.getresponse()

        for header in response.msg.getallmatchingheaders("Set-Cookie"):
            val = header.split(": ", 1)[1]
            cookie = val.split(";", 1)[0]
            #cookie = val
            if not cookie in self.cookies:
                self.cookies.append(cookie)

        return self.parse_response(response)	
	
#----------------------------------------------------------------------
if __name__ == "__main__":   
    mtgurl = 'http://mutagen.ru/?xmlrpc'
    api = xmlrpclib.ServerProxy(mtgurl, transport=CookieTransport(), verbose=1)
    m_id = api.mutagen.login('qwerty','qwerty1234')
    keytxt = u"взять кредит"
    ans = {'status':''}
    while not ans['status'] in ('finish','error'):
        ans =  api.mutagen.parser.get(keytxt.encode('utf-8'), 'wordstat_key_50')
        print ans['status']
        sleep(5)