xg_recommend.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/7/10
  4. import base64
  5. import datetime
  6. import json
  7. import os
  8. import random
  9. import string
  10. import subprocess
  11. import sys
  12. import time
  13. import requests
  14. import urllib3
  15. from requests.adapters import HTTPAdapter
  16. from selenium import webdriver
  17. from selenium.webdriver import DesiredCapabilities
  18. from selenium.webdriver.chrome.service import Service
  19. from selenium.webdriver.chrome.webdriver import WebDriver
  20. from selenium.webdriver.common.by import By
  21. sys.path.append(os.getcwd())
  22. from common.mq import MQ
  23. from common.feishu import Feishu
  24. from common.public import download_rule, get_config_from_mysql
  25. from common.common import Common
  26. from common.scheduling_db import MysqlHelper
  27. from common.userAgent import get_random_user_agent
  28. class XiguaRecommend:
  29. platform = "xigua"
  30. @classmethod
  31. def random_signature(cls):
  32. src_digits = string.digits # string_数字
  33. src_uppercase = string.ascii_uppercase # string_大写字母
  34. src_lowercase = string.ascii_lowercase # string_小写字母
  35. digits_num = random.randint(1, 6)
  36. uppercase_num = random.randint(1, 26 - digits_num - 1)
  37. lowercase_num = 26 - (digits_num + uppercase_num)
  38. password = random.sample(src_digits, digits_num) + random.sample(src_uppercase, uppercase_num) + random.sample(
  39. src_lowercase, lowercase_num)
  40. random.shuffle(password)
  41. new_password = 'AAAAAAAAAA' + ''.join(password)[10:-4] + 'AAAB'
  42. new_password_start = new_password[0:18]
  43. new_password_end = new_password[-7:]
  44. if new_password[18] == '8':
  45. new_password = new_password_start + 'w' + new_password_end
  46. elif new_password[18] == '9':
  47. new_password = new_password_start + 'x' + new_password_end
  48. elif new_password[18] == '-':
  49. new_password = new_password_start + 'y' + new_password_end
  50. elif new_password[18] == '.':
  51. new_password = new_password_start + 'z' + new_password_end
  52. else:
  53. new_password = new_password_start + 'y' + new_password_end
  54. return new_password
  55. @classmethod
  56. def get_video_url(cls, video_info):
  57. video_url_dict = {}
  58. # video_url
  59. if 'videoResource' not in video_info:
  60. video_url_dict["video_url"] = ''
  61. video_url_dict["audio_url"] = ''
  62. video_url_dict["video_width"] = 0
  63. video_url_dict["video_height"] = 0
  64. elif 'dash_120fps' in video_info['videoResource']:
  65. if "video_list" in video_info['videoResource']['dash_120fps'] and 'video_4' in \
  66. video_info['videoResource']['dash_120fps']['video_list']:
  67. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_4']['backup_url_1']
  68. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_4']['backup_url_1']
  69. if len(video_url) % 3 == 1:
  70. video_url += '=='
  71. elif len(video_url) % 3 == 2:
  72. video_url += '='
  73. elif len(audio_url) % 3 == 1:
  74. audio_url += '=='
  75. elif len(audio_url) % 3 == 2:
  76. audio_url += '='
  77. video_url = base64.b64decode(video_url).decode('utf8')
  78. audio_url = base64.b64decode(audio_url).decode('utf8')
  79. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_4']['vwidth']
  80. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_4']['vheight']
  81. video_url_dict["video_url"] = video_url
  82. video_url_dict["audio_url"] = audio_url
  83. video_url_dict["video_width"] = video_width
  84. video_url_dict["video_height"] = video_height
  85. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_3' in \
  86. video_info['videoResource']['dash_120fps']['video_list']:
  87. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_3']['backup_url_1']
  88. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_3']['backup_url_1']
  89. if len(video_url) % 3 == 1:
  90. video_url += '=='
  91. elif len(video_url) % 3 == 2:
  92. video_url += '='
  93. elif len(audio_url) % 3 == 1:
  94. audio_url += '=='
  95. elif len(audio_url) % 3 == 2:
  96. audio_url += '='
  97. video_url = base64.b64decode(video_url).decode('utf8')
  98. audio_url = base64.b64decode(audio_url).decode('utf8')
  99. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_3']['vwidth']
  100. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_3']['vheight']
  101. video_url_dict["video_url"] = video_url
  102. video_url_dict["audio_url"] = audio_url
  103. video_url_dict["video_width"] = video_width
  104. video_url_dict["video_height"] = video_height
  105. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_2' in \
  106. video_info['videoResource']['dash_120fps']['video_list']:
  107. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_2']['backup_url_1']
  108. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_2']['backup_url_1']
  109. if len(video_url) % 3 == 1:
  110. video_url += '=='
  111. elif len(video_url) % 3 == 2:
  112. video_url += '='
  113. elif len(audio_url) % 3 == 1:
  114. audio_url += '=='
  115. elif len(audio_url) % 3 == 2:
  116. audio_url += '='
  117. video_url = base64.b64decode(video_url).decode('utf8')
  118. audio_url = base64.b64decode(audio_url).decode('utf8')
  119. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_2']['vwidth']
  120. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_2']['vheight']
  121. video_url_dict["video_url"] = video_url
  122. video_url_dict["audio_url"] = audio_url
  123. video_url_dict["video_width"] = video_width
  124. video_url_dict["video_height"] = video_height
  125. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_1' in \
  126. video_info['videoResource']['dash_120fps']['video_list']:
  127. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_1']['backup_url_1']
  128. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_1']['backup_url_1']
  129. if len(video_url) % 3 == 1:
  130. video_url += '=='
  131. elif len(video_url) % 3 == 2:
  132. video_url += '='
  133. elif len(audio_url) % 3 == 1:
  134. audio_url += '=='
  135. elif len(audio_url) % 3 == 2:
  136. audio_url += '='
  137. video_url = base64.b64decode(video_url).decode('utf8')
  138. audio_url = base64.b64decode(audio_url).decode('utf8')
  139. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_1']['vwidth']
  140. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_1']['vheight']
  141. video_url_dict["video_url"] = video_url
  142. video_url_dict["audio_url"] = audio_url
  143. video_url_dict["video_width"] = video_width
  144. video_url_dict["video_height"] = video_height
  145. elif 'dynamic_video' in video_info['videoResource']['dash_120fps'] \
  146. and 'dynamic_video_list' in video_info['videoResource']['dash_120fps']['dynamic_video'] \
  147. and 'dynamic_audio_list' in video_info['videoResource']['dash_120fps']['dynamic_video'] \
  148. and len(
  149. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list']) != 0 \
  150. and len(
  151. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_audio_list']) != 0:
  152. video_url = \
  153. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1][
  154. 'backup_url_1']
  155. audio_url = \
  156. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_audio_list'][-1][
  157. 'backup_url_1']
  158. if len(video_url) % 3 == 1:
  159. video_url += '=='
  160. elif len(video_url) % 3 == 2:
  161. video_url += '='
  162. elif len(audio_url) % 3 == 1:
  163. audio_url += '=='
  164. elif len(audio_url) % 3 == 2:
  165. audio_url += '='
  166. video_url = base64.b64decode(video_url).decode('utf8')
  167. audio_url = base64.b64decode(audio_url).decode('utf8')
  168. video_width = \
  169. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1][
  170. 'vwidth']
  171. video_height = \
  172. video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1][
  173. 'vheight']
  174. video_url_dict["video_url"] = video_url
  175. video_url_dict["audio_url"] = audio_url
  176. video_url_dict["video_width"] = video_width
  177. video_url_dict["video_height"] = video_height
  178. else:
  179. video_url_dict["video_url"] = ''
  180. video_url_dict["audio_url"] = ''
  181. video_url_dict["video_width"] = 0
  182. video_url_dict["video_height"] = 0
  183. elif 'dash' in video_info['videoResource']:
  184. if "video_list" in video_info['videoResource']['dash'] and 'video_4' in \
  185. video_info['videoResource']['dash']['video_list']:
  186. video_url = video_info['videoResource']['dash']['video_list']['video_4']['backup_url_1']
  187. audio_url = video_info['videoResource']['dash']['video_list']['video_4']['backup_url_1']
  188. if len(video_url) % 3 == 1:
  189. video_url += '=='
  190. elif len(video_url) % 3 == 2:
  191. video_url += '='
  192. elif len(audio_url) % 3 == 1:
  193. audio_url += '=='
  194. elif len(audio_url) % 3 == 2:
  195. audio_url += '='
  196. video_url = base64.b64decode(video_url).decode('utf8')
  197. audio_url = base64.b64decode(audio_url).decode('utf8')
  198. video_width = video_info['videoResource']['dash']['video_list']['video_4']['vwidth']
  199. video_height = video_info['videoResource']['dash']['video_list']['video_4']['vheight']
  200. video_url_dict["video_url"] = video_url
  201. video_url_dict["audio_url"] = audio_url
  202. video_url_dict["video_width"] = video_width
  203. video_url_dict["video_height"] = video_height
  204. elif "video_list" in video_info['videoResource']['dash'] and 'video_3' in \
  205. video_info['videoResource']['dash']['video_list']:
  206. video_url = video_info['videoResource']['dash']['video_list']['video_3']['backup_url_1']
  207. audio_url = video_info['videoResource']['dash']['video_list']['video_3']['backup_url_1']
  208. if len(video_url) % 3 == 1:
  209. video_url += '=='
  210. elif len(video_url) % 3 == 2:
  211. video_url += '='
  212. elif len(audio_url) % 3 == 1:
  213. audio_url += '=='
  214. elif len(audio_url) % 3 == 2:
  215. audio_url += '='
  216. video_url = base64.b64decode(video_url).decode('utf8')
  217. audio_url = base64.b64decode(audio_url).decode('utf8')
  218. video_width = video_info['videoResource']['dash']['video_list']['video_3']['vwidth']
  219. video_height = video_info['videoResource']['dash']['video_list']['video_3']['vheight']
  220. video_url_dict["video_url"] = video_url
  221. video_url_dict["audio_url"] = audio_url
  222. video_url_dict["video_width"] = video_width
  223. video_url_dict["video_height"] = video_height
  224. elif "video_list" in video_info['videoResource']['dash'] and 'video_2' in \
  225. video_info['videoResource']['dash']['video_list']:
  226. video_url = video_info['videoResource']['dash']['video_list']['video_2']['backup_url_1']
  227. audio_url = video_info['videoResource']['dash']['video_list']['video_2']['backup_url_1']
  228. if len(video_url) % 3 == 1:
  229. video_url += '=='
  230. elif len(video_url) % 3 == 2:
  231. video_url += '='
  232. elif len(audio_url) % 3 == 1:
  233. audio_url += '=='
  234. elif len(audio_url) % 3 == 2:
  235. audio_url += '='
  236. video_url = base64.b64decode(video_url).decode('utf8')
  237. audio_url = base64.b64decode(audio_url).decode('utf8')
  238. video_width = video_info['videoResource']['dash']['video_list']['video_2']['vwidth']
  239. video_height = video_info['videoResource']['dash']['video_list']['video_2']['vheight']
  240. video_url_dict["video_url"] = video_url
  241. video_url_dict["audio_url"] = audio_url
  242. video_url_dict["video_width"] = video_width
  243. video_url_dict["video_height"] = video_height
  244. elif "video_list" in video_info['videoResource']['dash'] and 'video_1' in \
  245. video_info['videoResource']['dash']['video_list']:
  246. video_url = video_info['videoResource']['dash']['video_list']['video_1']['backup_url_1']
  247. audio_url = video_info['videoResource']['dash']['video_list']['video_1']['backup_url_1']
  248. if len(video_url) % 3 == 1:
  249. video_url += '=='
  250. elif len(video_url) % 3 == 2:
  251. video_url += '='
  252. elif len(audio_url) % 3 == 1:
  253. audio_url += '=='
  254. elif len(audio_url) % 3 == 2:
  255. audio_url += '='
  256. video_url = base64.b64decode(video_url).decode('utf8')
  257. audio_url = base64.b64decode(audio_url).decode('utf8')
  258. video_width = video_info['videoResource']['dash']['video_list']['video_1']['vwidth']
  259. video_height = video_info['videoResource']['dash']['video_list']['video_1']['vheight']
  260. video_url_dict["video_url"] = video_url
  261. video_url_dict["audio_url"] = audio_url
  262. video_url_dict["video_width"] = video_width
  263. video_url_dict["video_height"] = video_height
  264. elif 'dynamic_video' in video_info['videoResource']['dash'] \
  265. and 'dynamic_video_list' in video_info['videoResource']['dash']['dynamic_video'] \
  266. and 'dynamic_audio_list' in video_info['videoResource']['dash']['dynamic_video'] \
  267. and len(video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list']) != 0 \
  268. and len(video_info['videoResource']['dash']['dynamic_video']['dynamic_audio_list']) != 0:
  269. video_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  270. 'backup_url_1']
  271. audio_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_audio_list'][-1][
  272. 'backup_url_1']
  273. if len(video_url) % 3 == 1:
  274. video_url += '=='
  275. elif len(video_url) % 3 == 2:
  276. video_url += '='
  277. elif len(audio_url) % 3 == 1:
  278. audio_url += '=='
  279. elif len(audio_url) % 3 == 2:
  280. audio_url += '='
  281. video_url = base64.b64decode(video_url).decode('utf8')
  282. audio_url = base64.b64decode(audio_url).decode('utf8')
  283. video_width = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  284. 'vwidth']
  285. video_height = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  286. 'vheight']
  287. video_url_dict["video_url"] = video_url
  288. video_url_dict["audio_url"] = audio_url
  289. video_url_dict["video_width"] = video_width
  290. video_url_dict["video_height"] = video_height
  291. else:
  292. video_url_dict["video_url"] = ''
  293. video_url_dict["audio_url"] = ''
  294. video_url_dict["video_width"] = 0
  295. video_url_dict["video_height"] = 0
  296. elif 'normal' in video_info['videoResource']:
  297. if "video_list" in video_info['videoResource']['normal'] and 'video_4' in \
  298. video_info['videoResource']['normal']['video_list']:
  299. video_url = video_info['videoResource']['normal']['video_list']['video_4']['backup_url_1']
  300. audio_url = video_info['videoResource']['normal']['video_list']['video_4']['backup_url_1']
  301. if len(video_url) % 3 == 1:
  302. video_url += '=='
  303. elif len(video_url) % 3 == 2:
  304. video_url += '='
  305. elif len(audio_url) % 3 == 1:
  306. audio_url += '=='
  307. elif len(audio_url) % 3 == 2:
  308. audio_url += '='
  309. video_url = base64.b64decode(video_url).decode('utf8')
  310. audio_url = base64.b64decode(audio_url).decode('utf8')
  311. video_width = video_info['videoResource']['normal']['video_list']['video_4']['vwidth']
  312. video_height = video_info['videoResource']['normal']['video_list']['video_4']['vheight']
  313. video_url_dict["video_url"] = video_url
  314. video_url_dict["audio_url"] = audio_url
  315. video_url_dict["video_width"] = video_width
  316. video_url_dict["video_height"] = video_height
  317. elif "video_list" in video_info['videoResource']['normal'] and 'video_3' in \
  318. video_info['videoResource']['normal']['video_list']:
  319. video_url = video_info['videoResource']['normal']['video_list']['video_3']['backup_url_1']
  320. audio_url = video_info['videoResource']['normal']['video_list']['video_3']['backup_url_1']
  321. if len(video_url) % 3 == 1:
  322. video_url += '=='
  323. elif len(video_url) % 3 == 2:
  324. video_url += '='
  325. elif len(audio_url) % 3 == 1:
  326. audio_url += '=='
  327. elif len(audio_url) % 3 == 2:
  328. audio_url += '='
  329. video_url = base64.b64decode(video_url).decode('utf8')
  330. audio_url = base64.b64decode(audio_url).decode('utf8')
  331. video_width = video_info['videoResource']['normal']['video_list']['video_3']['vwidth']
  332. video_height = video_info['videoResource']['normal']['video_list']['video_3']['vheight']
  333. video_url_dict["video_url"] = video_url
  334. video_url_dict["audio_url"] = audio_url
  335. video_url_dict["video_width"] = video_width
  336. video_url_dict["video_height"] = video_height
  337. elif "video_list" in video_info['videoResource']['normal'] and 'video_2' in \
  338. video_info['videoResource']['normal']['video_list']:
  339. video_url = video_info['videoResource']['normal']['video_list']['video_2']['backup_url_1']
  340. audio_url = video_info['videoResource']['normal']['video_list']['video_2']['backup_url_1']
  341. if len(video_url) % 3 == 1:
  342. video_url += '=='
  343. elif len(video_url) % 3 == 2:
  344. video_url += '='
  345. elif len(audio_url) % 3 == 1:
  346. audio_url += '=='
  347. elif len(audio_url) % 3 == 2:
  348. audio_url += '='
  349. video_url = base64.b64decode(video_url).decode('utf8')
  350. audio_url = base64.b64decode(audio_url).decode('utf8')
  351. video_width = video_info['videoResource']['normal']['video_list']['video_2']['vwidth']
  352. video_height = video_info['videoResource']['normal']['video_list']['video_2']['vheight']
  353. video_url_dict["video_url"] = video_url
  354. video_url_dict["audio_url"] = audio_url
  355. video_url_dict["video_width"] = video_width
  356. video_url_dict["video_height"] = video_height
  357. elif "video_list" in video_info['videoResource']['normal'] and 'video_1' in \
  358. video_info['videoResource']['normal']['video_list']:
  359. video_url = video_info['videoResource']['normal']['video_list']['video_1']['backup_url_1']
  360. audio_url = video_info['videoResource']['normal']['video_list']['video_1']['backup_url_1']
  361. if len(video_url) % 3 == 1:
  362. video_url += '=='
  363. elif len(video_url) % 3 == 2:
  364. video_url += '='
  365. elif len(audio_url) % 3 == 1:
  366. audio_url += '=='
  367. elif len(audio_url) % 3 == 2:
  368. audio_url += '='
  369. video_url = base64.b64decode(video_url).decode('utf8')
  370. audio_url = base64.b64decode(audio_url).decode('utf8')
  371. video_width = video_info['videoResource']['normal']['video_list']['video_1']['vwidth']
  372. video_height = video_info['videoResource']['normal']['video_list']['video_1']['vheight']
  373. video_url_dict["video_url"] = video_url
  374. video_url_dict["audio_url"] = audio_url
  375. video_url_dict["video_width"] = video_width
  376. video_url_dict["video_height"] = video_height
  377. elif 'dynamic_video' in video_info['videoResource']['normal'] \
  378. and 'dynamic_video_list' in video_info['videoResource']['normal']['dynamic_video'] \
  379. and 'dynamic_audio_list' in video_info['videoResource']['normal']['dynamic_video'] \
  380. and len(video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list']) != 0 \
  381. and len(video_info['videoResource']['normal']['dynamic_video']['dynamic_audio_list']) != 0:
  382. video_url = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  383. 'backup_url_1']
  384. audio_url = video_info['videoResource']['normal']['dynamic_video']['dynamic_audio_list'][-1][
  385. 'backup_url_1']
  386. if len(video_url) % 3 == 1:
  387. video_url += '=='
  388. elif len(video_url) % 3 == 2:
  389. video_url += '='
  390. elif len(audio_url) % 3 == 1:
  391. audio_url += '=='
  392. elif len(audio_url) % 3 == 2:
  393. audio_url += '='
  394. video_url = base64.b64decode(video_url).decode('utf8')
  395. audio_url = base64.b64decode(audio_url).decode('utf8')
  396. video_width = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  397. 'vwidth']
  398. video_height = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  399. 'vheight']
  400. video_url_dict["video_url"] = video_url
  401. video_url_dict["audio_url"] = audio_url
  402. video_url_dict["video_width"] = video_width
  403. video_url_dict["video_height"] = video_height
  404. else:
  405. video_url_dict["video_url"] = ''
  406. video_url_dict["audio_url"] = ''
  407. video_url_dict["video_width"] = 0
  408. video_url_dict["video_height"] = 0
  409. else:
  410. video_url_dict["video_url"] = ''
  411. video_url_dict["audio_url"] = ''
  412. video_url_dict["video_width"] = 0
  413. video_url_dict["video_height"] = 0
  414. return video_url_dict
  415. @classmethod
  416. def get_comment_cnt(cls, item_id):
  417. url = "https://www.ixigua.com/tlb/comment/article/v5/tab_comments/?"
  418. params = {
  419. "tab_index": "0",
  420. "count": "10",
  421. "offset": "10",
  422. "group_id": str(item_id),
  423. "item_id": str(item_id),
  424. "aid": "1768",
  425. "msToken": "50-JJObWB07HfHs-BMJWT1eIDX3G-6lPSF_i-QwxBIXE9VVa-iN0jbEXR5pG2DKjXBmP299n6ZTuXzY-GAy968CCvouSAYIS4GzvGQT3pNlKNejr5G4-1g==",
  426. "X-Bogus": "DFSzswVOyGtANVeWtCLMqR/F6q9U",
  427. "_signature": cls.random_signature(),
  428. }
  429. headers = {
  430. 'authority': 'www.ixigua.com',
  431. 'accept': 'application/json, text/plain, */*',
  432. 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  433. 'cache-control': 'no-cache',
  434. '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',
  435. 'pragma': 'no-cache',
  436. 'referer': f'https://www.ixigua.com/{item_id}?logTag=3c5aa86a8600b9ab8540',
  437. 'sec-ch-ua': '"Microsoft Edge";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
  438. 'sec-ch-ua-mobile': '?0',
  439. 'sec-ch-ua-platform': '"macOS"',
  440. 'sec-fetch-dest': 'empty',
  441. 'sec-fetch-mode': 'cors',
  442. 'sec-fetch-site': 'same-origin',
  443. 'tt-anti-token': 'cBITBHvmYjEygzv-f9c78c1297722cf1f559c74b084e4525ce4900bdcf9e8588f20cc7c2e3234422',
  444. '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',
  445. 'x-secsdk-csrf-token': '000100000001f8e733cf37f0cd255a51aea9a81ff7bc0c09490cfe41ad827c3c5c18ec809279175e4d9f5553d8a5'
  446. }
  447. urllib3.disable_warnings()
  448. s = requests.session()
  449. # max_retries=3 重试3次
  450. s.mount('http://', HTTPAdapter(max_retries=3))
  451. s.mount('https://', HTTPAdapter(max_retries=3))
  452. response = s.get(url=url, headers=headers, params=params, verify=False, proxies=Common.tunnel_proxies(),
  453. timeout=5)
  454. response.close()
  455. if response.status_code != 200 or 'total_number' not in response.json() or response.json() == {}:
  456. return 0
  457. return response.json().get("total_number", 0)
  458. # 获取视频详情
  459. @classmethod
  460. def get_video_info(cls, log_type, crawler, item_id):
  461. url = 'https://www.ixigua.com/api/mixVideo/information?'
  462. headers = {
  463. "accept-encoding": "gzip, deflate",
  464. "accept-language": "zh-CN,zh-Hans;q=0.9",
  465. "user-agent": get_random_user_agent('pc'),
  466. "referer": "https://www.ixigua.com/7102614741050196520?logTag=0531c88ac04f38ab2c62",
  467. }
  468. params = {
  469. 'mixId': str(item_id),
  470. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfC'
  471. 'NVVIOBNjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  472. 'X-Bogus': 'DFSzswVupYTANCJOSBk0P53WxM-r',
  473. '_signature': '_02B4Z6wo0000119LvEwAAIDCuktNZ0y5wkdfS7jAALThuOR8D9yWNZ.EmWHKV0WSn6Px'
  474. 'fPsH9-BldyxVje0f49ryXgmn7Tzk-swEHNb15TiGqa6YF.cX0jW8Eds1TtJOIZyfc9s5emH7gdWN94',
  475. }
  476. cookies = {
  477. 'ixigua-a-s': '1',
  478. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfCNVVIOB'
  479. 'NjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  480. 'ttwid': '1%7C_yXQeHWwLZgCsgHClOwTCdYSOt_MjdOkgnPIkpi-Sr8%7C1661241238%7Cf57d0c5ef3f1d7'
  481. '6e049fccdca1ac54887c34d1f8731c8e51a49780ff0ceab9f8',
  482. 'tt_scid': 'QZ4l8KXDG0YAEaMCSbADdcybdKbUfG4BC6S4OBv9lpRS5VyqYLX2bIR8CTeZeGHR9ee3',
  483. 'MONITOR_WEB_ID': '0a49204a-7af5-4e96-95f0-f4bafb7450ad',
  484. '__ac_nonce': '06304878000964fdad287',
  485. '__ac_signature': '_02B4Z6wo00f017Rcr3AAAIDCUVxeW1tOKEu0fKvAAI4cvoYzV-wBhq7B6D8k0no7lb'
  486. 'FlvYoinmtK6UXjRIYPXnahUlFTvmWVtb77jsMkKAXzAEsLE56m36RlvL7ky.M3Xn52r9t1IEb7IR3ke8',
  487. 'ttcid': 'e56fabf6e85d4adf9e4d91902496a0e882',
  488. '_tea_utm_cache_1300': 'undefined',
  489. 'support_avif': 'false',
  490. 'support_webp': 'false',
  491. 'xiguavideopcwebid': '7134967546256016900',
  492. 'xiguavideopcwebid.sig': 'xxRww5R1VEMJN_dQepHorEu_eAc',
  493. }
  494. urllib3.disable_warnings()
  495. s = requests.session()
  496. # max_retries=3 重试3次
  497. s.mount('http://', HTTPAdapter(max_retries=3))
  498. s.mount('https://', HTTPAdapter(max_retries=3))
  499. response = s.get(url=url, headers=headers, params=params, cookies=cookies, verify=False,
  500. 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",
  523. time.localtime(int(video_info.get("video_publish_time", 0)))),
  524. "user_name": video_info.get("user_info", {}).get("name", ""),
  525. "user_id": str(video_info.get("user_info", {}).get("user_id", "")),
  526. "avatar_url": str(video_info.get("user_info", {}).get("avatar_url", "")),
  527. "cover_url": video_info.get("poster_url", ""),
  528. "audio_url": cls.get_video_url(video_info)["audio_url"],
  529. "video_url": cls.get_video_url(video_info)["video_url"],
  530. "session": f"xigua-search-{int(time.time())}"
  531. }
  532. return video_dict
  533. @classmethod
  534. def repeat_video(cls, log_type, crawler, video_id, env):
  535. sql = f""" select * from crawler_video where platform in ("{crawler}","{cls.platform}") and out_video_id="{video_id}"; """
  536. repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env)
  537. return len(repeat_video)
  538. @classmethod
  539. def quit(cls, log_type, crawler, env, driver: WebDriver):
  540. Common.logger(log_type, crawler).info("退出浏览器")
  541. Common.logging(log_type, crawler, env, "退出浏览器")
  542. driver.quit()
  543. quit_cmd = "ps aux | grep Chrome | grep -v grep | awk '{print $2}' | xargs kill -9"
  544. os.system(quit_cmd)
  545. @classmethod
  546. def get_videoList(cls, log_type, crawler, our_uid, rule_dict, env):
  547. mq = MQ(topic_name="topic_crawler_etl_" + env)
  548. Common.logger(log_type, crawler).info("启动 Chrome 浏览器")
  549. Common.logging(log_type, crawler, env, "启动 Chrome 浏览器")
  550. # kill 所有 Chrome 进程
  551. quit_cmd = "ps aux | grep Chrome | grep -v grep | awk '{print $2}' | xargs kill -9"
  552. os.system(quit_cmd)
  553. time.sleep(1)
  554. # 启动 Chrome,指定端口号:12306
  555. cmd = 'open -a "Google Chrome" --args --remote-debugging-port=12306'
  556. os.system(cmd)
  557. # 打印请求配置
  558. ca = DesiredCapabilities.CHROME
  559. ca["goog:loggingPrefs"] = {"performance": "ALL"}
  560. # 配置 chromedriver
  561. if env == "dev":
  562. chromedriver = "/Users/wangkun/Downloads/chromedriver/chromedriver_v114/chromedriver"
  563. else:
  564. # chromedriver = "/usr/bin/chromedriver"
  565. chromedriver = "/Users/kanyikan/Downloads/chromedriver/chromedriver_v114/chromedriver"
  566. # # 设置IP代理
  567. # proxy = Proxy()
  568. # proxy.proxy_type = ProxyType.MANUAL
  569. # proxy.http_proxy = Common.tunnel_proxies()["http"] # 代理的IP地址和端口号
  570. # 初始化浏览器
  571. browser = webdriver.ChromeOptions()
  572. # browser.add_argument(f'--proxy-server={Common.tunnel_proxies()}') # 代理的IP地址和端口号
  573. browser.add_experimental_option("debuggerAddress", "127.0.0.1:12306")
  574. # driver初始化
  575. driver = webdriver.Chrome(desired_capabilities=ca, options=browser, service=Service(chromedriver))
  576. driver.implicitly_wait(10)
  577. Common.logger(log_type, crawler).info("打开西瓜推荐页")
  578. Common.logging(log_type, crawler, env, "打开西瓜推荐页")
  579. driver.get(f"https://www.ixigua.com/")
  580. time.sleep(2)
  581. # 检查登录状态
  582. if len(driver.find_elements(By.XPATH, '//*[@class="BU-Component-Header-Avatar__image"]')) == 0:
  583. Common.logger(log_type, crawler).info("登录失效")
  584. Common.logging(log_type, crawler, env, "登录失效")
  585. driver.get_screenshot_as_file(f"./{crawler}/photos/logon_err.png")
  586. # 登录失效,报警
  587. if 20 >= datetime.datetime.now().hour >= 10:
  588. Feishu.bot(log_type, crawler, "西瓜推荐,登录失效")
  589. videoList_elements = driver.find_elements(By.XPATH, '//*[@class="HorizontalFeedCard HorizontalChannelBlockList__item"]')
  590. if len(videoList_elements) == 0:
  591. Common.logger(log_type, crawler).info("到底啦~~~~~~~~~~\n")
  592. Common.logging(log_type, crawler, env, "到底啦~~~~~~~~~~\n")
  593. cls.quit(log_type, crawler, env, driver)
  594. return
  595. for i, video_element in enumerate(videoList_elements):
  596. Common.logger(log_type, crawler).info(f"正在抓取第{i+1}条视频")
  597. Common.logging(log_type, crawler, env, f"正在抓取第{i+1}条视频")
  598. item_id = video_element.find_elements(By.XPATH, '//*[@class="HorizontalFeedCard__coverWrapper disableZoomAnimation"]')[i].get_attribute("href")
  599. item_id = item_id.replace("https://www.ixigua.com/", "").replace("?&", "")
  600. Common.logger(log_type, crawler).info(f"item_id:{item_id}")
  601. video_dict = cls.get_video_info(log_type, crawler, item_id)
  602. if video_dict is None:
  603. Common.logger(log_type, crawler).info("无效视频\n")
  604. Common.logging(log_type, crawler, env, "无效视频\n")
  605. continue
  606. for k, v in video_dict.items():
  607. Common.logger(log_type, crawler).info(f"{k}:{v}")
  608. Common.logging(log_type, crawler, env, f"{video_dict}")
  609. if download_rule(log_type=log_type, crawler=crawler, video_dict=video_dict, rule_dict=rule_dict) is False:
  610. Common.logger(log_type, crawler).info("不满足抓取规则\n")
  611. Common.logging(log_type, crawler, env, "不满足抓取规则\n")
  612. elif any(str(word) if str(word) in video_dict["video_title"] else False
  613. for word in get_config_from_mysql(log_type=log_type,
  614. source=crawler,
  615. env=env,
  616. text="filter",
  617. action="")) is True:
  618. Common.logger(log_type, crawler).info('已中过滤词\n')
  619. Common.logging(log_type, crawler, env, '已中过滤词\n')
  620. elif cls.repeat_video(log_type, crawler, video_dict["video_id"], env) != 0:
  621. Common.logger(log_type, crawler).info('视频已下载\n')
  622. Common.logging(log_type, crawler, env, '视频已下载\n')
  623. else:
  624. # Common.logger(log_type, crawler).info("满足下载规则\n")
  625. video_dict["out_user_id"] = video_dict["user_id"]
  626. video_dict["platform"] = crawler
  627. video_dict["strategy"] = log_type
  628. video_dict["out_video_id"] = video_dict["video_id"]
  629. video_dict["width"] = video_dict["video_width"]
  630. video_dict["height"] = video_dict["video_height"]
  631. video_dict["crawler_rule"] = json.dumps(rule_dict)
  632. video_dict["user_id"] = our_uid
  633. video_dict["publish_time"] = video_dict["publish_time_str"]
  634. video_dict["strategy_type"] = log_type
  635. mq.send_msg(video_dict)
  636. cls.quit(log_type, crawler, env, driver)
  637. if __name__ == "__main__":
  638. # XiguaRecommend.get_videoList("recommend", "xigua", "dev")
  639. print(subprocess.run(['crontab', '-l']))
  640. print(subprocess.run(['crontab', '-e']))
  641. pass