Android 借助 Python 实现自动上传蒲公英

来源:互联网 发布:linux stat struct 编辑:程序博客网 时间:2024/03/29 19:34

感言下:程序猿是懒出来的,一切动力来源于方便

使用前提

  • 本地电脑上安装Python 和 pip
  • 使用pip安装requests库( pip3 install requests )

附上蒲公英上传App文档 : https://www.pgyer.com/doc/view/api#uploadApp

万事俱备只欠东风,codeing -_- ~~

# UploadToPGY.py 主要上传文件# coding=utf-8# encoding=utf-8import requestsimport sysdef uploadFile():    # 获取运行传递过来的参数    _upload_url = sys.argv[1]    _api_key = sys.argv[2]    _apk_path = sys.argv[3]    _description = sys.argv[4]    # 上传apk    try:        file = {'file': open(_apk_path, 'rb')}        param = {'_api_key': _api_key, 'updateDescription':        _description}        req=requests.post(url=_upload_url,files=file,data=param,verify=False)        print(req.status_code)    except Exception as e:        print("upload:" + e)if __name__ == '__main__':    uploadFile()

把文件放在AndroidStudio工程目录下,配置build.gradle文件

task releaseToPGYER {        dependsOn 'assembleRelease'        doLast {            def _upload_url = "https://www.pgyer.com/apiv2/app/upload"            def _api_key = "$apikey(填写蒲公英上的key)"            def _apk_path = "$buildDir/outputs/apk/app-release.apk"            def _update_description = "Python auto upload -_- "            //执行Python脚本            def process = "python UploadToPGY.py ${_upload_url} ${_api_key} ${_apk_path} ${_update_description}".execute()            println "开始上传至蒲公英"            //获取Python脚本日志,便于出错调试            ByteArrayOutputStream result = new ByteArrayOutputStream()            def inputStream = process.getInputStream()            byte[] buffer = new byte[1024]            int length            while ((length = inputStream.read(buffer)) != -1) {                result.write(buffer, 0, length)            }            println(result.toString("UTF-8"))            println "上传结束"        }    }

最后嘛,当然是runing

在终端输入 ./gradlew releaseToPGYER

灵感来源:http://www.jianshu.com/p/0ec4c4e132e7

阅读全文
0 0
原创粉丝点击