xigua_follow_old.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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.xigua_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异常:{}\n', 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异常:{}\n', e)
  63. # PC端:西瓜用户主页视频列表. 注意:参数_signature有效期时长只有一小时
  64. @classmethod
  65. def get_follow_feeds_by_pc(cls, log_type, userid, env, uid):
  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_'
  90. 'enDc9m8Kgzi3Re7wpLIMSSE9dofTYdqQgvB7mHQbx_AMnVnf5lsByU',
  91. 'X-Bogus': 'DFSzswVuVvTANe2BSBBMCR/F6qyc',
  92. '_signature': Feishu.get_values_batch(log_type, 'xigua', '6tZHhs')[1][1],
  93. }
  94. cookies = {
  95. '__ac_signature': '_02B4Z6wo00f017vzS8QAAIDCwz2gwwDpX9-7009AAI4Bc4',
  96. 'MONITOR_WEB_ID': 'fd4244aa-2003-4e19-a2a4-715c19310a56',
  97. 'ixigua-a-s': '1',
  98. 'support_webp': 'true',
  99. 'support_avif': 'true',
  100. '_tea_utm_cache_1300': 'undefined',
  101. 'ttcid': '16a3b6b9b80b4a87ae258f5f3f101e6310',
  102. 'msToken': 'G8pL2oH-9Zl1hrLZPyOMSceMaII3ejKda2o-tgO1heYrj7b_fgm9vGlvwyLOA2H8oUShZgAYfxEvIuktT'
  103. '7OuxBuy85N-ousFfqxuAIrfruMEFZUTYp2z',
  104. 'tt_scid': 'a0zhISPImN-dVMMdbeb1Kzhl1x4oJS5Yr81FzH6qYk3jDtj1d2E5gsywN4rwna8ib398',
  105. 'ttwid': '1%7CvorN1HQjbSgBViRkEoZYEbqP_sQVoQqaUqGcFA-bzpA%7C1661324763%7Ce040213e1107973ebb0d'
  106. 'b64f0e77cfb027375f1fb5854bb40588d692d025af1f',
  107. }
  108. # Common.logger(log_type).info('offset:{}', cls.offset)
  109. urllib3.disable_warnings()
  110. response = requests.get(url=url, headers=headers, params=params,
  111. cookies=cookies, proxies=proxies, verify=False)
  112. # Common.logger(log_type).info('response:{}', response.text)
  113. cls.offset += 30
  114. if 'data' not in response.text or response.json()['data'] == '' or response.json()['code'] != 200:
  115. Common.logger(log_type).info('get_follow_feeds: response:{}', response.text)
  116. else:
  117. feeds = response.json()['data']['videoList']
  118. # print(len(feeds))
  119. for i in range(len(feeds)):
  120. # video_title
  121. if 'title' not in feeds[i]:
  122. video_title = 0
  123. else:
  124. video_title = feeds[i]['title'].strip().replace('手游', '')\
  125. .replace('/', '').replace('\/', '').replace('\n', '')
  126. # video_id
  127. if 'video_id' not in feeds[i]:
  128. video_id = 0
  129. else:
  130. video_id = feeds[i]['video_id']
  131. # gid
  132. if 'gid' not in feeds[i]:
  133. gid = 0
  134. else:
  135. gid = feeds[i]['gid']
  136. # play_cnt
  137. if 'video_detail_info' not in feeds[i]:
  138. play_cnt = 0
  139. elif 'video_watch_count' not in feeds[i]['video_detail_info']:
  140. play_cnt = 0
  141. else:
  142. play_cnt = feeds[i]['video_detail_info']['video_watch_count']
  143. # comment_cnt
  144. if 'comment_count' not in feeds[i]:
  145. comment_cnt = 0
  146. else:
  147. comment_cnt = feeds[i]['comment_count']
  148. # like_cnt
  149. if 'digg_count' not in feeds[i]:
  150. like_cnt = 0
  151. else:
  152. like_cnt = feeds[i]['digg_count']
  153. # share_cnt
  154. share_cnt = 0
  155. # video_duration
  156. if 'video_duration' not in feeds[i]:
  157. video_duration = 0
  158. else:
  159. video_duration = feeds[i]['video_duration']
  160. # send_time
  161. if 'publish_time' not in feeds[i]:
  162. send_time = 0
  163. else:
  164. send_time = feeds[i]['publish_time']
  165. # is_top
  166. if 'is_top' not in feeds[i]:
  167. is_top = 0
  168. else:
  169. is_top = feeds[i]['is_top']
  170. # user_name
  171. if 'user_info' not in feeds[i]:
  172. user_name = 0
  173. elif 'name' not in feeds[i]['user_info']:
  174. user_name = 0
  175. else:
  176. user_name = feeds[i]['user_info']['name']
  177. # user_id
  178. if 'user_info' not in feeds[i]:
  179. user_id = 0
  180. elif 'user_id' not in feeds[i]['user_info']:
  181. user_id = 0
  182. else:
  183. user_id = feeds[i]['user_info']['user_id']
  184. # head_url
  185. if 'user_info' not in feeds[i]:
  186. head_url = 0
  187. elif 'avatar_url' not in feeds[i]['user_info']:
  188. head_url = 0
  189. else:
  190. head_url = feeds[i]['user_info']['avatar_url']
  191. # cover_url
  192. if 'video_detail_info' not in feeds[i]:
  193. cover_url = 0
  194. elif 'detail_video_large_image' not in feeds[i]['video_detail_info']:
  195. cover_url = 0
  196. elif 'url' in feeds[i]['video_detail_info']['detail_video_large_image']:
  197. cover_url = feeds[i]['video_detail_info']['detail_video_large_image']['url']
  198. else:
  199. cover_url = feeds[i]['video_detail_info']['detail_video_large_image']['url_list'][0]['url']
  200. Common.logger(log_type).info(f'video_title:{video_title}')
  201. Common.logger(log_type).info(f'video_id:{video_id}')
  202. Common.logger(log_type).info(f'play_cnt:{play_cnt}')
  203. Common.logger(log_type).info('send_time:{}',
  204. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)))
  205. video_url_info = cls.get_video_info(log_type, gid)
  206. video_url = video_url_info[0]
  207. audio_url = video_url_info[1]
  208. video_width = video_url_info[2]
  209. video_height = video_url_info[-1]
  210. if gid == 0 or video_id == 0:
  211. Common.logger(log_type).info('无效视频\n')
  212. elif is_top is True and int(time.time()) - int(send_time) > 3600 * 24 * 10:
  213. Common.logger(log_type).info('置顶视频,且发布时间超过10天:{}\n',
  214. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)))
  215. elif int(time.time()) - int(send_time) > 3600 * 24 * 10:
  216. Common.logger(log_type).info('发布时间超过10天:{}\n',
  217. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)))
  218. cls.offset = 0
  219. return
  220. elif cls.download_rule(video_duration, video_width, video_height) is False:
  221. Common.logger(log_type).info('不满足抓取规则\n')
  222. elif any(word if word in video_title else False for word in cls.filter_words(log_type)) is True:
  223. Common.logger(log_type).info('标题已中过滤词:{}\n', video_title)
  224. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'e075e9') for x in y]:
  225. Common.logger(log_type).info('视频已下载\n')
  226. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', '3Ul6wZ') for x in y]:
  227. Common.logger(log_type).info('视频已下载\n')
  228. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'wjhpDs') for x in y]:
  229. Common.logger(log_type).info('视频已存在\n')
  230. else:
  231. Feishu.insert_columns(log_type, 'xigua', 'wjhpDs', 'ROWS', 1, 2)
  232. get_feeds_time = time.time()
  233. values = [[time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(get_feeds_time)),
  234. '关注榜',
  235. video_title,
  236. str(video_id),
  237. gid,
  238. play_cnt,
  239. comment_cnt,
  240. like_cnt,
  241. share_cnt,
  242. video_duration,
  243. str(video_width) + '*' + str(video_height),
  244. time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(send_time)),
  245. user_name,
  246. user_id,
  247. head_url,
  248. cover_url,
  249. video_url,
  250. audio_url]]
  251. time.sleep(1)
  252. Feishu.update_values(log_type, 'xigua', 'wjhpDs', 'A2:Z2', values)
  253. Common.logger(log_type).info('视频信息写入飞书成功\n')
  254. time.sleep(random.randint(1, 3))
  255. cls.download_publish(log_type, env, uid)
  256. except Exception as e:
  257. Common.logger(log_type).error('get_follow_feeds_by_pc异常:{}\n', e)
  258. # 获取视频详情
  259. @classmethod
  260. def get_video_info(cls, log_type, gid):
  261. try:
  262. url = 'https://www.ixigua.com/api/mixVideo/information?'
  263. headers = {
  264. "accept-encoding": "gzip, deflate, br",
  265. "accept-language": "zh-CN,zh-Hans;q=0.9",
  266. "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
  267. "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15",
  268. "referer": "https://www.ixigua.com/7102614741050196520?logTag=0531c88ac04f38ab2c62",
  269. }
  270. params = {
  271. 'mixId': gid,
  272. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfC'
  273. 'NVVIOBNjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  274. 'X-Bogus': 'DFSzswVupYTANCJOSBk0P53WxM-r',
  275. '_signature': '_02B4Z6wo0000119LvEwAAIDCuktNZ0y5wkdfS7jAALThuOR8D9yWNZ.EmWHKV0WSn6Px'
  276. 'fPsH9-BldyxVje0f49ryXgmn7Tzk-swEHNb15TiGqa6YF.cX0jW8Eds1TtJOIZyfc9s5emH7gdWN94',
  277. }
  278. cookies = {
  279. 'ixigua-a-s': '1',
  280. 'msToken': 'IlG0wd0Pylyw9ghcYiB2YseUmTwrsrqqhXrbIcsSaTcLTJyVlbYJzk20zw3UO-CfrfCNVVIOB'
  281. 'NjIl7vfBoxnVUwO9ZyzAI3umSKsT5-pef_RRfQCJwmA',
  282. 'ttwid': '1%7C_yXQeHWwLZgCsgHClOwTCdYSOt_MjdOkgnPIkpi-Sr8%7C1661241238%7Cf57d0c5ef3f1d7'
  283. '6e049fccdca1ac54887c34d1f8731c8e51a49780ff0ceab9f8',
  284. 'tt_scid': 'QZ4l8KXDG0YAEaMCSbADdcybdKbUfG4BC6S4OBv9lpRS5VyqYLX2bIR8CTeZeGHR9ee3',
  285. 'MONITOR_WEB_ID': '0a49204a-7af5-4e96-95f0-f4bafb7450ad',
  286. '__ac_nonce': '06304878000964fdad287',
  287. '__ac_signature': '_02B4Z6wo00f017Rcr3AAAIDCUVxeW1tOKEu0fKvAAI4cvoYzV-wBhq7B6D8k0no7lb'
  288. 'FlvYoinmtK6UXjRIYPXnahUlFTvmWVtb77jsMkKAXzAEsLE56m36RlvL7ky.M3Xn52r9t1IEb7IR3ke8',
  289. 'ttcid': 'e56fabf6e85d4adf9e4d91902496a0e882',
  290. '_tea_utm_cache_1300': 'undefined',
  291. 'support_avif': 'false',
  292. 'support_webp': 'false',
  293. 'xiguavideopcwebid': '7134967546256016900',
  294. 'xiguavideopcwebid.sig': 'xxRww5R1VEMJN_dQepHorEu_eAc',
  295. }
  296. urllib3.disable_warnings()
  297. response = requests.get(url=url, headers=headers, params=params, cookies=cookies, proxies=proxies,
  298. verify=False)
  299. if 'data' not in response.json() or response.json()['data'] == '':
  300. Common.logger(log_type).warning('get_video_info: response: {}', response)
  301. else:
  302. video_info = response.json()['data']['gidInformation']['packerData']['video']
  303. video_url = ''
  304. audio_url = ''
  305. video_width = ''
  306. video_height = ''
  307. # video_url
  308. if 'videoResource' not in video_info:
  309. video_url = 0
  310. audio_url = 0
  311. video_width = 0
  312. video_height = 0
  313. elif 'dash' in video_info['videoResource'] \
  314. and 'dynamic_video' in video_info['videoResource']['dash']\
  315. and 'dynamic_video_list' in video_info['videoResource']['dash']['dynamic_video']:
  316. video_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  317. 'main_url']
  318. audio_url = video_info['videoResource']['dash']['dynamic_video']['dynamic_audio_list'][-1][
  319. 'main_url']
  320. Common.logger(log_type).info(f'main_video_url:{video_url}')
  321. Common.logger(log_type).info(f'main_audio_url:{audio_url}\n')
  322. if len(video_url) % 3 == 1:
  323. video_url += '=='
  324. elif len(video_url) % 3 == 2:
  325. video_url += '='
  326. elif len(audio_url) % 3 == 1:
  327. audio_url += '=='
  328. elif len(audio_url) % 3 == 2:
  329. audio_url += '='
  330. Common.logger(log_type).info(f'%_video_url:{video_url}')
  331. Common.logger(log_type).info(f'%_audio_url:{audio_url}\n')
  332. video_url = base64.b64decode(video_url).decode('utf8')
  333. audio_url = base64.b64decode(audio_url).decode('utf8')
  334. Common.logger(log_type).info(f'base64_video_url:{video_url}')
  335. Common.logger(log_type).info(f'base64_audio_url:{audio_url}\n')
  336. video_width = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  337. 'vwidth']
  338. video_height = video_info['videoResource']['dash']['dynamic_video']['dynamic_video_list'][-1][
  339. 'vheight']
  340. elif 'dash_120fps' in video_info['videoResource']\
  341. and 'dynamic_video' in video_info['videoResource']['dash_120fps']\
  342. and 'dynamic_video_list' in video_info['videoResource']['dash_120fps']['dynamic_video']:
  343. video_url = video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_video_list'][-1][
  344. 'main_url']
  345. audio_url = video_info['videoResource']['dash_120fps']['dynamic_video']['dynamic_audio_list'][-1][
  346. 'main_url']
  347. if len(video_url) % 3 == 1:
  348. video_url += '=='
  349. elif len(video_url) % 3 == 2:
  350. video_url += '='
  351. elif len(audio_url) % 3 == 1:
  352. audio_url += '=='
  353. elif len(audio_url) % 3 == 2:
  354. audio_url += '='
  355. video_url = base64.b64decode(video_url).decode('utf8')
  356. audio_url = base64.b64decode(audio_url).decode('utf8')
  357. video_width = video_info['videoResource']['dash_120fps']['dynamic_video'][
  358. 'dynamic_video_list'][-1]['vwidth']
  359. video_height = video_info['videoResource']['dash_120fps']['dynamic_video'][
  360. 'dynamic_video_list'][-1]['vheight']
  361. elif 'normal' in video_info['videoResource']:
  362. video_list = video_info['videoResource']['normal']['video_list']
  363. if 'video_4' in video_list.keys():
  364. # Common.logger(log_type).info('{}', video_list['video_4'])
  365. video_url = video_list['video_4']['main_url']
  366. audio_url = video_list['video_4']['main_url']
  367. if len(video_url) % 3 == 1:
  368. video_url += '=='
  369. elif len(video_url) % 3 == 2:
  370. video_url += '='
  371. elif len(audio_url) % 3 == 1:
  372. audio_url += '=='
  373. elif len(audio_url) % 3 == 2:
  374. audio_url += '='
  375. video_url = base64.b64decode(video_url).decode('utf8')
  376. audio_url = base64.b64decode(audio_url).decode('utf8')
  377. video_width = video_list['video_4']['vwidth']
  378. video_height = video_list['video_4']['vheight']
  379. elif 'video_3' in video_list.keys():
  380. # Common.logger(log_type).info('{}', video_list['video_3'])
  381. video_url = video_list['video_3']['main_url']
  382. audio_url = video_list['video_3']['main_url']
  383. if len(video_url) % 3 == 1:
  384. video_url += '=='
  385. elif len(video_url) % 3 == 2:
  386. video_url += '='
  387. elif len(audio_url) % 3 == 1:
  388. audio_url += '=='
  389. elif len(audio_url) % 3 == 2:
  390. audio_url += '='
  391. video_url = base64.b64decode(video_url).decode('utf8')
  392. audio_url = base64.b64decode(audio_url).decode('utf8')
  393. video_width = video_list['video_3']['vwidth']
  394. video_height = video_list['video_3']['vheight']
  395. elif 'video_2' in video_list.keys():
  396. # Common.logger(log_type).info('{}', video_list['video_2'])
  397. video_url = video_list['video_2']['main_url']
  398. audio_url = video_list['video_2']['main_url']
  399. if len(video_url) % 3 == 1:
  400. video_url += '=='
  401. elif len(video_url) % 3 == 2:
  402. video_url += '='
  403. elif len(audio_url) % 3 == 1:
  404. audio_url += '=='
  405. elif len(audio_url) % 3 == 2:
  406. audio_url += '='
  407. video_url = base64.b64decode(video_url).decode('utf8')
  408. audio_url = base64.b64decode(audio_url).decode('utf8')
  409. video_width = video_list['video_2']['vwidth']
  410. video_height = video_list['video_2']['vheight']
  411. elif 'video_1' in video_list.keys():
  412. # Common.logger(log_type).info('{}', video_list['video_1'])
  413. video_url = video_list['video_1']['main_url']
  414. audio_url = video_list['video_1']['main_url']
  415. if len(video_url) % 3 == 1:
  416. video_url += '=='
  417. elif len(video_url) % 3 == 2:
  418. video_url += '='
  419. elif len(audio_url) % 3 == 1:
  420. audio_url += '=='
  421. elif len(audio_url) % 3 == 2:
  422. audio_url += '='
  423. video_url = base64.b64decode(video_url).decode('utf8')
  424. audio_url = base64.b64decode(audio_url).decode('utf8')
  425. video_width = video_list['video_1']['vwidth']
  426. video_height = video_list['video_1']['vheight']
  427. else:
  428. video_url = 0
  429. audio_url = 0
  430. video_width = 0
  431. video_height = 0
  432. return video_url, audio_url, video_width, video_height
  433. except Exception as e:
  434. Common.logger(log_type).error(f'get_video_info异常:{e}\n')
  435. # 获取所有用户主页视频
  436. @classmethod
  437. def get_all_person_videos(cls, log_type, env):
  438. try:
  439. user_list = cls.get_user_info_from_feishu(log_type)
  440. if len(user_list) == 0:
  441. Common.logger(log_type).warning('用户ID列表为空\n')
  442. else:
  443. for k, v in user_list.items():
  444. Common.logger(log_type).info('正在获取 {} 主页视频\n', k)
  445. GetSignature.get_signature('follow')
  446. cls.get_follow_feeds_by_pc(log_type, v.split(',')[0], env, v.split(',')[1])
  447. time.sleep(10)
  448. cls.offset = 0
  449. except Exception as e:
  450. Common.logger(log_type).error('get_all_person_videos异常:{}\n', e)
  451. # 合并音视频
  452. @classmethod
  453. def video_compose(cls, log_type, video_title):
  454. try:
  455. video_path = './videos/' + str(video_title) + '/video1.mp4'
  456. audio_path = './videos/' + str(video_title) + '/audio1.mp4'
  457. out_path = './videos/' + str(video_title) + '/video.mp4'
  458. cmd = 'ffmpeg -i ' + video_path + ' -i ' + audio_path + \
  459. ' -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 ' + out_path
  460. # print(cmd)
  461. subprocess.call(cmd, shell=True)
  462. for file in os.listdir('./videos/' + str(video_title)):
  463. if file.split('.mp4')[0] == 'video1' or file.split('.mp4')[0] == 'audio1':
  464. os.remove('./videos/' + str(video_title) + '/' + file)
  465. Common.logger(log_type).info('合成成功\n')
  466. except Exception as e:
  467. Common.logger(log_type).error(f'video_compose异常:{e}\n')
  468. # 下载 / 上传
  469. @classmethod
  470. def download_publish(cls, log_type, env, uid):
  471. try:
  472. feeds_sheet = Feishu.get_values_batch(log_type, 'xigua', 'wjhpDs')
  473. for i in range(1, len(feeds_sheet)):
  474. download_video_title = feeds_sheet[i][2]
  475. download_video_id = feeds_sheet[i][3]
  476. download_video_gid = feeds_sheet[i][4]
  477. download_play_cnt = feeds_sheet[i][5]
  478. download_comment_cnt = feeds_sheet[i][6]
  479. download_like_cnt = feeds_sheet[i][7]
  480. download_share_cnt = feeds_sheet[i][8]
  481. download_video_duration = feeds_sheet[i][9]
  482. download_video_width_height = feeds_sheet[i][10]
  483. download_send_time = feeds_sheet[i][11]
  484. download_user_name = feeds_sheet[i][12]
  485. download_user_id = feeds_sheet[i][13]
  486. download_head_url = feeds_sheet[i][14]
  487. download_cover_url = feeds_sheet[i][15]
  488. download_video_url = feeds_sheet[i][16]
  489. download_audio_url = feeds_sheet[i][17]
  490. Common.logger(log_type).info('正在判断第{}行:{}', i + 1, download_video_title)
  491. Common.logger(log_type).info('download_video_id:{}', download_video_id)
  492. Common.logger(log_type).info('download_video_duration:{}', download_video_duration)
  493. Common.logger(log_type).info('download_send_time:{}', download_send_time)
  494. # 过滤空行
  495. if download_video_title is None or download_video_id is None:
  496. Feishu.dimension_range(log_type, 'xigua', 'wjhpDs', 'ROWS', i + 1, i + 1)
  497. Common.logger(log_type).info('空行,删除成功\n')
  498. return
  499. elif str(download_video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', 'e075e9') for x in
  500. y]:
  501. Feishu.dimension_range(log_type, 'xigua', 'wjhpDs', 'ROWS', i + 1, i + 1)
  502. Common.logger(log_type).info('视频已下载,删除成功\n')
  503. return
  504. elif str(download_video_id) in [x for y in Feishu.get_values_batch(log_type, 'xigua', '3Ul6wZ') for x in
  505. y]:
  506. Feishu.dimension_range(log_type, 'xigua', 'wjhpDs', 'ROWS', i + 1, i + 1)
  507. Common.logger(log_type).info('视频已下载,删除成功\n')
  508. return
  509. else:
  510. # 下载封面
  511. Common.download_method(log_type=log_type, text='cover', d_name=download_video_title,
  512. d_url=download_cover_url)
  513. # 下载视频
  514. Common.download_method(log_type=log_type, text='video', d_name=download_video_title,
  515. d_url=download_video_url)
  516. # 下载音频
  517. Common.download_method(log_type=log_type, text='audio', d_name=download_video_title,
  518. d_url=download_audio_url)
  519. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  520. with open("./videos/" + download_video_title + "/" + "info.txt",
  521. "a", encoding="UTF-8") as f_a:
  522. f_a.write(str(download_video_id) + "\n" +
  523. str(download_video_title) + "\n" +
  524. str(download_video_duration) + "\n" +
  525. str(download_play_cnt) + "\n" +
  526. str(download_comment_cnt) + "\n" +
  527. str(download_like_cnt) + "\n" +
  528. str(download_share_cnt) + "\n" +
  529. str(download_video_width_height) + "\n" +
  530. str(int(time.mktime(
  531. time.strptime(download_send_time, "%Y/%m/%d %H:%M:%S")))) + "\n" +
  532. str(download_user_name) + "\n" +
  533. str(download_head_url) + "\n" +
  534. str(download_video_url) + "\n" +
  535. str(download_cover_url) + "\n" +
  536. "xigua"+str(int(time.time())))
  537. Common.logger("follow").info("==========视频信息已保存至info.txt==========")
  538. # 合成音视频
  539. cls.video_compose(log_type, download_video_title)
  540. # 上传视频
  541. Common.logger(log_type).info("开始上传视频:{}".format(download_video_title))
  542. our_video_id = Publish.upload_and_publish(log_type, env, uid)
  543. if env == 'dev':
  544. our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
  545. else:
  546. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
  547. Common.logger(log_type).info("视频上传完成:{}\n", download_video_title)
  548. # 视频ID工作表,插入首行
  549. Feishu.insert_columns(log_type, 'xigua', "e075e9", "ROWS", 1, 2)
  550. # 视频ID工作表,首行写入数据
  551. upload_time = int(time.time())
  552. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
  553. "关注榜",
  554. download_video_title,
  555. str(download_video_id),
  556. our_video_link,
  557. download_video_gid,
  558. download_play_cnt,
  559. download_comment_cnt,
  560. download_like_cnt,
  561. download_share_cnt,
  562. download_video_duration,
  563. download_video_width_height,
  564. download_send_time,
  565. download_user_name,
  566. download_user_id,
  567. download_head_url,
  568. download_cover_url,
  569. download_video_url,
  570. download_audio_url]]
  571. Common.logger(log_type).info('values:{}\n', values)
  572. time.sleep(1)
  573. Feishu.update_values(log_type, 'xigua', "e075e9", "F2:Z2", values)
  574. Common.logger(log_type).info("视频已保存至云文档:{}", download_video_title)
  575. # 删除行或列,可选 ROWS、COLUMNS
  576. Feishu.dimension_range(log_type, 'xigua', "wjhpDs", "ROWS", i + 1, i + 1)
  577. Common.logger(log_type).info(f"视频:{download_video_title},下载/上传成功\n")
  578. return
  579. except Exception as e:
  580. Common.logger(log_type).error('download_publish异常:{}\n', e)
  581. if __name__ == '__main__':
  582. pass