xigua_follow.py 41 KB

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