-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxhttp.py
361 lines (358 loc) · 13.7 KB
/
wxhttp.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/python
#encoding=utf-8
import json
import requests
import sys, os, zipfile
import time
from flask import Flask,request
from shutil import copy,rmtree
app = Flask(__name__)
class httpwx:
appid=''
newcon = '''{
"description": "项目配置文件",
"packOptions": {
"ignore": []
},
"setting": {
"urlCheck": false,
"es6": false,
"minified": true
},
"compileType": "miniprogram",
"libVersion": "2.9.2",
"appid": "miniappid",
"projectname": "11",
"simulatorType": "wechat",
"simulatorPluginLibVersion": {},
"condition": {
"search": {
"current": -1,
"list": []
},
"conversation": {
"current": -1,
"list": []
},
"game": {
"current": -1,
"list": []
},
"miniprogram": {
"current": -1,
"list": []
}
}
}'''
http_url=''
pid=''
desc=''
version=''
dir='./mp-weixin'
is_plugins=0
port=00
logdir=r'/root/web/python/update.log'
maindir=r'/root/web/python/'
configdata = ['域名', 'appid', '名称', '1.3.3', '简介', './private.key']
def vs(self,dir):
if os.path.exists(self.logdir):
vs = open(self.logdir, 'r')
con = vs.read()
vs.close()
print('版本', con)
with open(dir + '/update.log', 'w') as f:
f.write(str(con))
f.close()
def unzip_single(self,src_file, dest_dir, password):
''' 解压单个文件到目标文件夹。
'''
if password:
password = password.encode()
zf = zipfile.ZipFile(src_file)
try:
zf.extractall(path=dest_dir, pwd=password)
except RuntimeError as e:
print(e)
zf.close()
def unzip_all(self,source_dir, dest_dir, password):
if not os.path.isdir(source_dir): # 如果是单一文件
self.unzip_single(source_dir, dest_dir, password)
else:
it = os.scandir(source_dir)
for entry in it:
if entry.is_file() and os.path.splitext(entry.name)[1] == '.zip':
self.unzip_single(entry.path, dest_dir, password)
def checks(self):
# 获取最新的版本
files = open('update.log', 'w+')
indexversion = files.read()
rr = requests.get('http://www.baidu.com/program_version')
res = json.loads(rr.content)
resdata = res
if str(resdata['version']) != str(indexversion):
files.truncate()
files.write(str(resdata['version']))
# 下载包
download_url = resdata['url']
zipfile = requests.get(download_url)
path = './mp-weixin.zip'
with open(path, 'wb') as f:
for chunk in zipfile.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
self.unzip_all('mp-weixin.zip', os.getcwd(), '')
files.close()
def __init__(self,appid='123123',http_url='123123',pid='123',version='1.0.0',desc='1.0.0',is_plugins=0):
self.desc=desc
self.appid=appid
self.http_url=http_url
self.pid=pid
self.version=version
self.checks()
self.is_plugins=is_plugins
#获取端口号
file=open(r'/root/.config/wechat_web_devtools/Default/.ide')
con=file.read()
self.port=str(con)
#创建文件
dir=self.maindir+str(self.appid)
if os.path.exists(dir)==False:
os.mkdir(dir)
#拷贝
# os.mkdir(dir)
self.copy_die(self.maindir+'mp-weixin',dir)
#写入版本
self.vs(dir)
# self.checkplu()
else:
#判断是否需要更新
if os.path.exists(dir+'/update.log') and os.path.exists(self.logdir):
vss = open(self.logdir, 'r')
con = vss.read()
vss.close()
oldvs=open(dir+'/update.log','r+')
oldcon=oldvs.read()
if str(con) != str(oldcon):
oldvs.write(str(con))
oldvs.close()
#版本不一致 删除源目录 在copy
rootdir = dir
filelist = os.listdir(rootdir)
for f in filelist:
filepath = os.path.join(rootdir, f)
if os.path.isfile(filepath):
os.remove(filepath)
elif os.path.isdir(filepath):
rmtree(filepath, True)
self.copy_die(self.maindir+'mp-weixin', dir)
self.vs(dir)
# self.checkplu()
self.dir=dir
#干掉直播关键字 和直播连接
def checkfile(self,rootdir):
filelist = os.listdir(rootdir)
for f in filelist:
filepath = os.path.join(rootdir, f)
if os.path.isfile(filepath):
try:
files = open(filepath, 'r', encoding='utf-8')
con = files.read()
rr = con.replace('直播', '')
rr = rr.replace('"pages/live/list",', ' ')
rr = rr.replace('"pages/live/index",', ' ')
rr = rr.replace('"pages/live/streaming",', ' ')
files.close()
filesd = open(filepath, 'w')
filesd.write(rr)
filesd.close()
except:
print(filepath)
elif os.path.isdir(filepath):
self.checkfile(filepath)
def copy_die(self,dir, newdir):
for p in os.listdir(dir):
print(p)
filepath = newdir + '/' + p
oldpath = dir + '/' + p
if os.path.isdir(oldpath):
os.mkdir(filepath)
self.copy_die(oldpath, filepath)
if os.path.isfile(oldpath):
copy(oldpath, filepath)
def check(self):
if os.path.exists('./install.txt'):
file=open('./install.txt','r')
con=file.read()
rd=con.split('-')
file.close()
if str(rd[0])==self.appid:
# self.checkplu()
return False
return True
else:
with open('./install.txt','w') as f:
f.write(str(self.appid)+'-'+str(time.time()))
f.close()
self.updates()
return False
def updates(self):
a = 0
# 替换文件
newcons = self.newcon.replace('miniappid', self.appid)
file = open(self.dir + '/project.config.json', 'w+')
file.write(newcons)
file.close()
# 替换域名
one_file = self.dir + '/common/vendor.js'
self.tihuan(one_file, self.configdata[0], self.http_url)
# 替换小程序ID
self.tihuan(one_file, 'uniacid:"1"', 'uniacid:"' + str(self.pid) + '"')
def checkplu(self):
# 验证是否需要直播插件
if int(self.is_plugins) == 1:
file = open(self.dir + '/app.json', 'r', encoding='utf-8')
con = file.read()
arr = json.loads(con)
print('需要')
arr['subPackages'][0]['plugins'] = {
"live-player-plugin": {"version": "1.0.19", "provider": 'wx2b03c6e691cd7370'}}
file = open(self.dir + '/app.json', 'w+', encoding='utf-8')
file.write(json.dumps(arr))
else:
#替换所有的文件
self.checkfile(self.dir)
# 不休啊
file = open(self.dir + '/app.json', 'r', encoding='utf-8')
con = file.read()
arr = json.loads(con)
arr['subPackages'][0]['plugins'] = {}
file = open(self.dir + '/app.json', 'w+', encoding='utf-8')
file.write(json.dumps(arr))
def rmdirs(self,top):
for root, dirs, files in os.walk(top, topdown=False):
# 先删除文件
for name in files:
os.remove(os.path.join(root, name))
# 再删除空目录
for name in dirs:
os.rmdir(os.path.join(root, name))
#登陆
def login(self):
check = self.check()
if check:
return json.dumps({'code': 1, 'msg': '暂无流量,请稍后再试'})
else:
dir = self.maindir+'/loginresult/' + str(self.appid) + '/loginresult.json'
if os.path.exists(self.maindir+'loginresult/' + str(self.appid)) == False:
os.mkdir(self.maindir+'loginresult/' + str(self.appid))
url='http://127.0.0.1:'+str(self.port)+'/v2/login?qr-format=base64&result-output=%2Froot%2Fweb%2Fpython%2Floginresult%2F'+str(self.appid)+'%2Floginresult.json'
r=requests.get(url)
print('结果',r.text)
datas=json.loads(r.text)
return json.dumps({'code':0,'msg':'成功','data':datas['qrcode']})
def logincheck(self):
appid=self.appid
dir=self.maindir+'loginresult/'+str(self.appid)+'/loginresult.json'
con=''
if os.path.exists(dir):
with open(dir,'r') as file:
con=file.read()
file.close()
return json.dumps({'code':0,'msg':'获取成功','data':str(con)})
#预览
def preview(self):
self.checkplu()
self.updates()
projecturl = self.dir
print('项目目录',projecturl)
url='http://127.0.0.1:'+str(self.port)+'/v2/preview?project='+str(projecturl)+'&qr-format=base64'
r=requests.get(url)
self.reduction()
if r.text.find('{') >-1:
self.reduction()
con=json.loads(r.text)
return json.dumps({'code':1,'msg':con['message']})
return json.dumps({'code': 0, 'msg': '成功', 'data': {'base64': r.text}})
#上传
def upload(self):
self.checkplu()
projecturl = self.dir
out_put=self.maindir+'output.json'
url = 'http://127.0.0.1:' + str(self.port) + '/v2/upload?project=' + str(projecturl) + '&version=v'+str(self.version)+'&desc="'+str(self.desc)+'"&info-output='+str(out_put)
r = requests.get(url)
self.reduction()
#获取预览图片
res=json.loads(r.text)
if isinstance(res,str)==False and 'code' in res:
return json.dumps({'code':1,'msg':res['message']})
return json.dumps({'code': 0, 'msg': '成功', 'data': {'base64': res}})
def reduction(self):
one_file = self.dir + '/project.config.json'
# 替换APPid
self.tihuan(one_file, self.appid, self.configdata[1])
# 替换域名
one_file = self.dir + '/common/vendor.js'
self.tihuan(one_file, self.http_url, self.configdata[0])
# 替换小程序ID
self.tihuan(one_file, 'uniacid:"' + str(self.pid) + '"','uniacid:"1"')
try:
# 删除文件
if os.path.exists('./install.txt'):
os.remove('./install.txt')
dir = self.maindir + 'loginresult/' + str(self.appid) + '/loginresult.json'
if os.path.exists(dir):
os.remove(dir)
disr = self.maindir + str(self.appid)
if os.path.exists(disr):
self.rmdirs(disr)
os.removedirs(disr)
print(1)
except:
print('修改失败2')
def tihuan(self, file_path, sreach, replace):
try:
file = file_path
files = open(file, 'r', encoding='UTF-8')
content = files.read().encode('utf-8').decode('UTF-8').strip()
files.close()
new_two_file = open(file, 'w', encoding='UTF-8')
new_content = content.replace(sreach, replace)
new_two_file.write(new_content)
new_two_file.close()
file = file_path
files = open(file, 'r', encoding='UTF-8')
content = files.read().encode('utf-8').decode('UTF-8').strip()
files.close()
new_two_file = open(file, 'w', encoding='UTF-8')
new_content = content.replace(sreach, replace)
new_two_file.write(new_content)
new_two_file.close()
except:
print('修改失败')
@app.route('/apis', methods=['GET'])
def mains():
#http://192.168.100.6:5002/apis?action=login&appid=wxcc367d8b0374e4dd&http_url=192.168.100.6&pid=1
action = request.args.get("action")
appid = request.args.get("appid") #appid
http_url = request.args.get("http_url") #替换域名
pid = request.args.get("pid") #小程序id
version = request.args.get("version") #版本号
desc = request.args.get("desc") #描述
is_plugins = request.args.get("is_plugins") #1 需要插件 0不需有
if is_plugins:
print('需要插件')
else:
is_plugins=0
mm={}
a = httpwx(appid, http_url,pid,version,desc,is_plugins)
if(action=='login'):
mm=a.login()
elif action=='upload':
mm=a.upload()
elif action=='preview':
mm = a.preview()
else:
mm=a.logincheck()
return mm
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5002,debug=True)