zhihu_follow.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/10/31
  4. import os
  5. import sys
  6. import time
  7. import requests
  8. import urllib3
  9. sys.path.append(os.getcwd())
  10. from main.common import Common
  11. from main.feishu_lib import Feishu
  12. from main.follow_publish import Publish
  13. proxies = {'http': None, 'https': None}
  14. class ZhihuFollow:
  15. offset = 0
  16. @classmethod
  17. def get_users_from_feishu(cls, log_type):
  18. try:
  19. user_sheet = Feishu.get_values_batch(log_type, 'zhihu', '4NTla6')
  20. user_dict = {}
  21. for i in range(1, len(user_sheet)):
  22. user_name = user_sheet[i][0]
  23. url_token = user_sheet[i][1]
  24. referer = user_sheet[i][2]
  25. our_id = user_sheet[i][3]
  26. if user_name is None or url_token is None or our_id is None:
  27. pass
  28. else:
  29. user_dict[user_name] = str(url_token) + ',' + str(referer) + ',' + str(our_id)
  30. return user_dict
  31. except Exception as e:
  32. Common.logger(log_type).error('get_users_from_feishu异常:{}\n', e)
  33. @classmethod
  34. def get_follow_feeds(cls, log_type, url_token, referer, our_uid, env):
  35. while True:
  36. try:
  37. # Common.logger(log_type).info('offset:{}', cls.offset)
  38. url = "https://www.zhihu.com//api//v4//members//" + str(url_token) + "//zvideos?"
  39. headers = {
  40. 'pragma': 'no-cache',
  41. 'cache-control': 'no-cache',
  42. 'x-zse-93': '101_3_3.0',
  43. 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
  44. 'x-zst-81': '3_2.0aR_sn77yn6O92wOB8hPZnQr0EMYxc4f18wNBUgpTQ6nxERFZMRY0-4Lm-h3_tufIwJS8gcxTgJS_'
  45. 'AuPZNcXCTwxI78YxEM20s4PGDwN8gGcYAupMWufIoLVqr4gxrRPOI0cY7HL8qun9g93mFukyigcmebS_'
  46. 'FwOYPRP0E4rZUrN9DDom3hnynAUMnAVPF_PhaueTF7C89Uw8_w2YiDNBOcC_ggeL2HN1fTN0WvUq_'
  47. 'UY9aCLBMQO0VvUfoTp9y9eTV4xC8b3YCGNs2bSfXwCZpUw06hcMJ_w0B8NmhvwMcGe9iqCZlcSftv3qTug'
  48. '_-BVqgwL1r7H99CeV24C9_Up9HqHmj9CsqUH8tBNLUbxm1gOprAO_6CL0Vg_z9DVs9rS1R9x08wxG2Cxme9'
  49. 'FLlUc9jJu027gB3UVBJqpBbqUCrwo1nqHmggO_Nr9_BqcxfC2GnvSqNcxyIDOfiuw9dgCC27emaUpMxuVqq'
  50. 'uFYwUSL2BXs',
  51. 'accept': '*/*',
  52. 'referer': referer,
  53. 'sec-ch-ua': '"Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
  54. 'sec-ch-ua-mobile': '?0',
  55. 'sec-ch-ua-platform': '"macOS"',
  56. 'sec-fetch-dest': 'empty',
  57. 'sec-fetch-mode': 'cors',
  58. 'sec-fetch-site': 'same-origin',
  59. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 '
  60. '(KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
  61. 'x-ab-pb': 'CsIBCAAbAD8ARwC0AGkBagF0ATsCzALXAtgCTwNQA6ADoQOiA7cD8wP0AzMEjASNBKYE1gQR'
  62. 'BVEFiwWMBZ4FMAYxBusGJwd3B3gH2AfcB90HZwh0CHYIeQjaCD8JQglgCY0JwwnECcUJxgnH'
  63. 'CcgJyQnKCcsJzAnRCfQJBApJCmUKawqYCqUKqQq+CsQK1ArdCu0K/Qr+CjsLPAtDC0YLcQt2'
  64. 'C4ULhwuNC8AL1wvgC+UL5gssDDgMcQyPDKwMuQzDDMkM+AwSYQEAAAAAAAAAAAMAAAAAAAAA'
  65. 'AAAAAAQEAAQAAAAAAQAAAQAAAAACAgQAAAYAAAEBAAAAAAAAAAAAAAAAAwAAAAABAAAAAQEAA'
  66. 'AAAAQABAAAAAAAFAAIBAAAGAgYAAAECAAA=',
  67. 'x-requested-with': 'fetch',
  68. 'accept-encoding': 'gzip, deflate, br',
  69. 'x-zse-96': '2.0_wSyMHDQ7MZYfDZWRCM=Il3PaIuFuaTZf=spPXrAiJo83rlG65AU+b6IIBIr5nJ5L',
  70. }
  71. params = {
  72. 'offset': str(cls.offset),
  73. 'limit': '20',
  74. 'similar_aggregation': True,
  75. 'include': 'similar_zvideo,creation_relationship',
  76. }
  77. cookies = {
  78. '_zap': 'a17e5882-775f-49dd-b541-e854c0299999',
  79. 'd_c0': '"AFAcR4hr0RKPTn2-RL935cm51KQZn9-Xlb0=|1616071586"',
  80. '_9755xjdesxxd_': '32',
  81. 'YD00517437729195%3AWM_TID': 'yR6Kt%2FwMI2xEFEQRVBYrou2J%2F14yfdzt',
  82. 'q_c1': '947ece3f1ccd4ccd83b718c113fe3935|1658911548000|1658911548000',
  83. '_xsrf': 'cAM8ovVh43f0oJE2erl6ho8Y4CTZhFKF',
  84. '__snaker__id': 'mqTPtP3IwbuoHA2K',
  85. 'gdxidpyhxdE': 'a2d%2FeD%2BEUNxnnTaLDjQfd28sI39g3pnKAqS%2BmodWMqYcMa3AMzV5eom2APR2z5rDHAVvfubp'
  86. '%2BWEzkcxdb9UjWNT0gbshTpBmHzU1%2BMB%2BfW5sAAUz1%5CTWIWbP34z7SV4TCB%2BfbQtoAdRA'
  87. 'ZANzEbcollrLlPmyAG%2FtPICs5ewAvswneclw%3A1666061422009',
  88. 'YD00517437729195%3AWM_NI': 'bTzKLUbqXTrTGVFS91056cj27L%2F9PQ8MG8ofPHnJc98014ktS5xa09N8yJZho7j'
  89. 'EeQfeueH0sJLDH5YmaA2nCP1kQ1jaB4FChGGNA7HbM%2B5yRCuYOmGjfIs5LOdoxm'
  90. '1WSHY%3D',
  91. 'YD00517437729195%3AWM_NIKE': '9ca17ae2e6ffcda170e2e6eeb2f5628de7a5b2db5cfc8e8eb6d85f929e8aacc'
  92. '150b6ed9bd8b33c93afc089cf2af0fea7c3b92aa28cb6accf5fa2af98b5f1608'
  93. '58a8ab4bc3bf4ebfa84e93af594e1dad35bfc9389d9ee6e9498a2abe233fcf0f'
  94. 'fd5ea8091ebb882b3338ee8b7abd964ade9a6a6e85ba6bbbb86cb3cb19684a5c'
  95. 'b3f9bb3afa2b1668190ff8cd43baabf8ed5c549ac9989afc43d8cbe83ccb8739'
  96. '29dbbbace6785b6aaaaf23f9091ac90fb67f3899da9dc37e2a3',
  97. 'captcha_ticket_v2': '2|1:0|10:1666060537|17:captcha_ticket_v2|704:eyJ2YWxpZGF0ZSI6IkNOMzFfVkk0'
  98. 'TmhfRVJ1SGxhQW4yV0ZHWFlXTjdIWXM3TnZQSUFMY19MRGdVV1dMX3Vody0wMFAtYnA0UXBFV'
  99. 'nI5akI1NFNVajhZVEpLeXVLODJWajJoVU83MnBya2xyLi5TalExekwyZ3hQU3JjZTI4TkJweT'
  100. 'h2NEg5QjFyVUlBME1zSGtfUlRqQ3lDd1BkQ2Y3c3J3WWl0V0Qyci5FTWV6eEJRMUdQcGlWS2d'
  101. 'CXzRkQzhSWmNMT1ZmWnlxUDZWbTh6ZEdDdmVnVkhFTTFGVWNpNVJRV2NWLjZ6V2V4UUpJOWJi'
  102. 'bUdRZHpJQ0VQUFlZZU5ObVBKOTlKMUw1OWFxUmRxUzVtY1hDbXBsRzVBTnM0LmEyS1BDRGNTY'
  103. 'WVTWUlpMFdfc0JlTE44QlZCXzFkWjFLV2xoUmx3bkR6QjJMdUdOcm1IRXRaejQ2T0ViVzhVS3'
  104. 'ZSNjU2YUJEUk9tYUNscVM5cDk2T2cwY1JadlhEZFZOSlFSeG0wNkRUMEs5cHRLLS1hajFWaWt'
  105. 'EMnp4UU50MEpFZE9PRGs2RWtqdklnWnM0S0s0bUVBd0tGS2FIMklJaXVod1dMck9WOGdKLUpw'
  106. 'NjlYNU9hWWxNcEtCbk9pU2JYNUJfUnBmeU1hLlAwbUlBYXYwbnBvY0xRWmdMMXJpelN5OFVaV'
  107. 'l9QMXduWTByR1BtMyJ9|557dafa54cac0a5cd55b3e8ebbb626e6817010e985c2a3c60a82f'
  108. 'b00bcb8b72a',
  109. 'captcha_session_v2': '2|1:0|10:1666060537|18:captcha_session_v2|88:SUlnbE5uR29rUC83Z0VvWVpjTS9'
  110. 'JV3VKSFFYTUl4T1hFbVdZZnB2Wms1Z1cxYkZFQndYWHhqVDRGWEE0NzVaTQ==|a40c62b74a'
  111. '94ad618205342e3f87091b525a50fe9c8f153af02b08a3569fa7c0',
  112. 'z_c0': '2|1:0|10:1666767387|4:z_c0|92:Mi4xVFdsTlB3QUFBQUFBVUJ4SGlHdlJFaVlBQUFCZ0FsVk5CVjg3WkFB'
  113. 'OTBXb185c2wyUjJ6ZmQ1OTYtamliYWJWTUp3|abd71d40da50ae308430e26ce358d34cd4bccdfe575bf544d'
  114. '212019c7189ecdb',
  115. 'Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49': '1665574748,1666678329,1667194593',
  116. 'SESSIONID': 'hyrUgL9IzongRfviGSTmGR2sDyVliSwHuWy0fXouVk7',
  117. 'JOID': 'W14WBkoyfRA9faU0WD7qwUz2wuJAVQFkbBLmCBdAHXtcK8tNNNogTVJ6rzdabb3cImOxRThXGFkflrdU1CD0UWI=',
  118. 'osd': 'U10VAU46fhM6ea03WznuyU_1xeZIVgJjaBrlCxBEFXhfLM9FN9knSVp5rDBeZb7fJWe5RjtQHFEclbBQ3CP3VmY=',
  119. 'tst': 'v',
  120. 'NOT_UNREGISTER_WAITING': '1',
  121. 'Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49': '1667373061',
  122. 'unlock_ticket': 'AGBWwkpQuhUmAAAAYAJVTQ0fYmOTbjeHqRp3XiDPD3ZVB5KkeaEeYw==',
  123. 'KLBRSID': '031b5396d5ab406499e2ac6fe1bb1a43|1667373074|1667370552',
  124. }
  125. urllib3.disable_warnings()
  126. response = requests.get(url=url, headers=headers, params=params, cookies=cookies, verify=False,
  127. proxies=proxies)
  128. cls.offset += 20
  129. if 'data' not in response.json():
  130. Common.logger(log_type).error('response:{}\n', response.text)
  131. cls.offset = 0
  132. return
  133. elif len(response.json()['data']) == 0:
  134. Common.logger(log_type).error('response:{}\n', response.text)
  135. cls.offset = 0
  136. return
  137. else:
  138. feeds = response.json()['data']
  139. is_end = response.json()['paging']['is_end']
  140. for i in range(len(feeds)):
  141. # video_title
  142. if 'title' not in feeds[i]:
  143. video_title = 0
  144. else:
  145. video_title = feeds[i]['title']
  146. # video_id
  147. if 'video' not in feeds[i]:
  148. video_id = 0
  149. elif 'video_id' not in feeds[i]['video']:
  150. video_id = 0
  151. else:
  152. video_id = feeds[i]['video']['video_id']
  153. # play_cnt
  154. if 'play_count' not in feeds[i]:
  155. play_cnt = 0
  156. else:
  157. play_cnt = feeds[i]['play_count']
  158. # comment_cnt
  159. if 'comment_count' not in feeds[i]:
  160. comment_cnt = 0
  161. else:
  162. comment_cnt = feeds[i]['comment_count']
  163. # like_cnt
  164. if 'liked_count' not in feeds[i]:
  165. like_cnt = 0
  166. else:
  167. like_cnt = feeds[i]['liked_count']
  168. # share_cnt
  169. if 'share_count' not in feeds[i]:
  170. share_cnt = 0
  171. else:
  172. share_cnt = feeds[i]['share_count']
  173. # voteup_cnt 赞同数
  174. if 'voteup_count' not in feeds[i]:
  175. voteup_cnt = 0
  176. else:
  177. voteup_cnt = feeds[i]['voteup_count']
  178. # publish_time
  179. if 'published_at' not in feeds[i]:
  180. publish_time = 0
  181. else:
  182. publish_time = feeds[i]['published_at']
  183. # duration
  184. if 'video' not in feeds[i]:
  185. duration = 0
  186. elif 'duration' not in feeds[i]['video']:
  187. duration = 0
  188. else:
  189. duration = feeds[i]['video']['duration']
  190. # width / height / video_url
  191. if 'video' not in feeds[i]:
  192. video_width = 0
  193. video_height = 0
  194. video_url = 0
  195. elif 'playlist' in feeds[i]['video'] and 'fhd' in feeds[i]['video']['playlist'] \
  196. and 'width' in feeds[i]['video']['playlist']['fhd'] \
  197. and 'height' in feeds[i]['video']['playlist']['fhd'] \
  198. and 'play_url' in feeds[i]['video']['playlist']['fhd']:
  199. video_width = feeds[i]['video']['playlist']['fhd']['width']
  200. video_height = feeds[i]['video']['playlist']['fhd']['height']
  201. video_url = feeds[i]['video']['playlist']['fhd']['play_url']
  202. elif 'playlist' in feeds[i]['video'] and 'fhd' in feeds[i]['video']['playlist'] \
  203. and 'width' in feeds[i]['video']['playlist']['fhd'] \
  204. and 'height' in feeds[i]['video']['playlist']['fhd'] \
  205. and 'url' in feeds[i]['video']['playlist']['fhd']:
  206. video_width = feeds[i]['video']['playlist']['fhd']['width']
  207. video_height = feeds[i]['video']['playlist']['fhd']['height']
  208. video_url = feeds[i]['video']['playlist']['fhd']['url']
  209. elif 'playlist' in feeds[i]['video'] and 'hd' in feeds[i]['video']['playlist'] \
  210. and 'width' in feeds[i]['video']['playlist']['hd'] \
  211. and 'height' in feeds[i]['video']['playlist']['hd'] \
  212. and 'play_url' in feeds[i]['video']['playlist']['hd']:
  213. video_width = feeds[i]['video']['playlist']['hd']['width']
  214. video_height = feeds[i]['video']['playlist']['hd']['height']
  215. video_url = feeds[i]['video']['playlist']['hd']['play_url']
  216. elif 'playlist' in feeds[i]['video'] and 'hd' in feeds[i]['video']['playlist'] \
  217. and 'width' in feeds[i]['video']['playlist']['hd'] \
  218. and 'height' in feeds[i]['video']['playlist']['hd'] \
  219. and 'url' in feeds[i]['video']['playlist']['hd']:
  220. video_width = feeds[i]['video']['playlist']['hd']['width']
  221. video_height = feeds[i]['video']['playlist']['hd']['height']
  222. video_url = feeds[i]['video']['playlist']['hd']['url']
  223. elif 'playlist' in feeds[i]['video'] and 'ld' in feeds[i]['video']['playlist'] \
  224. and 'width' in feeds[i]['video']['playlist']['ld'] \
  225. and 'height' in feeds[i]['video']['playlist']['ld'] \
  226. and 'play_url' in feeds[i]['video']['playlist']['ld']:
  227. video_width = feeds[i]['video']['playlist']['ld']['width']
  228. video_height = feeds[i]['video']['playlist']['ld']['height']
  229. video_url = feeds[i]['video']['playlist']['ld']['play_url']
  230. elif 'playlist' in feeds[i]['video'] and 'ld' in feeds[i]['video']['playlist'] \
  231. and 'width' in feeds[i]['video']['playlist']['ld'] \
  232. and 'height' in feeds[i]['video']['playlist']['ld'] \
  233. and 'url' in feeds[i]['video']['playlist']['ld']:
  234. video_width = feeds[i]['video']['playlist']['ld']['width']
  235. video_height = feeds[i]['video']['playlist']['ld']['height']
  236. video_url = feeds[i]['video']['playlist']['ld']['url']
  237. elif 'playlist' in feeds[i]['video'] and 'sd' in feeds[i]['video']['playlist'] \
  238. and 'width' in feeds[i]['video']['playlist']['sd'] \
  239. and 'height' in feeds[i]['video']['playlist']['sd'] \
  240. and 'play_url' in feeds[i]['video']['playlist']['sd']:
  241. video_width = feeds[i]['video']['playlist']['sd']['width']
  242. video_height = feeds[i]['video']['playlist']['sd']['height']
  243. video_url = feeds[i]['video']['playlist']['sd']['play_url']
  244. elif 'playlist' in feeds[i]['video'] and 'sd' in feeds[i]['video']['playlist'] \
  245. and 'width' in feeds[i]['video']['playlist']['sd'] \
  246. and 'height' in feeds[i]['video']['playlist']['sd'] \
  247. and 'url' in feeds[i]['video']['playlist']['sd']:
  248. video_width = feeds[i]['video']['playlist']['sd']['width']
  249. video_height = feeds[i]['video']['playlist']['sd']['height']
  250. video_url = feeds[i]['video']['playlist']['sd']['url']
  251. elif 'playlist_v2' in feeds[i]['video'] and 'fhd' in feeds[i]['video']['playlist_v2'] \
  252. and 'width' in feeds[i]['video']['playlist_v2']['fhd'] \
  253. and 'height' in feeds[i]['video']['playlist_v2']['fhd'] \
  254. and 'play_url' in feeds[i]['video']['playlist_v2']['fhd']:
  255. video_width = feeds[i]['video']['playlist_v2']['fhd']['width']
  256. video_height = feeds[i]['video']['playlist_v2']['fhd']['height']
  257. video_url = feeds[i]['video']['playlist_v2']['fhd']['play_url']
  258. elif 'playlist_v2' in feeds[i]['video'] and 'fhd' in feeds[i]['video']['playlist_v2'] \
  259. and 'width' in feeds[i]['video']['playlist_v2']['fhd'] \
  260. and 'height' in feeds[i]['video']['playlist_v2']['fhd'] \
  261. and 'url' in feeds[i]['video']['playlist_v2']['fhd']:
  262. video_width = feeds[i]['video']['playlist_v2']['fhd']['width']
  263. video_height = feeds[i]['video']['playlist_v2']['fhd']['height']
  264. video_url = feeds[i]['video']['playlist_v2']['fhd']['url']
  265. elif 'playlist_v2' in feeds[i]['video'] and 'hd' in feeds[i]['video']['playlist_v2'] \
  266. and 'width' in feeds[i]['video']['playlist_v2']['hd'] \
  267. and 'height' in feeds[i]['video']['playlist_v2']['hd'] \
  268. and 'play_url' in feeds[i]['video']['playlist_v2']['hd']:
  269. video_width = feeds[i]['video']['playlist_v2']['hd']['width']
  270. video_height = feeds[i]['video']['playlist_v2']['hd']['height']
  271. video_url = feeds[i]['video']['playlist_v2']['hd']['play_url']
  272. elif 'playlist_v2' in feeds[i]['video'] and 'hd' in feeds[i]['video']['playlist_v2'] \
  273. and 'width' in feeds[i]['video']['playlist_v2']['hd'] \
  274. and 'height' in feeds[i]['video']['playlist_v2']['hd'] \
  275. and 'url' in feeds[i]['video']['playlist_v2']['hd']:
  276. video_width = feeds[i]['video']['playlist_v2']['hd']['width']
  277. video_height = feeds[i]['video']['playlist_v2']['hd']['height']
  278. video_url = feeds[i]['video']['playlist_v2']['hd']['url']
  279. elif 'playlist_v2' in feeds[i]['video'] and 'ld' in feeds[i]['video']['playlist_v2'] \
  280. and 'width' in feeds[i]['video']['playlist_v2']['ld'] \
  281. and 'height' in feeds[i]['video']['playlist_v2']['ld'] \
  282. and 'play_url' in feeds[i]['video']['playlist_v2']['ld']:
  283. video_width = feeds[i]['video']['playlist_v2']['ld']['width']
  284. video_height = feeds[i]['video']['playlist_v2']['ld']['height']
  285. video_url = feeds[i]['video']['playlist_v2']['ld']['play_url']
  286. elif 'playlist_v2' in feeds[i]['video'] and 'ld' in feeds[i]['video']['playlist_v2'] \
  287. and 'width' in feeds[i]['video']['playlist_v2']['ld'] \
  288. and 'height' in feeds[i]['video']['playlist_v2']['ld'] \
  289. and 'url' in feeds[i]['video']['playlist_v2']['ld']:
  290. video_width = feeds[i]['video']['playlist_v2']['ld']['width']
  291. video_height = feeds[i]['video']['playlist_v2']['ld']['height']
  292. video_url = feeds[i]['video']['playlist_v2']['ld']['url']
  293. elif 'playlist_v2' in feeds[i]['video'] and 'sd' in feeds[i]['video']['playlist_v2'] \
  294. and 'width' in feeds[i]['video']['playlist_v2']['sd'] \
  295. and 'height' in feeds[i]['video']['playlist_v2']['sd'] \
  296. and 'play_url' in feeds[i]['video']['playlist_v2']['sd']:
  297. video_width = feeds[i]['video']['playlist_v2']['sd']['width']
  298. video_height = feeds[i]['video']['playlist_v2']['sd']['height']
  299. video_url = feeds[i]['video']['playlist_v2']['sd']['play_url']
  300. elif 'playlist_v2' in feeds[i]['video'] and 'sd' in feeds[i]['video']['playlist_v2'] \
  301. and 'width' in feeds[i]['video']['playlist_v2']['sd'] \
  302. and 'height' in feeds[i]['video']['playlist_v2']['sd'] \
  303. and 'url' in feeds[i]['video']['playlist_v2']['sd']:
  304. video_width = feeds[i]['video']['playlist_v2']['sd']['width']
  305. video_height = feeds[i]['video']['playlist_v2']['sd']['height']
  306. video_url = feeds[i]['video']['playlist_v2']['sd']['url']
  307. else:
  308. video_width = 0
  309. video_height = 0
  310. video_url = 0
  311. # cover_url
  312. if 'video' not in feeds[i]:
  313. cover_url = 0
  314. elif 'thumbnail' not in feeds[i]['video']:
  315. cover_url = 0
  316. else:
  317. cover_url = feeds[i]['video']['thumbnail']
  318. # user_name / uid / user_type / url_token / avatar_url
  319. if 'author' not in feeds[i]:
  320. user_name = 0
  321. uid = 0
  322. user_type = 0
  323. url_token = 0
  324. avatar_url = 0
  325. elif 'author' in feeds[i] \
  326. and 'name' in feeds[i]['author'] \
  327. and 'uid' in feeds[i]['author'] \
  328. and 'user_type' in feeds[i]['author'] \
  329. and 'url_token' in feeds[i]['author'] \
  330. and 'avatar_url_template' in feeds[i]['author']:
  331. user_name = feeds[i]['author']['name']
  332. uid = feeds[i]['author']['uid']
  333. user_type = feeds[i]['author']['user_type']
  334. url_token = feeds[i]['author']['url_token']
  335. avatar_url = feeds[i]['author']['avatar_url_template']
  336. elif 'author' in feeds[i] \
  337. and 'name' in feeds[i]['author'] \
  338. and 'uid' in feeds[i]['author'] \
  339. and 'user_type' in feeds[i]['author'] \
  340. and 'url_token' in feeds[i]['author'] \
  341. and 'avatar_url' in feeds[i]['author']:
  342. user_name = feeds[i]['author']['name']
  343. uid = feeds[i]['author']['uid']
  344. user_type = feeds[i]['author']['user_type']
  345. url_token = feeds[i]['author']['url_token']
  346. avatar_url = feeds[i]['author']['avatar_url']
  347. else:
  348. user_name = 0
  349. uid = 0
  350. user_type = 0
  351. url_token = 0
  352. avatar_url = 0
  353. Common.logger(log_type).info('video_title:{}', video_title)
  354. Common.logger(log_type).info('duration:{}秒', int(duration))
  355. Common.logger(log_type).info(
  356. 'publish_time:{}', time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(publish_time)))
  357. if video_title == 0 or cover_url == 0 or video_url == 0:
  358. Common.logger(log_type).info('无效视频\n')
  359. elif int(time.time()) - int(publish_time) >= 3600 * 24 * 30:
  360. Common.logger(log_type).info('发布时间超过30天\n')
  361. cls.offset = 0
  362. return
  363. elif int(duration) < 60:
  364. Common.logger(log_type).info('时长{}<60秒\n', int(duration))
  365. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'zhihu', '8871e3') for x in
  366. y]:
  367. Common.logger(log_type).info('视频已下载\n')
  368. elif str(video_id) in [x for y in Feishu.get_values_batch(log_type, 'zhihu', '4MGuux') for x in
  369. y]:
  370. Common.logger(log_type).info('视频已下载\n')
  371. else:
  372. Common.download_method(log_type, 'cover', video_title, cover_url)
  373. Common.download_method(log_type, 'video', video_title, video_url)
  374. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  375. with open("./videos/" + video_title + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
  376. f_a.write(str(video_id) + "\n" +
  377. str(video_title) + "\n" +
  378. str(int(duration)) + "\n" +
  379. str(play_cnt) + "\n" +
  380. str(comment_cnt) + "\n" +
  381. str(like_cnt) + "\n" +
  382. str(share_cnt) + "\n" +
  383. str(video_width) + '*' + str(video_height) + "\n" +
  384. str(publish_time) + "\n" +
  385. str(user_name) + "\n" +
  386. str(avatar_url) + "\n" +
  387. str(video_url) + "\n" +
  388. str(cover_url) + "\n" +
  389. "zhihu_follow" + str(int(time.time())))
  390. Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
  391. our_video_id = Publish.upload_and_publish(log_type, env, our_uid)
  392. if env == 'dev':
  393. our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(
  394. our_video_id) + "/info"
  395. else:
  396. our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
  397. our_video_id) + "/info"
  398. Common.logger(log_type).info("视频上传完成:{}", video_title)
  399. Feishu.insert_columns(log_type, 'zhihu', '4MGuux', 'ROWS', 1, 2)
  400. time.sleep(1)
  401. upload_time = int(time.time())
  402. values = [[
  403. time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
  404. "定向榜",
  405. video_title,
  406. str(video_id),
  407. our_video_link,
  408. play_cnt,
  409. comment_cnt,
  410. like_cnt,
  411. share_cnt,
  412. voteup_cnt,
  413. int(duration),
  414. str(video_width) + '*' + str(video_height),
  415. time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(int(publish_time))),
  416. user_name,
  417. uid,
  418. 'https://www.zhihu.com/' + user_type + '/' + url_token,
  419. user_type,
  420. url_token,
  421. avatar_url,
  422. cover_url,
  423. video_url]]
  424. Feishu.update_values(log_type, 'zhihu', "4MGuux", "F2:Z2", values)
  425. Common.logger(log_type).info("视频已保存至云文档:{}\n", video_title)
  426. if is_end is True:
  427. Common.logger(log_type).info('到底了\n')
  428. cls.offset = 0
  429. return
  430. except Exception as e:
  431. Common.logger(log_type).error('get_follow_feeds异常:{}', e)
  432. @classmethod
  433. def get_all_users_feeds(cls, log_type, env):
  434. try:
  435. user_list = cls.get_users_from_feishu(log_type)
  436. if len(user_list) == 0:
  437. Common.logger(log_type).info('定向用户列表为空\n')
  438. else:
  439. for k, v in user_list.items():
  440. user_name = k
  441. url_token = v.split(',')[0]
  442. referer = v.split(',')[1]
  443. our_uid = v.split(',')[2]
  444. Common.logger(log_type).info('开始抓取 {} 主页视频\n', user_name)
  445. cls.get_follow_feeds(log_type, url_token, referer, our_uid, env)
  446. Common.logger(log_type).info('{} 主页视频抓取完毕,休眠 60 秒\n', user_name)
  447. cls.offset = 0
  448. time.sleep(60)
  449. except Exception as e:
  450. Common.logger(log_type).error('get_all_users_feeds异常:{}', e)
  451. if __name__ == '__main__':
  452. ZhihuFollow.get_all_users_feeds('follow', 'dev')
  453. pass