xigua_follow.py 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/2/17
  4. import base64
  5. import json
  6. import os
  7. import shutil
  8. import sys
  9. import time
  10. import requests
  11. import urllib3
  12. from selenium.webdriver import DesiredCapabilities
  13. from selenium.webdriver.chrome.service import Service
  14. from selenium.webdriver.common.by import By
  15. from seleniumwire import webdriver
  16. from common.db import MysqlHelper
  17. sys.path.append(os.getcwd())
  18. from common.common import Common
  19. from common.feishu import Feishu
  20. from common.publish import Publish
  21. proxies = {"http": None, "https": None}
  22. class Follow:
  23. # 个人主页视频翻页参数
  24. offset = 0
  25. platform = "西瓜视频"
  26. # 下载规则
  27. @staticmethod
  28. def download_rule(duration, width, height):
  29. if int(duration) >= 60:
  30. if int(width) >= 720 or int(height) >= 720:
  31. return True
  32. else:
  33. return False
  34. else:
  35. return False
  36. # 过滤词库
  37. @classmethod
  38. def filter_words(cls, log_type, crawler):
  39. try:
  40. filter_words_sheet = Feishu.get_values_batch(log_type, crawler, 'KGB4Hc')
  41. filter_words_list = []
  42. for x in filter_words_sheet:
  43. for y in x:
  44. if y is None:
  45. pass
  46. else:
  47. filter_words_list.append(y)
  48. return filter_words_list
  49. except Exception as e:
  50. Common.logger(log_type, crawler).error(f'filter_words异常:{e}\n')
  51. # 获取用户信息(字典格式). 注意:部分 user_id 字符类型是 int / str
  52. @classmethod
  53. def get_user_info_from_feishu(cls, log_type, crawler):
  54. try:
  55. user_sheet = Feishu.get_values_batch(log_type, crawler, '5tlTYB')
  56. user_dict = {}
  57. for i in range(1, len(user_sheet)):
  58. user_name = user_sheet[i][0]
  59. out_id = user_sheet[i][1]
  60. our_id = user_sheet[i][3]
  61. if user_name is None or out_id is None or our_id is None:
  62. pass
  63. else:
  64. user_dict[user_name] = str(out_id) + ',' + str(our_id)
  65. return user_dict
  66. except Exception as e:
  67. Common.logger(log_type, crawler).error(f'get_user_id_from_feishu异常:{e}\n')
  68. @classmethod
  69. def get_signature(cls, log_type, crawler, out_uid, machine):
  70. try:
  71. # 打印请求配置
  72. ca = DesiredCapabilities.CHROME
  73. ca["goog:loggingPrefs"] = {"performance": "ALL"}
  74. # 不打开浏览器运行
  75. chrome_options = webdriver.ChromeOptions()
  76. chrome_options.add_argument("--headless")
  77. chrome_options.add_argument('--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')
  78. chrome_options.add_argument("--no-sandbox")
  79. # driver初始化
  80. if machine == 'aliyun' or machine == 'aliyun_hk':
  81. driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options)
  82. elif machine == 'macpro':
  83. driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options,
  84. service=Service('/Users/lieyunye/Downloads/chromedriver_v86/chromedriver'))
  85. elif machine == 'macair':
  86. driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options,
  87. service=Service('/Users/piaoquan/Downloads/chromedriver'))
  88. else:
  89. driver = webdriver.Chrome(desired_capabilities=ca, options=chrome_options, service=Service('/Users/wangkun/Downloads/chromedriver/chromedriver_v110/chromedriver'))
  90. driver.implicitly_wait(10)
  91. driver.get(f'https://www.ixigua.com/home/{out_uid}/')
  92. time.sleep(3)
  93. data_src = driver.find_elements(By.XPATH, '//img[@class="tt-img BU-MagicImage tt-img-loaded"]')[1].get_attribute("data-src")
  94. signature = data_src.split("x-signature=")[-1]
  95. # print(f"data_src:{data_src}")
  96. # print(f"signature:{signature}")
  97. return signature
  98. except Exception as e:
  99. Common.logger(log_type, crawler).error(f'get_signature异常:{e}\n')
  100. # 获取视频详情
  101. @classmethod
  102. def get_video_url(cls, log_type, crawler, gid):
  103. try:
  104. url = 'https://www.ixigua.com/api/mixVideo/information?'
  105. headers = {
  106. "accept-encoding": "gzip, deflate",
  107. "accept-language": "zh-CN,zh-Hans;q=0.9",
  108. "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
  109. "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15",
  110. "referer": "https://www.ixigua.com/7102614741050196520?logTag=0531c88ac04f38ab2c62",
  111. }
  112. params = {
  113. 'mixId': gid,
  114. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfC'
  115. 'NVVIOBNjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  116. 'X-Bogus': 'DFSzswVupYTANCJOSBk0P53WxM-r',
  117. '_signature': '_02B4Z6wo0000119LvEwAAIDCuktNZ0y5wkdfS7jAALThuOR8D9yWNZ.EmWHKV0WSn6Px'
  118. 'fPsH9-BldyxVje0f49ryXgmn7Tzk-swEHNb15TiGqa6YF.cX0jW8Eds1TtJOIZyfc9s5emH7gdWN94',
  119. }
  120. cookies = {
  121. 'ixigua-a-s': '1',
  122. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfCNVVIOB'
  123. 'NjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  124. 'ttwid': '1%7C_yXQeHWwLZgCsgHClOwTCdYSOt_MjdOkgnPIkpi-Sr8%7C1661241238%7Cf57d0c5ef3f1d7'
  125. '6e049fccdca1ac54887c34d1f8731c8e51a49780ff0ceab9f8',
  126. 'tt_scid': 'QZ4l8KXDG0YAEaMCSbADdcybdKbUfG4BC6S4OBv9lpRS5VyqYLX2bIR8CTeZeGHR9ee3',
  127. 'MONITOR_WEB_ID': '0a49204a-7af5-4e96-95f0-f4bafb7450ad',
  128. '__ac_nonce': '06304878000964fdad287',
  129. '__ac_signature': '_02B4Z6wo00f017Rcr3AAAIDCUVxeW1tOKEu0fKvAAI4cvoYzV-wBhq7B6D8k0no7lb'
  130. 'FlvYoinmtK6UXjRIYPXnahUlFTvmWVtb77jsMkKAXzAEsLE56m36RlvL7ky.M3Xn52r9t1IEb7IR3ke8',
  131. 'ttcid': 'e56fabf6e85d4adf9e4d91902496a0e882',
  132. '_tea_utm_cache_1300': 'undefined',
  133. 'support_avif': 'false',
  134. 'support_webp': 'false',
  135. 'xiguavideopcwebid': '7134967546256016900',
  136. 'xiguavideopcwebid.sig': 'xxRww5R1VEMJN_dQepHorEu_eAc',
  137. }
  138. urllib3.disable_warnings()
  139. response = requests.get(url=url, headers=headers, params=params, cookies=cookies, verify=False)
  140. if 'data' not in response.json() or response.json()['data'] == '':
  141. Common.logger(log_type, crawler).warning('get_video_info: response: {}', response)
  142. else:
  143. video_info = response.json()['data']['gidInformation']['packerData']['video']
  144. video_url_dict = {}
  145. # video_url
  146. if 'videoResource' not in video_info:
  147. video_url_dict["video_url"] = ''
  148. video_url_dict["audio_url"] = ''
  149. video_url_dict["video_width"] = 0
  150. video_url_dict["video_height"] = 0
  151. elif 'dash_120fps' in video_info['videoResource']:
  152. if "video_list" in video_info['videoResource']['dash_120fps'] and 'video_4' in video_info['videoResource']['dash_120fps']['video_list']:
  153. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_4']['backup_url_1']
  154. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_4']['backup_url_1']
  155. if len(video_url) % 3 == 1:
  156. video_url += '=='
  157. elif len(video_url) % 3 == 2:
  158. video_url += '='
  159. elif len(audio_url) % 3 == 1:
  160. audio_url += '=='
  161. elif len(audio_url) % 3 == 2:
  162. audio_url += '='
  163. video_url = base64.b64decode(video_url).decode('utf8')
  164. audio_url = base64.b64decode(audio_url).decode('utf8')
  165. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_4']['vwidth']
  166. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_4']['vheight']
  167. video_url_dict["video_url"] = video_url
  168. video_url_dict["audio_url"] = audio_url
  169. video_url_dict["video_width"] = video_width
  170. video_url_dict["video_height"] = video_height
  171. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_3' in video_info['videoResource']['dash_120fps']['video_list']:
  172. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_3']['backup_url_1']
  173. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_3']['backup_url_1']
  174. if len(video_url) % 3 == 1:
  175. video_url += '=='
  176. elif len(video_url) % 3 == 2:
  177. video_url += '='
  178. elif len(audio_url) % 3 == 1:
  179. audio_url += '=='
  180. elif len(audio_url) % 3 == 2:
  181. audio_url += '='
  182. video_url = base64.b64decode(video_url).decode('utf8')
  183. audio_url = base64.b64decode(audio_url).decode('utf8')
  184. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_3']['vwidth']
  185. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_3']['vheight']
  186. video_url_dict["video_url"] = video_url
  187. video_url_dict["audio_url"] = audio_url
  188. video_url_dict["video_width"] = video_width
  189. video_url_dict["video_height"] = video_height
  190. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_2' in video_info['videoResource']['dash_120fps']['video_list']:
  191. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_2']['backup_url_1']
  192. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_2']['backup_url_1']
  193. if len(video_url) % 3 == 1:
  194. video_url += '=='
  195. elif len(video_url) % 3 == 2:
  196. video_url += '='
  197. elif len(audio_url) % 3 == 1:
  198. audio_url += '=='
  199. elif len(audio_url) % 3 == 2:
  200. audio_url += '='
  201. video_url = base64.b64decode(video_url).decode('utf8')
  202. audio_url = base64.b64decode(audio_url).decode('utf8')
  203. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_2']['vwidth']
  204. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_2']['vheight']
  205. video_url_dict["video_url"] = video_url
  206. video_url_dict["audio_url"] = audio_url
  207. video_url_dict["video_width"] = video_width
  208. video_url_dict["video_height"] = video_height
  209. elif "video_list" in video_info['videoResource']['dash_120fps'] and 'video_1' in video_info['videoResource']['dash_120fps']['video_list']:
  210. video_url = video_info['videoResource']['dash_120fps']['video_list']['video_1']['backup_url_1']
  211. audio_url = video_info['videoResource']['dash_120fps']['video_list']['video_1']['backup_url_1']
  212. if len(video_url) % 3 == 1:
  213. video_url += '=='
  214. elif len(video_url) % 3 == 2:
  215. video_url += '='
  216. elif len(audio_url) % 3 == 1:
  217. audio_url += '=='
  218. elif len(audio_url) % 3 == 2:
  219. audio_url += '='
  220. video_url = base64.b64decode(video_url).decode('utf8')
  221. audio_url = base64.b64decode(audio_url).decode('utf8')
  222. video_width = video_info['videoResource']['dash_120fps']['video_list']['video_1']['vwidth']
  223. video_height = video_info['videoResource']['dash_120fps']['video_list']['video_1']['vheight']
  224. video_url_dict["video_url"] = video_url
  225. video_url_dict["audio_url"] = audio_url
  226. video_url_dict["video_width"] = video_width
  227. video_url_dict["video_height"] = video_height
  228. elif 'dynamic_video' in video_info['videoResource']['dash_120fps'] \
  229. and 'dynamic_video_list' in video_info['videoResource']['dash_120fps']['dynamic_video'] \
  230. and 'dynamic_audio_list' in video_info['videoResource']['dash_120fps']['dynamic_video'] \
  231. and len(video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list']) != 0 \
  232. and len(video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_audio_list']) != 0:
  233. video_url = video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1]['backup_url_1']
  234. audio_url = video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_audio_list'][-1]['backup_url_1']
  235. if len(video_url) % 3 == 1:
  236. video_url += '=='
  237. elif len(video_url) % 3 == 2:
  238. video_url += '='
  239. elif len(audio_url) % 3 == 1:
  240. audio_url += '=='
  241. elif len(audio_url) % 3 == 2:
  242. audio_url += '='
  243. video_url = base64.b64decode(video_url).decode('utf8')
  244. audio_url = base64.b64decode(audio_url).decode('utf8')
  245. video_width = video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1]['vwidth']
  246. video_height = video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1]['vheight']
  247. video_url_dict["video_url"] = video_url
  248. video_url_dict["audio_url"] = audio_url
  249. video_url_dict["video_width"] = video_width
  250. video_url_dict["video_height"] = video_height
  251. else:
  252. video_url_dict["video_url"] = ''
  253. video_url_dict["audio_url"] = ''
  254. video_url_dict["video_width"] = 0
  255. video_url_dict["video_height"] = 0
  256. elif 'dash' in video_info['videoResource']:
  257. if "video_list" in video_info['videoResource']['dash'] and 'video_4' in video_info['videoResource']['dash']['video_list']:
  258. video_url = video_info['videoResource']['dash']['video_list']['video_4']['backup_url_1']
  259. audio_url = video_info['videoResource']['dash']['video_list']['video_4']['backup_url_1']
  260. if len(video_url) % 3 == 1:
  261. video_url += '=='
  262. elif len(video_url) % 3 == 2:
  263. video_url += '='
  264. elif len(audio_url) % 3 == 1:
  265. audio_url += '=='
  266. elif len(audio_url) % 3 == 2:
  267. audio_url += '='
  268. video_url = base64.b64decode(video_url).decode('utf8')
  269. audio_url = base64.b64decode(audio_url).decode('utf8')
  270. video_width = video_info['videoResource']['dash']['video_list']['video_4']['vwidth']
  271. video_height = video_info['videoResource']['dash']['video_list']['video_4']['vheight']
  272. video_url_dict["video_url"] = video_url
  273. video_url_dict["audio_url"] = audio_url
  274. video_url_dict["video_width"] = video_width
  275. video_url_dict["video_height"] = video_height
  276. elif "video_list" in video_info['videoResource']['dash'] and 'video_3' in video_info['videoResource']['dash']['video_list']:
  277. video_url = video_info['videoResource']['dash']['video_list']['video_3']['backup_url_1']
  278. audio_url = video_info['videoResource']['dash']['video_list']['video_3']['backup_url_1']
  279. if len(video_url) % 3 == 1:
  280. video_url += '=='
  281. elif len(video_url) % 3 == 2:
  282. video_url += '='
  283. elif len(audio_url) % 3 == 1:
  284. audio_url += '=='
  285. elif len(audio_url) % 3 == 2:
  286. audio_url += '='
  287. video_url = base64.b64decode(video_url).decode('utf8')
  288. audio_url = base64.b64decode(audio_url).decode('utf8')
  289. video_width = video_info['videoResource']['dash']['video_list']['video_3']['vwidth']
  290. video_height = video_info['videoResource']['dash']['video_list']['video_3']['vheight']
  291. video_url_dict["video_url"] = video_url
  292. video_url_dict["audio_url"] = audio_url
  293. video_url_dict["video_width"] = video_width
  294. video_url_dict["video_height"] = video_height
  295. elif "video_list" in video_info['videoResource']['dash'] and 'video_2' in video_info['videoResource']['dash']['video_list']:
  296. video_url = video_info['videoResource']['dash']['video_list']['video_2']['backup_url_1']
  297. audio_url = video_info['videoResource']['dash']['video_list']['video_2']['backup_url_1']
  298. if len(video_url) % 3 == 1:
  299. video_url += '=='
  300. elif len(video_url) % 3 == 2:
  301. video_url += '='
  302. elif len(audio_url) % 3 == 1:
  303. audio_url += '=='
  304. elif len(audio_url) % 3 == 2:
  305. audio_url += '='
  306. video_url = base64.b64decode(video_url).decode('utf8')
  307. audio_url = base64.b64decode(audio_url).decode('utf8')
  308. video_width = video_info['videoResource']['dash']['video_list']['video_2']['vwidth']
  309. video_height = video_info['videoResource']['dash']['video_list']['video_2']['vheight']
  310. video_url_dict["video_url"] = video_url
  311. video_url_dict["audio_url"] = audio_url
  312. video_url_dict["video_width"] = video_width
  313. video_url_dict["video_height"] = video_height
  314. elif "video_list" in video_info['videoResource']['dash'] and 'video_1' in video_info['videoResource']['dash']['video_list']:
  315. video_url = video_info['videoResource']['dash']['video_list']['video_1']['backup_url_1']
  316. audio_url = video_info['videoResource']['dash']['video_list']['video_1']['backup_url_1']
  317. if len(video_url) % 3 == 1:
  318. video_url += '=='
  319. elif len(video_url) % 3 == 2:
  320. video_url += '='
  321. elif len(audio_url) % 3 == 1:
  322. audio_url += '=='
  323. elif len(audio_url) % 3 == 2:
  324. audio_url += '='
  325. video_url = base64.b64decode(video_url).decode('utf8')
  326. audio_url = base64.b64decode(audio_url).decode('utf8')
  327. video_width = video_info['videoResource']['dash']['video_list']['video_1']['vwidth']
  328. video_height = video_info['videoResource']['dash']['video_list']['video_1']['vheight']
  329. video_url_dict["video_url"] = video_url
  330. video_url_dict["audio_url"] = audio_url
  331. video_url_dict["video_width"] = video_width
  332. video_url_dict["video_height"] = video_height
  333. elif 'dynamic_video' in video_info['videoResource']['dash'] \
  334. and 'dynamic_video_list' in video_info['videoResource']['dash']['dynamic_video'] \
  335. and 'dynamic_audio_list' in video_info['videoResource']['dash']['dynamic_video'] \
  336. and len(video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list']) != 0 \
  337. and len(video_info['videoResource']['dash']['dynamic_video']['dynamic_audio_list']) != 0:
  338. video_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1]['backup_url_1']
  339. audio_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_audio_list'][-1]['backup_url_1']
  340. if len(video_url) % 3 == 1:
  341. video_url += '=='
  342. elif len(video_url) % 3 == 2:
  343. video_url += '='
  344. elif len(audio_url) % 3 == 1:
  345. audio_url += '=='
  346. elif len(audio_url) % 3 == 2:
  347. audio_url += '='
  348. video_url = base64.b64decode(video_url).decode('utf8')
  349. audio_url = base64.b64decode(audio_url).decode('utf8')
  350. video_width = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1]['vwidth']
  351. video_height = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1]['vheight']
  352. video_url_dict["video_url"] = video_url
  353. video_url_dict["audio_url"] = audio_url
  354. video_url_dict["video_width"] = video_width
  355. video_url_dict["video_height"] = video_height
  356. else:
  357. video_url_dict["video_url"] = ''
  358. video_url_dict["audio_url"] = ''
  359. video_url_dict["video_width"] = 0
  360. video_url_dict["video_height"] = 0
  361. elif 'normal' in video_info['videoResource']:
  362. if "video_list" in video_info['videoResource']['normal'] and 'video_4' in \
  363. video_info['videoResource']['normal']['video_list']:
  364. video_url = video_info['videoResource']['normal']['video_list']['video_4']['backup_url_1']
  365. audio_url = video_info['videoResource']['normal']['video_list']['video_4']['backup_url_1']
  366. if len(video_url) % 3 == 1:
  367. video_url += '=='
  368. elif len(video_url) % 3 == 2:
  369. video_url += '='
  370. elif len(audio_url) % 3 == 1:
  371. audio_url += '=='
  372. elif len(audio_url) % 3 == 2:
  373. audio_url += '='
  374. video_url = base64.b64decode(video_url).decode('utf8')
  375. audio_url = base64.b64decode(audio_url).decode('utf8')
  376. video_width = video_info['videoResource']['normal']['video_list']['video_4']['vwidth']
  377. video_height = video_info['videoResource']['normal']['video_list']['video_4']['vheight']
  378. video_url_dict["video_url"] = video_url
  379. video_url_dict["audio_url"] = audio_url
  380. video_url_dict["video_width"] = video_width
  381. video_url_dict["video_height"] = video_height
  382. elif "video_list" in video_info['videoResource']['normal'] and 'video_3' in \
  383. video_info['videoResource']['normal']['video_list']:
  384. video_url = video_info['videoResource']['normal']['video_list']['video_3']['backup_url_1']
  385. audio_url = video_info['videoResource']['normal']['video_list']['video_3']['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']['video_list']['video_3']['vwidth']
  397. video_height = video_info['videoResource']['normal']['video_list']['video_3']['vheight']
  398. video_url_dict["video_url"] = video_url
  399. video_url_dict["audio_url"] = audio_url
  400. video_url_dict["video_width"] = video_width
  401. video_url_dict["video_height"] = video_height
  402. elif "video_list" in video_info['videoResource']['normal'] and 'video_2' in \
  403. video_info['videoResource']['normal']['video_list']:
  404. video_url = video_info['videoResource']['normal']['video_list']['video_2']['backup_url_1']
  405. audio_url = video_info['videoResource']['normal']['video_list']['video_2']['backup_url_1']
  406. if len(video_url) % 3 == 1:
  407. video_url += '=='
  408. elif len(video_url) % 3 == 2:
  409. video_url += '='
  410. elif len(audio_url) % 3 == 1:
  411. audio_url += '=='
  412. elif len(audio_url) % 3 == 2:
  413. audio_url += '='
  414. video_url = base64.b64decode(video_url).decode('utf8')
  415. audio_url = base64.b64decode(audio_url).decode('utf8')
  416. video_width = video_info['videoResource']['normal']['video_list']['video_2']['vwidth']
  417. video_height = video_info['videoResource']['normal']['video_list']['video_2']['vheight']
  418. video_url_dict["video_url"] = video_url
  419. video_url_dict["audio_url"] = audio_url
  420. video_url_dict["video_width"] = video_width
  421. video_url_dict["video_height"] = video_height
  422. elif "video_list" in video_info['videoResource']['normal'] and 'video_1' in \
  423. video_info['videoResource']['normal']['video_list']:
  424. video_url = video_info['videoResource']['normal']['video_list']['video_1']['backup_url_1']
  425. audio_url = video_info['videoResource']['normal']['video_list']['video_1']['backup_url_1']
  426. if len(video_url) % 3 == 1:
  427. video_url += '=='
  428. elif len(video_url) % 3 == 2:
  429. video_url += '='
  430. elif len(audio_url) % 3 == 1:
  431. audio_url += '=='
  432. elif len(audio_url) % 3 == 2:
  433. audio_url += '='
  434. video_url = base64.b64decode(video_url).decode('utf8')
  435. audio_url = base64.b64decode(audio_url).decode('utf8')
  436. video_width = video_info['videoResource']['normal']['video_list']['video_1']['vwidth']
  437. video_height = video_info['videoResource']['normal']['video_list']['video_1']['vheight']
  438. video_url_dict["video_url"] = video_url
  439. video_url_dict["audio_url"] = audio_url
  440. video_url_dict["video_width"] = video_width
  441. video_url_dict["video_height"] = video_height
  442. elif 'dynamic_video' in video_info['videoResource']['normal'] \
  443. and 'dynamic_video_list' in video_info['videoResource']['normal']['dynamic_video'] \
  444. and 'dynamic_audio_list' in video_info['videoResource']['normal']['dynamic_video'] \
  445. and len(video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list']) != 0 \
  446. and len(video_info['videoResource']['normal']['dynamic_video']['dynamic_audio_list']) != 0:
  447. video_url = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  448. 'backup_url_1']
  449. audio_url = video_info['videoResource']['normal']['dynamic_video']['dynamic_audio_list'][-1][
  450. 'backup_url_1']
  451. if len(video_url) % 3 == 1:
  452. video_url += '=='
  453. elif len(video_url) % 3 == 2:
  454. video_url += '='
  455. elif len(audio_url) % 3 == 1:
  456. audio_url += '=='
  457. elif len(audio_url) % 3 == 2:
  458. audio_url += '='
  459. video_url = base64.b64decode(video_url).decode('utf8')
  460. audio_url = base64.b64decode(audio_url).decode('utf8')
  461. video_width = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  462. 'vwidth']
  463. video_height = video_info['videoResource']['normal']['dynamic_video']['dynamic_video_list'][-1][
  464. 'vheight']
  465. video_url_dict["video_url"] = video_url
  466. video_url_dict["audio_url"] = audio_url
  467. video_url_dict["video_width"] = video_width
  468. video_url_dict["video_height"] = video_height
  469. else:
  470. video_url_dict["video_url"] = ''
  471. video_url_dict["audio_url"] = ''
  472. video_url_dict["video_width"] = 0
  473. video_url_dict["video_height"] = 0
  474. else:
  475. video_url_dict["video_url"] = ''
  476. video_url_dict["audio_url"] = ''
  477. video_url_dict["video_width"] = 0
  478. video_url_dict["video_height"] = 0
  479. return video_url_dict
  480. except Exception as e:
  481. Common.logger(log_type, crawler).error(f'get_video_url:{e}\n')
  482. @classmethod
  483. def get_videolist(cls, log_type, crawler, strategy, our_uid, out_uid, oss_endpoint, env, machine):
  484. signature = cls.get_signature(log_type, crawler, out_uid, machine)
  485. while True:
  486. url = "https://www.ixigua.com/api/videov2/author/new_video_list?"
  487. params = {
  488. 'to_user_id': str(out_uid),
  489. 'offset': str(cls.offset),
  490. 'limit': '30',
  491. 'maxBehotTime': '0',
  492. 'order': 'new',
  493. 'isHome': '0',
  494. 'msToken': 'G0eRzNkw189a8TLaXjc6nTHVMQwh9XcxVAqTbGKi7iPJdQcLwS3-XRrJ3MZ7QBfqErpxp3EX1WtvWOIcZ3NIgr41hgcd-v64so_RRj3YCRw1UsKW8mIssNLlIMspsg==',
  495. 'X-Bogus': 'DFSzswVuEkUANjW9ShFTgR/F6qHt',
  496. '_signature': signature,
  497. }
  498. headers = {
  499. 'authority': 'www.ixigua.com',
  500. 'accept': 'application/json, text/plain, */*',
  501. 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  502. 'cache-control': 'no-cache',
  503. 'cookie': f'MONITOR_WEB_ID=7168304743566296612; __ac_signature={signature}; ixigua-a-s=1; support_webp=true; support_avif=false; csrf_session_id=a5355d954d3c63ed1ba35faada452b4d; msToken=G0eRzNkw189a8TLaXjc6nTHVMQwh9XcxVAqTbGKi7iPJdQcLwS3-XRrJ3MZ7QBfqErpxp3EX1WtvWOIcZ3NIgr41hgcd-v64so_RRj3YCRw1UsKW8mIssNLlIMspsg==; tt_scid=o4agqz7u9SKPwfBoPt6S82Cw0q.9KDtqmNe0JHxMqmpxNHQWq1BmrQdgVU6jEoX7ed99; ttwid=1%7CHHtv2QqpSGuSu8r-zXF1QoWsvjmNi1SJrqOrZzg-UCY%7C1676618894%7Cee5ad95378275f282f230a7ffa9947ae7eff40d0829c5a2568672a6dc90a1c96; ixigua-a-s=1',
  504. 'pragma': 'no-cache',
  505. 'referer': f'https://www.ixigua.com/home/{out_uid}/video/?preActiveKey=hotsoon&list_entrance=userdetail',
  506. 'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"',
  507. 'sec-ch-ua-mobile': '?0',
  508. 'sec-ch-ua-platform': '"macOS"',
  509. 'sec-fetch-dest': 'empty',
  510. 'sec-fetch-mode': 'cors',
  511. 'sec-fetch-site': 'same-origin',
  512. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41',
  513. 'x-secsdk-csrf-token': '00010000000119e3f9454d1dcbb288704cda1960f241e2d19bd21f2fd283520c3615a990ac5a17448bfbb902a249'
  514. }
  515. urllib3.disable_warnings()
  516. response = requests.get(url=url, headers=headers, params=params, proxies=proxies, verify=False)
  517. cls.offset += 30
  518. if response.status_code != 200:
  519. Common.logger(log_type, crawler).warning(f"get_videolist_response:{response.text}\n")
  520. elif 'data' not in response.text:
  521. Common.logger(log_type, crawler).warning(f"get_videolist_response:{response.text}\n")
  522. elif 'videoList' not in response.json()["data"]:
  523. Common.logger(log_type, crawler).warning(f"get_videolist_response:{response.json()}\n")
  524. else:
  525. videoList = response.json()['data']['videoList']
  526. for i in range(len(videoList)):
  527. # video_title
  528. if 'title' not in videoList[i]:
  529. video_title = 0
  530. else:
  531. video_title = videoList[i]['title'].strip().replace('手游', '') \
  532. .replace('/', '').replace('\/', '').replace('\n', '')
  533. # video_id
  534. if 'video_id' not in videoList[i]:
  535. video_id = 0
  536. else:
  537. video_id = videoList[i]['video_id']
  538. # gid
  539. if 'gid' not in videoList[i]:
  540. gid = 0
  541. else:
  542. gid = videoList[i]['gid']
  543. # play_cnt
  544. if 'video_detail_info' not in videoList[i]:
  545. play_cnt = 0
  546. elif 'video_watch_count' not in videoList[i]['video_detail_info']:
  547. play_cnt = 0
  548. else:
  549. play_cnt = videoList[i]['video_detail_info']['video_watch_count']
  550. # comment_cnt
  551. if 'comment_count' not in videoList[i]:
  552. comment_cnt = 0
  553. else:
  554. comment_cnt = videoList[i]['comment_count']
  555. # like_cnt
  556. if 'digg_count' not in videoList[i]:
  557. like_cnt = 0
  558. else:
  559. like_cnt = videoList[i]['digg_count']
  560. # share_cnt
  561. share_cnt = 0
  562. # video_duration
  563. if 'video_duration' not in videoList[i]:
  564. video_duration = 0
  565. else:
  566. video_duration = videoList[i]['video_duration']
  567. # send_time
  568. if 'publish_time' not in videoList[i]:
  569. publish_time = 0
  570. else:
  571. publish_time = videoList[i]['publish_time']
  572. publish_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(publish_time))
  573. # is_top
  574. if 'is_top' not in videoList[i]:
  575. is_top = 0
  576. else:
  577. is_top = videoList[i]['is_top']
  578. # user_name
  579. if 'user_info' not in videoList[i]:
  580. user_name = 0
  581. elif 'name' not in videoList[i]['user_info']:
  582. user_name = 0
  583. else:
  584. user_name = videoList[i]['user_info']['name']
  585. # user_id
  586. if 'user_info' not in videoList[i]:
  587. user_id = 0
  588. elif 'user_id' not in videoList[i]['user_info']:
  589. user_id = 0
  590. else:
  591. user_id = videoList[i]['user_info']['user_id']
  592. # avatar_url
  593. if 'user_info' not in videoList[i]:
  594. avatar_url = 0
  595. elif 'avatar_url' not in videoList[i]['user_info']:
  596. avatar_url = 0
  597. else:
  598. avatar_url = videoList[i]['user_info']['avatar_url']
  599. # cover_url
  600. if 'video_detail_info' not in videoList[i]:
  601. cover_url = 0
  602. elif 'detail_video_large_image' not in videoList[i]['video_detail_info']:
  603. cover_url = 0
  604. elif 'url' in videoList[i]['video_detail_info']['detail_video_large_image']:
  605. cover_url = videoList[i]['video_detail_info']['detail_video_large_image']['url']
  606. else:
  607. cover_url = videoList[i]['video_detail_info']['detail_video_large_image']['url_list'][0]['url']
  608. if gid == 0 or video_id == 0 or cover_url == 0:
  609. Common.logger(log_type, crawler).info('无效视频\n')
  610. elif is_top is True and int(time.time()) - int(publish_time) > 3600 * 24 * 10:
  611. Common.logger(log_type, crawler).info(f'置顶视频,且发布时间超过10天:{publish_time_str}\n')
  612. elif int(time.time()) - int(publish_time) > 3600 * 24 * 10:
  613. Common.logger(log_type, crawler).info(f'发布时间超过10天:{publish_time_str}\n')
  614. cls.offset = 0
  615. return
  616. else:
  617. video_url_dict = cls.get_video_url(log_type, crawler, gid)
  618. video_url = video_url_dict["video_url"]
  619. audio_url = video_url_dict["audio_url"]
  620. video_width = video_url_dict["video_width"]
  621. video_height = video_url_dict["video_height"]
  622. video_dict = {'video_title': video_title,
  623. 'video_id': video_id,
  624. 'gid': gid,
  625. 'play_cnt': play_cnt,
  626. 'comment_cnt': comment_cnt,
  627. 'like_cnt': like_cnt,
  628. 'share_cnt': share_cnt,
  629. 'video_width': video_width,
  630. 'video_height': video_height,
  631. 'duration': video_duration,
  632. 'publish_time_stamp': publish_time,
  633. 'publish_time_str': publish_time_str,
  634. 'is_top': is_top,
  635. 'user_name': user_name,
  636. 'user_id': user_id,
  637. 'avatar_url': avatar_url,
  638. 'cover_url': cover_url,
  639. 'audio_url': audio_url,
  640. 'video_url': video_url,
  641. 'session': signature}
  642. for k, v in video_dict.items():
  643. Common.logger(log_type, crawler).info(f"{k}:{v}")
  644. cls.download_publish(log_type=log_type,
  645. crawler=crawler,
  646. video_dict=video_dict,
  647. strategy=strategy,
  648. our_uid=our_uid,
  649. oss_endpoint=oss_endpoint,
  650. env=env,
  651. machine=machine)
  652. # 下载 / 上传
  653. @classmethod
  654. def download_publish(cls, log_type, crawler, strategy, video_dict, our_uid, oss_endpoint, env, machine):
  655. # try:
  656. if cls.download_rule(video_dict['duration'], video_dict['video_width'], video_dict['video_height']) is False:
  657. Common.logger(log_type, crawler).info('不满足抓取规则\n')
  658. elif any(word if word in video_dict['video_title'] else False for word in cls.filter_words(log_type, crawler)) is True:
  659. Common.logger(log_type, crawler).info('标题已中过滤词:{}\n', video_dict['video_title'])
  660. elif str(video_dict['video_id']) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'e075e9') for x in y]:
  661. Common.logger(log_type, crawler).info('视频已下载\n')
  662. elif str(video_dict['video_id']) in [x for y in Feishu.get_values_batch(log_type, 'xigua', '3Ul6wZ') for x in y]:
  663. Common.logger(log_type, crawler).info('视频已下载\n')
  664. elif str(video_dict['video_id']) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'QOWqMo') for x in y]:
  665. Common.logger(log_type, crawler).info('视频已下载\n')
  666. elif str(video_dict['video_id']) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'wjhpDs') for x in y]:
  667. Common.logger(log_type, crawler).info('视频已存在\n')
  668. else:
  669. # 下载封面
  670. Common.download_method(log_type=log_type, crawler=crawler, text='cover', title=video_dict['video_title'], url=video_dict['cover_url'])
  671. # 下载视频
  672. Common.download_method(log_type=log_type, crawler=crawler, text='xigua_video', title=video_dict['video_title'], url=video_dict['video_url'])
  673. # 下载音频
  674. Common.download_method(log_type=log_type, crawler=crawler, text='xigua_audio', title=video_dict['video_title'], url=video_dict['audio_url'])
  675. # 保存视频信息至txt
  676. Common.save_video_info(log_type=log_type, crawler=crawler, video_dict=video_dict)
  677. # 合成音视频
  678. Common.video_compose(log_type=log_type, crawler=crawler, video_dir=f"./{crawler}/videos/{video_dict['video_title']}")
  679. # 上传视频
  680. Common.logger(log_type, crawler).info("开始上传视频...")
  681. our_video_id = Publish.upload_and_publish(log_type=log_type,
  682. crawler=crawler,
  683. strategy=strategy,
  684. our_uid=our_uid,
  685. env=env,
  686. oss_endpoint=oss_endpoint)
  687. if env == 'dev':
  688. our_video_link = f"https://testadmin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
  689. else:
  690. our_video_link = f"https://admin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
  691. Common.logger(log_type, crawler).info("视频上传完成")
  692. if our_video_id is None:
  693. # 删除视频文件夹
  694. shutil.rmtree(f"./{crawler}/videos/{video_dict['video_title']}/")
  695. return
  696. # 视频写入飞书
  697. Feishu.insert_columns(log_type, 'xigua', "e075e9", "ROWS", 1, 2)
  698. upload_time = int(time.time())
  699. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(upload_time)),
  700. "定向榜",
  701. video_dict['video_title'],
  702. str(video_dict['video_id']),
  703. our_video_link,
  704. video_dict['gid'],
  705. video_dict['play_cnt'],
  706. video_dict['comment_cnt'],
  707. video_dict['like_cnt'],
  708. video_dict['share_cnt'],
  709. video_dict['duration'],
  710. str(video_dict['video_width']) + '*' + str(video_dict['video_height']),
  711. video_dict['publish_time_str'],
  712. video_dict['user_name'],
  713. video_dict['user_id'],
  714. video_dict['avatar_url'],
  715. video_dict['cover_url'],
  716. video_dict['video_url'],
  717. video_dict['audio_url']]]
  718. time.sleep(1)
  719. Feishu.update_values(log_type, 'xigua', "e075e9", "F2:Z2", values)
  720. Common.logger(log_type, crawler).info(f"视频已保存至云文档\n")
  721. # 视频信息保存数据库
  722. sql = f""" insert into crawler_video(video_id,
  723. user_id,
  724. out_user_id,
  725. platform,
  726. strategy,
  727. out_video_id,
  728. video_title,
  729. cover_url,
  730. video_url,
  731. duration,
  732. publish_time,
  733. play_cnt,
  734. crawler_rule,
  735. width,
  736. height)
  737. values({our_video_id},
  738. "{our_uid}",
  739. "{video_dict['user_id']}",
  740. "{cls.platform}",
  741. "定向爬虫策略",
  742. "{video_dict['video_id']}",
  743. "{video_dict['video_title']}",
  744. "{video_dict['cover_url']}",
  745. "{video_dict['video_url']}",
  746. {int(video_dict['duration'])},
  747. "{video_dict['publish_time_str']}",
  748. {int(video_dict['play_cnt'])},
  749. "4,5,6",
  750. {int(video_dict['video_width'])},
  751. {int(video_dict['video_height'])}) """
  752. MysqlHelper.update_values(log_type, crawler, sql, env, machine)
  753. Common.logger(log_type, crawler).info('视频信息插入数据库成功!\n')
  754. # except Exception as e:
  755. # Common.logger(log_type, crawler).error(f'download_publish异常:{e}\n')
  756. if __name__ == '__main__':
  757. # print(Follow.get_signature("follow", "xigua", "95420624045", "local"))
  758. Follow.get_videolist("follow", "xigua", "95420624045", "local")
  759. pass