ssnnyfq_recommend.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/11/7
  4. import os
  5. import random
  6. import sys
  7. import time
  8. import ffmpeg
  9. import requests
  10. import urllib3
  11. sys.path.append(os.getcwd())
  12. from main.common import Common
  13. from main.feishu_lib import Feishu
  14. from main.ssnnyfq_publish import Publish
  15. class Recommend:
  16. page = 0
  17. @classmethod
  18. def get_video_info_from_local(cls, video_path):
  19. probe = ffmpeg.probe(video_path)
  20. video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
  21. if video_stream is None:
  22. print('No video stream found!')
  23. return
  24. width = int(video_stream['width'])
  25. height = int(video_stream['height'])
  26. duration = float(video_stream['duration'])
  27. return width, height, duration
  28. @classmethod
  29. def get_feeds(cls, log_type, env):
  30. try:
  31. while True:
  32. url = 'https://www.jzkksp.com/index/home/get_home_list.html'
  33. headers = {
  34. 'content-type': 'application/x-www-form-urlencoded',
  35. 'Accept-Encoding': 'gzip,compress,br,deflate',
  36. 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) '
  37. 'AppleWebKit/605.1.15 (KHTML, like Gecko) '
  38. 'Mobile/15E148 MicroMessenger/8.0.25(0x1800192b) NetType/WIFI Language/zh_CN',
  39. 'Referer': 'https://servicewechat.com/wxd4c54f60812f6f36/1/page-frame.html',
  40. }
  41. cls.page += 1
  42. data = {
  43. 'token': '851ae159fd33f955bf433e7c47a4a298',
  44. 'time': '1667905857000',
  45. 'str_data': 'uT551tU8',
  46. 'page': str(cls.page),
  47. 'limit': '10',
  48. 'appid': 'wxd4c54f60812f6f36',
  49. 'version': '1.4.1',
  50. 'openid': 'oDAjy5SCFe7Ml3PNgiow3ncozL1o'
  51. }
  52. urllib3.disable_warnings()
  53. response = requests.post(url=url, headers=headers, data=data, verify=False)
  54. if 'data' not in response.json():
  55. Common.logger(log_type).warning('response:{}\n', response.text)
  56. cls.page = 0
  57. return
  58. elif len(response.json()['data']['video_list']['data']) == 0:
  59. Common.logger(log_type).warning('response:{}', response.json())
  60. Common.logger(log_type).info('视频列表返回空,退出\n')
  61. cls.page = 0
  62. return
  63. else:
  64. feeds = response.json()['data']['video_list']['data']
  65. Common.logger(log_type).info('page:{}\n', cls.page)
  66. for i in range(len(feeds)):
  67. # video_title
  68. if 'title' not in feeds[i]:
  69. video_title = 0
  70. else:
  71. video_title = feeds[i]['title']
  72. # video_id
  73. if 'id' not in feeds[i]:
  74. video_id = 0
  75. else:
  76. video_id = feeds[i]['id']
  77. # play_cnt
  78. if 'browse' not in feeds[i]:
  79. play_cnt = 0
  80. else:
  81. play_cnt = feeds[i]['browse']
  82. # publish_time
  83. if 'createtime' not in feeds[i]:
  84. publish_time = 0
  85. else:
  86. publish_time = feeds[i]['createtime']
  87. # cover_url
  88. if 'thumb' not in feeds[i]:
  89. cover_url = 0
  90. else:
  91. cover_url = feeds[i]['thumb']
  92. # video_url
  93. if 'url' not in feeds[i]:
  94. video_url = 0
  95. else:
  96. video_url = feeds[i]['url']
  97. Common.logger(log_type).info('video_title:{}', video_title)
  98. Common.logger(log_type).info('video_id:{}', video_id)
  99. Common.logger(log_type).info('play_cnt:{}', play_cnt)
  100. Common.logger(log_type).info('publish_time:{}', publish_time)
  101. Common.logger(log_type).info('cover_url:{}', cover_url)
  102. Common.logger(log_type).info('video_url:{}', video_url)
  103. if video_id == 0 or video_title == 0 or cover_url == 0 or video_url == 0:
  104. Common.logger(log_type).info('无效视频\n')
  105. elif int(video_id) in [x for y in Feishu.get_values_batch(log_type, 'ssnnyfq', '290bae') for x in y]:
  106. Common.logger(log_type).info('视频已下载\n')
  107. else:
  108. Common.download_method(log_type, 'cover', video_title, cover_url)
  109. Common.download_method(log_type, 'video', video_title, video_url)
  110. # 获取视频时长
  111. video_info = cls.get_video_info_from_local(
  112. "./videos/" + video_title + "/video.mp4")
  113. video_width = str(video_info[0])
  114. video_height = str(video_info[1])
  115. duration = video_info[2]
  116. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  117. with open("./videos/" + video_title
  118. + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  119. f_a.write("ssnnyfq" + str(int(time.time())) + "\n" +
  120. str(video_title) + "\n" +
  121. str(int(duration)) + "\n" +
  122. str(play_cnt) + "\n" +
  123. '0' + "\n" +
  124. '0' + "\n" +
  125. '0' + "\n" +
  126. str(video_width) + '*' + str(video_height) + "\n" +
  127. str(int(time.time())) + "\n" +
  128. '岁岁年年迎福气小程序' + "\n" +
  129. str(cover_url) + "\n" +
  130. str(video_url) + "\n" +
  131. str(cover_url) + "\n" +
  132. "suisuiniannianyingfuqi" + str(int(time.time())))
  133. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  134. # 上传视频
  135. Common.logger(log_type).info("开始上传视频:{}".format(video_title))
  136. if env == 'dev':
  137. our_video_id = Publish.upload_and_publish(log_type, env, "width")
  138. our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(
  139. our_video_id) + "/info"
  140. else:
  141. our_video_id = Publish.upload_and_publish(log_type, env, "width")
  142. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  143. our_video_id) + "/info"
  144. Common.logger(log_type).info("视频上传完成,保存至已下载表")
  145. # 视频ID工作表,插入首行
  146. Feishu.insert_columns(log_type, "ssnnyfq", "290bae", "ROWS", 1, 2)
  147. # 视频ID工作表,首行写入数据
  148. upload_time = int(time.time())
  149. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
  150. "推荐榜",
  151. video_title,
  152. video_id,
  153. our_video_link,
  154. play_cnt,
  155. int(duration),
  156. str(video_width) + '*' + str(video_height),
  157. cover_url,
  158. video_url]]
  159. time.sleep(1)
  160. Feishu.update_values(log_type, "ssnnyfq", "290bae", "F2:V2", values)
  161. Common.logger(log_type).info("视频:{},下载/上传成功,随机休眠 10-30 秒\n", video_title)
  162. time.sleep(random.randint(10, 30))
  163. except Exception as e:
  164. Common.logger(log_type).error('get_feeds异常:{}\n', e)
  165. if __name__ == '__main__':
  166. Recommend.get_feeds('recommend', 'dev')