云打码 1

#coding:utf-8
import http.client, mimetypes, urllib, json, time, requests


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

class YDMHttp:
    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': self.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': self.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, filename, 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': filename}
        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, filename, codetype, timeout):
        cid = self.upload(filename, 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 report(self, cid):
        data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
        response = self.request(data)
        if (response):
            return response['ret']
        else:
            return -9001

    def post_url(self, url, fields, files=[]):
        for key in files:
            files[key] = open(files[key], 'rb');
        res = requests.post(url, files=files, data=fields)
        return res.text


class Ydm:

    def __init__(self,image_path):
        self.username = '你的账号'
        self.password = '你的密码'

        # 下面不用改
        self.appid = 3938
        self.appkey = '00f263175a261b9ae407ef8abf1cd040'
        self.filename = image_path
        self.codetype = 1004
        self.timeout = 60

    def get_text(self):
        yundama = YDMHttp(self.username, self.password, self.appid, self.appkey)
        # 登陆云打码
        uid = yundama.login();
        print('uid: %s' % uid)

        # 查询余额
        balance = yundama.balance();
        print('balance: %s' % balance)

        # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
        cid, result = yundama.decode(self.filename, self.codetype, self.timeout);
        print('cid: %s, result: %s' % (cid, result))
        if result:
            return result

云打码

import time

import requests
from requests.exceptions import ConnectionError

class Yundama():
    def __init__(self, username, password, app_id, app_key, api_url=YUNDAMA_API_URL):
        self.username = username
        self.password = password
        self.app_id = str(app_id) if not isinstance(app_id, str) else app_id
        self.app_key = app_key
        self.api_url = api_url

    def login(self):
        """
        登录云打码账户
        :return:
        """
        try:
            data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.app_id,
                    'appkey': self.app_key}
            response = requests.post(self.api_url, data=data)
            if response.status_code == 200:
                result = response.json()
                print(result)
                if 'ret' in result.keys() and result.get('ret') < 0:
                    return self.error(result.get('ret'))
                else:
                    return result
            return None
        except ConnectionError:
            return None

    def upload(self, files, timeout, code_type):
        """
        上传验证码得到识别结果
        :param files:
        :param timeout:
        :param code_type:
        :return:
        """
        try:
            data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.app_id,
                    'appkey': self.app_key, 'codetype': str(code_type), 'timeout': str(timeout)}
            response = requests.post(self.api_url, data=data, files=files)
            if response.status_code == 200:
                return response.json()
            return None
        except ConnectionError:
            return None

    def retry(self, cid, try_count=1):
        """
        临时识别不出, 传入cid重试
        :param cid: 验证码ID
        :param try_count: 重试次数
        :return: 验证码结果
        """
        if try_count >= YUNDAMA_MAX_RETRY:
            return None
        print('Retrying: ', cid, 'Count: ', try_count)
        time.sleep(2)
        try:
            data = {'method': 'result', 'cid': cid}
            print(data)
            response = requests.post(self.api_url, data=data)
            if response.status_code == 200:
                result = response.json()
                print(result)
                if 'ret' in result.keys() and result.get('ret') < 0:
                    print(self.error(result.get('ret')))
                if result.get('ret') == 0 and 'text' in result.keys():
                    return result.get('text')
                else:
                    return self.retry(cid, try_count + 1)
            return None
        except ConnectionError:
            return None

    def identify(self, file=None, stream=None, timeout=60, code_type=5000):
        """
        主函数
        :param file: 文件名
        :param stream: 文件流, 优先于文件名
        :param timeout: 超时时间
        :param code_type: 验证码类型
        :return: 识别结果
        """
        if stream:
            files = {'file': stream}
        elif file:
            files = {'file': open(file, 'rb')}
        else:
            return None
        result = self.upload(files, timeout, code_type)
        if 'ret' in result.keys() and result.get('ret') < 0:
            print(self.error(result.get('ret')))
        if result.get('text'):
            print('验证码识别成功', result.get('text'))
            return result.get('text')
        else:
            return self.retry(result.get('cid'))

    def error(self, code):
        """
        报错原因
        :param code: 错误码
        :return: 错误原因
        """
        map = {
            -1001: '密码错误',
            -1002: '软件ID/密钥有误',
            -1003: '用户被封',
            -1004: 'IP被封',
            -1005: '软件被封',
            -1006: '登录IP与绑定的区域不匹配',
            -1007: '账号余额为零',
            -2001: '验证码类型有误',
            -2002: '验证码图片太大',
            -2003: '验证码图片损坏',
            -2004: '上传验证码图片失败',
            -3001: '验证码ID不存在    ',
            -3002: '验证码还在识别',
            -3003: '验证码识别超时',
            -3004: '验证码看不清',
            -3005: '验证码报错失败',
            -4001: '充值卡号不正确或已使用',
            -5001: '注册用户失败'
        }
        return '云打码' + map.get(code)


if __name__ == '__main__':
    ydm = Yundama(YUNDAMA_USERNAME, YUNDAMA_PASSWORD, YUNDAMA_APP_ID, YUNDAMA_APP_KEY)
    result = ydm.identify(file='getimage.jpg')
    print(result)
HTTPX 基础教程-新乡seo|网站优化,网站建设_微信公众号:zeropython—昊天博客