follow_list.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/7/7
  4. import os
  5. import random
  6. import sys
  7. import time
  8. import requests
  9. import urllib3
  10. sys.path.append(os.getcwd())
  11. from main.common import Common
  12. from main.feishu_lib import Feishu
  13. from main.publish import Publish
  14. proxies = {"http": None, "https": None}
  15. class Follow:
  16. # 已抓取视频数量
  17. get_video_count = []
  18. # 小程序:关注列表翻页参数
  19. follow_pcursor = ""
  20. # 小程序:个人主页视频列表翻页参数
  21. person_pcursor = ""
  22. # 视频发布时间
  23. send_time = 0
  24. # 配置微信
  25. wechat_sheet = Feishu.get_values_batch("follow", "kuaishou", "WFF4jw")
  26. Referer = wechat_sheet[2][3]
  27. NS_sig3 = wechat_sheet[3][3]
  28. NS_sig3_origin = wechat_sheet[4][3]
  29. did = wechat_sheet[5][3]
  30. session_key = wechat_sheet[6][3]
  31. unionid = wechat_sheet[7][3]
  32. eUserStableOpenId = wechat_sheet[8][3]
  33. openId = wechat_sheet[9][3]
  34. eOpenUserId = wechat_sheet[10][3]
  35. kuaishou_wechat_app_st = wechat_sheet[11][3]
  36. passToken = wechat_sheet[12][3]
  37. userId = wechat_sheet[13][3]
  38. # 过滤敏感词
  39. @classmethod
  40. def sensitive_words(cls):
  41. # 敏感词库列表
  42. word_list = []
  43. # 从云文档读取所有敏感词,添加到词库列表
  44. lists = Feishu.get_values_batch("follow", "kuaishou", "HIKVvs")
  45. for i in lists:
  46. for j in i:
  47. # 过滤空的单元格内容
  48. if j is None:
  49. pass
  50. else:
  51. word_list.append(j)
  52. return word_list
  53. # 下载规则
  54. @staticmethod
  55. def download_rule(d_duration, d_width, d_height, d_play_cnt, d_like_cnt, d_share_cnt):
  56. """
  57. 下载视频的基本规则
  58. :param d_duration: 时长
  59. :param d_width: 宽
  60. :param d_height: 高
  61. :param d_play_cnt: 播放量
  62. :param d_like_cnt: 点赞量
  63. :param d_share_cnt: 分享量
  64. :return: 满足规则,返回 True;反之,返回 False
  65. """
  66. if int(float(d_duration)) >= 30:
  67. if int(d_width) >= 720 or int(d_height) >= 720:
  68. if int(d_play_cnt) >= 5000:
  69. if int(d_like_cnt) >= 5000 or int(d_share_cnt) >= 1000:
  70. return True
  71. else:
  72. return False
  73. else:
  74. return False
  75. else:
  76. return False
  77. else:
  78. return False
  79. # 从小程序中,关注用户列表同步至云文档
  80. @classmethod
  81. def get_follow_users_to_feishu(cls, log_type):
  82. try:
  83. follow_list = []
  84. follow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "2OLxLr")
  85. url = "https://wxmini-api.uyouqu.com/rest/wd/wechatApp/relation/fol?"
  86. headers = {
  87. "content-type": "application/json",
  88. "Accept-Encoding": "gzip,compress,br,deflate",
  89. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  90. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'
  91. ' MicroMessenger/8.0.20(0x18001442) NetType/WIFI Language/zh_CN',
  92. "Referer": str(cls.Referer),
  93. }
  94. params = {
  95. "__NS_sig3": str(cls.NS_sig3),
  96. "__NS_sig3_origin": str(cls.NS_sig3_origin)
  97. }
  98. cookies = {
  99. "did": str(cls.did),
  100. "preMinaVersion": "v3.109.0",
  101. "sid": "kuaishou.wechat.app",
  102. "appId": "ks_wechat_small_app_2",
  103. "clientid": "13",
  104. "client_key": "f60ac815",
  105. "kpn": "WECHAT_SMALL_APP",
  106. "kpf": "OUTSIDE_ANDROID_H5",
  107. "language": "zh_CN",
  108. "smallAppVersion": "v3.114.0",
  109. "session_key": str(cls.session_key),
  110. "unionid": str(cls.unionid),
  111. "eUserStableOpenId": str(cls.eUserStableOpenId),
  112. "openId": str(cls.openId),
  113. "eOpenUserId": str(cls.eOpenUserId),
  114. "kuaishou.wechat.app_st": str(cls.kuaishou_wechat_app_st),
  115. "passToken": str(cls.passToken),
  116. "userId": str(cls.userId)
  117. }
  118. json_text = {
  119. "count": 20,
  120. "pcursor": str(cls.follow_pcursor),
  121. "ftype": 1
  122. }
  123. urllib3.disable_warnings()
  124. r = requests.post(url=url, headers=headers, params=params,
  125. cookies=cookies, json=json_text, proxies=proxies, verify=False)
  126. if "fols" not in r.json():
  127. Common.logger(log_type).warning("从小程序中获取关注用户列表:{}", r.text)
  128. else:
  129. users = r.json()["fols"]
  130. for i in range(len(users)):
  131. uid = users[i]["targetId"]
  132. nick = users[i]["targetName"]
  133. sex = users[i]["targetSex"]
  134. description = users[i]["targetUserText"]
  135. if "followReason" in users[i]:
  136. follow_reason = users[i]["followReason"]
  137. else:
  138. follow_reason = ""
  139. follow_time = users[i]["time"]
  140. is_friend = users[i]["isFriend"]
  141. # print(f"uid:{uid}")
  142. follow_list.append(uid)
  143. # print(f"follow_list:{follow_list}")
  144. # 同步已关注的用户至云文档
  145. if uid not in [j for i in follow_sheet for j in i]:
  146. time.sleep(1)
  147. Feishu.insert_columns(log_type, "kuaishou", "2OLxLr", "ROWS", 1, 2)
  148. time.sleep(1)
  149. values = [[uid, nick, sex, description, follow_reason, follow_time, str(is_friend)]]
  150. Feishu.update_values(log_type, "kuaishou", "2OLxLr", "A2:L2", values)
  151. else:
  152. Common.logger(log_type).info("用户:{},在云文档中已存在", nick)
  153. cls.follow_pcursor = r.json()["pcursor"]
  154. # 翻页,直至到底了
  155. if cls.follow_pcursor != "no_more":
  156. cls.get_follow_users_to_feishu(log_type)
  157. else:
  158. Common.logger(log_type).info("从小程序中同步关注用户至云文档完成\n")
  159. except Exception as e:
  160. Common.logger(log_type).error("从小程序中,关注用户列表同步至云文档异常:{}\n", e)
  161. # 从云文档获取关注用户列表
  162. @classmethod
  163. def get_follow_users(cls, log_type):
  164. try:
  165. follow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "2OLxLr")
  166. if len(follow_sheet) == 1:
  167. Common.logger(log_type).info("暂无关注用户")
  168. else:
  169. follow_dict = {}
  170. for i in range(1, len(follow_sheet)):
  171. uid = follow_sheet[i][0]
  172. nick = follow_sheet[i][1]
  173. follow_dict[nick] = uid
  174. return follow_dict
  175. except Exception as e:
  176. Common.logger(log_type).error("从云文档获取关注用户列表异常:{}\n", e)
  177. # 从云文档获取取消关注用户列表
  178. @classmethod
  179. def get_unfollow_users(cls, log_type):
  180. try:
  181. unfollow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "WRveYg")
  182. if len(unfollow_sheet) == 1:
  183. Common.logger(log_type).info("暂无取消关注用户")
  184. else:
  185. unfollow_list = []
  186. nick_list = []
  187. for i in range(1, len(unfollow_sheet)):
  188. uid = unfollow_sheet[i][0]
  189. nick = unfollow_sheet[i][1]
  190. nick_list.append(nick)
  191. unfollow_list.append(uid)
  192. Common.logger(log_type).info("取消关注用户列表:{}", nick_list)
  193. return unfollow_list
  194. except Exception as e:
  195. Common.logger(log_type).error("从云文档获取取消关注用户列表异常:{}", e)
  196. # 小程序:关注/取消关注用户
  197. @classmethod
  198. def follow_unfollow(cls, log_type, is_follow, uid):
  199. try:
  200. url = "https://wxmini-api.uyouqu.com/rest/wd/wechatApp/relation/follow?"
  201. headers = {
  202. "content-type": "application/json",
  203. "Accept-Encoding": "gzip,compress,br,deflate",
  204. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)'
  205. ' AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'
  206. ' MicroMessenger/8.0.20(0x18001442) NetType/WIFI Language/zh_CN',
  207. "Referer": str(cls.Referer),
  208. }
  209. params = {
  210. "__NS_sig3": str(cls.NS_sig3),
  211. "__NS_sig3_origin": str(cls.NS_sig3_origin)
  212. }
  213. cookies = {
  214. "did": str(cls.did),
  215. "preMinaVersion": "v3.109.0",
  216. "sid": "kuaishou.wechat.app",
  217. "appId": "ks_wechat_small_app_2",
  218. "clientid": "13",
  219. "client_key": "f60ac815",
  220. "kpn": "WECHAT_SMALL_APP",
  221. "kpf": "OUTSIDE_ANDROID_H5",
  222. "language": "zh_CN",
  223. "smallAppVersion": "v3.114.0",
  224. "session_key": str(cls.session_key),
  225. "unionid": str(cls.unionid),
  226. "eUserStableOpenId": str(cls.eUserStableOpenId),
  227. "openId": str(cls.openId),
  228. "eOpenUserId": str(cls.eOpenUserId),
  229. "kuaishou.wechat.app_st": str(cls.kuaishou_wechat_app_st),
  230. "passToken": str(cls.passToken),
  231. "userId": str(cls.userId)
  232. }
  233. if is_follow == "follow":
  234. ftype = 1
  235. elif is_follow == "unfollow":
  236. ftype = 2
  237. else:
  238. ftype = 1
  239. json_text = {
  240. "touid": uid,
  241. "ftype": ftype,
  242. "page_ref": 84
  243. }
  244. r = requests.post(url=url, headers=headers, cookies=cookies, params=params, json=json_text)
  245. if is_follow == "follow":
  246. if r.json()["result"] != 1:
  247. Common.logger(log_type).warning("{}", r.text)
  248. else:
  249. Common.logger(log_type).info("关注:{}, {}", uid, r)
  250. else:
  251. if r.json()["result"] != 1:
  252. Common.logger(log_type).warning("{}", r.text)
  253. else:
  254. Common.logger(log_type).info("取消关注:{}, {}", uid, r)
  255. except Exception as e:
  256. Common.logger(log_type).error("关注/取消关注异常:{}", e)
  257. # 获取个人主页视频
  258. @classmethod
  259. def get_user_videos(cls, log_type, uid):
  260. try:
  261. time.sleep(1)
  262. url = "https://wxmini-api.uyouqu.com/rest/wd/wechatApp/feed/profile?"
  263. headers = {
  264. "content-type": "application/json",
  265. "Accept-Encoding": "gzip,compress,br,deflate",
  266. "User-Agent": 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) '
  267. 'AppleWebKit/605.1.15 (KHTML, like Gecko)'
  268. ' Mobile/15E148 MicroMessenger/8.0.25(0x1800192a) NetType/WIFI Language/zh_CN',
  269. "Referer": str(cls.Referer),
  270. }
  271. params = {
  272. "__NS_sig3": str(cls.NS_sig3),
  273. "__NS_sig3_origin": str(cls.NS_sig3_origin)
  274. }
  275. cookies = {
  276. "did": str(cls.did),
  277. "preMinaVersion": "v3.114.0",
  278. "sid": "kuaishou.wechat.app",
  279. "appId": "ks_wechat_small_app_2",
  280. "clientid": "13",
  281. "client_key": "f60ac815",
  282. "kpn": "WECHAT_SMALL_APP",
  283. "kpf": "OUTSIDE_IOS_H5",
  284. "language": "zh_CN",
  285. "smallAppVersion": "v3.129.0",
  286. "mod": "iPhone(11<iPhone12%2C1>)",
  287. "sys": "iOS%2014.7.1",
  288. 'wechatVersion': '8.0.26',
  289. "brand": "iPhone",
  290. "session_key": str(cls.session_key),
  291. "unionid": str(cls.unionid),
  292. "eUserStableOpenId": str(cls.eUserStableOpenId),
  293. "openId": str(cls.openId),
  294. "eOpenUserId": str(cls.eOpenUserId),
  295. "kuaishou.wechat.app_st": str(cls.kuaishou_wechat_app_st),
  296. "passToken": str(cls.passToken),
  297. "userId": str(cls.userId)
  298. }
  299. json_text = {
  300. "count": 12,
  301. "pcursor": str(cls.person_pcursor),
  302. "eid": str(uid)
  303. }
  304. urllib3.disable_warnings()
  305. r = requests.post(url=url, headers=headers, params=params, cookies=cookies,
  306. json=json_text, proxies=proxies, verify=False)
  307. # Common.logger(log_type).info("response:{}\n\n", r.text)
  308. if "feeds" not in r.json():
  309. # Feishu.bot(log_type, "follow:get_videos_from_person:"+r.text)
  310. Common.logger(log_type).warning("response:{}", r.text)
  311. elif r.json()["feeds"] == 0:
  312. Common.logger(log_type).warning("用户主页无视频\n")
  313. return
  314. else:
  315. feeds = r.json()["feeds"]
  316. for i in range(len(feeds)):
  317. # 视频标题过滤话题及处理特殊字符
  318. kuaishou_title = feeds[i]["caption"]
  319. title_split1 = kuaishou_title.split(" #")
  320. if title_split1[0] != "":
  321. title1 = title_split1[0]
  322. else:
  323. title1 = title_split1[-1]
  324. title_split2 = title1.split(" #")
  325. if title_split2[0] != "":
  326. title2 = title_split2[0]
  327. else:
  328. title2 = title_split2[-1]
  329. title_split3 = title2.split("@")
  330. if title_split3[0] != "":
  331. title3 = title_split3[0]
  332. else:
  333. title3 = title_split3[-1]
  334. video_title = title3.strip().replace("\n", "") \
  335. .replace("/", "").replace("快手", "").replace(" ", "") \
  336. .replace(" ", "").replace("&NBSP", "").replace("\r", "") \
  337. .replace("#", "").replace(".", "。").replace("\\", "") \
  338. .replace(":", "").replace("*", "").replace("?", "") \
  339. .replace("?", "").replace('"', "").replace("<", "") \
  340. .replace(">", "").replace("|", "").replace("@", "")[:40]
  341. if "photoId" not in feeds[i]:
  342. video_id = "0"
  343. else:
  344. video_id = feeds[i]["photoId"]
  345. if "viewCount" not in feeds[i]:
  346. video_play_cnt = "0"
  347. else:
  348. video_play_cnt = feeds[i]["viewCount"]
  349. if "likeCount" not in feeds[i]:
  350. video_like_cnt = "0"
  351. else:
  352. video_like_cnt = feeds[i]["likeCount"]
  353. if "shareCount" not in feeds[i]:
  354. video_share_cnt = "0"
  355. else:
  356. video_share_cnt = feeds[i]["shareCount"]
  357. if "commentCount" not in feeds[i]:
  358. video_comment_cnt = "0"
  359. else:
  360. video_comment_cnt = feeds[i]["commentCount"]
  361. if "duration" not in feeds[i]:
  362. video_duration = "0"
  363. else:
  364. video_duration = int(int(feeds[i]["duration"]) / 1000)
  365. if "width" not in feeds[i] or "height" not in feeds[i]:
  366. video_width = "0"
  367. video_height = "0"
  368. else:
  369. video_width = feeds[i]["width"]
  370. video_height = feeds[i]["height"]
  371. if "timestamp" not in feeds[i]:
  372. video_send_time = "0"
  373. else:
  374. video_send_time = feeds[i]["timestamp"]
  375. cls.send_time = int(int(video_send_time) / 1000)
  376. if "userName" not in feeds[i]:
  377. user_name = "0"
  378. else:
  379. user_name = feeds[i]["userName"].strip().replace("\n", "") \
  380. .replace("/", "").replace("快手", "").replace(" ", "") \
  381. .replace(" ", "").replace("&NBSP", "").replace("\r", "")
  382. if "userId" not in feeds[i]:
  383. user_id = "0"
  384. else:
  385. user_id = feeds[i]["userId"]
  386. if "headUrl" not in feeds[i]:
  387. head_url = "0"
  388. else:
  389. head_url = feeds[i]["headUrl"]
  390. if "webpCoverUrls" in feeds[i]:
  391. cover_url = feeds[i]["webpCoverUrls"][-1]["url"]
  392. elif "coverUrls" not in feeds[i]:
  393. cover_url = "0"
  394. elif len(feeds[i]["coverUrls"]) == 0:
  395. cover_url = "0"
  396. else:
  397. cover_url = feeds[i]["coverUrls"][0]["url"]
  398. if "mainMvUrls" not in feeds[i]:
  399. video_url = "0"
  400. elif len(feeds[i]["mainMvUrls"]) == 0:
  401. video_url = "0"
  402. else:
  403. video_url = feeds[i]["mainMvUrls"][0]["url"]
  404. Common.logger(log_type).info("video_title:{}".format(video_title))
  405. Common.logger(log_type).info("user_name:{}".format(user_name))
  406. Common.logger(log_type).info("video_id:{}".format(video_id))
  407. Common.logger(log_type).info("video_play_cnt:{}".format(video_play_cnt))
  408. Common.logger(log_type).info("video_like_cnt:{}".format(video_like_cnt))
  409. Common.logger(log_type).info("video_share_cnt:{}".format(video_share_cnt))
  410. # Common.logger(log_type).info("video_comment_cnt:{}".format(video_comment_cnt))
  411. Common.logger(log_type).info("video_duration:{}秒".format(video_duration))
  412. # Common.logger(log_type).info("video_resolution:{}".format(video_resolution))
  413. Common.logger(log_type).info("video_send_time:{}".format(
  414. time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000))))
  415. # Common.logger(log_type).info("user_id:{}".format(user_id))
  416. # Common.logger(log_type).info("head_url:{}".format(head_url))
  417. # Common.logger(log_type).info("cover_url:{}".format(cover_url))
  418. Common.logger(log_type).info("video_url:{}".format(video_url))
  419. # 过滤无效视频
  420. if video_id == "0" \
  421. or head_url == "0" \
  422. or cover_url == "0" \
  423. or video_url == "0" \
  424. or video_duration == "0" \
  425. or video_send_time == "0" \
  426. or user_name == "0" \
  427. or user_id == "0" \
  428. or video_title == "":
  429. Common.logger(log_type).info("无效视频\n")
  430. # # 视频发布时间 <= 7 天
  431. # elif int(time.time()) - int(int(video_send_time) / 1000) > 604800:
  432. # Common.logger("follow").info("发布时间:{},超过7天\n", time.strftime(
  433. # "%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)))
  434. # cls.person_pcursor = ""
  435. # break
  436. # 判断敏感词
  437. elif cls.download_rule(video_duration, video_width, video_height, video_play_cnt,
  438. video_like_cnt, video_share_cnt) is False:
  439. Common.logger(log_type).info("不满足下载规则\n".format(kuaishou_title))
  440. elif any(word if word in kuaishou_title else False for word in cls.sensitive_words()) is True:
  441. Common.logger(log_type).info("视频已中敏感词:{}\n".format(kuaishou_title))
  442. # 从 云文档 去重:https://w42nne6hzg.feishu.cn/sheets/shtcnp4SaJt37q6OOOrYzPMjQkg?sheet=3cd128
  443. elif video_id in [j for m in Feishu.get_values_batch(log_type, "kuaishou", "3cd128") for j in m]:
  444. Common.logger(log_type).info("该视频已下载:{}\n", video_title)
  445. # 从 云文档 去重:https://w42nne6hzg.feishu.cn/sheets/shtcnp4SaJt37q6OOOrYzPMjQkg?sheet=wW5cyb
  446. elif video_id in [j for n in Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb") for j in n]:
  447. Common.logger(log_type).info("该视频已在feeds中:{}\n", video_title)
  448. else:
  449. Feishu.insert_columns("follow", "kuaishou", "wW5cyb", "ROWS", 1, 2)
  450. # 获取当前时间
  451. get_feeds_time = int(time.time())
  452. # 工作表中写入数据
  453. values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(get_feeds_time))),
  454. "用户主页",
  455. video_id,
  456. video_title,
  457. video_play_cnt,
  458. video_comment_cnt,
  459. video_like_cnt,
  460. video_share_cnt,
  461. video_duration,
  462. str(video_width) + "*" + str(video_height),
  463. time.strftime(
  464. "%Y/%m/%d %H:%M:%S", time.localtime(int(video_send_time) / 1000)),
  465. user_name,
  466. user_id,
  467. head_url,
  468. cover_url,
  469. video_url]]
  470. # 等待 1s,防止操作云文档太频繁,导致报错
  471. time.sleep(1)
  472. Feishu.update_values("follow", "kuaishou", "wW5cyb", "A2:P2", values)
  473. Common.logger("follow").info("添加视频至follow_feeds成功:{}\n", video_title)
  474. cls.get_video_count.append(video_id)
  475. # 抓取足够多数量的视频
  476. if len(cls.get_video_count) >= 2:
  477. Common.logger(log_type).info('已抓取{}:{}条视频\n', user_name, len(cls.get_video_count))
  478. cls.person_pcursor = ""
  479. cls.get_video_count = []
  480. return
  481. if len(cls.get_video_count) < 2:
  482. # 翻页
  483. cls.person_pcursor = r.json()["pcursor"]
  484. cls.get_user_videos(log_type, uid)
  485. except Exception as e:
  486. Common.logger(log_type).error("get_videos_from_person异常:{}\n", e)
  487. # 获取所有关注列表的用户视频
  488. @classmethod
  489. def get_videos_from_follow(cls, log_type, env):
  490. try:
  491. user_list = cls.get_follow_users(log_type)
  492. if len(user_list) == 0:
  493. Common.logger(log_type).warning('用户ID列表为空\n')
  494. else:
  495. for k, v in user_list.items():
  496. Common.logger(log_type).info('正在获取 {} 主页视频\n', k)
  497. cls.get_user_videos(log_type, str(v))
  498. cls.run_download_publish(log_type, env)
  499. time.sleep(random.randint(10, 30))
  500. except Exception as e:
  501. Common.logger(log_type).error('get_videos_from_follow异常:{}\n', e)
  502. # 下载/上传
  503. @classmethod
  504. def download_publish(cls, log_type, env):
  505. try:
  506. follow_feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
  507. for i in range(1, len(follow_feeds_sheet)):
  508. time.sleep(1)
  509. download_video_id = follow_feeds_sheet[i][2]
  510. download_video_title = follow_feeds_sheet[i][3]
  511. download_video_play_cnt = follow_feeds_sheet[i][4]
  512. download_video_comment_cnt = follow_feeds_sheet[i][5]
  513. download_video_like_cnt = follow_feeds_sheet[i][6]
  514. download_video_share_cnt = follow_feeds_sheet[i][7]
  515. download_video_duration = follow_feeds_sheet[i][8]
  516. download_video_resolution = follow_feeds_sheet[i][9]
  517. download_video_send_time = follow_feeds_sheet[i][10]
  518. download_user_name = follow_feeds_sheet[i][11]
  519. download_user_id = follow_feeds_sheet[i][12]
  520. download_head_url = follow_feeds_sheet[i][13]
  521. download_cover_url = follow_feeds_sheet[i][14]
  522. download_video_url = follow_feeds_sheet[i][15]
  523. Common.logger(log_type).info("正在判断第{}行,视频:{}", i + 1, download_video_title)
  524. # 过滤空行及空标题视频
  525. if download_video_id is None \
  526. or download_video_id == "" \
  527. or download_video_title is None \
  528. or download_video_title == "":
  529. # 删除行或列,可选 ROWS、COLUMNS
  530. Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
  531. Common.logger(log_type).warning("标题为空或空行,删除成功\n")
  532. return
  533. # # 下载规则
  534. # elif cls.download_rule(
  535. # download_video_duration, download_video_resolution.split("*")[0],
  536. # download_video_resolution.split("*")[-1], download_video_play_cnt, download_video_like_cnt,
  537. # download_video_share_cnt) is False:
  538. # # 删除行或列,可选 ROWS、COLUMNS
  539. # Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
  540. # Common.logger(log_type).info("不满足下载规则:{},删除成功\n", download_video_title)
  541. # return
  542. # 从已下载视频表中去重
  543. elif download_video_id in [j for m in Feishu.get_values_batch(
  544. log_type, "kuaishou", "3cd128") for j in m]:
  545. # 删除行或列,可选 ROWS、COLUMNS
  546. Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
  547. Common.logger(log_type).info("视频已下载:{},删除成功\n", download_video_title)
  548. return
  549. else:
  550. # 下载封面
  551. Common.download_method(log_type=log_type, text="cover",
  552. d_name=str(download_video_title), d_url=str(download_cover_url))
  553. # 下载视频
  554. Common.download_method(log_type=log_type, text="video",
  555. d_name=str(download_video_title), d_url=str(download_video_url))
  556. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  557. with open("./videos/" + download_video_title + "/" + "info.txt",
  558. "a", encoding="UTF-8") as f_a:
  559. f_a.write(str(download_video_id) + "\n" +
  560. str(download_video_title) + "\n" +
  561. str(download_video_duration) + "\n" +
  562. str(download_video_play_cnt) + "\n" +
  563. str(download_video_comment_cnt) + "\n" +
  564. str(download_video_like_cnt) + "\n" +
  565. str(download_video_share_cnt) + "\n" +
  566. str(download_video_resolution) + "\n" +
  567. str(int(time.mktime(
  568. time.strptime(download_video_send_time, "%Y/%m/%d %H:%M:%S")))) + "\n" +
  569. str(download_user_name) + "\n" +
  570. str(download_head_url) + "\n" +
  571. str(download_video_url) + "\n" +
  572. str(download_cover_url) + "\n" +
  573. "kuaishou_person")
  574. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  575. # 上传视频
  576. Common.logger(log_type).info("开始上传视频:{}".format(download_video_title))
  577. our_video_id = Publish.upload_and_publish(log_type, env, "play")
  578. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
  579. Common.logger(log_type).info("视频上传完成:{}", download_video_title)
  580. # 保存视频 ID 到云文档:https://w42nne6hzg.feishu.cn/sheets/shtcnp4SaJt37q6OOOrYzPMjQkg?sheet=3cd128
  581. Common.logger(log_type).info("保存视频ID至云文档:{}", download_video_title)
  582. # 视频ID工作表,插入首行
  583. Feishu.insert_columns(log_type, "kuaishou", "3cd128", "ROWS", 1, 2)
  584. # 视频ID工作表,首行写入数据
  585. upload_time = int(time.time())
  586. values = [[our_video_id,
  587. time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
  588. "用户主页",
  589. str(download_video_id),
  590. str(download_video_title),
  591. our_video_link,
  592. download_video_play_cnt,
  593. download_video_comment_cnt,
  594. download_video_like_cnt,
  595. download_video_share_cnt,
  596. download_video_duration,
  597. str(download_video_resolution),
  598. str(download_video_send_time),
  599. str(download_user_name),
  600. str(download_user_id),
  601. str(download_head_url),
  602. str(download_cover_url),
  603. str(download_video_url)]]
  604. time.sleep(1)
  605. Feishu.update_values(log_type, "kuaishou", "3cd128", "E2:Z2", values)
  606. # 删除行或列,可选 ROWS、COLUMNS
  607. Feishu.dimension_range(log_type, "kuaishou", "wW5cyb", "ROWS", i + 1, i + 1)
  608. Common.logger(log_type).info("视频:{},下载/上传成功\n", download_video_title)
  609. return
  610. except Exception as e:
  611. Common.logger(log_type).error("download_publish异常:{}\n", e)
  612. # 执行下载/上传
  613. @classmethod
  614. def run_download_publish(cls, log_type, env):
  615. try:
  616. while True:
  617. follow_feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
  618. if len(follow_feeds_sheet) == 1:
  619. Common.logger(log_type).info("下载/上传完成\n")
  620. break
  621. else:
  622. cls.download_publish(log_type, env)
  623. except Exception as e:
  624. Common.logger(log_type).error("run_download_publish异常:{}\n", e)
  625. if __name__ == "__main__":
  626. # Follow.follow_unfollow("follow", "follow", "687090964")
  627. # Follow.get_videos_from_person("follow", "2413743952")
  628. # Follow.get_follow_users_to_feishu('follow')
  629. # Follow.get_follow_users('follow')
  630. Follow.get_videos_from_follow('follow')