publish.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/1/16
  4. import json
  5. import os
  6. import random
  7. import shutil
  8. import time
  9. import oss2
  10. import requests
  11. import urllib3
  12. from main.common import Common
  13. proxies = {"http": None, "https": None}
  14. class Publish:
  15. @classmethod
  16. def publish_video_dev(cls, log_type, request_data):
  17. """
  18. loginUid 站内uid (随机)
  19. appType 默认:888888
  20. crawlerSrcId 站外视频ID
  21. crawlerSrcCode 渠道(自定义 KYK)
  22. crawlerSrcPublishTimestamp 视频原发布时间
  23. crawlerTaskTimestamp 爬虫创建时间(可以是当前时间)
  24. videoPath 视频oss地址
  25. coverImgPath 视频封面oss地址
  26. title 标题
  27. totalTime 视频时长
  28. viewStatus 视频的有效状态 默认1
  29. versionCode 版本 默认1
  30. :return:
  31. """
  32. Common.logger(log_type).info('publish request data: {}'.format(request_data))
  33. result = cls.request_post('https://videotest.yishihui.com/longvideoapi/crawler/video/send', request_data)
  34. Common.logger(log_type).info('publish result: {}'.format(result))
  35. video_id = result["data"]["id"]
  36. Common.logger(log_type).info('video_id: {}'.format(video_id))
  37. if result['code'] != 0:
  38. Common.logger(log_type).error('pushlish failure msg = {}'.format(result['msg']))
  39. else:
  40. Common.logger(log_type).info('publish success video_id = : {}'.format(request_data['crawlerSrcId']))
  41. return video_id
  42. @classmethod
  43. def publish_video_prod(cls, log_type, request_data):
  44. """
  45. loginUid 站内uid (随机)
  46. appType 默认:888888
  47. crawlerSrcId 站外视频ID
  48. crawlerSrcCode 渠道(自定义 KYK)
  49. crawlerSrcPublishTimestamp 视频原发布时间
  50. crawlerTaskTimestamp 爬虫创建时间(可以是当前时间)
  51. videoPath 视频oss地址
  52. coverImgPath 视频封面oss地址
  53. title 标题
  54. totalTime 视频时长
  55. viewStatus 视频的有效状态 默认1
  56. versionCode 版本 默认1
  57. :return:
  58. """
  59. result = cls.request_post('https://longvideoapi.piaoquantv.com/longvideoapi/crawler/video/send', request_data)
  60. # Common.logger(log_type).info('publish result: {}'.format(result))
  61. video_id = result["data"]["id"]
  62. # Common.logger(log_type).info('video_id: {}'.format(video_id))
  63. if result['code'] != 0:
  64. Common.logger(log_type).error('pushlish failure msg = {}'.format(result['msg']))
  65. else:
  66. Common.logger(log_type).info('publish success video_id = : {}'.format(request_data['crawlerSrcId']))
  67. return video_id
  68. @classmethod
  69. def request_post(cls, request_url, request_data):
  70. """
  71. post 请求 HTTP接口
  72. :param request_url: 接口URL
  73. :param request_data: 请求参数
  74. :return: res_data json格式
  75. """
  76. urllib3.disable_warnings()
  77. response = requests.post(url=request_url, data=request_data, proxies=proxies, verify=False)
  78. if response.status_code == 200:
  79. res_data = json.loads(response.text)
  80. return res_data
  81. # 以下代码展示了基本的文件上传、下载、罗列、删除用法。
  82. # 首先初始化AccessKeyId、AccessKeySecret、Endpoint等信息。
  83. # 通过环境变量获取,或者把诸如“<你的AccessKeyId>”替换成真实的AccessKeyId等。
  84. #
  85. # 以杭州区域为例,Endpoint可以是:
  86. # http://oss-cn-hangzhou.aliyuncs.com
  87. # https://oss-cn-hangzhou.aliyuncs.com
  88. # 分别以HTTP、HTTPS协议访问。
  89. access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', 'LTAIP6x1l3DXfSxm')
  90. access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', 'KbTaM9ars4OX3PMS6Xm7rtxGr1FLon')
  91. bucket_name = os.getenv('OSS_TEST_BUCKET', 'art-pubbucket')
  92. # OSS 内网
  93. endpoint = os.getenv('OSS_TEST_ENDPOINT', 'oss-cn-hangzhou-internal.aliyuncs.com')
  94. # OSS 外网
  95. # endpoint = os.getenv('OSS_TEST_ENDPOINT', 'oss-cn-hangzhou.aliyuncs.com')
  96. # 确认上面的参数都填写正确了
  97. for param in (access_key_id, access_key_secret, bucket_name, endpoint):
  98. assert '<' not in param, '请设置参数:' + param
  99. # 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
  100. bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
  101. """
  102. 处理流程:
  103. 1. 定时(每天凌晨1点执行一次)循环files文件下的内容 结构:files -> 视频文件夹 -> 视频文件 + 封面图 + 基本信息
  104. 2. 视频文件和封面上传到oss
  105. - 视频文件oss目录 longvideo/crawler_local/video/prod/文件名
  106. - 视频封面oss目录 longvideo/crawler_local/image/prod/文件名
  107. 3. 发布视频
  108. - 读取 基本信息 调用发布接口
  109. """
  110. # env 日期20220225 文件名
  111. oss_file_path_video = 'longvideo/crawler_local/video/{}/{}/{}'
  112. oss_file_path_image = 'longvideo/crawler_local/image/{}/{}/{}'
  113. @classmethod
  114. def put_file(cls, log_type, oss_file, local_file):
  115. cls.bucket.put_object_from_file(oss_file, local_file)
  116. Common.logger(log_type).info("put oss file = {}, local file = {} success".format(oss_file, local_file))
  117. # 清除本地文件
  118. @classmethod
  119. def remove_local_file(cls, log_type, local_file):
  120. os.remove(local_file)
  121. Common.logger(log_type).info("remove local file = {} success".format(local_file))
  122. # 清除本地文件夹
  123. @classmethod
  124. def remove_local_file_dir(cls, log_type, local_file):
  125. os.rmdir(local_file)
  126. Common.logger(log_type).info("remove local file dir = {} success".format(local_file))
  127. local_file_path = './videos'
  128. video_file = 'video'
  129. image_file = 'image'
  130. info_file = 'info'
  131. uids_dev = [6267140, 6267141]
  132. uids_prod_up = [26117675, 26117676, 26117677, 26117678, 26117679, 26117680]
  133. uids_prod_play = [26117675, 26117676, 26117677, 26117678, 26117679, 26117680]
  134. @classmethod
  135. def upload_and_publish(cls, log_type, env, job):
  136. """
  137. 上传视频到 oss
  138. :param log_type: 选择的 log
  139. :param env: 测试环境:dev,正式环境:prod
  140. :param job: 上升榜:up,播放量:play
  141. """
  142. Common.logger(log_type).info("upload_and_publish starting...")
  143. today = time.strftime("%Y%m%d", time.localtime())
  144. # videos 目录下的所有视频文件夹
  145. files = os.listdir(cls.local_file_path)
  146. for fv in files:
  147. try:
  148. # 单个视频文件夹
  149. fi_d = os.path.join(cls.local_file_path, fv)
  150. # 确认为视频文件夹
  151. if os.path.isdir(fi_d):
  152. Common.logger(log_type).info('dir = {}'.format(fi_d))
  153. # 列出所有视频文件夹
  154. dir_files = os.listdir(fi_d)
  155. data = {'appType': '888888',
  156. 'crawlerSrcCode': 'GONGZHONGHAO_XINXIN',
  157. 'viewStatus': '1',
  158. 'versionCode': '1'}
  159. now_timestamp = int(round(time.time() * 1000))
  160. data['crawlerTaskTimestamp'] = str(now_timestamp)
  161. global uid
  162. if env == "dev":
  163. uid = str(random.choice(cls.uids_dev))
  164. elif env == "prod" and job == "up":
  165. uid = str(random.choice(cls.uids_prod_up))
  166. elif env == "prod" and job == "play":
  167. uid = str(random.choice(cls.uids_prod_play))
  168. data['loginUid'] = uid
  169. # 单个视频文件夹下的所有视频文件
  170. for fi in dir_files:
  171. # 视频文件夹下的所有文件路径
  172. fi_path = fi_d + '/' + fi
  173. Common.logger(log_type).info('dir fi_path = {}'.format(fi_path))
  174. # 读取 info.txt,赋值给 data
  175. if cls.info_file in fi:
  176. f = open(fi_path, "r", encoding="UTF-8")
  177. # 读取数据 数据准确性写入的时候保证 读取暂不处理
  178. for i in range(14):
  179. line = f.readline()
  180. line = line.replace('\n', '')
  181. if line is not None and len(line) != 0 and not line.isspace():
  182. # Common.logger(log_type).info("line = {}".format(line))
  183. if i == 0:
  184. data['crawlerSrcId'] = line
  185. elif i == 1:
  186. data['title'] = line
  187. elif i == 2:
  188. data['totalTime'] = line
  189. elif i == 8:
  190. data['crawlerSrcPublishTimestamp'] = line
  191. else:
  192. Common.logger(log_type).warning("{} line is None".format(fi_path))
  193. f.close()
  194. # remove info.txt
  195. cls.remove_local_file(log_type, fi_path)
  196. # 刷新数据
  197. dir_files = os.listdir(fi_d)
  198. for fi in dir_files:
  199. fi_path = fi_d + '/' + fi
  200. # Common.logger(log_type).info('dir fi_path = {}'.format(fi_path))
  201. # 上传oss
  202. if cls.video_file in fi:
  203. global oss_video_file
  204. if env == "dev":
  205. oss_video_file = cls.oss_file_path_video.format("dev", today, data['crawlerSrcId'])
  206. elif env == "prod":
  207. oss_video_file = cls.oss_file_path_video.format("prod", today, data['crawlerSrcId'])
  208. Common.logger(log_type).info("oss_video_file = {}".format(oss_video_file))
  209. cls.put_file(log_type, oss_video_file, fi_path)
  210. data['videoPath'] = oss_video_file
  211. Common.logger(log_type).info("videoPath = {}".format(oss_video_file))
  212. elif cls.image_file in fi:
  213. global oss_image_file
  214. if env == "dev":
  215. oss_image_file = cls.oss_file_path_image.format("env", today, data['crawlerSrcId'])
  216. elif env == "prod":
  217. oss_image_file = cls.oss_file_path_image.format("prod", today, data['crawlerSrcId'])
  218. Common.logger(log_type).info("oss_image_file = {}".format(oss_image_file))
  219. cls.put_file(log_type, oss_image_file, fi_path)
  220. data['coverImgPath'] = oss_image_file
  221. Common.logger(log_type).info("coverImgPath = {}".format(oss_image_file))
  222. # 全部remove
  223. cls.remove_local_file(log_type, fi_path)
  224. # 发布
  225. if env == "dev":
  226. video_id = cls.publish_video_dev(log_type, data)
  227. elif env == "prod":
  228. video_id = cls.publish_video_prod(log_type, data)
  229. else:
  230. video_id = cls.publish_video_dev(log_type, data)
  231. cls.remove_local_file_dir(log_type, fi_d)
  232. return video_id
  233. else:
  234. Common.logger(log_type).error('file not a dir = {}'.format(fi_d))
  235. except Exception as e:
  236. # 删除视频文件夹
  237. shutil.rmtree("./videos/" + fv + "/")
  238. Common.logger(log_type).exception('upload_and_publish error', e)