zhihu_follow.py 30 KB

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