gongzhonghao4_author.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/3/28
  4. import datetime
  5. import difflib
  6. import json
  7. import os
  8. import shutil
  9. import sys
  10. import time
  11. from hashlib import md5
  12. import requests
  13. import urllib3
  14. from selenium.webdriver import DesiredCapabilities
  15. from selenium.webdriver.chrome.service import Service
  16. from selenium.webdriver.common.by import By
  17. from selenium import webdriver
  18. sys.path.append(os.getcwd())
  19. from common.common import Common
  20. from common.feishu import Feishu
  21. from common.publish import Publish
  22. from common.scheduling_db import MysqlHelper
  23. from common.public import get_config_from_mysql
  24. class GongzhonghaoAuthor4:
  25. platform = "公众号"
  26. # 基础门槛规则
  27. @staticmethod
  28. def download_rule(log_type, crawler, video_dict, rule_dict):
  29. """
  30. 下载视频的基本规则
  31. :param log_type: 日志
  32. :param crawler: 哪款爬虫
  33. :param video_dict: 视频信息,字典格式
  34. :param rule_dict: 规则信息,字典格式
  35. :return: 满足规则,返回 True;反之,返回 False
  36. """
  37. rule_play_cnt_min = rule_dict.get('play_cnt', {}).get('min', 0)
  38. rule_play_cnt_max = rule_dict.get('play_cnt', {}).get('max', 100000000)
  39. if rule_play_cnt_max == 0:
  40. rule_play_cnt_max = 100000000
  41. rule_duration_min = rule_dict.get('duration', {}).get('min', 0)
  42. rule_duration_max = rule_dict.get('duration', {}).get('max', 100000000)
  43. if rule_duration_max == 0:
  44. rule_duration_max = 100000000
  45. rule_period_min = rule_dict.get('period', {}).get('min', 0)
  46. # rule_period_max = rule_dict.get('period', {}).get('max', 100000000)
  47. # if rule_period_max == 0:
  48. # rule_period_max = 100000000
  49. rule_fans_cnt_min = rule_dict.get('fans_cnt', {}).get('min', 0)
  50. rule_fans_cnt_max = rule_dict.get('fans_cnt', {}).get('max', 100000000)
  51. if rule_fans_cnt_max == 0:
  52. rule_fans_cnt_max = 100000000
  53. rule_videos_cnt_min = rule_dict.get('videos_cnt', {}).get('min', 0)
  54. rule_videos_cnt_max = rule_dict.get('videos_cnt', {}).get('max', 100000000)
  55. if rule_videos_cnt_max == 0:
  56. rule_videos_cnt_max = 100000000
  57. rule_like_cnt_min = rule_dict.get('like_cnt', {}).get('min', 0)
  58. rule_like_cnt_max = rule_dict.get('like_cnt', {}).get('max', 100000000)
  59. if rule_like_cnt_max == 0:
  60. rule_like_cnt_max = 100000000
  61. rule_width_min = rule_dict.get('width', {}).get('min', 0)
  62. rule_width_max = rule_dict.get('width', {}).get('max', 100000000)
  63. if rule_width_max == 0:
  64. rule_width_max = 100000000
  65. rule_height_min = rule_dict.get('height', {}).get('min', 0)
  66. rule_height_max = rule_dict.get('height', {}).get('max', 100000000)
  67. if rule_height_max == 0:
  68. rule_height_max = 100000000
  69. rule_share_cnt_min = rule_dict.get('share_cnt', {}).get('min', 0)
  70. rule_share_cnt_max = rule_dict.get('share_cnt', {}).get('max', 100000000)
  71. if rule_share_cnt_max == 0:
  72. rule_share_cnt_max = 100000000
  73. rule_comment_cnt_min = rule_dict.get('comment_cnt', {}).get('min', 0)
  74. rule_comment_cnt_max = rule_dict.get('comment_cnt', {}).get('max', 100000000)
  75. if rule_comment_cnt_max == 0:
  76. rule_comment_cnt_max = 100000000
  77. rule_publish_time_min = rule_dict.get('publish_time', {}).get('min', 0)
  78. rule_publish_time_max = rule_dict.get('publish_time', {}).get('max', 0)
  79. if rule_publish_time_max == 0:
  80. rule_publish_time_max = 4102415999000 # 2099-12-31 23:59:59
  81. Common.logger(log_type, crawler).info(
  82. f'rule_duration_max:{rule_duration_max} >= duration:{int(float(video_dict["duration"]))} >= rule_duration_min:{int(rule_duration_min)}')
  83. Common.logger(log_type, crawler).info(
  84. f'rule_play_cnt_max:{int(rule_play_cnt_max)} >= play_cnt:{int(video_dict["play_cnt"])} >= rule_play_cnt_min:{int(rule_play_cnt_min)}')
  85. Common.logger(log_type, crawler).info(
  86. f'now:{int(time.time())} - publish_time_stamp:{int(video_dict["publish_time_stamp"])} <= {3600 * 24 * int(rule_period_min)}')
  87. Common.logger(log_type, crawler).info(
  88. f'rule_like_cnt_max:{int(rule_like_cnt_max)} >= like_cnt:{int(video_dict["like_cnt"])} >= rule_like_cnt_min:{int(rule_like_cnt_min)}')
  89. Common.logger(log_type, crawler).info(
  90. f'rule_comment_cnt_max:{int(rule_comment_cnt_max)} >= comment_cnt:{int(video_dict["comment_cnt"])} >= rule_comment_cnt_min:{int(rule_comment_cnt_min)}')
  91. Common.logger(log_type, crawler).info(
  92. f'rule_share_cnt_max:{int(rule_share_cnt_max)} >= share_cnt:{int(video_dict["share_cnt"])} >= rule_share_cnt_min:{int(rule_share_cnt_min)}')
  93. Common.logger(log_type, crawler).info(
  94. f'rule_width_max:{int(rule_width_max)} >= video_width:{int(video_dict["video_width"])} >= rule_width_min:{int(rule_width_min)}')
  95. Common.logger(log_type, crawler).info(
  96. f'rule_height_max:{int(rule_height_max)} >= video_height:{int(video_dict["video_height"])} >= rule_height_min:{int(rule_height_min)}')
  97. Common.logger(log_type, crawler).info(
  98. f'rule_publish_time_max:{int(rule_publish_time_max)} >= publish_time_stamp:{int(video_dict["publish_time_stamp"])} >= rule_publish_time_min:{int(rule_publish_time_min)}')
  99. if int(rule_duration_max) >= int(float(video_dict["duration"])) >= int(rule_duration_min) \
  100. and int(rule_play_cnt_max) >= int(video_dict['play_cnt']) >= int(rule_play_cnt_min) \
  101. and int(rule_like_cnt_max) >= int(video_dict['like_cnt']) >= int(rule_like_cnt_min) \
  102. and int(rule_comment_cnt_max) >= int(video_dict['comment_cnt']) >= int(rule_comment_cnt_min) \
  103. and int(rule_share_cnt_max) >= int(video_dict['share_cnt']) >= int(rule_share_cnt_min) \
  104. and int(rule_width_max) >= int(video_dict['video_width']) >= int(rule_width_min) \
  105. and int(rule_height_max) >= int(video_dict['video_height']) >= int(rule_height_min) \
  106. and int(rule_publish_time_max) >= int(video_dict['publish_time_stamp']) >= int(rule_publish_time_min) \
  107. and int(time.time()) - int(video_dict["publish_time_stamp"]) <= 3600 * 24 * int(rule_period_min):
  108. return True
  109. else:
  110. return False
  111. @classmethod
  112. def title_like(cls, log_type, crawler, title, env):
  113. select_sql = f""" select * from crawler_video where platform="公众号" """
  114. video_list = MysqlHelper.get_values(log_type, crawler, select_sql, env, action="")
  115. if len(video_list) == 0:
  116. return None
  117. for video_dict in video_list:
  118. video_title = video_dict["video_title"]
  119. if difflib.SequenceMatcher(None, title, video_title).quick_ratio() >= 0.8:
  120. return True
  121. else:
  122. pass
  123. # 获取 token
  124. @classmethod
  125. def get_token(cls, log_type, crawler, env):
  126. select_sql = f""" select * from crawler_config where source="{crawler}" and title LIKE "%公众号_4%";"""
  127. configs = MysqlHelper.get_values(log_type, crawler, select_sql, env, action="")
  128. if len(configs) == 0:
  129. Feishu.bot(log_type, crawler, "公众号_4:未配置token")
  130. time.sleep(60)
  131. return None
  132. token_dict = {
  133. "token_id": configs[0]["id"],
  134. "title": configs[0]["title"],
  135. "token": dict(eval(configs[0]["config"]))["token"],
  136. "cookie": dict(eval(configs[0]["config"]))["cookie"],
  137. "update_time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(configs[0]["update_time"]/1000))),
  138. "operator": configs[0]["operator"]
  139. }
  140. # for k, v in token_dict.items():
  141. # print(f"{k}:{v}")
  142. return token_dict
  143. # 获取用户 fakeid
  144. @classmethod
  145. def get_fakeid(cls, log_type, crawler, wechat_name, env):
  146. while True:
  147. token_dict = cls.get_token(log_type, crawler, env)
  148. url = "https://mp.weixin.qq.com/cgi-bin/searchbiz?"
  149. headers = {
  150. "accept": "*/*",
  151. "accept-encoding": "gzip, deflate, br",
  152. "accept-language": "zh-CN,zh;q=0.9",
  153. "referer": "https://mp.weixin.qq.com/cgi-bin/appmsg?"
  154. "t=media/appmsg_edit_v2&action=edit&isNew=1"
  155. "&type=77&createType=5&token=1011071554&lang=zh_CN",
  156. 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
  157. "sec-ch-ua-mobile": "?0",
  158. "sec-ch-ua-platform": '"Windows"',
  159. "sec-fetch-dest": "empty",
  160. "sec-fetch-mode": "cors",
  161. "sec-fetch-site": "same-origin",
  162. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
  163. " (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
  164. "x-requested-with": "XMLHttpRequest",
  165. 'cookie': token_dict['cookie'],
  166. }
  167. params = {
  168. "action": "search_biz",
  169. "begin": "0",
  170. "count": "5",
  171. "query": str(wechat_name),
  172. "token": token_dict['token'],
  173. "lang": "zh_CN",
  174. "f": "json",
  175. "ajax": "1",
  176. }
  177. urllib3.disable_warnings()
  178. r = requests.get(url=url, headers=headers, params=params, verify=False)
  179. r.close()
  180. if r.json()["base_resp"]["err_msg"] == "invalid session":
  181. Common.logger(log_type, crawler).warning(f"status_code:{r.status_code}")
  182. Common.logger(log_type, crawler).warning(f"get_fakeid:{r.text}\n")
  183. if 20 >= datetime.datetime.now().hour >= 10:
  184. Feishu.bot(log_type, crawler, f"{token_dict['title']}\n操作人:{token_dict['operator']}\n更换日期:{token_dict['update_time']} \n过期啦,请扫码更换token\nhttps://mp.weixin.qq.com/")
  185. time.sleep(60 * 10)
  186. continue
  187. if r.json()["base_resp"]["err_msg"] == "freq control":
  188. Common.logger(log_type, crawler).warning(f"status_code:{r.status_code}")
  189. Common.logger(log_type, crawler).warning(f"get_fakeid:{r.text}\n")
  190. if 20 >= datetime.datetime.now().hour >= 10:
  191. Feishu.bot(log_type, crawler, f"{token_dict['title']}\n操作人:{token_dict['operator']}\n更换日期:{token_dict['update_time']} \n频控啦,请扫码更换其他公众号token\nhttps://mp.weixin.qq.com/")
  192. time.sleep(60 * 10)
  193. continue
  194. if "list" not in r.json() or len(r.json()["list"]) == 0:
  195. Common.logger(log_type, crawler).warning(f"status_code:{r.status_code}")
  196. Common.logger(log_type, crawler).warning(f"get_fakeid:{r.text}\n")
  197. if 20 >= datetime.datetime.now().hour >= 10:
  198. Feishu.bot(log_type, crawler, f"{token_dict['title']}\n操作人:{token_dict['operator']}\n更换日期:{token_dict['update_time']} \n频控啦,请扫码更换其他公众号token\nhttps://mp.weixin.qq.com/")
  199. time.sleep(60 * 10)
  200. continue
  201. fakeid = r.json()["list"][0]["fakeid"]
  202. head_url = r.json()["list"][0]["round_head_img"]
  203. fakeid_dict = {'fakeid': fakeid, 'head_url': head_url}
  204. return fakeid_dict
  205. # 获取腾讯视频下载链接
  206. @classmethod
  207. def get_tencent_video_url(cls, video_id):
  208. url = 'https://vv.video.qq.com/getinfo?vids=' + str(video_id) + '&platform=101001&charge=0&otype=json'
  209. response = requests.get(url=url).text.replace('QZOutputJson=', '').replace('"};', '"}')
  210. response = json.loads(response)
  211. url = response['vl']['vi'][0]['ul']['ui'][0]['url']
  212. fvkey = response['vl']['vi'][0]['fvkey']
  213. video_url = url + str(video_id) + '.mp4?vkey=' + fvkey
  214. return video_url
  215. @classmethod
  216. def get_video_url(cls, article_url, env):
  217. # 打印请求配置
  218. ca = DesiredCapabilities.CHROME
  219. ca["goog:loggingPrefs"] = {"performance": "ALL"}
  220. # 不打开浏览器运行
  221. chrome_options = webdriver.ChromeOptions()
  222. chrome_options.add_argument("headless")
  223. chrome_options.add_argument(
  224. f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36')
  225. chrome_options.add_argument("--no-sandbox")
  226. # driver初始化
  227. if env == "prod":
  228. driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options)
  229. else:
  230. driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service(
  231. '/Users/wangkun/Downloads/chromedriver/chromedriver_v111/chromedriver'))
  232. driver.implicitly_wait(10)
  233. # Common.logger(log_type, crawler).info('打开文章链接')
  234. driver.get(article_url)
  235. time.sleep(1)
  236. if len(driver.find_elements(By.XPATH, '//div[@class="js_video_poster video_poster"]/*[2]')) != 0:
  237. video_url = driver.find_element(
  238. By.XPATH, '//div[@class="js_video_poster video_poster"]/*[2]').get_attribute('src')
  239. elif len(driver.find_elements(By.XPATH, '//span[@class="js_tx_video_container"]/*[1]')) != 0:
  240. iframe = driver.find_element(By.XPATH, '//span[@class="js_tx_video_container"]/*[1]').get_attribute(
  241. 'src')
  242. video_id = iframe.split('vid=')[-1].split('&')[0]
  243. video_url = cls.get_tencent_video_url(video_id)
  244. else:
  245. video_url = 0
  246. driver.quit()
  247. return video_url
  248. # 获取文章列表
  249. @classmethod
  250. def get_videoList(cls, log_type, crawler, wechat_name, rule_dict, user_name, uid, oss_endpoint, env):
  251. begin = 0
  252. while True:
  253. token_dict = cls.get_token(log_type, crawler, env)
  254. fakeid_dict = cls.get_fakeid(log_type=log_type,
  255. crawler=crawler,
  256. wechat_name=wechat_name,
  257. env=env)
  258. url = "https://mp.weixin.qq.com/cgi-bin/appmsg?"
  259. headers = {
  260. "accept": "*/*",
  261. "accept-encoding": "gzip, deflate, br",
  262. "accept-language": "zh-CN,zh;q=0.9",
  263. "referer": "https://mp.weixin.qq.com/cgi-bin/appmsg?"
  264. "t=media/appmsg_edit_v2&action=edit&isNew=1"
  265. "&type=77&createType=5&token=" + str(token_dict['token']) + "&lang=zh_CN",
  266. 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
  267. "sec-ch-ua-mobile": "?0",
  268. "sec-ch-ua-platform": '"Windows"',
  269. "sec-fetch-dest": "empty",
  270. "sec-fetch-mode": "cors",
  271. "sec-fetch-site": "same-origin",
  272. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
  273. " (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
  274. "x-requested-with": "XMLHttpRequest",
  275. 'cookie': token_dict['cookie'],
  276. }
  277. params = {
  278. "action": "list_ex",
  279. "begin": str(begin),
  280. "count": "5",
  281. "fakeid": fakeid_dict['fakeid'],
  282. "type": "9",
  283. "query": "",
  284. "token": str(token_dict['token']),
  285. "lang": "zh_CN",
  286. "f": "json",
  287. "ajax": "1",
  288. }
  289. urllib3.disable_warnings()
  290. r = requests.get(url=url, headers=headers, params=params, verify=False)
  291. r.close()
  292. if r.json()["base_resp"]["err_msg"] == "invalid session":
  293. Common.logger(log_type, crawler).warning(f"status_code:{r.status_code}")
  294. Common.logger(log_type, crawler).warning(f"get_videoList:{r.text}\n")
  295. if 20 >= datetime.datetime.now().hour >= 10:
  296. Feishu.bot(log_type, crawler, f"{token_dict['title']}\n操作人:{token_dict['operator']}\n更换日期:{token_dict['update_time']}\n过期啦,请扫码更换token\nhttps://mp.weixin.qq.com/")
  297. time.sleep(60 * 10)
  298. continue
  299. if r.json()["base_resp"]["err_msg"] == "freq control":
  300. Common.logger(log_type, crawler).warning(f"status_code:{r.status_code}")
  301. Common.logger(log_type, crawler).warning(f"get_videoList:{r.text}\n")
  302. if 20 >= datetime.datetime.now().hour >= 10:
  303. Feishu.bot(log_type, crawler,f"{token_dict['title']}\n操作人:{token_dict['operator']}\n更换日期:{token_dict['update_time']} \n频控啦,请扫码更换其他公众号token\nhttps://mp.weixin.qq.com/")
  304. time.sleep(60 * 10)
  305. continue
  306. if 'app_msg_list' not in r.json():
  307. Common.logger(log_type, crawler).warning(f"status_code:{r.status_code}")
  308. Common.logger(log_type, crawler).warning(f"get_videoList:{r.text}\n")
  309. if 20 >= datetime.datetime.now().hour >= 10:
  310. Feishu.bot(log_type, crawler, f"{token_dict['title']}\n操作人:{token_dict['operator']}\n更换日期:{token_dict['update_time']}\n频控啦,请扫码更换其他公众号token\nhttps://mp.weixin.qq.com/")
  311. time.sleep(60 * 10)
  312. continue
  313. if len(r.json()['app_msg_list']) == 0:
  314. Common.logger(log_type, crawler).info('没有更多视频了\n')
  315. return
  316. else:
  317. begin += 5
  318. app_msg_list = r.json()['app_msg_list']
  319. for article_url in app_msg_list:
  320. # title
  321. video_title = article_url.get("title", "").replace('/', '').replace('\n', '') \
  322. .replace('.', '').replace('“', '').replace('”', '').replace(' ', '')\
  323. .replace('"', '').replace("'", "")
  324. # aid
  325. aid = article_url.get('aid', '')
  326. # create_time
  327. create_time = article_url.get('create_time', 0)
  328. publish_time_stamp = int(create_time)
  329. publish_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(publish_time_stamp))
  330. avatar_url = fakeid_dict['head_url']
  331. # cover_url
  332. cover_url = article_url.get('cover', '')
  333. # article_url
  334. article_url = article_url.get('link', '')
  335. video_url = cls.get_video_url(article_url, env)
  336. video_dict = {
  337. 'video_id': aid,
  338. 'video_title': video_title,
  339. 'publish_time_stamp': publish_time_stamp,
  340. 'publish_time_str': publish_time_str,
  341. 'user_name': user_name,
  342. 'play_cnt': 0,
  343. 'comment_cnt': 0,
  344. 'like_cnt': 0,
  345. 'share_cnt': 0,
  346. 'user_id': fakeid_dict['fakeid'],
  347. 'avatar_url': avatar_url,
  348. 'cover_url': cover_url,
  349. 'article_url': article_url,
  350. 'video_url': video_url,
  351. 'session': f'gongzhonghao-author1-{int(time.time())}'
  352. }
  353. for k, v in video_dict.items():
  354. Common.logger(log_type, crawler).info(f"{k}:{v}")
  355. if int(time.time()) - publish_time_stamp > 3600 * 24 * int(rule_dict.get('period', {}).get('min', 1000)):
  356. Common.logger(log_type, crawler).info(f"发布时间超过{int(rule_dict.get('period', {}).get('min', 1000))}天\n")
  357. cls.begin = 0
  358. return
  359. if video_dict['article_url'] == 0 or video_dict['video_url'] == 0:
  360. Common.logger(log_type, crawler).info("文章涉嫌违反相关法律法规和政策\n")
  361. # 标题敏感词过滤
  362. elif any(str(word) if str(word) in video_dict['video_title'] else False
  363. for word in get_config_from_mysql(log_type=log_type,
  364. source=crawler,
  365. env=env,
  366. text="filter",
  367. action="")) is True:
  368. Common.logger(log_type, crawler).info("标题已中过滤词\n")
  369. # 已下载判断
  370. elif cls.repeat_video(log_type, crawler, video_dict['video_id'], env) != 0:
  371. Common.logger(log_type, crawler).info("视频已下载\n")
  372. # 标题相似度
  373. elif cls.title_like(log_type, crawler, video_dict['video_title'], env) is True:
  374. Common.logger(log_type, crawler).info(f'标题相似度>=80%:{video_dict["video_title"]}\n')
  375. else:
  376. cls.download_publish(log_type=log_type,
  377. crawler=crawler,
  378. video_dict=video_dict,
  379. rule_dict=rule_dict,
  380. uid=uid,
  381. oss_endpoint=oss_endpoint,
  382. env=env)
  383. Common.logger(log_type, crawler).info('休眠 60 秒\n')
  384. time.sleep(60)
  385. @classmethod
  386. def repeat_video(cls, log_type, crawler, video_id, env):
  387. sql = f""" select * from crawler_video where platform="公众号" and out_video_id="{video_id}"; """
  388. repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env)
  389. return len(repeat_video)
  390. # 下载/上传
  391. @classmethod
  392. def download_publish(cls, log_type, crawler, video_dict, rule_dict, uid, oss_endpoint, env):
  393. # 下载视频
  394. Common.download_method(log_type=log_type, crawler=crawler, text="video",
  395. title=video_dict["video_title"], url=video_dict["video_url"])
  396. md_title = md5(video_dict['video_title'].encode('utf8')).hexdigest()
  397. # 获取视频时长
  398. ffmpeg_dict = Common.ffmpeg(log_type, crawler,
  399. f"./{crawler}/videos/{video_dict['video_title']}/video.mp4")
  400. if ffmpeg_dict is None:
  401. # 删除视频文件夹
  402. shutil.rmtree(f"./{crawler}/videos/{md_title}")
  403. Common.logger(log_type, crawler).info("视频size=0,删除成功\n")
  404. return
  405. video_dict["video_width"] = ffmpeg_dict["width"]
  406. video_dict["video_height"] = ffmpeg_dict["height"]
  407. video_dict["duration"] = ffmpeg_dict["duration"]
  408. video_size = ffmpeg_dict["size"]
  409. Common.logger(log_type, crawler).info(f'video_width:{video_dict["video_width"]}')
  410. Common.logger(log_type, crawler).info(f'video_height:{video_dict["video_height"]}')
  411. Common.logger(log_type, crawler).info(f'duration:{video_dict["duration"]}')
  412. Common.logger(log_type, crawler).info(f'video_size:{video_size}')
  413. # 视频size=0,直接删除
  414. if int(video_size) == 0 or cls.download_rule(log_type, crawler, video_dict, rule_dict) is False:
  415. # 删除视频文件夹
  416. shutil.rmtree(f"./{crawler}/videos/{md_title}")
  417. Common.logger(log_type, crawler).info("视频size=0,删除成功\n")
  418. return
  419. if cls.download_rule(log_type, crawler, video_dict, rule_dict) is False:
  420. shutil.rmtree(f"./{crawler}/videos/{md_title}")
  421. Common.logger(log_type, crawler).info("不满足抓取规则,删除成功\n")
  422. return
  423. # 下载封面
  424. Common.download_method(log_type=log_type, crawler=crawler, text="cover",
  425. title=video_dict["video_title"], url=video_dict["cover_url"])
  426. # 保存视频信息至 "./videos/{video_title}/info.txt"
  427. Common.save_video_info(log_type=log_type, crawler=crawler, video_dict=video_dict)
  428. # 上传视频
  429. Common.logger(log_type, crawler).info("开始上传视频...")
  430. strategy = "定向榜爬虫策略"
  431. our_video_id = Publish.upload_and_publish(log_type=log_type,
  432. crawler=crawler,
  433. strategy=strategy,
  434. our_uid=uid,
  435. oss_endpoint=oss_endpoint,
  436. env=env)
  437. if env == 'prod':
  438. our_video_link = f"https://admin.piaoquantv.com/cms/post-detail/{str(our_video_id)}/info"
  439. else:
  440. our_video_link = f"https://testadmin.piaoquantv.com/cms/post-detail/{str(our_video_id)}/info"
  441. Common.logger(log_type, crawler).info("视频上传完成")
  442. if our_video_id is None:
  443. # 删除视频文件夹
  444. shutil.rmtree(f"./{crawler}/videos/{video_dict['video_title']}")
  445. return
  446. insert_sql = f""" insert into crawler_video(video_id,
  447. out_user_id,
  448. platform,
  449. strategy,
  450. out_video_id,
  451. video_title,
  452. cover_url,
  453. video_url,
  454. duration,
  455. publish_time,
  456. play_cnt,
  457. crawler_rule,
  458. width,
  459. height)
  460. values({our_video_id},
  461. "{video_dict['user_id']}",
  462. "{cls.platform}",
  463. "定向爬虫策略",
  464. "{video_dict['video_id']}",
  465. "{video_dict['video_title']}",
  466. "{video_dict['cover_url']}",
  467. "{video_dict['video_url']}",
  468. {int(video_dict['duration'])},
  469. "{video_dict['publish_time_str']}",
  470. {int(video_dict['play_cnt'])},
  471. '{json.dumps(rule_dict)}',
  472. {int(video_dict['video_width'])},
  473. {int(video_dict['video_height'])}) """
  474. Common.logger(log_type, crawler).info(f"insert_sql:{insert_sql}")
  475. MysqlHelper.update_values(log_type, crawler, insert_sql, env)
  476. Common.logger(log_type, crawler).info('视频信息插入数据库成功!')
  477. # 视频写入飞书
  478. Feishu.insert_columns(log_type, crawler, "47e39d", "ROWS", 1, 2)
  479. # 视频ID工作表,首行写入数据
  480. upload_time = int(time.time())
  481. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(upload_time)),
  482. "用户主页",
  483. video_dict['video_title'],
  484. video_dict['video_id'],
  485. our_video_link,
  486. int(video_dict['duration']),
  487. f"{video_dict['video_width']}*{video_dict['video_height']}",
  488. video_dict['publish_time_str'],
  489. video_dict['user_name'],
  490. video_dict['user_id'],
  491. video_dict['avatar_url'],
  492. video_dict['cover_url'],
  493. video_dict['article_url'],
  494. video_dict['video_url']]]
  495. time.sleep(0.5)
  496. Feishu.update_values(log_type, crawler, "47e39d", "F2:Z2", values)
  497. Common.logger(log_type, crawler).info('视频下载/上传成功\n')
  498. @classmethod
  499. def get_all_videos(cls, log_type, crawler, user_list, rule_dict, oss_endpoint, env):
  500. if len(user_list) == 0:
  501. Common.logger(log_type, crawler).warning(f"抓取用户列表为空\n")
  502. return
  503. for user in user_list:
  504. # try:
  505. user_name = user['nick_name']
  506. wechat_name = user['link']
  507. uid = user['uid']
  508. Common.logger(log_type, crawler).info(f'获取 {user_name} 公众号视频\n')
  509. cls.get_videoList(log_type=log_type,
  510. crawler=crawler,
  511. wechat_name=wechat_name,
  512. rule_dict=rule_dict,
  513. user_name=user_name,
  514. uid=uid,
  515. oss_endpoint=oss_endpoint,
  516. env=env)
  517. Common.logger(log_type, crawler).info('休眠 60 秒\n')
  518. time.sleep(60)
  519. # except Exception as e:
  520. # Common.logger(log_type, crawler).info(f'get_all_videos异常:{e}\n')
  521. if __name__ == "__main__":
  522. GongzhonghaoAuthor4.get_token("author", "gongzhonghao", "dev")
  523. # print(get_config_from_mysql("author", "gongzhonghao", "dev", "filter", action=""))
  524. pass