|
@@ -1,89 +0,0 @@
|
1
|
|
-#!/usr/bin/python3
|
2
|
|
-# coding=utf-8
|
3
|
|
-# -*- coding: utf-8 -*-
|
4
|
|
-
|
5
|
|
-import os
|
6
|
|
-import time
|
7
|
|
-import sys
|
8
|
|
-from libs.mysql_kwai_show_video import MysqlKwaiShowVideo
|
9
|
|
-from libs.ali_oss import AliOss
|
10
|
|
-from log.print_log import PrintLog
|
11
|
|
-
|
12
|
|
-
|
13
|
|
-def upload_to_oss(live_id):
|
14
|
|
- try:
|
15
|
|
- file_name = live_id + '.flv'
|
16
|
|
-
|
17
|
|
- local_path = sys.path[0] + '/file/' + file_name
|
18
|
|
- oss_path = 'live_video/' + file_name
|
19
|
|
-
|
20
|
|
- PrintLog.print(live_id + ' 开始上传')
|
21
|
|
- bucket = AliOss.get_auth_bucket()
|
22
|
|
- bucket.put_object_from_file(oss_path, local_path, progress_callback=percentage)
|
23
|
|
- PrintLog.print(live_id + ' 上传完成')
|
24
|
|
-
|
25
|
|
- try:
|
26
|
|
- os.remove(local_path)
|
27
|
|
- PrintLog.print(live_id + ' 删除文件成功')
|
28
|
|
- except Exception as e:
|
29
|
|
- PrintLog.print('删除文件抛出异常:' + str(e))
|
30
|
|
-
|
31
|
|
- except Exception as e:
|
32
|
|
- PrintLog.print('上传文件抛出异常:' + str(e))
|
33
|
|
- raise e
|
34
|
|
- pass
|
35
|
|
-
|
36
|
|
-
|
37
|
|
-# 当无法确定待上传的数据长度时,total_bytes的值为None。
|
38
|
|
-def percentage(consumed_bytes, total_bytes):
|
39
|
|
- global start_time
|
40
|
|
- current_time = time.time()
|
41
|
|
- current_consumed_m_bytes = round(consumed_bytes / 1024 / 1024, 2)
|
42
|
|
- rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
|
43
|
|
- diff_time = round(current_time - start_time)
|
44
|
|
-
|
45
|
|
- if total_bytes:
|
46
|
|
- print('\r{0}% '.format(rate), end='')
|
47
|
|
- sys.stdout.flush()
|
48
|
|
- if diff_time > 60:
|
49
|
|
- start_time = current_time
|
50
|
|
- PrintLog.print('\r已上传' + str(current_consumed_m_bytes) + 'M (' + str(rate) + '%)')
|
51
|
|
-
|
52
|
|
-
|
53
|
|
-def upload_live_video():
|
54
|
|
-
|
55
|
|
- PrintLog.print('获取需要上传的直播')
|
56
|
|
-
|
57
|
|
- video_info = MysqlKwaiShowVideo().get_downloaded_video()
|
58
|
|
-
|
59
|
|
- if video_info is None:
|
60
|
|
- time.sleep(60)
|
61
|
|
- return
|
62
|
|
-
|
63
|
|
- id, live_id = video_info
|
64
|
|
- upload_to_oss(live_id)
|
65
|
|
-
|
66
|
|
- try:
|
67
|
|
- url = AliOss.BaseUrl + live_id + '.flv'
|
68
|
|
- MysqlKwaiShowVideo().set_video_url(id, url)
|
69
|
|
- PrintLog.print(live_id + ' 更新数据表成功')
|
70
|
|
- except Exception as e:
|
71
|
|
- PrintLog.print('更新数据表抛出异常:' + str(e))
|
72
|
|
- time.sleep(1)
|
73
|
|
-
|
74
|
|
-
|
75
|
|
-if __name__ == "__main__":
|
76
|
|
- while True:
|
77
|
|
- try:
|
78
|
|
- hour = int(time.strftime("%H", time.localtime()))
|
79
|
|
- if hour > 17:
|
80
|
|
- PrintLog.print('当前时间段不进行上传操作')
|
81
|
|
- time.sleep(3600)
|
82
|
|
- continue
|
83
|
|
-
|
84
|
|
- start_time = time.time()
|
85
|
|
- upload_live_video()
|
86
|
|
- except Exception as e:
|
87
|
|
- PrintLog.print('抛出异常:' + str(e))
|
88
|
|
- time.sleep(1)
|
89
|
|
- continue
|