Python 小冰顏值測試

據(jù)上次的PHP版小冰顏值測試,這次學(xué)習(xí)了Python,所以寫了一個Python版本的

程序清單

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2017-05-31 18:19:42
# @Author  : sungod (xtype@qq.com)
# @Link    : http://xtype.cn/

import urllib2
import urllib
import base64
import json
import sys
import os
import time
import re
import cookielib


class Ice(object):
    """docstring for Ice"""
    ice_page = 'https://kan.msxiaobing.com/ImageGame/Portal?task=yanzhi'
    ice_api = 'https://kan.msxiaobing.com/Api/ImageAnalyze/Process?{}'
    upload_file_api = 'http://kan.msxiaobing.com/Api/Image/UploadBase64'

    def __init__(self, img_data):
        self.img_data = img_data

    def upLoadFile(self):
        imgdataBase64 = base64.b64encode(self.img_data)
        req = urllib2.Request(self.upload_file_api, imgdataBase64)

        try:
            response = urllib2.urlopen(req)
        except Exception:
            print ('Server Error.')
            exit()
        else:
            return response.read()

    def test(self):
        ret = json.loads(self.upLoadFile())
        data = {
            'MsgId': '%d063' % int(time.time()),
            'CreateTime': int(time.time()),
            'Content[imageUrl]': '%s%s' % (ret['Host'], ret['Url']),
        }

        param = {
            'service': 'yanzhi',
        }

        api = self.ice_api.format(urllib.urlencode(param))

        cookie = cookielib.CookieJar()
        handler = urllib2.HTTPCookieProcessor(cookie)
        opener = urllib2.build_opener(handler)

        opener.open(self.ice_page)

        req = urllib2.Request(
            api,
            urllib.urlencode(data),
            headers={'Referer': self.ice_page},
        )

        response = opener.open(req)
        if response.code == 200:
            ret = json.loads(response.read())
            if ret is None:
                print 'Server Error.'
                exit()
            if 'content' in ret:
                print str(ret['content']['text'].encode('utf-8'))
                print ret['content']['imageUrl']
                exit()
        print 'Server Error.'


if __name__ == '__main__':
    if len(sys.argv) == 1:
        print 'Error: iceface.py [localfile|http].'
        exit()

    # is localfile?
    if not os.path.isfile(sys.argv[-1]):
        rec = re.match(r'^[a-zA-z]+://[^\s]*$', sys.argv[-1])
        if rec is None:
            print 'Param error.'
            exit()
        try:
            print 'Download File...'
            req = urllib2.urlopen(sys.argv[-1])
        except Exception:
            print 'Download File error.'
            exit()
        else:
            imgdata = req.read()
    else:
        with open(sys.argv[-1], 'rb') as f:
            imgdata = f.read()
    o = Ice(imgdata)
    o.test()

效果圖

效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • 最近剽猂一只貓實在太火了,而且各個領(lǐng)域的大牛給他背書 事件一:李笑來老師不但在公號、還在得到訂閱專欄發(fā)出...
    Ameizeng閱讀 411評論 0 0
  • 文/李智彬秋風(fēng)卷起清波亂,霧繞輕塵滿。山巒若隱水連綿,柳岸蟬鳴水映荷花顏。閑談且飲清香茗,遙望山成嶺。落花流水相嬋...
    李智彬閱讀 423評論 1 1

友情鏈接更多精彩內(nèi)容