xigua_follow.py 41 KB

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