利用python将iOS安装包上传到蒲公英的脚本

作者: shaneZhang 分类: ios技术 发布时间: 2019-01-05 19:43

本脚本有两个参数,一个是ipa的路径,一个是ipa的名字,需要以xxx.ipa结尾。另外本脚本中还需要自行修改蒲公英上传的appkey和userkey和发送邮件函数中的邮件smtp相关的信息。

# coding=utf-8
import urllib2
import time
import os
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import json
import sys

# 蒲公英应用上传地址
url = 'https://www.pgyer.com/apiv2/app/upload'
# 蒲公英提供的 用户Key
uKey = ''
# 蒲公英提供的 API Key
_api_key = ''
# 安装应用时需要输入的密码,这个可不填
installPassword = ''

paramsCount = len(sys.argv)
if (paramsCount != 3):
    print "本文件只支持以下的二个参数同时配置,缺一不可"
    print "第一个参数:ipa的路径"
    print "第二个参数:ipa的名字,如xx.ipa"
    exit()

ipa_path = sys.argv[1]
ipa_name = sys.argv[2]

if os.path.isfile(ipa_path):
    print "file is founded"
else:
    print "file not found"
    exit()
# 上传文件的文件名(这个可随便取,但一定要以 ipa 结尾)
file_name = ipa_name
# ipa 文件路径
ipa_file_path = ipa_path
print ipa_file_path


# 请求字典编码
def _encode_multipart(params_dict):
    boundary = '----------%s' % hex(int(time.time() * 1000))
    data = []
    for k, v in params_dict.items():
        data.append('--%s' % boundary)
        if hasattr(v, 'read'):
            filename = getattr(v, 'name', '')
            content = v.read()
            decoded_content = content.decode('ISO-8859-1')
            data.append('Content-Disposition: form-data; name="%s"; filename="aa.ipa"' % k)
            data.append('Content-Type: application/octet-stream\r\n')
            data.append(decoded_content)
        else:
            data.append('Content-Disposition: form-data; name="%s"\r\n' % k)
            data.append(v if isinstance(v, str) else v.decode('utf-8'))
    data.append('--%s--\r\n' % boundary)
    return '\r\n'.join(data), boundary


# 处理 蒲公英 上传结果
def handle_resule(result):
    print 'upload success'
    print result
    json_result = json.loads(result)
    if json_result['code'] is 0:
        send_Email(json_result)


# 发送邮件
def send_Email(json_result):
    appName = json_result['data']['buildName']
    appKey = json_result['data']['buildKey']
    appVersion = json_result['data']['buildVersion']
    appBuildVersion = json_result['data']['buildVersionNo']
    appShortcutUrl = json_result['data']['buildShortcutUrl']
    # 邮件接受者
    mail_receiver = ['']
    # 根据不同邮箱配置 host,user,和pwd
    mail_host = ''
    mail_user = ''
    mail_pwd = ''
    mail_to = ','.join(mail_receiver)
    msg = MIMEMultipart()
    environsString = '<h3>本次打包相关信息</h3><p>'
    environsString += '<p>公司内网下载地址: http://10.100.16.252:8008/examples/' + file_name + '</p>'
    environsString += '<p>你也可从蒲公英网站在线安装 : ' + 'http://www.pgyer.com/' + str(
        appShortcutUrl) + '   密码 : ' + installPassword + '</p>'
    environsString += '<li><a href="itms-services://?action=download-manifest&url=https://ssl.pgyer.com/app/plist/' + str(
        appKey) + '">点我直接安装</a></li>'
    message = environsString
    body = MIMEText(message, _subtype='html', _charset='utf-8')
    msg.attach(body)
    msg['To'] = mail_to
    msg['from'] = mail_user
    msg['subject'] = '最新打包文件'

    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user, mail_pwd)
        s.sendmail(mail_user, mail_receiver, msg.as_string())
        s.close()
        print 'send email success'
    except Exception, e:
        print e

#############################################################
def upload_to_pgy():
    # 请求参数字典
    params = {
        'uKey': uKey,
        '_api_key': _api_key,
        'file': open(ipa_file_path, 'rb'),
        'buildInstallType': '2'
    }
    print params
    coded_params, boundary = _encode_multipart(params)
    req = urllib2.Request(url, coded_params.encode('ISO-8859-1'))
    req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
    try:
        resp = urllib2.urlopen(req)
        body = resp.read().decode('utf-8')
        handle_resule(body)
    except urllib2.HTTPError as e:
        print(e.fp.read())
        #如果上传失败就重新上传
        upload_to_pgy()


if __name__ == "__main__":
    upload_to_pgy()

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注