-
Notifications
You must be signed in to change notification settings - Fork 120
/
run_server.py
321 lines (291 loc) · 11.7 KB
/
run_server.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
import json
import random
import time
import os
import traceback
import requests
from flask import Flask, render_template, request, url_for, redirect
from pymongo import MongoClient
import sys
from login import Login
import re
import configparser
import math
from flask_limiter import Limiter
app = Flask(__name__)
# 限流
limiter = Limiter(app, default_limits=["200 per day", "50 per hour"])
config = configparser.ConfigParser()
config.read('config.ini', encoding='utf-8')
api_config = config['Web_API']
path_config = config['Path']
if sys.argv[1] == 'test':
client = MongoClient(host=path_config['Mongo_host_local'], port=int(path_config['Mongo_port']))
else:
client = MongoClient(host=path_config['Mongo_host_server'], port=int(path_config['Mongo_port']))
collection = client['handling_vedio']['vedios']
video_path = path_config['Video_path']
error_path = path_config['Error_path']
if os.path.exists(error_path):
pass
else:
print(f'error_path 目录不存在, 开始创建...')
os.mkdir(error_path)
LOGIN = None
PREDATA = None
USEFUL_NUM = None
@app.route('/')
def root():
return redirect(url_for('video_list'))
@app.route('/video_list')
def video_list():
# pagesize默认固定长度 page传递的参数,分页作用
global USEFUL_NUM
kill_orphan_chrome()
useful_num = int(collection.count_documents({'video_update_time': {'$gt': time.time() - 5 * 60 * 60}}))
print('可用视频数量: ', useful_num)
info = {'status': 0, 'video_list': [], 'has_more': False, 'message': None, 'page': 1, 'page_index': []}
if useful_num > 0:
page_size = 50
page_num = int(math.ceil(useful_num/page_size))
info['page_index'] = [i for i in range(1, page_num+1)]
temp = request.args.get('page', 1)
if re.search(r'\d', str(temp)):
page = int(temp)
else:
page = 1
info['page'] = page
info['message'] = '获取视频列表成功!'
index = page_size*page
if index > useful_num:
info['has_more'] = False
else:
info['has_more'] = True
videos = collection.find({'video_update_time': {'$gt': time.time() - 5 * 60 * 60}}).limit(50).skip(index - 50)
for video in videos:
del video['_id']
video['video_title'] = video['video_title'].strip().replace('\n', '')
if video['video_title']:
if len(video['video_title']) > 40:
video['video_title'] = video['video_title'][:40]
if 'has_handling' in video.keys():
if video['has_handling'] is True:
video['has_handling'] = '已搬运'
else:
video['has_handling'] = '未搬运'
else:
video['has_handling'] = '未搬运'
info['video_list'].append(video)
else:
info['status'] = -1
info['message'] = 'there is no useful video'
return render_template('video_list.html', data=info)
@app.route('/video', methods=['GET'])
def index():
video_id = request.args.get('video_id')
if video_id:
return_data = collection.find_one({'video_id': video_id})
if return_data:
del return_data['_id']
video_url = return_data['video_url']
# if return_data['video_datafrom'] == '抖音' or 'douyin' in video_url:
# video_url = video_url.replace('https://', 'http://')
print('video_url: ', video_url)
return render_template('home.html', data=json.dumps(return_data), origin_video_url=video_url)
else:
return 'no data '
else:
return 'video_id is wrong !!!'
@app.route('/jump', methods=['POST'])
def jump():
video_id = request.form.get('video_id')
temp = collection.find_one({'video_id': video_id})
if temp:
del temp['_id']
return render_template('home.html', data=json.dumps(temp))
else:
return render_template('home.html', data=json.dumps({}))
@app.route('/pre_handling', methods=['POST'])
def pre_handling_video():
print('pre_args: ', request.form)
global PREDATA
PREDATA = dict(request.form)
return 'True'
@app.route('/handling', methods=['GET', 'POST'])
def handling_video():
print('url: ', request.url)
print('handling form: ', request.form)
data = PREDATA
if data:
video_datafrom = data['video_datafrom']
login = Login()
global LOGIN
LOGIN = login
if video_datafrom == "抖音" or video_datafrom == 'douyin':
# login.tiktok_login()
return render_template('handling.html', data=json.dumps(data), dict=data, video_id=data['video_id'], login_pic_url='None')
else:
login_pic_url = login.douyin_login()
if login_pic_url:
pass
else:
login_pic_url = ''
return render_template('handling.html', data=json.dumps(data), dict=data, video_id=data['video_id'], login_pic_url=login_pic_url)
else:
return redirect(url_for('video_list'))
@app.route('/download_video', methods=['POST'])
def download_video():
path = path_config['video_path']
video_url = request.form.get('video_url')
video_id = request.form.get('video_id')
video_name = video_id + '.mp4'
file_path = path + video_name
if os.path.exists(file_path):
print(f'下载: {video_id} 无需下载')
return video_name
else:
# video_url = video_url
video_url = video_url.replace('https://', 'http://')
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
for i in range(3):
try:
if 'tiktok' in video_url or 'youtube' in video_url or 'google' in video_url:
resp = requests.get(url=video_url, headers=headers)
else:
resp = requests.get(url=video_url, headers=headers)
if resp.status_code < 300:
with open(file_path, 'wb')as f:
f.write(resp.content)
print(f'下载: {video_id} 成功! ')
return video_name
else:
continue
except:
print(f'下载: {video_id} 失败: {traceback.format_exc()}')
return '下载失败!'
@app.route('/refresh_login_pic', methods=['POST'])
def refresh_login_pic():
kill_orphan_chrome()
douyin_login = Login()
global LOGIN
LOGIN = douyin_login
login_pic_url = douyin_login.douyin_login()
if login_pic_url:
pass
else:
login_pic_url = '刷新失败'
print(f'刷新登录二维码: {login_pic_url}')
return login_pic_url
@app.route('/publish', methods=['POST'])
def publish():
global LOGIN
path = path_config['video_path']
video_id = request.form.get('video_id')
video_datafrom = request.form.get('video_datafrom')
account = request.form.get('account')
password = request.form.get('password')
title = request.form.get('title', ' ')
publish_platform = request.form.get('publish_platform')
video_name = video_id + '.mp4'
print(f'title: {title} account: {account} password: {password} publish: {publish_platform}')
video_url = request.form.get('video_url')
video_url = video_url.replace('https://', 'http://')
file_path = path+video_name
print(f'video_path: {video_path} video_url: {video_url}')
for i in range(3):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
try:
print(f'第{i+1}次下载视频')
if 'tiktok' in video_url or 'youtube' in video_url or 'google' in video_url:
resp = requests.get(url=video_url, headers=headers, stream=True)
else:
resp = requests.get(url=video_url, headers=headers, stream=True)
if resp.status_code < 300:
with open(file_path, 'wb')as f:
f.write(resp.content)
print(f'下载视频完成')
break
else:
print(f'下载视频异常, 开始重试...')
continue
except Exception as e:
print(f'下载视频异常: {e}')
continue
if os.path.exists(file_path):
print(f'开始发布视频')
try:
if publish_platform == 'tiktok':
if account is not None and password is not None and account != '' and password != '':
login = Login()
LOGIN = login
result = login.tiktok_login(account=account, password=password)
else:
result = '账号或密码为空'
elif publish_platform == 'youtube':
if account is not None and password is not None and account != '' and password != '':
login = Login()
LOGIN = login
result = login.youtube_login(account=account, password=password)
else:
result = '账号或密码为空'
else:
result = LOGIN.douyin_upload(video_path=file_path, title=title)
except Exception as e:
print(f'发布视频, 页面异常: {e}')
result = '页面异常'
if LOGIN:
if LOGIN.broswer:
LOGIN.broswer.save_screenshot(error_path+'error'+str(int(time.time()))+'.png')
LOGIN.broswer.quit()
if result is True:
collection.update_one({'video_id': video_id}, {'$set': {'has_handling': True}})
message = 'success'
else:
message = result
else:
message = '网络环境异常, 请点击下载视频后重试!'
# else:
# message = '找不到可用的chrome, 请返回刷新!'
if LOGIN:
if LOGIN.broswer:
LOGIN.broswer.quit()
return message
@app.route('/hcaptcha', methods=['POST'])
def hcaptcha():
code = request.form.get('code')
video_id = request.form.get('video_id')
if re.search(r'\D', code):
print(f'请输入正确的验证码: {code}')
return 'error code'
else:
if LOGIN:
result = LOGIN.hcaptcha(code=code)
if result is True:
collection.update_one({'video_id': video_id}, {'$set': {'has_handling': True}})
message = 'success'
else:
message = '验证失败'
else:
message = '验证, 无可用的chrome'
return message
def kill_orphan_chrome():
for i in range(2):
if sys.argv[1] == 'test':
break
else:
try:
if os.popen('ps -f --ppid 1 | grep chromedriver').read():
os.system("ps -f --ppid 1 | grep chromedriver | awk '{print $2}' | xargs kill -9")
except:
pass
try:
if os.popen('ps -f --ppid 1 | grep chrome').read():
os.system("ps -f --ppid 1 | grep chrome | awk '{print $2}' | xargs kill -9")
except:
pass
time.sleep(random.uniform(0.3, 0.5))
if __name__ == '__main__':
if sys.argv[1] == 'test':
app.run(host=api_config['Host'], port=int(api_config['Port']), debug=True)
else:
app.run(host=api_config['Host'], port=int(api_config['Port']), threaded=True)