|
@@ -1,87 +0,0 @@
|
1
|
|
-#!/usr/bin/python3
|
2
|
|
-# coding=utf-8
|
3
|
|
-# -*- coding: utf-8 -*-
|
4
|
|
-
|
5
|
|
-import requests
|
6
|
|
-import time
|
7
|
|
-import json
|
8
|
|
-import threading
|
9
|
|
-from libs.rds_wait_upload_show_video_set import RdsWaitUploadShowVideoSet
|
10
|
|
-from libs.mysql_kwai_show_video import MysqlKwaiShowVideo
|
11
|
|
-from log.print_log import PrintLog
|
12
|
|
-
|
13
|
|
-
|
14
|
|
-def download_video(live_video_params):
|
15
|
|
- params = json.loads(live_video_params)
|
16
|
|
-
|
17
|
|
- for play_url in params['play_urls']:
|
18
|
|
- try:
|
19
|
|
- MysqlKwaiShowVideo().set_video_status(params['id'], '1')
|
20
|
|
-
|
21
|
|
- # requests.get返回的是一个可迭代对象(Iterable),此时Python SDK会通过Chunked Encoding方式上传。
|
22
|
|
- input = requests.get(play_url, stream=True)
|
23
|
|
-
|
24
|
|
- PrintLog.print(params['live_id'] + ' 开始下载:' + play_url)
|
25
|
|
-
|
26
|
|
- f = open('file/' + params['live_id'] + ".flv", "ab") # 有则追加,没有则创建
|
27
|
|
-
|
28
|
|
- # 将视频数据分片写入文件
|
29
|
|
- for chunk in input.iter_content(chunk_size=512):
|
30
|
|
- if chunk:
|
31
|
|
- f.write(chunk)
|
32
|
|
-
|
33
|
|
- f.close()
|
34
|
|
-
|
35
|
|
- PrintLog.print(params['live_id'] + ' 下载完成:' + play_url)
|
36
|
|
-
|
37
|
|
- tmp_url = 'https://ks.wenxingshuju.com/showvideo/' + params['live_id'] + '.flv'
|
38
|
|
- res = MysqlKwaiShowVideo().set_video_tmp_url(params['id'], tmp_url)
|
39
|
|
- return
|
40
|
|
- except Exception as e:
|
41
|
|
- PrintLog.print(params['live_id'] + '下载异常:' + play_url + "\r\n" + str(e))
|
42
|
|
- MysqlKwaiShowVideo().set_video_status(params['id'], '3')
|
43
|
|
- continue
|
44
|
|
-
|
45
|
|
-
|
46
|
|
-def get_active_thread_set():
|
47
|
|
- # 待爬取的直播ID集合
|
48
|
|
- active_thread_set = set()
|
49
|
|
- for active_thread in threading.enumerate(): # 线程列表
|
50
|
|
- if active_thread.getName() != 'MainThread':
|
51
|
|
- active_thread_set.add(active_thread.getName())
|
52
|
|
-
|
53
|
|
- return active_thread_set
|
54
|
|
-
|
55
|
|
-
|
56
|
|
-if __name__ == "__main__":
|
57
|
|
-
|
58
|
|
- print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' ' + "主方法开始执行")
|
59
|
|
-
|
60
|
|
- rds_set = RdsWaitUploadShowVideoSet()
|
61
|
|
-
|
62
|
|
- while True:
|
63
|
|
-
|
64
|
|
- # 从 待下载直播视频集合中 列表中获取一条
|
65
|
|
- live_video_params = rds_set.pop_params()
|
66
|
|
-
|
67
|
|
- # 如果没有,则等一秒,循环
|
68
|
|
- if live_video_params is None:
|
69
|
|
- time.sleep(1)
|
70
|
|
- continue
|
71
|
|
-
|
72
|
|
- try:
|
73
|
|
- params = json.loads(live_video_params)
|
74
|
|
-
|
75
|
|
- thread_name = 'show_download_' + params['live_id']
|
76
|
|
- # 判断得到的直播间ID在爬取中
|
77
|
|
- # 判断线程中是否已经有该任务,有则跳过
|
78
|
|
- if thread_name in get_active_thread_set():
|
79
|
|
- print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' ' + " %s 已经在执行" % thread_name)
|
80
|
|
- continue
|
81
|
|
-
|
82
|
|
- task = threading.Thread(target=download_video, args=(live_video_params,), name=thread_name)
|
83
|
|
- task.start() # 准备就绪,等待cpu执行
|
84
|
|
- except Exception as e:
|
85
|
|
- PrintLog.print('抛出异常:' + str(e) + '\n' + live_video_params)
|
86
|
|
- time.sleep(1)
|
87
|
|
- continue
|