demo.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # @Author: wangkun
  2. # @Time: 2月 25, 2022
  3. import datetime
  4. import logging
  5. import os
  6. import sys
  7. import time
  8. sys.path.append(os.getcwd())
  9. from main.feishu_lib import Feishu
  10. from main.publish import Publish
  11. from main.common import Common
  12. path = "../videoinfo/"
  13. class Demo:
  14. @classmethod
  15. def read_videoname(cls):
  16. with open(path + "videoname.txt", "r", encoding="utf8") as f:
  17. content = f.read()
  18. name = content
  19. return name
  20. @classmethod
  21. def write_videoname(cls):
  22. n = int(float(cls.read_videoname()))
  23. with open(path + "videoname.txt", "w", encoding="utf8") as f:
  24. f.write(str(n + 1))
  25. @classmethod
  26. def split_text(cls):
  27. path1 = "./chlsfiles/"
  28. files = os.listdir(path1)
  29. for file in files:
  30. print(file)
  31. print("\n==========\n")
  32. a = os.path.splitext(file)
  33. print(a[-1])
  34. # print(file[0])
  35. # print(file[-1])
  36. @classmethod
  37. def split_query(cls):
  38. url = "encfilekey=Cvvj5Ix3eez3Y79SxtvVL0L7CkPM6dFibFeI6caGYwFHPibicjZB7UrwpKsG9wQrl01" \
  39. "IlTMtIjicibib9iaGoaL2sxR7vGt4sofODkBGnic9EOCZjgCQTlO7ZTNjribxkDmPq40E2RMdXxzGlWDwYfmkWz4Q15g" \
  40. "&token=AxricY7RBHdVbVKZOkvCOWwYWwj9zBqet1eia6GZib8GhmdrytdBu5HcPARtxW7l0AAzppsekDUXXQ&idx=1&adap" \
  41. "tivelytrans=943&bizid=1023&dotrans=2991&hy=SH&m=d04052b0ab0e3dd64567fa60b16d0898&scene=0&t=1&taskid=0"
  42. a = url.split("encfilekey=")[-1].split("&token=")[0]
  43. print(a)
  44. @classmethod
  45. def distent(cls):
  46. filekey = "Cvvj5Ix3eez3Y79SxtvVL0L7CkPM6dFibFeI6caGYwFEiaKINib2TA0ibV5JuXfIA5JysRWeytU3kTufCr7KFPLgwMiauCic" \
  47. "UypoEbA31w7w3sibnraPEwcMcG6LqxSmdFN5S8AfhuibFicv86oBfSuSXcibrCqw"
  48. filekey_txt_path = "../videoinfo/filekey.txt"
  49. with open(filekey_txt_path, 'r', encoding="utf8") as f:
  50. content = f.read()
  51. print(content)
  52. for line in content:
  53. if filekey in line.strip():
  54. print("yes")
  55. elif line.strip() == "":
  56. print("null")
  57. else:
  58. print("no")
  59. # 创建日志
  60. @classmethod
  61. def create_log(cls):
  62. # 创建日志文件夹。如果不存在,则创建
  63. log_dir = "../logs/" # 日志文件夹名称
  64. log_path = os.getcwd() + os.sep + log_dir # 日志路径
  65. if not os.path.isdir(log_path):
  66. os.makedirs(log_path)
  67. log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
  68. date_format = "%Y-%m-%d %p %H:%M:%S"
  69. # 初始化
  70. logging.basicConfig(filename=log_path + 'logs.logs', level=logging.INFO, format=log_format, datefmt=date_format)
  71. logger = logging.getLogger('shipinhao')
  72. logger.info("这是第一条日志")
  73. @classmethod
  74. def split_title(cls):
  75. title = Feishu.get_values_batch('demo', 'shipinhao', 'FSDlBy')[3][2]
  76. print(title)
  77. print('\n')
  78. # print(title.split('\n')[0].split('#')[0])
  79. print(title.split('\n')[0].replace('#', ''))
  80. # 下载 、上传
  81. @classmethod
  82. def download_publish(cls, log_type, env):
  83. try:
  84. recommend_feeds_sheet = Feishu.get_values_batch(log_type, 'shipinhao', 'FSDlBy')
  85. for i in range(1, len(recommend_feeds_sheet)):
  86. download_title = recommend_feeds_sheet[i][2].strip().replace('"', '') \
  87. .replace('“', '').replace('“', '…').replace("\n", "") \
  88. .replace("/", "").replace("\r", "").replace("#", "") \
  89. .replace(".", "。").replace("\\", "").replace("&NBSP", "") \
  90. .replace(":", "").replace("*", "").replace("?", "") \
  91. .replace("?", "").replace('"', "").replace("<", "") \
  92. .replace(">", "").replace("|", "").replace(" ", "")
  93. download_duration = recommend_feeds_sheet[i][3]
  94. download_like_cnt = recommend_feeds_sheet[i][4]
  95. download_share_cnt = recommend_feeds_sheet[i][5]
  96. download_favorite_cnt = recommend_feeds_sheet[i][6]
  97. download_comment_cnt = recommend_feeds_sheet[i][7]
  98. download_username = recommend_feeds_sheet[i][8]
  99. download_head_url = recommend_feeds_sheet[i][9]
  100. download_cover_url = recommend_feeds_sheet[i][10]
  101. download_video_url = recommend_feeds_sheet[i][11]
  102. Common.logger(log_type).info("download_title:{}", download_title)
  103. Common.logger(log_type).info("download_username:{}", download_username)
  104. Common.logger(log_type).info("download_video_url:{}", download_video_url)
  105. if download_title is None or download_duration is None or download_video_url is None:
  106. Feishu.dimension_range(log_type, 'shipinhao', 'FSDlBy', 'ROWS', i + 1, i + 1)
  107. Common.logger(log_type).info('空行,删除成功\n')
  108. return
  109. elif str(download_title) in [x for y in Feishu.get_values_batch(log_type, 'shipinhao', 'c77cf9') for x
  110. in y]:
  111. Feishu.dimension_range(log_type, 'shipinhao', 'FSDlBy', 'ROWS', i + 1, i + 1)
  112. Common.logger(log_type).info('视频已下载,删除成功\n')
  113. return
  114. else:
  115. # 下载封面
  116. Common.download_method(log_type=log_type, text="cover",
  117. d_name=str(download_title), d_url=str(download_cover_url))
  118. # 下载视频
  119. Common.download_method(log_type=log_type, text="video",
  120. d_name=str(download_title), d_url=str(download_video_url))
  121. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  122. with open("./videos/" + download_title
  123. + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  124. f_a.write('shipinhao' + str(int(time.time())) + "\n" +
  125. str(download_title) + "\n" +
  126. str(download_duration) + "\n" +
  127. str(download_favorite_cnt) + "\n" +
  128. str(download_comment_cnt) + "\n" +
  129. str(download_like_cnt) + "\n" +
  130. str(download_share_cnt) + "\n" +
  131. str(1920 * 1080) + "\n" +
  132. str(int(time.time())) + "\n" +
  133. str(download_username) + "\n" +
  134. str(download_head_url) + "\n" +
  135. str(download_video_url) + "\n" +
  136. str(download_cover_url) + "\n" +
  137. "shipinhao"+str(int(time.time())))
  138. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  139. Common.logger(log_type).info("开始上传视频:{}".format(download_title))
  140. our_video_id = Publish.upload_and_publish(log_type, env, "play")
  141. if env == 'dev':
  142. our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
  143. else:
  144. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
  145. Common.logger(log_type).info("视频上传完成:{}", our_video_link)
  146. # # 视频ID工作表,插入首行
  147. # Feishu.insert_columns(log_type, "shipinhao", "c77cf9", "ROWS", 1, 2)
  148. # # 视频ID工作表,首行写入数据
  149. # upload_time = int(time.time())
  150. # values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
  151. # "话题榜",
  152. # str(download_title),
  153. # our_video_link,
  154. # download_duration,
  155. # download_like_cnt,
  156. # download_share_cnt,
  157. # download_favorite_cnt,
  158. # download_comment_cnt,
  159. # download_username,
  160. # str(download_head_url),
  161. # str(download_cover_url),
  162. # str(download_video_url)]]
  163. # time.sleep(1)
  164. # Feishu.update_values(log_type, "shipinhao", "c77cf9", "F2:V2", values)
  165. # 删除行或列,可选 ROWS、COLUMNS
  166. time.sleep(1)
  167. Feishu.dimension_range(log_type, "shipinhao", "FSDlBy", "ROWS", i + 1, i + 1)
  168. Common.logger(log_type).info("下载/上传成功:{}\n", download_title)
  169. return
  170. except Exception as e:
  171. Feishu.dimension_range(log_type, "shipinhao", "FSDlBy", "ROWS", 2, 2)
  172. Common.logger(log_type).error('download_publish异常,删除视频信息成功:{}\n', e)
  173. @classmethod
  174. def run_download_publish(cls, log_type, env):
  175. while True:
  176. feeds_sht = Feishu.get_values_batch(log_type, 'shipinhao', 'FSDlBy')
  177. if len(feeds_sht) == 1:
  178. Common.logger(log_type).info('下载/上传完成\n')
  179. break
  180. else:
  181. cls.download_publish(log_type, env)
  182. @classmethod
  183. def get_sht(cls):
  184. sht = Feishu.get_values_batch('recommend', 'shipinhao', 'c77cf9')
  185. print(len(sht))
  186. if __name__ == "__main__":
  187. # print(str(datetime.datetime.now()).split('.')[0])
  188. # print(time.time())
  189. # Demo.run_download_publish('topic', 'prod')
  190. Demo.get_sht()
  191. pass