forked from dforel/autoSplitUploadAliImg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alipic.py
143 lines (124 loc) · 5.09 KB
/
alipic.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import requests, os, sys, time
from urllib3 import encode_multipart_formdata
from concurrent.futures import ThreadPoolExecutor, as_completed
from threading import Thread
from util import getCurrentTime
class UploadWrap(object):
_instance=None
taskMap={
"id":None
}
def startWork(self, inputM3u8, tempFilePath):
id=getCurrentTime()
(fileName,extension) = os.path.splitext(inputM3u8)
uploadTask = SplitToolMan(inputM3u8,fileName+"_out.m3u8",tempFilePath) # keepprint will print out one line every 2 seconds
uploadTask.runBackground()
self.taskMap.update({id: uploadTask})
return id
def get_status(self, id):
print(self.taskMap)
if(self.taskMap.__contains__(id)):
return True,self.taskMap[id].getStatus()
return False,"未找到"
def get_log(self, id):
if(self.taskMap.__contains__(id)):
return True,self.taskMap[id].getResultLog()
return False,"未找到"
def get_m3u8(self, id):
if(self.taskMap.__contains__(id)):
return True,self.taskMap[id].getOutputM3u8()
return False,"未找到"
def __new__(cls, *args, **kwargs):
if cls._instance is None:
obj = object.__new__(cls)
cls._instance = obj
return cls._instance
@classmethod
def instance(cls, *args, **kwargs):
if not hasattr(UploadWrap, "_instance"):
with UploadWrap._instance_lock:
if not hasattr(UploadWrap, "_instance"):
SplitFatory._instance = UploadWrap(*args, **kwargs)
return UploadWrap._instance
class SplitToolMan:
"""帮忙切割上传的工具人"""
total = 0
current=0
error_retry=0
timeout=0
inputM3u8=""
outputM3u8=""
tempFilePath=""
resultLog=[]
status="UNSTART" # RUNNING FINISHED
url = 'https://kfupload.alibaba.com/mupload'
headers = {'user-agent':'iAliexpress/6.22.1 (iPhone; iOS 12.1.2; Scale/2.00)', 'Accept':'application/json', 'Accept-Encoding':'gzip,deflate,sdch', 'Connection':'close'}
def __init__(self, inputM3u8, outputM3u8, tempFilePath='./', error_retry=10, timeout=40):
self.inputM3u8=inputM3u8
self.outputM3u8=outputM3u8
self.tempFilePath=tempFilePath
self.error_retry=error_retry
self.timeout=timeout
def m_upload(self,filename):
fakename = os.path.splitext(filename)[0] + '.jpg'
payload = {'scene':'aeMessageCenterV2ImageRule', 'name':fakename, 'file': (fakename,open(self.tempFilePath+'/'+filename,'rb').read())}
encode_data = encode_multipart_formdata(payload)
data = encode_data[0]
self.headers['Content-Type'] = encode_data[1]
self.current = self.current + 1
for _ in range(self.error_retry):
try:
r = requests.post(self.url, headers=self.headers, data=data, timeout = self.timeout)
self.appendLog(r.text)
if r and 'url' in r.text:
#print(filename + " upload")
#print(r.json()['url'])
self.appendLog("total:{0},current:{1}".format(self.total,self.current));
return r.json()['url']
else:
#失败的时候等待1秒重试
time.sleep(1)
raise Exception('upload filename:{1} error'.format(filename))
except:
self.appendLog('Failed to upload ' + filename + '\n')
continue
self.appendLog("total:{0},current:{1}".format(self.total,self.current));
return filename + ''
def appendLog(self, log):
self.resultLog.append(log)
def getStatus(self):
return self.status
def getResultLog(self):
return self.resultLog
def getOutputM3u8(self):
return self.outputM3u8
def startWork(self):
self.status = "RUNNING"
m3u8 = open(self.inputM3u8)
new_m3u8 = open(self.outputM3u8, 'w')
file_upload = {t.strip():'' for t in m3u8.readlines() if t[0]!='#' and t.find("http")<0}
m3u8.seek(0)
# 如果有失败的,可以调节下方的workers数量,减少一些试试看
#print(file_upload);
self.total = len(file_upload)
self.current = 0;
executor = ThreadPoolExecutor(max_workers=4)
futures = {executor.submit(self.m_upload, filename):filename for filename in file_upload.keys()}
for future in as_completed(futures):
file_upload[futures[future]] = future.result()
for line in m3u8:
if line[0] != '#' and line.find("http")<0:
new_m3u8.write(file_upload[line.strip()] + '\n')
else:
new_m3u8.write(line)
self.status = "FINISHED"
self.appendLog("Complete")
def runBackground(self):
t1 = Thread(target=self.startWork)
t1.start()
if __name__ == '__main__':
toolMan=SplitToolMan(sys.argv[1],sys.argv[2],sys.argv[2])
toolMan.startWork()
#print("Complete")