-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_to_pgyer.py
90 lines (79 loc) · 3.22 KB
/
upload_to_pgyer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#coding=utf-8
from __future__ import print_function
import os
import requests
import time
import re
from datetime import datetime
# configuration for pgyer
USER_KEY = "917e5933d************023b83e29dc"
API_KEY = "d4e418428e7************e24310b21"
PGYER_UPLOAD_URL = "https://www.pgyer.com/apiv1/app/upload"
def parseUploadResult(jsonResult):
print('post response: %s' % jsonResult)
resultCode = jsonResult['code']
if resultCode != 0:
print("Upload Fail!")
raise Exception("Reason: %s" % jsonResult['message'])
print("Upload Success")
appKey = jsonResult['data']['appKey']
app_download_page_url = "https://www.pgyer.com/%s" % appKey
print("appDownloadPage: %s" % app_download_page_url)
return app_download_page_url
def uploadIpaToPgyer(ipaPath, updateDescription):
print("Begin to upload ipa to Pgyer: %s" % ipaPath)
headers = {'enctype': 'multipart/form-data'}
payload = {
'uKey': USER_KEY,
'_api_key': API_KEY,
'publishRange': '2', # 直接发布
'isPublishToPublic': '2', # 不发布到广场
'updateDescription': updateDescription # 版本更新描述
}
try_times = 0
while try_times < 5:
try:
print("uploading ... %s" % datetime.now())
ipa_file = {'file': open(ipaPath, 'rb')}
resp = requests.post(PGYER_UPLOAD_URL, headers=headers, files=ipa_file, data=payload)
assert resp.status_code == requests.codes.ok
result = resp.json()
app_download_page_url = parseUploadResult(result)
return app_download_page_url
except requests.exceptions.ConnectionError:
print("requests.exceptions.ConnectionError occured!")
time.sleep(60)
print("try again ... %s" % datetime.now())
try_times += 1
except Exception as e:
print("Exception occured: %s" % str(e))
time.sleep(60)
print("try again ... %s" % datetime.now())
try_times += 1
if try_times >= 5:
raise Exception("Failed to upload ipa to Pgyer, retried 5 times.")
def parseQRCodeImageUrl(app_download_page_url):
try_times = 0
while try_times < 3:
try:
response = requests.get(app_download_page_url)
regex = '<img src=\"(.*?)\" style='
m = re.search(regex, response.content)
assert m is not None
appQRCodeURL = m.group(1)
print("appQRCodeURL: %s" % appQRCodeURL)
return appQRCodeURL
except AssertionError:
try_times += 1
time.sleep(60)
print("Can not locate QRCode image. retry ... %s: %s" % (try_times, datetime.now()))
if try_times >= 3:
raise Exception("Failed to locate QRCode image in download page, retried 3 times.")
def saveQRCodeImage(app_download_page_url, output_folder):
appQRCodeURL = parseQRCodeImageUrl(app_download_page_url)
response = requests.get(appQRCodeURL)
qr_image_file_path = os.path.join(output_folder, 'QRCode.png')
if response.status_code == 200:
with open(qr_image_file_path, 'wb') as f:
f.write(response.content)
print('Save QRCode image to file: %s' % qr_image_file_path)