xigua_follow.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/8/23
  4. import base64
  5. import os
  6. import random
  7. import subprocess
  8. import sys
  9. import time
  10. import requests
  11. import urllib3
  12. sys.path.append(os.getcwd())
  13. from main.common import Common
  14. from main.feishu import Feishu
  15. from main.publish import Publish
  16. from main.get_signature import GetSignature
  17. proxies = {"http": None, "https": None}
  18. class Follow:
  19. # 个人主页视频翻页参数
  20. offset = 0
  21. # 获取用户信息(字典格式). 注意:部分 user_id 字符类型是 int / str
  22. @classmethod
  23. def get_user_info_from_feishu(cls, log_type):
  24. try:
  25. user_sheet = Feishu.get_values_batch(log_type, 'xigua', '5tlTYB')
  26. user_dict = {}
  27. for i in range(1, len(user_sheet)):
  28. user_name = user_sheet[i][0]
  29. user_id = user_sheet[i][1]
  30. our_id = user_sheet[i][3]
  31. if user_name is None or user_id is None or our_id is None:
  32. pass
  33. else:
  34. user_dict[user_name] = str(user_id)+','+str(our_id)
  35. return user_dict
  36. except Exception as e:
  37. Common.logger(log_type).error('get_user_id_from_feishu异常:{}', e)
  38. # 下载规则
  39. @staticmethod
  40. def download_rule(duration, width, height):
  41. if int(duration) >= 60:
  42. if int(width) >= 720 or int(height) >= 720:
  43. return True
  44. else:
  45. return False
  46. else:
  47. return False
  48. # 过滤词库
  49. @classmethod
  50. def filter_words(cls, log_type):
  51. try:
  52. filter_words_sheet = Feishu.get_values_batch(log_type, 'xigua', 'KGB4Hc')
  53. filter_words_list = []
  54. for x in filter_words_sheet:
  55. for y in x:
  56. if y is None:
  57. pass
  58. else:
  59. filter_words_list.append(y)
  60. return filter_words_list
  61. except Exception as e:
  62. Common.logger(log_type).error('filter_words异常:{}', e)
  63. # PC端:西瓜用户主页视频列表. 注意:参数_signature有效期时长只有一小时
  64. @classmethod
  65. def get_follow_feeds_by_pc(cls, log_type, userid):
  66. try:
  67. url = "https://www.ixigua.com/api/videov2/author/new_video_list?"
  68. headers = {
  69. 'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
  70. 'accept': 'application/json, text/plain, */*',
  71. 'sec-ch-ua-mobile': '?0',
  72. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko)'
  73. ' Chrome/103.0.0.0 Safari/537.36',
  74. 'sec-ch-ua-platform': '"macOS"',
  75. 'sec-fetch-site': 'same-origin',
  76. 'sec-fetch-mode': 'cors',
  77. 'sec-fetch-dest': 'document',
  78. 'referer': 'https://www.ixigua.com/home/' + str(userid),
  79. 'accept-encoding': 'gzip, deflate, br',
  80. 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
  81. }
  82. params = {
  83. 'to_user_id': str(userid),
  84. 'offset': str(cls.offset),
  85. 'limit': '30',
  86. 'maxBehotTime': '0',
  87. 'order': 'new',
  88. 'isHome': '0',
  89. 'msToken': '2ZHINOMBPK-qlCKApv37xVCBKkXyPli8mTYNlTSXvr17eZ0Ea8B__Otimkx6q_enDc9m8Kgzi3Re7wpLIMSSE9dofTYdqQgvB7mHQbx_AMnVnf5lsByU',
  90. 'X-Bogus': 'DFSzswVuVvTANe2BSBBMCR/F6qyc',
  91. '_signature': Feishu.get_values_batch(log_type, 'xigua', '6tZHhs')[1][1],
  92. }
  93. cookies = {
  94. '__ac_signature': '_02B4Z6wo00f017vzS8QAAIDCwz2gwwDpX9-7009AAI4Bc4',
  95. 'MONITOR_WEB_ID': 'fd4244aa-2003-4e19-a2a4-715c19310a56',
  96. 'ixigua-a-s': '1',
  97. 'support_webp': 'true',
  98. 'support_avif': 'true',
  99. '_tea_utm_cache_1300': 'undefined',
  100. 'ttcid': '16a3b6b9b80b4a87ae258f5f3f101e6310',
  101. 'msToken': 'G8pL2oH-9Zl1hrLZPyOMSceMaII3ejKda2o-tgO1heYrj7b_fgm9vGlvwyLOA2H8oUShZgAYfxEvIuktT7OuxBuy85N-ousFfqxuAIrfruMEFZUTYp2z',
  102. 'tt_scid': 'a0zhISPImN-dVMMdbeb1Kzhl1x4oJS5Yr81FzH6qYk3jDtj1d2E5gsywN4rwna8ib398',
  103. 'ttwid': '1%7CvorN1HQjbSgBViRkEoZYEbqP_sQVoQqaUqGcFA-bzpA%7C1661324763%7Ce040213e1107973ebb0db64f0e77cfb027375f1fb5854bb40588d692d025af1f',
  104. }
  105. urllib3.disable_warnings()
  106. response = requests.get(url=url, headers=headers, params=params, cookies=cookies, proxies=proxies, verify=False)
  107. # Common.logger(log_type).info('response:{}', response.text)
  108. cls.offset += 30
  109. if 'data' not in response.text or response.json()['data'] == '' or response.json()['code'] != 200:
  110. Common.logger(log_type).info('get_follow_feeds: response:{}', response.text)
  111. else:
  112. feeds = response.json()['data']['videoList']
  113. # print(len(feeds))
  114. for i in range(len(feeds)):
  115. # video_title
  116. if 'title' not in feeds[i]:
  117. video_title = 0
  118. else:
  119. video_title = feeds[i]['title'].strip().replace('手游', '')\
  120. .replace('/', '').replace('\/', '').replace('\n', '')
  121. # video_id
  122. if 'video_id' not in feeds[i]:
  123. video_id = 0
  124. else:
  125. video_id = feeds[i]['video_id']
  126. # gid
  127. if 'gid' not in feeds[i]:
  128. gid = 0
  129. else:
  130. gid = feeds[i]['gid']
  131. # play_cnt
  132. if 'video_detail_info' not in feeds[i]:
  133. play_cnt = 0
  134. elif 'video_watch_count' not in feeds[i]['video_detail_info']:
  135. play_cnt = 0
  136. else:
  137. play_cnt = feeds[i]['video_detail_info']['video_watch_count']
  138. # comment_cnt
  139. if 'comment_count' not in feeds[i]:
  140. comment_cnt = 0
  141. else:
  142. comment_cnt = feeds[i]['comment_count']
  143. # like_cnt
  144. if 'digg_count' not in feeds[i]:
  145. like_cnt = 0
  146. else:
  147. like_cnt = feeds[i]['digg_count']
  148. # share_cnt
  149. share_cnt = 0
  150. # video_duration
  151. if 'video_duration' not in feeds[i]:
  152. video_duration = 0
  153. else:
  154. video_duration = feeds[i]['video_duration']
  155. # send_time
  156. if 'publish_time' not in feeds[i]:
  157. send_time = 0
  158. else:
  159. send_time = feeds[i]['publish_time']
  160. # is_top
  161. if 'is_top' not in feeds[i]:
  162. is_top = 0
  163. else:
  164. is_top = feeds[i]['is_top']
  165. # user_name
  166. if 'user_info' not in feeds[i]:
  167. user_name = 0
  168. elif 'name' not in feeds[i]['user_info']:
  169. user_name = 0
  170. else:
  171. user_name = feeds[i]['user_info']['name']
  172. # user_id
  173. if 'user_info' not in feeds[i]:
  174. user_id = 0
  175. elif 'user_id' not in feeds[i]['user_info']:
  176. user_id = 0
  177. else:
  178. user_id = feeds[i]['user_info']['user_id']
  179. # head_url
  180. if 'user_info' not in feeds[i]:
  181. head_url = 0
  182. elif 'avatar_url' not in feeds[i]['user_info']:
  183. head_url = 0
  184. else:
  185. head_url = feeds[i]['user_info']['avatar_url']
  186. # cover_url
  187. if 'video_detail_info' not in feeds[i]:
  188. cover_url = 0
  189. elif 'detail_video_large_image' not in feeds[i]['video_detail_info']:
  190. cover_url = 0
  191. elif 'url' in feeds[i]['video_detail_info']['detail_video_large_image']:
  192. cover_url = feeds[i]['video_detail_info']['detail_video_large_image']['url']
  193. else:
  194. cover_url = feeds[i]['video_detail_info']['detail_video_large_image']['url_list'][0]['url']
  195. video_url_info = cls.get_video_info(log_type, gid)
  196. video_width = video_url_info[2]
  197. video_height = video_url_info[-1]
  198. video_url = video_url_info[0]
  199. audio_url = video_url_info[1]
  200. Common.logger(log_type).info('video_title:{}', video_title)
  201. Common.logger(log_type).info('video_id:{}', video_id)
  202. Common.logger(log_type).info('play_cnt:{}', play_cnt)
  203. # Common.logger(log_type).info('is_top:{}, {}', type(is_top), is_top)
  204. Common.logger(log_type).info('send_time:{}',
  205. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)))
  206. if gid == 0 or video_id == 0:
  207. Common.logger(log_type).info('无效视频\n')
  208. elif is_top is True and int(time.time()) - int(send_time) > 3600 * 24 * 10:
  209. Common.logger(log_type).info('置顶视频,且发布时间超过10天:{}\n',
  210. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)))
  211. elif int(time.time()) - int(send_time) > 3600 * 24 * 10:
  212. Common.logger(log_type).info('发布时间超过10天:{}\n',
  213. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)))
  214. cls.offset = 0
  215. return
  216. elif cls.download_rule(video_duration, video_width, video_height) is False:
  217. Common.logger(log_type).info('不满足抓取规则\n')
  218. elif any(word if word in video_title else False for word in cls.filter_words(log_type)) is True:
  219. Common.logger(log_type).info('标题已中过滤词:{}\n', video_title)
  220. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'e075e9') for x in y]:
  221. Common.logger(log_type).info('视频已下载\n')
  222. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'wjhpDs') for x in y]:
  223. Common.logger(log_type).info('视频已存在\n')
  224. else:
  225. Feishu.insert_columns(log_type, 'xigua', 'wjhpDs', 'ROWS', 1, 2)
  226. get_feeds_time = time.time()
  227. values = [[time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(get_feeds_time)),
  228. '关注榜',
  229. video_title,
  230. str(video_id),
  231. gid,
  232. play_cnt,
  233. comment_cnt,
  234. like_cnt,
  235. share_cnt,
  236. video_duration,
  237. str(video_width) + '*' + str(video_height),
  238. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)),
  239. user_name,
  240. user_id,
  241. head_url,
  242. cover_url,
  243. video_url,
  244. audio_url]]
  245. time.sleep(1)
  246. Feishu.update_values(log_type, 'xigua', 'wjhpDs', 'A2:Z2', values)
  247. Common.logger(log_type).info('视频信息写入飞书成功\n')
  248. time.sleep(random.randint(1, 3))
  249. except Exception as e:
  250. Common.logger(log_type).error('get_follow_feeds_by_pc异常:{}\n', e)
  251. # 获取视频详情
  252. @classmethod
  253. def get_video_info(cls, log_type, gid):
  254. try:
  255. url = 'https://www.ixigua.com/api/mixVideo/information?'
  256. headers = {
  257. "accept-encoding": "gzip, deflate, br",
  258. "accept-language": "zh-CN,zh-Hans;q=0.9",
  259. "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
  260. "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15",
  261. "referer": "https://www.ixigua.com/7102614741050196520?logTag=0531c88ac04f38ab2c62",
  262. }
  263. params = {
  264. 'mixId': gid,
  265. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfC'
  266. 'NVVIOBNjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  267. 'X-Bogus': 'DFSzswVupYTANCJOSBk0P53WxM-r',
  268. '_signature': '_02B4Z6wo0000119LvEwAAIDCuktNZ0y5wkdfS7jAALThuOR8D9yWNZ.EmWHKV0WSn6Px'
  269. 'fPsH9-BldyxVje0f49ryXgmn7Tzk-swEHNb15TiGqa6YF.cX0jW8Eds1TtJOIZyfc9s5emH7gdWN94',
  270. }
  271. cookies = {
  272. 'ixigua-a-s': '1',
  273. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfCNVVIOB'
  274. 'NjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  275. 'ttwid': '1%7C_yXQeHWwLZgCsgHClOwTCdYSOt_MjdOkgnPIkpi-Sr8%7C1661241238%7Cf57d0c5ef3f1d7'
  276. '6e049fccdca1ac54887c34d1f8731c8e51a49780ff0ceab9f8',
  277. 'tt_scid': 'QZ4l8KXDG0YAEaMCSbADdcybdKbUfG4BC6S4OBv9lpRS5VyqYLX2bIR8CTeZeGHR9ee3',
  278. 'MONITOR_WEB_ID': '0a49204a-7af5-4e96-95f0-f4bafb7450ad',
  279. '__ac_nonce': '06304878000964fdad287',
  280. '__ac_signature': '_02B4Z6wo00f017Rcr3AAAIDCUVxeW1tOKEu0fKvAAI4cvoYzV-wBhq7B6D8k0no7lb'
  281. 'FlvYoinmtK6UXjRIYPXnahUlFTvmWVtb77jsMkKAXzAEsLE56m36RlvL7ky.M3Xn52r9t1IEb7IR3ke8',
  282. 'ttcid': 'e56fabf6e85d4adf9e4d91902496a0e882',
  283. '_tea_utm_cache_1300': 'undefined',
  284. 'support_avif': 'false',
  285. 'support_webp': 'false',
  286. 'xiguavideopcwebid': '7134967546256016900',
  287. 'xiguavideopcwebid.sig': 'xxRww5R1VEMJN_dQepHorEu_eAc',
  288. }
  289. urllib3.disable_warnings()
  290. response = requests.get(url=url, headers=headers, params=params, cookies=cookies, proxies=proxies,
  291. verify=False)
  292. if 'data' not in response.json() or response.json()['data'] == '':
  293. Common.logger(log_type).warning('get_video_info: response: {}', response)
  294. else:
  295. video_info = response.json()['data']['gidInformation']['packerData']['video']
  296. video_url = ''
  297. audio_url = ''
  298. video_width = ''
  299. video_height = ''
  300. # video_url
  301. if 'videoResource' not in video_info:
  302. video_url = 0
  303. audio_url = 0
  304. video_width = 0
  305. video_height = 0
  306. elif 'dash' in video_info['videoResource']:
  307. video_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  308. 'main_url']
  309. audio_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_audio_list'][-1][
  310. 'main_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']['dash']['dynamic_video']['dynamic_video_list'][-1][
  314. 'vwidth']
  315. video_height = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  316. 'vheight']
  317. elif 'normal' in video_info['videoResource']:
  318. video_list = video_info['videoResource']['normal']['video_list']
  319. if 'video_4' in video_list.keys():
  320. # Common.logger(log_type).info('{}', video_list['video_4'])
  321. video_url = video_list['video_4']['main_url']
  322. audio_url = video_list['video_4']['main_url']
  323. video_url = base64.b64decode(video_url).decode('utf8')
  324. audio_url = base64.b64decode(audio_url).decode('utf8')
  325. video_width = video_list['video_4']['vwidth']
  326. video_height = video_list['video_4']['vheight']
  327. elif 'video_3' in video_list.keys():
  328. # Common.logger(log_type).info('{}', video_list['video_3'])
  329. video_url = video_list['video_3']['main_url']
  330. audio_url = video_list['video_3']['main_url']
  331. video_url = base64.b64decode(video_url).decode('utf8')
  332. audio_url = base64.b64decode(audio_url).decode('utf8')
  333. video_width = video_list['video_3']['vwidth']
  334. video_height = video_list['video_3']['vheight']
  335. elif 'video_2' in video_list.keys():
  336. # Common.logger(log_type).info('{}', video_list['video_2'])
  337. video_url = video_list['video_2']['main_url']
  338. audio_url = video_list['video_2']['main_url']
  339. video_url = base64.b64decode(video_url).decode('utf8')
  340. audio_url = base64.b64decode(audio_url).decode('utf8')
  341. video_width = video_list['video_2']['vwidth']
  342. video_height = video_list['video_2']['vheight']
  343. elif 'video_1' in video_list.keys():
  344. # Common.logger(log_type).info('{}', video_list['video_1'])
  345. video_url = video_list['video_1']['main_url']
  346. audio_url = video_list['video_1']['main_url']
  347. video_url = base64.b64decode(video_url).decode('utf8')
  348. audio_url = base64.b64decode(audio_url).decode('utf8')
  349. video_width = video_list['video_1']['vwidth']
  350. video_height = video_list['video_1']['vheight']
  351. else:
  352. video_url = 0
  353. audio_url = 0
  354. video_width = 0
  355. video_height = 0
  356. return video_url, audio_url, video_width, video_height
  357. except Exception as e:
  358. Common.logger(log_type).error('get_video_info异常:{}', e)
  359. # APP端:西瓜视频用户主页
  360. @classmethod
  361. def get_follow_feeds_by_app(cls, log_type, userid):
  362. while True:
  363. try:
  364. url = "https://api5-normal-quic-lq.ixigua.com/video/app/user/videolist_tab/v3/?"
  365. headers = {
  366. 'Host': 'api5-normal-quic-lq.ixigua.com',
  367. 'Cookie': 'passport_csrf_token=9dc29668504aefd8f810d194c1591b27; passport_csrf_token_default=9dc29668504aefd8f810d194c1591b27; d_ticket=8cc008f231ad00a57481e490f82f4bedebe99; n_mh=Zi1ukqZaOfwMQ8RKEEaBFHPd94g9LJFrf_5jskG0uhY; odin_tt=79986f6d46fe14e0f0cf5c6d831005ef2d2ba797151d32eb7678d9ec14a770349dcc7f5cce1746a00dc493838a94db296ef2135712d40b5de1b4ebb170e7e3bf; sessionid=cd61dd6003146ce5b8d19b1eeb29d5b6; sessionid_ss=cd61dd6003146ce5b8d19b1eeb29d5b6; sid_guard=cd61dd6003146ce5b8d19b1eeb29d5b6%7C1661320113%7C5184000%7CSun%2C+23-Oct-2022+05%3A48%3A33+GMT; sid_tt=cd61dd6003146ce5b8d19b1eeb29d5b6; uid_tt=6544aadbdc13b980ab4906f550c70af5; uid_tt_ss=6544aadbdc13b980ab4906f550c70af5; install_id=541373572069224; ttreq=1$27a2ec895a960525ef828e684768bef579920543; msToken=6hA48Lf7RVYOl0Okgng_KQzBwfUpN2M5tB6opL8N6YB3EX0VsNQNhGH4kT-vRxO3Yjac8E4w7Zk4rkFF5JCRTilK',
  368. 'x-tt-token': '00cd61dd6003146ce5b8d19b1eeb29d5b603e056899dfc41b69bf336d3ce3bfc61b2822bbd85f84cfdfb3bf876b7bb71ea85363bff7cb21186b571d3418b30838538c78e169a0db8500261060669094c3ed23032496d65f19a0fa66fc54cc4eed2c55-1.0.1',
  369. 'request-startime': '683091411.831285',
  370. 'x-vc-bdturing-sdk-version': '2.2.8',
  371. 'x-ss-cookie': 'install_id=541373572069224; msToken=6hA48Lf7RVYOl0Okgng_KQzBwfUpN2M5tB6opL8N6YB3EX0VsNQNhGH4kT-vRxO3Yjac8E4w7Zk4rkFF5JCRTilK; ttreq=1$27a2ec895a960525ef828e684768bef579920543; d_ticket=8cc008f231ad00a57481e490f82f4bedebe99; n_mh=Zi1ukqZaOfwMQ8RKEEaBFHPd94g9LJFrf_5jskG0uhY; odin_tt=79986f6d46fe14e0f0cf5c6d831005ef2d2ba797151d32eb7678d9ec14a770349dcc7f5cce1746a00dc493838a94db296ef2135712d40b5de1b4ebb170e7e3bf; sessionid=cd61dd6003146ce5b8d19b1eeb29d5b6; sessionid_ss=cd61dd6003146ce5b8d19b1eeb29d5b6; sid_guard=cd61dd6003146ce5b8d19b1eeb29d5b6%7C1661320113%7C5184000%7CSun%2C+23-Oct-2022+05%3A48%3A33+GMT; sid_tt=cd61dd6003146ce5b8d19b1eeb29d5b6; uid_tt=6544aadbdc13b980ab4906f550c70af5; uid_tt_ss=6544aadbdc13b980ab4906f550c70af5; passport_csrf_token=9dc29668504aefd8f810d194c1591b27; passport_csrf_token_default=9dc29668504aefd8f810d194c1591b27',
  372. 'tt-request-time': '1661398611831',
  373. 'user-agent': 'Video 6.8.8 rv:6.8.8.12 (iPhone; iOS 14.7.1; zh_CN) Cronet',
  374. 'sdk-version': '2',
  375. 'x-tt-dt': 'AAARLMRFIGV63HLKR2OFYMAN4ECX3S3FF7T6VF3ZUGZVJHJRTAR6TZ6TXKNYXU5US4L72542CDEO4CJAORJUPSELHB52LINBZAWN7DIMVSPRKPKSIJYA2S2ZS7PIYZQBQ3OFWJETR35OAD55FXYP6OY',
  376. 'passport-sdk-version': '5.14.3',
  377. 'x-bd-kmsv': '1',
  378. 'x-ss-dp': '32',
  379. 'x-tt-trace-id': '00-d312f8fb0dae06939d00507998be0020-d312f8fb0dae0693-01',
  380. 'x-argus': 'OoPWDUi7xa1FAheuXaB4U+12sViNA+0vZEq7RpA1HvKF5CreKftmWWAtl1ndNdJNbk4zPogps8WNxsRJWdgZOzLg5CUTwVWrMQ/ptLgYrFTXbKf4P4CpqSRoJEHca/LVYRXUrTxTsi+AS7u/S3BTCrzm6nwvZB43GyiLGyN1W38poinJoMkPltgUNoSkAilVXCTu3iSWFLUYayOF7MwFRnYFxU4vBu+XmYCtl74XVCCARZD6uYf/cjkIH9wRD+uv0HBNlI70mqjaQOTYtlINi2i61yctngEjgwpV6s+4GLWQQYY6KXq+eu9mEppFDLSI9WY=',
  381. 'x-gorgon': '8404e06000002dfc1ace57427120b4f72a226ce677bde6d67b92',
  382. 'x-khronos': '1661398611',
  383. 'x-ladon': '7bRfCQvXSDeU17k7XA6Y7TSO0rsUmxbxtqt+apKfuSx/juZZ'
  384. }
  385. params = {
  386. 'anti_addiction_model': '0',
  387. 'version_code': '10.8.8',
  388. 'app_name': 'video_article',
  389. 'device_id': '3061492313228551',
  390. 'channel': 'App%20Store',
  391. 'resolution': '828*1792',
  392. 'aid': '32',
  393. 'ab_feature': 'z1',
  394. 'ab_version': '668851,4601580,668854,4594840,4601552,4622288,4641673,668858,4601444,668859,4601563,668856,4601562,668855,4601507,668853,4601558,668852,4601533',
  395. 'update_version_code': '108812',
  396. 'cdid': '7425DF80-0324-4CEF-AAEC-6596F45F2C7A',
  397. 'ac': 'WIFI',
  398. 'os_version': '14.7.1',
  399. 'user_version': '6.8.8',
  400. 'ssmix': 'a',
  401. 'ipad_adapter_enable': '0',
  402. 'device_platform': 'iphone',
  403. 'iid': '541373572069224',
  404. 'device_type': 'iPhone%2011',
  405. 'ab_client': 'a1,f2,f7,e1',
  406. 'cdid_ts': '1661312788',
  407. 'offset': str(cls.offset),
  408. 'orderby': 'publishtime',
  409. 'to_user_id': userid,
  410. 'count': '20',
  411. 'language': 'zh-Hans-CN',
  412. 'loc_mode': '0',
  413. 'ab_version_vid_list': '4413540%2C2190089',
  414. 'enable_publish_status': '0',
  415. 'play_param': 'codec_type%3A7%2Cenable_dash%3A1%2Cresolution%3A828%2A1792%2Cis_order_flow%3A-1%2Cis_hdr%3A1',
  416. 'client_extra': '%7B%22last_ad_position%22%3A-1%7D',
  417. }
  418. urllib3.disable_warnings()
  419. response = requests.get(url=url, headers=headers, params=params, proxies=proxies, verify=False)
  420. cls.offset += 30
  421. if 'data' not in response.text or response.json()['code'] != 0 or len(response.json()['data']) == 0:
  422. Common.logger(log_type).warning('get_follow_feeds_by_app: response: {}', response.text)
  423. else:
  424. feeds = response.json()['data']
  425. for i in range(len(feeds)):
  426. # video_title
  427. if 'title' in feeds[i]:
  428. video_title = feeds[i]['title'].strip().replace('手游', '')
  429. else:
  430. video_title = 0
  431. # video_id
  432. if 'video_id' in feeds[i]:
  433. video_id = feeds[i]['video_id']
  434. else:
  435. video_id = 0
  436. # gid
  437. if 'gid' in feeds[i]:
  438. gid = feeds[i]['gid']
  439. else:
  440. gid = 0
  441. # play_cnt
  442. if 'video_detail_info' not in feeds[i]:
  443. play_cnt = 0
  444. elif 'video_watch_count' not in feeds[i]['video_detail_info']:
  445. play_cnt = 0
  446. else:
  447. play_cnt = feeds[i]['video_detail_info']['video_watch_count']
  448. # comment_cnt
  449. if 'comment_count' in feeds[i]:
  450. comment_count = feeds[i]['comment_count']
  451. else:
  452. comment_count = 0
  453. # like_cnt
  454. if 'digg_count' in feeds[i]:
  455. like_cnt = feeds[i]['digg_count']
  456. else:
  457. like_cnt = 0
  458. # share_cnt
  459. if 'share_count' in feeds[i]:
  460. share_cnt = feeds[i]['share_count']
  461. else:
  462. share_cnt = 0
  463. # video_duration
  464. if 'video_duration' in feeds[i]:
  465. video_duration = feeds[i]['video_duration']
  466. else:
  467. video_duration = 0
  468. # send_time
  469. if 'publish_time' in feeds[i]:
  470. send_time = feeds[i]['publish_time']
  471. else:
  472. send_time = 0
  473. # user_name
  474. if 'user_info' not in feeds[i]:
  475. user_name = 0
  476. elif 'name' not in feeds[i]['user_info']:
  477. user_name = 0
  478. else:
  479. user_name = feeds[i]['user_info']['name']
  480. # user_id
  481. if 'user_info' not in feeds[i]:
  482. user_id = 0
  483. elif 'user_id' not in feeds[i]['user_info']:
  484. user_id = 0
  485. else:
  486. user_id = feeds[i]['user_info']['user_id']
  487. # head_url
  488. if 'user_info' not in feeds[i]:
  489. head_url = 0
  490. elif 'avatar_url' not in feeds[i]['user_info']:
  491. head_url = 0
  492. else:
  493. head_url = feeds[i]['user_info']['avatar_url']
  494. # cover_url
  495. if 'video_detail_info' not in feeds[i]:
  496. cover_url = 0
  497. elif 'detail_video_large_image' not in feeds[i]['video_detail_info']:
  498. cover_url = 0
  499. elif 'url' not in feeds[i]['video_detail_info']['detail_video_large_image']:
  500. cover_url = 0
  501. else:
  502. cover_url = feeds[i]['video_detail_info']['detail_video_large_image']['url']
  503. url_info = cls.get_video_info(log_type, gid)
  504. video_url = url_info[0]
  505. audio_url = url_info[1]
  506. video_width = url_info[2]
  507. video_height = url_info[3]
  508. Common.logger(log_type).info('video_title:{}', video_title)
  509. Common.logger(log_type).info('video_id:{}', video_id)
  510. Common.logger(log_type).info('play_cnt:{}', play_cnt)
  511. Common.logger(log_type).info('video_duration:{}', video_duration)
  512. Common.logger(log_type).info('video_width_height:{}', str(video_width) + '*' + str(video_height))
  513. Common.logger(log_type).info('send_time:{}',
  514. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)))
  515. if gid == 0 or video_url == 0 or audio_url == 0:
  516. Common.logger(log_type).info('无效视频:{}\n', video_title)
  517. elif int(time.time()) - int(send_time) > 3600 * 24 * 10:
  518. Common.logger(log_type).info('发布时间超过10天:{}\n', time.strftime('%Y/%m/%d %H:%M:%S'),
  519. time.localtime(send_time))
  520. cls.offset = 0
  521. return
  522. elif cls.download_rule(video_duration, video_width, video_height) is False:
  523. Common.logger(log_type).info('不满足抓取规则\n')
  524. elif any(word if word in video_title else False for word in cls.filter_words(log_type)) is True:
  525. Common.logger(log_type).info('标题已中过滤词:{}\n', video_title)
  526. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'e075e9') for x in y]:
  527. Common.logger(log_type).info('视频已下载:{}\n', video_title)
  528. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'wjhpDs') for x in y]:
  529. Common.logger(log_type).info('视频已存在:{}\n', video_title)
  530. else:
  531. Feishu.insert_columns(log_type, 'xigua', 'wjhpDs', 'ROWS', 1, 2)
  532. get_feeds_time = int(time.time())
  533. values = [[time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(get_feeds_time)),
  534. '关注榜',
  535. video_title,
  536. str(video_id),
  537. gid,
  538. int(play_cnt),
  539. int(comment_count),
  540. int(like_cnt),
  541. int(share_cnt),
  542. video_duration,
  543. str(video_width) + '*' + str(video_height),
  544. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)),
  545. user_name,
  546. str(user_id),
  547. head_url,
  548. cover_url,
  549. video_url,
  550. audio_url]]
  551. time.sleep(1)
  552. Feishu.update_values(log_type, 'xigua', 'wjhpDs', 'A2:Z2', values)
  553. Common.logger(log_type).info('当前视频信息写入飞书成功\n')
  554. time.sleep(random.randint(1, 3))
  555. except Exception as e:
  556. Common.logger(log_type).error('get_follow_feeds_by_app异常:{}\n', e)
  557. # 获取所有用户主页视频
  558. @classmethod
  559. def get_all_person_videos(cls, log_type, env):
  560. try:
  561. user_list = cls.get_user_info_from_feishu(log_type)
  562. if len(user_list) == 0:
  563. Common.logger(log_type).warning('用户ID列表为空\n')
  564. else:
  565. for k, v in user_list.items():
  566. Common.logger(log_type).info('正在获取 {} 主页视频\n', k)
  567. GetSignature.get_signature('follow')
  568. # cls.get_follow_feeds_by_app(log_type, v.split(',')[0])
  569. cls.get_follow_feeds_by_pc(log_type, v.split(',')[0])
  570. time.sleep(1)
  571. cls.run_download_publish(log_type, env, v.split(',')[-1])
  572. time.sleep(random.randint(5, 10))
  573. except Exception as e:
  574. Common.logger(log_type).error('get_all_person_videos异常:{}\n', e)
  575. # 合并音视频
  576. @classmethod
  577. def video_compose(cls, log_type, video_title):
  578. video_path = './videos/' + str(video_title) + '/video1.mp4'
  579. audio_path = './videos/' + str(video_title) + '/audio1.mp4'
  580. out_path = './videos/' + str(video_title) + '/video.mp4'
  581. cmd = 'ffmpeg -i ' + video_path + ' -i ' + audio_path + ' -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 ' + out_path
  582. # print(cmd)
  583. subprocess.call(cmd, shell=True)
  584. for file in os.listdir('./videos/' + str(video_title)):
  585. if file.split('.mp4')[0] == 'video1' or file.split('.mp4')[0] == 'audio1':
  586. os.remove('./videos/' + str(video_title) + '/' + file)
  587. Common.logger(log_type).info('合成成功')
  588. # 下载 / 上传
  589. @classmethod
  590. def download_publish(cls, log_type, env, uid):
  591. try:
  592. feeds_sheet = Feishu.get_values_batch(log_type, 'xigua', 'wjhpDs')
  593. for i in range(1, len(feeds_sheet)):
  594. download_video_title = feeds_sheet[i][2]
  595. download_video_id = feeds_sheet[i][3]
  596. download_video_gid = feeds_sheet[i][4]
  597. download_play_cnt = feeds_sheet[i][5]
  598. download_comment_cnt = feeds_sheet[i][6]
  599. download_like_cnt = feeds_sheet[i][7]
  600. download_share_cnt = feeds_sheet[i][8]
  601. download_video_duration = feeds_sheet[i][9]
  602. download_video_width_height = feeds_sheet[i][10]
  603. download_send_time = feeds_sheet[i][11]
  604. download_user_name = feeds_sheet[i][12]
  605. download_user_id = feeds_sheet[i][13]
  606. download_head_url = feeds_sheet[i][14]
  607. download_cover_url = feeds_sheet[i][15]
  608. download_video_url = feeds_sheet[i][16]
  609. download_audio_url = feeds_sheet[i][17]
  610. Common.logger(log_type).info('正在判断第{}行:{}', i + 1, download_video_title)
  611. Common.logger(log_type).info('download_video_id:{}', download_video_id)
  612. Common.logger(log_type).info('download_video_duration:{}', download_video_duration)
  613. Common.logger(log_type).info('download_send_time:{}', download_send_time)
  614. # 过滤空行
  615. if download_video_title is None or download_video_id is None:
  616. Feishu.dimension_range(log_type, 'xigua', 'wjhpDs', 'ROWS', i + 1, i + 1)
  617. Common.logger(log_type).info('空行,删除成功\n')
  618. return
  619. elif str(download_video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'e075e9') for x in
  620. y]:
  621. Feishu.dimension_range(log_type, 'xigua', 'wjhpDs', 'ROWS', i + 1, i + 1)
  622. Common.logger(log_type).info('视频已下载,删除成功\n')
  623. return
  624. else:
  625. # 下载封面
  626. Common.download_method(log_type=log_type, text='cover', d_name=download_video_title,
  627. d_url=download_cover_url)
  628. # 下载视频
  629. Common.download_method(log_type=log_type, text='video', d_name=download_video_title,
  630. d_url=download_video_url)
  631. # 下载音频
  632. Common.download_method(log_type=log_type, text='audio', d_name=download_video_title,
  633. d_url=download_audio_url)
  634. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  635. with open("./videos/" + download_video_title + "/" + "info.txt",
  636. "a", encoding="UTF-8") as f_a:
  637. f_a.write(str(download_video_id) + "\n" +
  638. str(download_video_title) + "\n" +
  639. str(download_video_duration) + "\n" +
  640. str(download_play_cnt) + "\n" +
  641. str(download_comment_cnt) + "\n" +
  642. str(download_like_cnt) + "\n" +
  643. str(download_share_cnt) + "\n" +
  644. str(download_video_width_height) + "\n" +
  645. str(int(time.mktime(
  646. time.strptime(download_send_time, "%Y/%m/%d %H:%M:%S")))) + "\n" +
  647. str(download_user_name) + "\n" +
  648. str(download_head_url) + "\n" +
  649. str(download_video_url) + "\n" +
  650. str(download_cover_url) + "\n" +
  651. "xigua"+str(int(time.time())))
  652. Common.logger("follow").info("==========视频信息已保存至info.txt==========")
  653. # 合成音视频
  654. cls.video_compose(log_type, download_video_title)
  655. # 上传视频
  656. Common.logger(log_type).info("开始上传视频:{}".format(download_video_title))
  657. our_video_id = Publish.upload_and_publish(log_type, env, uid)
  658. if env == 'dev':
  659. our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
  660. else:
  661. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
  662. Common.logger(log_type).info("视频上传完成:{}\n", download_video_title)
  663. # 视频ID工作表,插入首行
  664. Feishu.insert_columns(log_type, 'xigua', "e075e9", "ROWS", 1, 2)
  665. # 视频ID工作表,首行写入数据
  666. upload_time = int(time.time())
  667. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
  668. "关注榜",
  669. download_video_title,
  670. str(download_video_id),
  671. our_video_link,
  672. download_video_gid,
  673. download_play_cnt,
  674. download_comment_cnt,
  675. download_like_cnt,
  676. download_share_cnt,
  677. download_video_duration,
  678. download_video_width_height,
  679. download_send_time,
  680. download_user_name,
  681. download_user_id,
  682. download_head_url,
  683. download_cover_url,
  684. download_video_url,
  685. download_audio_url]]
  686. Common.logger(log_type).info('values:{}\n', values)
  687. time.sleep(1)
  688. Feishu.update_values(log_type, 'xigua', "e075e9", "F2:Z2", values)
  689. Common.logger(log_type).info("视频已保存至云文档:{}", download_video_title)
  690. # 删除行或列,可选 ROWS、COLUMNS
  691. Feishu.dimension_range(log_type, 'xigua', "wjhpDs", "ROWS", i + 1, i + 1)
  692. Common.logger(log_type).info("视频:{},下载/上传成功\n", download_video_title)
  693. return
  694. except Exception as e:
  695. Common.logger(log_type).error('download_publish异常:{}\n', e)
  696. Feishu.dimension_range(log_type, 'xigua', "wjhpDs", "ROWS", 2, 2)
  697. # 执行 下载 / 上传
  698. @classmethod
  699. def run_download_publish(cls, log_type, env, uid):
  700. try:
  701. while True:
  702. if len(Feishu.get_values_batch(log_type, 'xigua', 'wjhpDs')) == 1:
  703. Common.logger(log_type).info('下载 / 上传 完成\n')
  704. break
  705. else:
  706. cls.download_publish(log_type, env, uid)
  707. time.sleep(random.randint(1, 3))
  708. except Exception as e:
  709. Common.logger(log_type).error('run_download_publish异常:{}\n', e)
  710. if __name__ == '__main__':
  711. Follow.get_follow_feeds_by_pc('follow', '95420624045')
  712. # Follow.get_follow_feeds_by_app('xigua', '6431477489')
  713. # Follow.get_follow_feeds_by_app('follow', '3865480345435996')
  714. # Follow.get_user_info_from_feishu('follow')
  715. # Follow.filter_words('follow')
  716. # Follow.get_all_person_videos('follow', 'dev')
  717. # Follow.download_publish('follow', 'dev', '6267141')
  718. pass