import json
import time
import requests
import logging

logger = logging.getLogger(__name__)


######################################################################

class YDMHttpV2:
    apiurl = 'http://api.yundama.com/api.php'
    username = ''
    password = ''
    appid = ''
    appkey = ''

    def __init__(self, username, password, appid, appkey):
        self.username = username
        self.password = password
        self.appid = str(appid)
        self.appkey = appkey

    def request(self, fields, files={}):
        response = self.post_url(self.apiurl, fields, files)
        response = json.loads(response)
        return response

    def balance(self):
        data = {'method': 'balance', 'username': self.username, 'password': <PASSWORD>, 'appid': self.appid,
                'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['balance']
        else:
            return -9001

    def login(self):
        data = {'method': 'login', 'username': self.username, 'password': <PASSWORD>, 'appid': self.appid,
                'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['uid']
        else:
            return -9001

    def upload(self, filebin, codetype, timeout):
        data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
        file = {'file': filebin}
        response = self.request(data, file)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['cid']
        else:
            return -9001

    def result(self, cid):
        data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'cid': str(cid)}
        response = self.request(data)
        return response and response['text'] or ''

    def decode(self, filebin, codetype, timeout):
        cid = self.upload(filebin, codetype, timeout)
        if (cid > 0):
            for i in range(0, timeout):
                result = self.result(cid)
                if (result != ''):
                    return cid, result
                else:
                    time.sleep(1)
            return -3003, ''
        else:
            return cid, ''

    def post_url(self, url, fields, files={}):
        res = requests.post(url, files=files, data=fields)
        return res.text


def verify_captcha(filebin, codetype=5000):
    username = 'afsen'
    password = '<PASSWORD>'
    appid = 4790
    appkey = '0b83d2278af34671b5512e521743f921'
    timeout = 60
    yundama = YDMHttpV2(username, password, appid, appkey)
    uid = yundama.login()
    logger.info('uid: %s' % uid)
    balance = yundama.balance()
    logger.info('balance: %s' % balance)
    cid, result = yundama.decode(filebin, codetype, timeout)
    logger.info('cid: %s, result: %s' % (cid, result))
    return cid, result


if __name__ == "__main__":
    verify_captcha(iname='')

import json
import time
import requests
import logging

logger = logging.getLogger(__name__)


######################################################################

class YDMHttpV2:
    apiurl = 'http://api.yundama.com/api.php'
    username = ''
    password = ''
    appid = ''
    appkey = ''

    def __init__(self, username, password, appid, appkey):
        self.username = username
        self.password = password
        self.appid = str(appid)
        self.appkey = appkey

    def request(self, fields, files={}):
        response = self.post_url(self.apiurl, fields, files)
        response = json.loads(response)
        return response

    def balance(self):
        data = {'method': 'balance', 'username': self.username, 'password': <PASSWORD>, 'appid': self.appid,
                'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['balance']
        else:
            return -9001

    def login(self):
        data = {'method': 'login', 'username': self.username, 'password': <PASSWORD>, 'appid': self.appid,
                'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['uid']
        else:
            return -9001

    def upload(self, filebin, codetype, timeout):
        data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
        file = {'file': filebin}
        response = self.request(data, file)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['cid']
        else:
            return -9001

    def result(self, cid):
        data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'cid': str(cid)}
        response = self.request(data)
        return response and response['text'] or ''

    def decode(self, filebin, codetype, timeout):
        cid = self.upload(filebin, codetype, timeout)
        if (cid > 0):
            for i in range(0, timeout):
                result = self.result(cid)
                if (result != ''):
                    return cid, result
                else:
                    time.sleep(1)
            return -3003, ''
        else:
            return cid, ''

    def post_url(self, url, fields, files={}):
        res = requests.post(url, files=files, data=fields)
        return res.text


def verify_captcha(filebin, codetype=5000):
    username = 'afsen'
    password = '<PASSWORD>'
    appid = 4790
    appkey = '0b83d2278af34671b5512e521743f921'
    timeout = 60
    yundama = YDMHttpV2(username, password, appid, appkey)
    uid = yundama.login()
    logger.info('uid: %s' % uid)
    balance = yundama.balance()
    logger.info('balance: %s' % balance)
    cid, result = yundama.decode(filebin, codetype, timeout)
    logger.info('cid: %s, result: %s' % (cid, result))
    return cid, result


if __name__ == "__main__":
    verify_captcha(iname='')

######################################################################