xigua_search_publish_time.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/5/26
  4. import base64
  5. import json
  6. import os
  7. import random
  8. import shutil
  9. import string
  10. import sys
  11. import time
  12. from hashlib import md5
  13. import requests
  14. import urllib3
  15. from requests.adapters import HTTPAdapter
  16. from selenium.webdriver import DesiredCapabilities
  17. from selenium.webdriver.chrome.service import Service
  18. from selenium import webdriver
  19. from selenium.webdriver.common.by import By
  20. sys.path.append(os.getcwd())
  21. from common.mq import MQ
  22. from common.scheduling_db import MysqlHelper
  23. from common.common import Common
  24. from common.feishu import Feishu
  25. from common.publish import Publish
  26. from common.public import get_config_from_mysql, download_rule
  27. from common.userAgent import get_random_user_agent
  28. class XiguasearchScheduling:
  29. # 已下载视频数
  30. download_cnt = 0
  31. platform = "西瓜视频"
  32. @classmethod
  33. def random_signature(cls):
  34. src_digits = string.digits # string_数字
  35. src_uppercase = string.ascii_uppercase # string_大写字母
  36. src_lowercase = string.ascii_lowercase # string_小写字母
  37. digits_num = random.randint(1, 6)
  38. uppercase_num = random.randint(1, 26 - digits_num - 1)
  39. lowercase_num = 26 - (digits_num + uppercase_num)
  40. password = random.sample(src_digits, digits_num) + random.sample(src_uppercase, uppercase_num) + random.sample(
  41. src_lowercase, lowercase_num)
  42. random.shuffle(password)
  43. new_password = 'AAAAAAAAAA' + ''.join(password)[10:-4] + 'AAAB'
  44. new_password_start = new_password[0:18]
  45. new_password_end = new_password[-7:]
  46. if new_password[18] == '8':
  47. new_password = new_password_start + 'w' + new_password_end
  48. elif new_password[18] == '9':
  49. new_password = new_password_start + 'x' + new_password_end
  50. elif new_password[18] == '-':
  51. new_password = new_password_start + 'y' + new_password_end
  52. elif new_password[18] == '.':
  53. new_password = new_password_start + 'z' + new_password_end
  54. else:
  55. new_password = new_password_start + 'y' + new_password_end
  56. return new_password
  57. @classmethod
  58. def get_video_url(cls, video_info):
  59. video_url_dict = {}
  60. # video_url
  61. if 'videoResource' not in video_info:
  62. video_url_dict["video_url"] = ''
  63. video_url_dict["audio_url"] = ''
  64. video_url_dict["video_width"] = 0
  65. video_url_dict["video_height"] = 0
  66. elif 'dash_120fps' in video_info['videoResource']:
  67. if "video_list" in video_info['videoResource']['dash_120fps'] and 'video_4' in \
  68. video_info['videoResource']['dash_120fps']['video_list']:
  69. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_4']['backup_url_1']
  70. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_4']['backup_url_1']
  71. if len(video_url) % 3 == 1:
  72. video_url += '=='
  73. elif len(video_url) % 3 == 2:
  74. video_url += '='
  75. elif len(audio_url) % 3 == 1:
  76. audio_url += '=='
  77. elif len(audio_url) % 3 == 2:
  78. audio_url += '='
  79. video_url = base64.b64decode(video_url).decode('utf8')
  80. audio_url = base64.b64decode(audio_url).decode('utf8')
  81. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_4']['vwidth']
  82. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_4']['vheight']
  83. video_url_dict["video_url"] = video_url
  84. video_url_dict["audio_url"] = audio_url
  85. video_url_dict["video_width"] = video_width
  86. video_url_dict["video_height"] = video_height
  87. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_3' in \
  88. video_info['videoResource']['dash_120fps']['video_list']:
  89. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_3']['backup_url_1']
  90. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_3']['backup_url_1']
  91. if len(video_url) % 3 == 1:
  92. video_url += '=='
  93. elif len(video_url) % 3 == 2:
  94. video_url += '='
  95. elif len(audio_url) % 3 == 1:
  96. audio_url += '=='
  97. elif len(audio_url) % 3 == 2:
  98. audio_url += '='
  99. video_url = base64.b64decode(video_url).decode('utf8')
  100. audio_url = base64.b64decode(audio_url).decode('utf8')
  101. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_3']['vwidth']
  102. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_3']['vheight']
  103. video_url_dict["video_url"] = video_url
  104. video_url_dict["audio_url"] = audio_url
  105. video_url_dict["video_width"] = video_width
  106. video_url_dict["video_height"] = video_height
  107. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_2' in \
  108. video_info['videoResource']['dash_120fps']['video_list']:
  109. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_2']['backup_url_1']
  110. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_2']['backup_url_1']
  111. if len(video_url) % 3 == 1:
  112. video_url += '=='
  113. elif len(video_url) % 3 == 2:
  114. video_url += '='
  115. elif len(audio_url) % 3 == 1:
  116. audio_url += '=='
  117. elif len(audio_url) % 3 == 2:
  118. audio_url += '='
  119. video_url = base64.b64decode(video_url).decode('utf8')
  120. audio_url = base64.b64decode(audio_url).decode('utf8')
  121. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_2']['vwidth']
  122. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_2']['vheight']
  123. video_url_dict["video_url"] = video_url
  124. video_url_dict["audio_url"] = audio_url
  125. video_url_dict["video_width"] = video_width
  126. video_url_dict["video_height"] = video_height
  127. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_1' in \
  128. video_info['videoResource']['dash_120fps']['video_list']:
  129. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_1']['backup_url_1']
  130. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_1']['backup_url_1']
  131. if len(video_url) % 3 == 1:
  132. video_url += '=='
  133. elif len(video_url) % 3 == 2:
  134. video_url += '='
  135. elif len(audio_url) % 3 == 1:
  136. audio_url += '=='
  137. elif len(audio_url) % 3 == 2:
  138. audio_url += '='
  139. video_url = base64.b64decode(video_url).decode('utf8')
  140. audio_url = base64.b64decode(audio_url).decode('utf8')
  141. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_1']['vwidth']
  142. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_1']['vheight']
  143. video_url_dict["video_url"] = video_url
  144. video_url_dict["audio_url"] = audio_url
  145. video_url_dict["video_width"] = video_width
  146. video_url_dict["video_height"] = video_height
  147. elif 'dynamic_video' in video_info['videoResource']['dash_120fps'] \
  148. and 'dynamic_video_list' in video_info['videoResource']['dash_120fps']['dynamic_video'] \
  149. and 'dynamic_audio_list' in video_info['videoResource']['dash_120fps']['dynamic_video'] \
  150. and len(
  151. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list']) != 0 \
  152. and len(
  153. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_audio_list']) != 0:
  154. video_url = \
  155. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1][
  156. 'backup_url_1']
  157. audio_url = \
  158. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_audio_list'][-1][
  159. 'backup_url_1']
  160. if len(video_url) % 3 == 1:
  161. video_url += '=='
  162. elif len(video_url) % 3 == 2:
  163. video_url += '='
  164. elif len(audio_url) % 3 == 1:
  165. audio_url += '=='
  166. elif len(audio_url) % 3 == 2:
  167. audio_url += '='
  168. video_url = base64.b64decode(video_url).decode('utf8')
  169. audio_url = base64.b64decode(audio_url).decode('utf8')
  170. video_width = \
  171. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1][
  172. 'vwidth']
  173. video_height = \
  174. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1][
  175. 'vheight']
  176. video_url_dict["video_url"] = video_url
  177. video_url_dict["audio_url"] = audio_url
  178. video_url_dict["video_width"] = video_width
  179. video_url_dict["video_height"] = video_height
  180. else:
  181. video_url_dict["video_url"] = ''
  182. video_url_dict["audio_url"] = ''
  183. video_url_dict["video_width"] = 0
  184. video_url_dict["video_height"] = 0
  185. elif 'dash' in video_info['videoResource']:
  186. if "video_list" in video_info['videoResource']['dash'] and 'video_4' in \
  187. video_info['videoResource']['dash']['video_list']:
  188. video_url = video_info['videoResource']['dash']['video_list']['video_4']['backup_url_1']
  189. audio_url = video_info['videoResource']['dash']['video_list']['video_4']['backup_url_1']
  190. if len(video_url) % 3 == 1:
  191. video_url += '=='
  192. elif len(video_url) % 3 == 2:
  193. video_url += '='
  194. elif len(audio_url) % 3 == 1:
  195. audio_url += '=='
  196. elif len(audio_url) % 3 == 2:
  197. audio_url += '='
  198. video_url = base64.b64decode(video_url).decode('utf8')
  199. audio_url = base64.b64decode(audio_url).decode('utf8')
  200. video_width = video_info['videoResource']['dash']['video_list']['video_4']['vwidth']
  201. video_height = video_info['videoResource']['dash']['video_list']['video_4']['vheight']
  202. video_url_dict["video_url"] = video_url
  203. video_url_dict["audio_url"] = audio_url
  204. video_url_dict["video_width"] = video_width
  205. video_url_dict["video_height"] = video_height
  206. elif "video_list" in video_info['videoResource']['dash'] and 'video_3' in \
  207. video_info['videoResource']['dash']['video_list']:
  208. video_url = video_info['videoResource']['dash']['video_list']['video_3']['backup_url_1']
  209. audio_url = video_info['videoResource']['dash']['video_list']['video_3']['backup_url_1']
  210. if len(video_url) % 3 == 1:
  211. video_url += '=='
  212. elif len(video_url) % 3 == 2:
  213. video_url += '='
  214. elif len(audio_url) % 3 == 1:
  215. audio_url += '=='
  216. elif len(audio_url) % 3 == 2:
  217. audio_url += '='
  218. video_url = base64.b64decode(video_url).decode('utf8')
  219. audio_url = base64.b64decode(audio_url).decode('utf8')
  220. video_width = video_info['videoResource']['dash']['video_list']['video_3']['vwidth']
  221. video_height = video_info['videoResource']['dash']['video_list']['video_3']['vheight']
  222. video_url_dict["video_url"] = video_url
  223. video_url_dict["audio_url"] = audio_url
  224. video_url_dict["video_width"] = video_width
  225. video_url_dict["video_height"] = video_height
  226. elif "video_list" in video_info['videoResource']['dash'] and 'video_2' in \
  227. video_info['videoResource']['dash']['video_list']:
  228. video_url = video_info['videoResource']['dash']['video_list']['video_2']['backup_url_1']
  229. audio_url = video_info['videoResource']['dash']['video_list']['video_2']['backup_url_1']
  230. if len(video_url) % 3 == 1:
  231. video_url += '=='
  232. elif len(video_url) % 3 == 2:
  233. video_url += '='
  234. elif len(audio_url) % 3 == 1:
  235. audio_url += '=='
  236. elif len(audio_url) % 3 == 2:
  237. audio_url += '='
  238. video_url = base64.b64decode(video_url).decode('utf8')
  239. audio_url = base64.b64decode(audio_url).decode('utf8')
  240. video_width = video_info['videoResource']['dash']['video_list']['video_2']['vwidth']
  241. video_height = video_info['videoResource']['dash']['video_list']['video_2']['vheight']
  242. video_url_dict["video_url"] = video_url
  243. video_url_dict["audio_url"] = audio_url
  244. video_url_dict["video_width"] = video_width
  245. video_url_dict["video_height"] = video_height
  246. elif "video_list" in video_info['videoResource']['dash'] and 'video_1' in \
  247. video_info['videoResource']['dash']['video_list']:
  248. video_url = video_info['videoResource']['dash']['video_list']['video_1']['backup_url_1']
  249. audio_url = video_info['videoResource']['dash']['video_list']['video_1']['backup_url_1']
  250. if len(video_url) % 3 == 1:
  251. video_url += '=='
  252. elif len(video_url) % 3 == 2:
  253. video_url += '='
  254. elif len(audio_url) % 3 == 1:
  255. audio_url += '=='
  256. elif len(audio_url) % 3 == 2:
  257. audio_url += '='
  258. video_url = base64.b64decode(video_url).decode('utf8')
  259. audio_url = base64.b64decode(audio_url).decode('utf8')
  260. video_width = video_info['videoResource']['dash']['video_list']['video_1']['vwidth']
  261. video_height = video_info['videoResource']['dash']['video_list']['video_1']['vheight']
  262. video_url_dict["video_url"] = video_url
  263. video_url_dict["audio_url"] = audio_url
  264. video_url_dict["video_width"] = video_width
  265. video_url_dict["video_height"] = video_height
  266. elif 'dynamic_video' in video_info['videoResource']['dash'] \
  267. and 'dynamic_video_list' in video_info['videoResource']['dash']['dynamic_video'] \
  268. and 'dynamic_audio_list' in video_info['videoResource']['dash']['dynamic_video'] \
  269. and len(video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list']) != 0 \
  270. and len(video_info['videoResource']['dash']['dynamic_video']['dynamic_audio_list']) != 0:
  271. video_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  272. 'backup_url_1']
  273. audio_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_audio_list'][-1][
  274. 'backup_url_1']
  275. if len(video_url) % 3 == 1:
  276. video_url += '=='
  277. elif len(video_url) % 3 == 2:
  278. video_url += '='
  279. elif len(audio_url) % 3 == 1:
  280. audio_url += '=='
  281. elif len(audio_url) % 3 == 2:
  282. audio_url += '='
  283. video_url = base64.b64decode(video_url).decode('utf8')
  284. audio_url = base64.b64decode(audio_url).decode('utf8')
  285. video_width = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  286. 'vwidth']
  287. video_height = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  288. 'vheight']
  289. video_url_dict["video_url"] = video_url
  290. video_url_dict["audio_url"] = audio_url
  291. video_url_dict["video_width"] = video_width
  292. video_url_dict["video_height"] = video_height
  293. else:
  294. video_url_dict["video_url"] = ''
  295. video_url_dict["audio_url"] = ''
  296. video_url_dict["video_width"] = 0
  297. video_url_dict["video_height"] = 0
  298. elif 'normal' in video_info['videoResource']:
  299. if "video_list" in video_info['videoResource']['normal'] and 'video_4' in \
  300. video_info['videoResource']['normal']['video_list']:
  301. video_url = video_info['videoResource']['normal']['video_list']['video_4']['backup_url_1']
  302. audio_url = video_info['videoResource']['normal']['video_list']['video_4']['backup_url_1']
  303. if len(video_url) % 3 == 1:
  304. video_url += '=='
  305. elif len(video_url) % 3 == 2:
  306. video_url += '='
  307. elif len(audio_url) % 3 == 1:
  308. audio_url += '=='
  309. elif len(audio_url) % 3 == 2:
  310. audio_url += '='
  311. video_url = base64.b64decode(video_url).decode('utf8')
  312. audio_url = base64.b64decode(audio_url).decode('utf8')
  313. video_width = video_info['videoResource']['normal']['video_list']['video_4']['vwidth']
  314. video_height = video_info['videoResource']['normal']['video_list']['video_4']['vheight']
  315. video_url_dict["video_url"] = video_url
  316. video_url_dict["audio_url"] = audio_url
  317. video_url_dict["video_width"] = video_width
  318. video_url_dict["video_height"] = video_height
  319. elif "video_list" in video_info['videoResource']['normal'] and 'video_3' in \
  320. video_info['videoResource']['normal']['video_list']:
  321. video_url = video_info['videoResource']['normal']['video_list']['video_3']['backup_url_1']
  322. audio_url = video_info['videoResource']['normal']['video_list']['video_3']['backup_url_1']
  323. if len(video_url) % 3 == 1:
  324. video_url += '=='
  325. elif len(video_url) % 3 == 2:
  326. video_url += '='
  327. elif len(audio_url) % 3 == 1:
  328. audio_url += '=='
  329. elif len(audio_url) % 3 == 2:
  330. audio_url += '='
  331. video_url = base64.b64decode(video_url).decode('utf8')
  332. audio_url = base64.b64decode(audio_url).decode('utf8')
  333. video_width = video_info['videoResource']['normal']['video_list']['video_3']['vwidth']
  334. video_height = video_info['videoResource']['normal']['video_list']['video_3']['vheight']
  335. video_url_dict["video_url"] = video_url
  336. video_url_dict["audio_url"] = audio_url
  337. video_url_dict["video_width"] = video_width
  338. video_url_dict["video_height"] = video_height
  339. elif "video_list" in video_info['videoResource']['normal'] and 'video_2' in \
  340. video_info['videoResource']['normal']['video_list']:
  341. video_url = video_info['videoResource']['normal']['video_list']['video_2']['backup_url_1']
  342. audio_url = video_info['videoResource']['normal']['video_list']['video_2']['backup_url_1']
  343. if len(video_url) % 3 == 1:
  344. video_url += '=='
  345. elif len(video_url) % 3 == 2:
  346. video_url += '='
  347. elif len(audio_url) % 3 == 1:
  348. audio_url += '=='
  349. elif len(audio_url) % 3 == 2:
  350. audio_url += '='
  351. video_url = base64.b64decode(video_url).decode('utf8')
  352. audio_url = base64.b64decode(audio_url).decode('utf8')
  353. video_width = video_info['videoResource']['normal']['video_list']['video_2']['vwidth']
  354. video_height = video_info['videoResource']['normal']['video_list']['video_2']['vheight']
  355. video_url_dict["video_url"] = video_url
  356. video_url_dict["audio_url"] = audio_url
  357. video_url_dict["video_width"] = video_width
  358. video_url_dict["video_height"] = video_height
  359. elif "video_list" in video_info['videoResource']['normal'] and 'video_1' in \
  360. video_info['videoResource']['normal']['video_list']:
  361. video_url = video_info['videoResource']['normal']['video_list']['video_1']['backup_url_1']
  362. audio_url = video_info['videoResource']['normal']['video_list']['video_1']['backup_url_1']
  363. if len(video_url) % 3 == 1:
  364. video_url += '=='
  365. elif len(video_url) % 3 == 2:
  366. video_url += '='
  367. elif len(audio_url) % 3 == 1:
  368. audio_url += '=='
  369. elif len(audio_url) % 3 == 2:
  370. audio_url += '='
  371. video_url = base64.b64decode(video_url).decode('utf8')
  372. audio_url = base64.b64decode(audio_url).decode('utf8')
  373. video_width = video_info['videoResource']['normal']['video_list']['video_1']['vwidth']
  374. video_height = video_info['videoResource']['normal']['video_list']['video_1']['vheight']
  375. video_url_dict["video_url"] = video_url
  376. video_url_dict["audio_url"] = audio_url
  377. video_url_dict["video_width"] = video_width
  378. video_url_dict["video_height"] = video_height
  379. elif 'dynamic_video' in video_info['videoResource']['normal'] \
  380. and 'dynamic_video_list' in video_info['videoResource']['normal']['dynamic_video'] \
  381. and 'dynamic_audio_list' in video_info['videoResource']['normal']['dynamic_video'] \
  382. and len(video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list']) != 0 \
  383. and len(video_info['videoResource']['normal']['dynamic_video']['dynamic_audio_list']) != 0:
  384. video_url = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  385. 'backup_url_1']
  386. audio_url = video_info['videoResource']['normal']['dynamic_video']['dynamic_audio_list'][-1][
  387. 'backup_url_1']
  388. if len(video_url) % 3 == 1:
  389. video_url += '=='
  390. elif len(video_url) % 3 == 2:
  391. video_url += '='
  392. elif len(audio_url) % 3 == 1:
  393. audio_url += '=='
  394. elif len(audio_url) % 3 == 2:
  395. audio_url += '='
  396. video_url = base64.b64decode(video_url).decode('utf8')
  397. audio_url = base64.b64decode(audio_url).decode('utf8')
  398. video_width = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  399. 'vwidth']
  400. video_height = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  401. 'vheight']
  402. video_url_dict["video_url"] = video_url
  403. video_url_dict["audio_url"] = audio_url
  404. video_url_dict["video_width"] = video_width
  405. video_url_dict["video_height"] = video_height
  406. else:
  407. video_url_dict["video_url"] = ''
  408. video_url_dict["audio_url"] = ''
  409. video_url_dict["video_width"] = 0
  410. video_url_dict["video_height"] = 0
  411. else:
  412. video_url_dict["video_url"] = ''
  413. video_url_dict["audio_url"] = ''
  414. video_url_dict["video_width"] = 0
  415. video_url_dict["video_height"] = 0
  416. return video_url_dict
  417. @classmethod
  418. def get_comment_cnt(cls, item_id):
  419. url = "https://www.ixigua.com/tlb/comment/article/v5/tab_comments/?"
  420. params = {
  421. "tab_index": "0",
  422. "count": "10",
  423. "offset": "10",
  424. "group_id": str(item_id),
  425. "item_id": str(item_id),
  426. "aid": "1768",
  427. "msToken": "50-JJObWB07HfHs-BMJWT1eIDX3G-6lPSF_i-QwxBIXE9VVa-iN0jbEXR5pG2DKjXBmP299n6ZTuXzY-GAy968CCvouSAYIS4GzvGQT3pNlKNejr5G4-1g==",
  428. "X-Bogus": "DFSzswVOyGtANVeWtCLMqR/F6q9U",
  429. "_signature": cls.random_signature(),
  430. }
  431. headers = {
  432. 'authority': 'www.ixigua.com',
  433. 'accept': 'application/json, text/plain, */*',
  434. 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  435. 'cache-control': 'no-cache',
  436. 'cookie': 'MONITOR_WEB_ID=67cb5099-a022-4ec3-bb8e-c4de6ba51dd0; passport_csrf_token=72b2574f3c99f8ba670e42df430218fd; passport_csrf_token_default=72b2574f3c99f8ba670e42df430218fd; sid_guard=c7472b508ea631823ba765a60cf8757f%7C1680867422%7C3024002%7CFri%2C+12-May-2023+11%3A37%3A04+GMT; uid_tt=c13f47d51767f616befe32fb3e9f485a; uid_tt_ss=c13f47d51767f616befe32fb3e9f485a; sid_tt=c7472b508ea631823ba765a60cf8757f; sessionid=c7472b508ea631823ba765a60cf8757f; sessionid_ss=c7472b508ea631823ba765a60cf8757f; sid_ucp_v1=1.0.0-KGUzNWYxNmRkZGJiZjgxY2MzZWNkMTEzMTkwYjY1Yjg5OTY5NzVlNmMKFQiu3d-eqQIQ3oDAoQYYGCAMOAhACxoCaGwiIGM3NDcyYjUwOGVhNjMxODIzYmE3NjVhNjBjZjg3NTdm; ssid_ucp_v1=1.0.0-KGUzNWYxNmRkZGJiZjgxY2MzZWNkMTEzMTkwYjY1Yjg5OTY5NzVlNmMKFQiu3d-eqQIQ3oDAoQYYGCAMOAhACxoCaGwiIGM3NDcyYjUwOGVhNjMxODIzYmE3NjVhNjBjZjg3NTdm; odin_tt=b893608d4dde2e1e8df8cd5d97a0e2fbeafc4ca762ac72ebef6e6c97e2ed19859bb01d46b4190ddd6dd17d7f9678e1de; SEARCH_CARD_MODE=7168304743566296612_0; support_webp=true; support_avif=false; csrf_session_id=a5355d954d3c63ed1ba35faada452b4d; tt_scid=7Pux7s634-z8DYvCM20y7KigwH5u7Rh6D9C-RROpnT.aGMEcz6Vsxp.oai47wJqa4f86; ttwid=1%7CHHtv2QqpSGuSu8r-zXF1QoWsvjmNi1SJrqOrZzg-UCY%7C1683858689%7Ca5223fe1500578e01e138a0d71d6444692018296c4c24f5885af174a65873c95; ixigua-a-s=3; msToken=50-JJObWB07HfHs-BMJWT1eIDX3G-6lPSF_i-QwxBIXE9VVa-iN0jbEXR5pG2DKjXBmP299n6ZTuXzY-GAy968CCvouSAYIS4GzvGQT3pNlKNejr5G4-1g==; __ac_nonce=0645dcbf0005064517440; __ac_signature=_02B4Z6wo00f01FEGmAwAAIDBKchzCGqn-MBRJpyAAHAjieFC5GEg6gGiwz.I4PRrJl7f0GcixFrExKmgt6QI1i1S-dQyofPEj2ugWTCnmKUdJQv-wYuDofeKNe8VtMtZq2aKewyUGeKU-5Ud21; ixigua-a-s=3',
  437. 'pragma': 'no-cache',
  438. 'referer': f'https://www.ixigua.com/{item_id}?logTag=3c5aa86a8600b9ab8540',
  439. 'sec-ch-ua': '"Microsoft Edge";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
  440. 'sec-ch-ua-mobile': '?0',
  441. 'sec-ch-ua-platform': '"macOS"',
  442. 'sec-fetch-dest': 'empty',
  443. 'sec-fetch-mode': 'cors',
  444. 'sec-fetch-site': 'same-origin',
  445. 'tt-anti-token': 'cBITBHvmYjEygzv-f9c78c1297722cf1f559c74b084e4525ce4900bdcf9e8588f20cc7c2e3234422',
  446. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35',
  447. 'x-secsdk-csrf-token': '000100000001f8e733cf37f0cd255a51aea9a81ff7bc0c09490cfe41ad827c3c5c18ec809279175e4d9f5553d8a5'
  448. }
  449. urllib3.disable_warnings()
  450. s = requests.session()
  451. # max_retries=3 重试3次
  452. s.mount('http://', HTTPAdapter(max_retries=3))
  453. s.mount('https://', HTTPAdapter(max_retries=3))
  454. response = s.get(url=url, headers=headers, params=params, verify=False, proxies=Common.tunnel_proxies(), timeout=5)
  455. response.close()
  456. if response.status_code != 200 or 'total_number' not in response.json() or response.json() == {}:
  457. return 0
  458. return response.json().get("total_number", 0)
  459. # 获取视频详情
  460. @classmethod
  461. def get_video_info(cls, log_type, crawler, item_id):
  462. url = 'https://www.ixigua.com/api/mixVideo/information?'
  463. headers = {
  464. "accept-encoding": "gzip, deflate",
  465. "accept-language": "zh-CN,zh-Hans;q=0.9",
  466. "user-agent": get_random_user_agent('pc'),
  467. "referer": "https://www.ixigua.com/7102614741050196520?logTag=0531c88ac04f38ab2c62",
  468. }
  469. params = {
  470. 'mixId': str(item_id),
  471. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfC'
  472. 'NVVIOBNjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  473. 'X-Bogus': 'DFSzswVupYTANCJOSBk0P53WxM-r',
  474. '_signature': '_02B4Z6wo0000119LvEwAAIDCuktNZ0y5wkdfS7jAALThuOR8D9yWNZ.EmWHKV0WSn6Px'
  475. 'fPsH9-BldyxVje0f49ryXgmn7Tzk-swEHNb15TiGqa6YF.cX0jW8Eds1TtJOIZyfc9s5emH7gdWN94',
  476. }
  477. cookies = {
  478. 'ixigua-a-s': '1',
  479. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfCNVVIOB'
  480. 'NjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  481. 'ttwid': '1%7C_yXQeHWwLZgCsgHClOwTCdYSOt_MjdOkgnPIkpi-Sr8%7C1661241238%7Cf57d0c5ef3f1d7'
  482. '6e049fccdca1ac54887c34d1f8731c8e51a49780ff0ceab9f8',
  483. 'tt_scid': 'QZ4l8KXDG0YAEaMCSbADdcybdKbUfG4BC6S4OBv9lpRS5VyqYLX2bIR8CTeZeGHR9ee3',
  484. 'MONITOR_WEB_ID': '0a49204a-7af5-4e96-95f0-f4bafb7450ad',
  485. '__ac_nonce': '06304878000964fdad287',
  486. '__ac_signature': '_02B4Z6wo00f017Rcr3AAAIDCUVxeW1tOKEu0fKvAAI4cvoYzV-wBhq7B6D8k0no7lb'
  487. 'FlvYoinmtK6UXjRIYPXnahUlFTvmWVtb77jsMkKAXzAEsLE56m36RlvL7ky.M3Xn52r9t1IEb7IR3ke8',
  488. 'ttcid': 'e56fabf6e85d4adf9e4d91902496a0e882',
  489. '_tea_utm_cache_1300': 'undefined',
  490. 'support_avif': 'false',
  491. 'support_webp': 'false',
  492. 'xiguavideopcwebid': '7134967546256016900',
  493. 'xiguavideopcwebid.sig': 'xxRww5R1VEMJN_dQepHorEu_eAc',
  494. }
  495. urllib3.disable_warnings()
  496. s = requests.session()
  497. # max_retries=3 重试3次
  498. s.mount('http://', HTTPAdapter(max_retries=3))
  499. s.mount('https://', HTTPAdapter(max_retries=3))
  500. response = s.get(url=url, headers=headers, params=params, cookies=cookies, verify=False, proxies=Common.tunnel_proxies(), timeout=5)
  501. response.close()
  502. if response.status_code != 200 or 'data' not in response.json() or response.json()['data'] == {}:
  503. Common.logger(log_type, crawler).warning(f"get_video_info:{response.status_code}, {response.text}\n")
  504. return None
  505. else:
  506. video_info = response.json()['data'].get("gidInformation", {}).get("packerData", {}).get("video", {})
  507. if video_info == {}:
  508. return None
  509. video_dict = {
  510. "video_title": video_info.get("title", ""),
  511. "video_id": video_info.get("videoResource", {}).get("vid", ""),
  512. "gid": str(item_id),
  513. "play_cnt": int(video_info.get("video_watch_count", 0)),
  514. "like_cnt": int(video_info.get("video_like_count", 0)),
  515. "comment_cnt": int(cls.get_comment_cnt(item_id)),
  516. "share_cnt": 0,
  517. "favorite_cnt": 0,
  518. "duration": int(video_info.get("video_duration", 0)),
  519. "video_width": int(cls.get_video_url(video_info)["video_width"]),
  520. "video_height": int(cls.get_video_url(video_info)["video_height"]),
  521. "publish_time_stamp": int(video_info.get("video_publish_time", 0)),
  522. "publish_time_str": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(video_info.get("video_publish_time", 0)))),
  523. "user_name": video_info.get("user_info", {}).get("name", ""),
  524. "user_id": str(video_info.get("user_info", {}).get("user_id", "")),
  525. "avatar_url": str(video_info.get("user_info", {}).get("avatar_url", "")),
  526. "cover_url": video_info.get("poster_url", ""),
  527. "audio_url": cls.get_video_url(video_info)["audio_url"],
  528. "video_url": cls.get_video_url(video_info)["video_url"],
  529. "session": f"xigua-search-{int(time.time())}"
  530. }
  531. return video_dict
  532. @classmethod
  533. def get_videoList(cls, log_type, crawler, user_dict, rule_dict, env):
  534. mq = MQ(topic_name="topic_crawler_etl_" + env)
  535. # 打印请求配置
  536. ca = DesiredCapabilities.CHROME
  537. ca["goog:loggingPrefs"] = {"performance": "ALL"}
  538. # # 不打开浏览器运行
  539. chrome_options = webdriver.ChromeOptions()
  540. chrome_options.add_argument(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')
  541. chrome_options.add_argument("--headless")
  542. chrome_options.add_argument("--window-size=1920,1080")
  543. chrome_options.add_argument("--no-sandbox")
  544. if env == "dev":
  545. chromedriver = "/Users/wangkun/Downloads/chromedriver/chromedriver_v114/chromedriver"
  546. else:
  547. chromedriver = "/usr/bin/chromedriver"
  548. # driver初始化
  549. driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service(chromedriver))
  550. driver.implicitly_wait(10)
  551. Common.logger(log_type, crawler).info(f"打开搜索页:{user_dict['link']}")
  552. Common.logging(log_type, crawler, env, f"打开搜索页:{user_dict['link']}")
  553. driver.get(f"https://www.ixigua.com/search/{user_dict['link']}/")
  554. time.sleep(2)
  555. Common.logger(log_type, crawler).info("关闭登录弹框")
  556. Common.logging(log_type, crawler, env, "关闭登录弹框")
  557. if driver.find_elements(By.XPATH, '//*[@class="xg-notification-close"]') != 0:
  558. driver.find_element(By.XPATH, '//*[@class="xg-notification-close"]').click()
  559. driver.get_screenshot_as_file(f"./{crawler}/photos/{user_dict['link']}-关闭登录弹框.png")
  560. Common.logger(log_type, crawler).info("展开筛选按钮")
  561. Common.logging(log_type, crawler, env, "展开筛选按钮")
  562. driver.find_element(By.XPATH, '//*[@class="searchPageV2__header-icons-categories"]').click()
  563. Common.logger(log_type, crawler).info("点击最新排序")
  564. Common.logging(log_type, crawler, env, "点击最新排序")
  565. driver.find_element(By.XPATH, '//*[@class="searchPageV2__header-categories-wrapper"]/*[1]/*[2]/*[1]').click()
  566. time.sleep(1)
  567. driver.get_screenshot_as_file(f"./{crawler}/photos/{user_dict['link']}-最新排序.png")
  568. Common.logger(log_type, crawler).info("收起筛选按钮\n")
  569. Common.logging(log_type, crawler, env, "收起筛选按钮\n")
  570. driver.find_element(By.XPATH, '//*[@class="searchPageV2__header-icons-categories"]').click()
  571. index = 0
  572. num = 0
  573. while True:
  574. # video_elements = driver.find_elements(By.XPATH, '//*[@class="HorizontalFeedCard searchPageV2__card single"]')
  575. video_elements = driver.find_elements(By.XPATH, '//*[@class="HorizontalFeedCard searchPageV2__card"]')
  576. video_element_temp = video_elements[index:]
  577. if len(video_element_temp) == 0:
  578. Common.logger(log_type, crawler).info('到底啦~~~~~~~~~~~~~\n')
  579. Common.logging(log_type, crawler, env, '到底啦~~~~~~~~~~~~~\n')
  580. driver.quit()
  581. return
  582. for i, video_element in enumerate(video_element_temp):
  583. try:
  584. if cls.download_cnt >= int(rule_dict.get("videos_cnt", {}).get("min", 30)):
  585. Common.logger(log_type, crawler).info(f"搜索词: {user_dict['link']},已下载视频数: {cls.download_cnt}\n")
  586. Common.logging(log_type, crawler, env, f"搜索词: {user_dict['link']},已下载视频数: {cls.download_cnt}\n")
  587. driver.quit()
  588. return
  589. if video_element is None:
  590. Common.logger(log_type, crawler).info('到底啦~\n')
  591. Common.logging(log_type, crawler, env, '到底啦~\n')
  592. driver.quit()
  593. return
  594. driver.find_elements(By.XPATH, '//*[@class="HorizontalFeedCard searchPageV2__card"]')
  595. num += 1
  596. Common.logger(log_type, crawler).info(f'拖动"视频"列表第{num}个至屏幕中间')
  597. Common.logging(log_type, crawler, env, f'拖动"视频"列表第{num}个至屏幕中间')
  598. driver.execute_script("arguments[0].scrollIntoView({block:'center',inline:'center'})", video_element)
  599. time.sleep(1)
  600. driver.get_screenshot_as_file(f"./{crawler}/photos/{user_dict['link']}-{num}.png")
  601. title = video_element.find_elements(By.XPATH, '//*[@class="HorizontalFeedCard__coverWrapper disableZoomAnimation"]')[index+i-1].get_attribute('title')
  602. publish_day = video_element.find_elements(By.XPATH, '//*[@class="HorizontalFeedCard-accessories-bottomInfo__statistics"]')[index+i-1].text.split('· ')[-1]
  603. Common.logger(log_type, crawler).info(f"标题:{title}")
  604. Common.logging(log_type, crawler, env, f"标题:{title}")
  605. Common.logger(log_type, crawler).info(f"发布时间:{publish_day}")
  606. Common.logging(log_type, crawler, env, f"发布时间:{publish_day}")
  607. if "年" in publish_day:
  608. Common.logger(log_type, crawler).info("发布时间超过 1 年\n")
  609. Common.logging(log_type, crawler, env, "发布时间超过 1 年\n")
  610. driver.quit()
  611. return
  612. item_id = video_element.find_elements(By.XPATH, '//*[@class="HorizontalFeedCard__coverWrapper disableZoomAnimation"]')[index+i-1].get_attribute('href')
  613. item_id = item_id.split("com/")[-1].split("?&")[0]
  614. video_dict = cls.get_video_info(log_type, crawler, item_id)
  615. if video_dict is None:
  616. Common.logger(log_type, crawler).info("无效视频\n")
  617. Common.logging(log_type, crawler, env, "无效视频\n")
  618. continue
  619. for k, v in video_dict.items():
  620. Common.logger(log_type, crawler).info(f"{k}:{v}")
  621. Common.logging(log_type, crawler, env, f"{video_dict}")
  622. # if int((int(time.time()) - int(video_dict["publish_time_stamp"])) / (3600 * 24)) > int(rule_dict.get("period", {}).get("max", 1000)):
  623. # Common.logger(log_type, crawler).info(f'发布时间超过{int(rule_dict.get("period", {}).get("max", 1000))}天\n')
  624. # driver.quit()
  625. # return
  626. if download_rule(log_type=log_type, crawler=crawler, video_dict=video_dict, rule_dict=rule_dict) is False:
  627. Common.logger(log_type, crawler).info("不满足抓取规则\n")
  628. Common.logging(log_type, crawler, env, "不满足抓取规则\n")
  629. elif any(str(word) if str(word) in video_dict["video_title"] else False
  630. for word in get_config_from_mysql(log_type=log_type,
  631. source=crawler,
  632. env=env,
  633. text="filter",
  634. action="")) is True:
  635. Common.logger(log_type, crawler).info('已中过滤词\n')
  636. Common.logging(log_type, crawler, env, '已中过滤词\n')
  637. elif cls.repeat_video(log_type, crawler, video_dict["video_id"], env) != 0:
  638. Common.logger(log_type, crawler).info('视频已下载\n')
  639. Common.logging(log_type, crawler, env, '视频已下载\n')
  640. else:
  641. # title_score = get_title_score(log_type, "kuaishou", "16QspO", "0usaDk", video_dict["video_title"])
  642. # if title_score <= 0.3:
  643. # Common.logger(log_type, crawler).info(f"权重分:{title_score}<=0.3\n")
  644. # Common.logging(log_type, crawler, env, f"权重分:{title_score}<=0.3\n")
  645. # continue
  646. # Common.logger(log_type, crawler).info(f"权重分:{title_score}>0.3\n")
  647. # Common.logging(log_type, crawler, env, f"权重分:{title_score}>0.3\n")
  648. # cls.download_publish(log_type=log_type,
  649. # crawler=crawler,
  650. # user_dict=user_dict,
  651. # video_dict=video_dict,
  652. # rule_dict=rule_dict,
  653. # title_score=title_score,
  654. # env=env)
  655. video_dict["out_user_id"] = video_dict["user_id"]
  656. video_dict["platform"] = crawler
  657. video_dict["strategy"] = log_type
  658. video_dict["out_video_id"] = video_dict["video_id"]
  659. video_dict["width"] = video_dict["video_width"]
  660. video_dict["height"] = video_dict["video_height"]
  661. video_dict["crawler_rule"] = json.dumps(rule_dict)
  662. video_dict["user_id"] = user_dict["uid"]
  663. video_dict["publish_time"] = video_dict["publish_time_str"]
  664. video_dict["strategy_type"] = log_type
  665. mq.send_msg(video_dict)
  666. cls.download_cnt += 1
  667. Common.logging(log_type, crawler, env, "已下载视频数+1\n")
  668. except Exception as e:
  669. Common.logger(log_type, crawler).warning(f"抓取单条视频异常:{e}\n")
  670. Common.logging(log_type, crawler, env, f"抓取单条视频异常:{e}\n")
  671. Common.logger(log_type, crawler).info('已抓取完一组视频,休眠10秒\n')
  672. Common.logging(log_type, crawler, env, '已抓取完一组视频,休眠10秒\n')
  673. time.sleep(10)
  674. index = index + len(video_element_temp)
  675. @classmethod
  676. def repeat_video(cls, log_type, crawler, video_id, env):
  677. # sql = f""" select * from crawler_video where platform="{cls.platform}" and out_video_id="{video_id}"; """
  678. sql = f""" select * from crawler_video where platform in ("{crawler}","{cls.platform}") and out_video_id="{video_id}"; """
  679. repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env, action="")
  680. return len(repeat_video)
  681. # 下载 / 上传
  682. @classmethod
  683. def download_publish(cls, log_type, crawler, user_dict, video_dict, rule_dict, title_score, env):
  684. Common.download_method(log_type=log_type, crawler=crawler, text='xigua_video',
  685. title=video_dict['video_title'], url=video_dict['video_url'])
  686. # 下载音频
  687. Common.download_method(log_type=log_type, crawler=crawler, text='xigua_audio',
  688. title=video_dict['video_title'], url=video_dict['audio_url'])
  689. # 合成音视频
  690. Common.video_compose(log_type=log_type, crawler=crawler,
  691. video_dir=f"./{crawler}/videos/{video_dict['video_title']}")
  692. md_title = md5(video_dict['video_title'].encode('utf8')).hexdigest()
  693. try:
  694. if os.path.getsize(f"./{crawler}/videos/{md_title}/video.mp4") == 0:
  695. # 删除视频文件夹
  696. shutil.rmtree(f"./{crawler}/videos/{md_title}")
  697. Common.logger(log_type, crawler).info("视频size=0,删除成功\n")
  698. Common.logging(log_type, crawler, env, "视频size=0,删除成功\n")
  699. return
  700. except FileNotFoundError:
  701. # 删除视频文件夹
  702. shutil.rmtree(f"./{crawler}/videos/{md_title}")
  703. Common.logger(log_type, crawler).info("视频文件不存在,删除文件夹成功\n")
  704. Common.logging(log_type, crawler, env, "视频文件不存在,删除文件夹成功\n")
  705. return
  706. # 下载封面
  707. Common.download_method(log_type=log_type, crawler=crawler, text='cover',
  708. title=video_dict['video_title'], url=video_dict['cover_url'])
  709. # 保存视频信息至txt
  710. Common.save_video_info(log_type=log_type, crawler=crawler, video_dict=video_dict)
  711. # 上传视频
  712. Common.logger(log_type, crawler).info("开始上传视频...")
  713. Common.logging(log_type, crawler, env, "开始上传视频...")
  714. if env == "dev":
  715. oss_endpoint = "out"
  716. our_video_id = Publish.upload_and_publish(log_type=log_type,
  717. crawler=crawler,
  718. strategy="搜索抓取策略",
  719. our_uid=user_dict["uid"],
  720. env=env,
  721. oss_endpoint=oss_endpoint)
  722. our_video_link = f"https://testadmin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
  723. else:
  724. oss_endpoint = "inner"
  725. our_video_id = Publish.upload_and_publish(log_type=log_type,
  726. crawler=crawler,
  727. strategy="搜索抓取策略",
  728. our_uid=user_dict["uid"],
  729. env=env,
  730. oss_endpoint=oss_endpoint)
  731. our_video_link = f"https://admin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
  732. if our_video_id is None:
  733. try:
  734. # 删除视频文件夹
  735. shutil.rmtree(f"./{crawler}/videos/{md_title}")
  736. return
  737. except FileNotFoundError:
  738. return
  739. # 视频信息保存数据库
  740. insert_sql = f""" insert into crawler_video(video_id,
  741. user_id,
  742. out_user_id,
  743. platform,
  744. strategy,
  745. out_video_id,
  746. video_title,
  747. cover_url,
  748. video_url,
  749. duration,
  750. publish_time,
  751. play_cnt,
  752. crawler_rule,
  753. width,
  754. height)
  755. values({our_video_id},
  756. {user_dict["uid"]},
  757. "{video_dict['user_id']}",
  758. "{cls.platform}",
  759. "搜索爬虫策略",
  760. "{video_dict['video_id']}",
  761. "{video_dict['video_title']}",
  762. "{video_dict['cover_url']}",
  763. "{video_dict['video_url']}",
  764. {int(video_dict['duration'])},
  765. "{video_dict['publish_time_str']}",
  766. {int(video_dict['play_cnt'])},
  767. '{json.dumps(rule_dict)}',
  768. {int(video_dict['video_width'])},
  769. {int(video_dict['video_height'])}) """
  770. Common.logger(log_type, crawler).info(f"insert_sql:{insert_sql}")
  771. Common.logging(log_type, crawler, env, f"insert_sql:{insert_sql}")
  772. MysqlHelper.update_values(log_type, crawler, insert_sql, env, action="")
  773. cls.download_cnt += 1
  774. Common.logger(log_type, crawler).info("视频信息写入数据库完成")
  775. Common.logging(log_type, crawler, env, "视频信息写入数据库完成")
  776. # 视频信息写入飞书
  777. Feishu.insert_columns(log_type, crawler, "BUNvGC", "ROWS", 1, 2)
  778. values = [[title_score,
  779. user_dict["link"],
  780. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))),
  781. "关键词搜索",
  782. video_dict['video_title'],
  783. str(video_dict['video_id']),
  784. our_video_link,
  785. video_dict['gid'],
  786. video_dict['play_cnt'],
  787. video_dict['comment_cnt'],
  788. video_dict['like_cnt'],
  789. video_dict['share_cnt'],
  790. video_dict['duration'],
  791. str(video_dict['video_width']) + '*' + str(video_dict['video_height']),
  792. video_dict['publish_time_str'],
  793. video_dict['user_name'],
  794. video_dict['user_id'],
  795. video_dict['avatar_url'],
  796. video_dict['cover_url'],
  797. video_dict['video_url'],
  798. video_dict['audio_url']]]
  799. time.sleep(0.5)
  800. Feishu.update_values(log_type, crawler, "BUNvGC", "D2:Z2", values)
  801. Common.logger(log_type, crawler).info('视频信息写入飞书完成\n')
  802. Common.logging(log_type, crawler, env, '视频信息写入飞书完成\n')
  803. @classmethod
  804. def get_search_videos(cls, log_type, crawler, user_list, rule_dict, env):
  805. Common.logger(log_type, crawler).info(f"搜索词总数:{len(user_list)}\n")
  806. Common.logging(log_type, crawler, env, f"搜索词总数:{len(user_list)}\n")
  807. for user_dict in user_list:
  808. try:
  809. cls.download_cnt = 0
  810. Common.logger(log_type, crawler).info(f"开始抓取 {user_dict['link']} 视频")
  811. Common.logging(log_type, crawler, env, f"开始抓取 {user_dict['link']} 视频")
  812. cls.get_videoList(log_type=log_type,
  813. crawler=crawler,
  814. user_dict=user_dict,
  815. rule_dict=rule_dict,
  816. env=env)
  817. except Exception as e:
  818. Common.logger(log_type, crawler).error(f"抓取{user_dict['link']}视频时异常:{e}\n")
  819. Common.logging(log_type, crawler, env, f"抓取{user_dict['link']}视频时异常:{e}\n")
  820. if __name__ == '__main__':
  821. pass