xiaoniangao_follow.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/3/13
  4. import json
  5. import os
  6. import random
  7. import shutil
  8. import sys
  9. import time
  10. import requests
  11. import urllib3
  12. sys.path.append(os.getcwd())
  13. from common.common import Common
  14. from common.scheduling_db import MysqlHelper
  15. from common.publish import Publish
  16. from common.feishu import Feishu
  17. from common.public import filter_word
  18. proxies = {"http": None, "https": None}
  19. class XiaoniangaoFollow:
  20. platform = "小年糕"
  21. # 小程序个人主页视频列表翻页参数
  22. next_t = None
  23. # 基础门槛规则
  24. @staticmethod
  25. def download_rule(video_dict):
  26. """
  27. 下载视频的基本规则
  28. :param video_dict: 视频信息,字典格式
  29. :return: 满足规则,返回 True;反之,返回 False
  30. """
  31. # 视频时长
  32. if int(float(video_dict['duration'])) >= 40:
  33. # 宽或高
  34. if int(video_dict['video_width']) >= 0 or int(video_dict['video_height']) >= 0:
  35. # 播放量
  36. if int(video_dict['play_cnt']) >= 500:
  37. # 分享量
  38. if int(video_dict['share_cnt']) >= 0:
  39. return True
  40. else:
  41. return False
  42. else:
  43. return False
  44. else:
  45. return False
  46. else:
  47. return False
  48. # 从云文档获取关注用户列表
  49. @classmethod
  50. def get_users(cls):
  51. # try:
  52. # while True:
  53. # follow_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "oNpThi")
  54. # if follow_sheet is None:
  55. # time.sleep(1)
  56. # continue
  57. # if len(follow_sheet) == 1:
  58. # Common.logger(log_type, crawler).info("暂无定向爬取账号")
  59. # else:
  60. # user_list = []
  61. # for i in range(1, len(follow_sheet)):
  62. # profile_id = follow_sheet[i][0]
  63. # profile_mid = follow_sheet[i][1]
  64. # user_name = follow_sheet[i][2]
  65. # user_dict = {
  66. # "profile_id": str(profile_id),
  67. # "profile_mid": str(profile_mid),
  68. # "user_name": user_name,
  69. # }
  70. # user_list.append(user_dict)
  71. # return user_list
  72. # except Exception as e:
  73. # Common.logger(log_type, crawler).error("从云文档获取关注用户列表异常:{}", e)
  74. user_list = [{'profile_id': '43549964', 'profile_mid': '116311065', 'user_name': '青峰明月老人'},
  75. {'profile_id': '43486474', 'profile_mid': '1113556014', 'user_name': '好人一生平安'},
  76. {'profile_id': '43590330', 'profile_mid': '192501730', 'user_name': '风轻云淡'},
  77. {'profile_id': '43559741', 'profile_mid': '90261331', 'user_name': '歌一曲'},
  78. {'profile_id': '43785654', 'profile_mid': '104703232', 'user_name': '涧欣'},
  79. {'profile_id': '44211463', 'profile_mid': '1149063512', 'user_name': '美食分享'},
  80. {'profile_id': '46661040', 'profile_mid': '293457861', 'user_name': '阿妞'},
  81. {'profile_id': '46731019', 'profile_mid': '1059483000', 'user_name': '早睡早起锻炼身体'},
  82. {'profile_id': '46644244', 'profile_mid': '609255292', 'user_name': '雨太漫长'},
  83. {'profile_id': '46553912', 'profile_mid': '1160417287', 'user_name': '葛锡琴'},
  84. {'profile_id': '46802824', 'profile_mid': '1157645694', 'user_name': '留白'},
  85. {'profile_id': '46795509', 'profile_mid': '1161593368', 'user_name': '绒花漫舞'},
  86. {'profile_id': '46837469', 'profile_mid': '1160417275', 'user_name': '苏超灵'},
  87. {'profile_id': '46830553', 'profile_mid': '1160417336', 'user_name': '山里红'},
  88. {'profile_id': '50541284', 'profile_mid': '1210725734', 'user_name': '语琴'},
  89. {'profile_id': '50541428', 'profile_mid': '1222976096', 'user_name': '血性娘子'},
  90. {'profile_id': '50364411', 'profile_mid': '1192924729', 'user_name': '好运莲莲'},
  91. {'profile_id': '50499182', 'profile_mid': '329676836', 'user_name': '花好月圆'},
  92. {'profile_id': '50449938', 'profile_mid': '341183833', 'user_name': '花开富贵'},
  93. {'profile_id': '50199770', 'profile_mid': '1166029432', 'user_name': '兰花草1'},
  94. {'profile_id': '50299362', 'profile_mid': '1013092427', 'user_name': '感悟人生'},
  95. {'profile_id': '50539176', 'profile_mid': '1203033756', 'user_name': '风之别鹤'},
  96. {'profile_id': '50541092', 'profile_mid': '1237693278', 'user_name': '莲心'},
  97. {'profile_id': '50523015', 'profile_mid': '260159327', 'user_name': '微笑😊123'},
  98. {'profile_id': '50216970', 'profile_mid': '1219153129', 'user_name': '秋夕'},
  99. {'profile_id': '50286181', 'profile_mid': '1113733998', 'user_name': '上善若水'},
  100. {'profile_id': '50505135', 'profile_mid': '185546', 'user_name': '秋水揽星河'},
  101. {'profile_id': '50555007', 'profile_mid': '1113739184', 'user_name': '淡墨青衫'},
  102. {'profile_id': '50500343', 'profile_mid': '1072539904', 'user_name': '微笑向明天'},
  103. {'profile_id': '50628735', 'profile_mid': '1142824266', 'user_name': '清爽'},
  104. {'profile_id': '50580367', 'profile_mid': '26055443', 'user_name': '玉儿Syy'},
  105. {'profile_id': '50585024', 'profile_mid': '1105334217', 'user_name': '老树开花🌸'},
  106. {'profile_id': '50380683', 'profile_mid': '63682047', 'user_name': '海王岛浩哥'},
  107. {'profile_id': '46386724', 'profile_mid': '271535337', 'user_name': '雪梅'},
  108. {'profile_id': '50636398', 'profile_mid': '237071458', 'user_name': '看花人'},
  109. {'profile_id': '50624991', 'profile_mid': '1202244309', 'user_name': '无关风月'},
  110. {'profile_id': '50561528', 'profile_mid': '1134790664', 'user_name': '老有所乐'},
  111. {'profile_id': '50612235', 'profile_mid': '1162974507', 'user_name': '花素千昔'},
  112. {'profile_id': '50933310', 'profile_mid': '1237661365', 'user_name': '淡淡浅香'},
  113. {'profile_id': '50492961', 'profile_mid': '1160417339', 'user_name': '胡淑华'},
  114. {'profile_id': '49965729', 'profile_mid': '1160417117', 'user_name': '百姓生活'},
  115. {'profile_id': '50928693', 'profile_mid': '1163097085', 'user_name': '多福'},
  116. {'profile_id': '50819153', 'profile_mid': '224365299', 'user_name': '欢天喜地'},
  117. {'profile_id': '51510752', 'profile_mid': '1160417314', 'user_name': '郝建兵'},
  118. {'profile_id': '51470002', 'profile_mid': '631317964', 'user_name': '九牛二虎'},
  119. {'profile_id': '51466430', 'profile_mid': '177806025', 'user_name': '越来越好'},
  120. {'profile_id': '51480157', 'profile_mid': '1000526700', 'user_name': '光伯伯'},
  121. {'profile_id': '51396620', 'profile_mid': '1160417243', 'user_name': '亚萍'},
  122. {'profile_id': '51538137', 'profile_mid': '154002316', 'user_name': 'hua jean-pierre 許遵訓'},
  123. {'profile_id': '51533676', 'profile_mid': '197908577', 'user_name': '萍水相逢'},
  124. {'profile_id': '51488254', 'profile_mid': '153685063', 'user_name': '英勇'},
  125. {'profile_id': '51425929', 'profile_mid': '1161593426', 'user_name': '百年字号'},
  126. {'profile_id': '51514370', 'profile_mid': '1198361106', 'user_name': '品味人生'},
  127. {'profile_id': '51405010', 'profile_mid': '1161593390', 'user_name': '低调生活'},
  128. {'profile_id': '51535995', 'profile_mid': '690938173', 'user_name': '世界风'},
  129. {'profile_id': '51468900', 'profile_mid': '1242534599', 'user_name': '人生如茶'},
  130. {'profile_id': '51489772', 'profile_mid': '269038284', 'user_name': '韵律'},
  131. {'profile_id': '51377241', 'profile_mid': '194287386', 'user_name': '霹雳雷天发'},
  132. {'profile_id': '51538527', 'profile_mid': '1064479786', 'user_name': '鑫'},
  133. {'profile_id': '51365161', 'profile_mid': '1161593351', 'user_name': '黄金三十分'},
  134. {'profile_id': '51527297', 'profile_mid': '1163011474', 'user_name': '指日东升'},
  135. {'profile_id': '51356555', 'profile_mid': '76036942', 'user_name': '利剑'},
  136. {'profile_id': '51382702', 'profile_mid': '1160417349', 'user_name': '丽丽'},
  137. {'profile_id': '51536925', 'profile_mid': '1160417133', 'user_name': '福海'},
  138. {'profile_id': '51525148', 'profile_mid': '1160417333', 'user_name': '君子兰'},
  139. {'profile_id': '51530896', 'profile_mid': '1162996019', 'user_name': '你好,朋友'},
  140. {'profile_id': '51532418', 'profile_mid': '1162998153', 'user_name': '旭日东升'},
  141. {'profile_id': '51329950', 'profile_mid': '285062559', 'user_name': '阿静'},
  142. {'profile_id': '51512535', 'profile_mid': '114981390', 'user_name': '泽南'},
  143. {'profile_id': '51480066', 'profile_mid': '1160417334', 'user_name': '克平'},
  144. {'profile_id': '51520908', 'profile_mid': '1161593408', 'user_name': '永生'},
  145. {'profile_id': '51496944', 'profile_mid': '1160417145', 'user_name': '一生快乐'},
  146. {'profile_id': '51511833', 'profile_mid': '1105154277', 'user_name': '老周.物流全国.'},
  147. {'profile_id': '51515937', 'profile_mid': '1178673137', 'user_name': '风起风落'},
  148. {'profile_id': '51539903', 'profile_mid': '1175023459', 'user_name': '花开福贵 &吉祥'},
  149. {'profile_id': '51394812', 'profile_mid': '1160417173', 'user_name': '尔东'},
  150. {'profile_id': '51512298', 'profile_mid': '22171304', 'user_name': '张杰真'},
  151. {'profile_id': '51472642', 'profile_mid': '175855074', 'user_name': '张顺淑'},
  152. {'profile_id': '51548416', 'profile_mid': '1116847111', 'user_name': '沁水百合'},
  153. {'profile_id': '51456595', 'profile_mid': '1075862992', 'user_name': '永保青春'},
  154. {'profile_id': '51502433', 'profile_mid': '1162980250', 'user_name': '午夜想念'},
  155. {'profile_id': '51495781', 'profile_mid': '1162961480', 'user_name': '快乐每一天'},
  156. {'profile_id': '51527180', 'profile_mid': '1161593427', 'user_name': '彩云追月'},
  157. {'profile_id': '51537082', 'profile_mid': '1163005063', 'user_name': '峥嵘岁月'},
  158. {'profile_id': '51510904', 'profile_mid': '64295407', 'user_name': '文文'},
  159. {'profile_id': '51497047', 'profile_mid': '59349938', 'user_name': '老马识途'},
  160. {'profile_id': '51544604', 'profile_mid': '1160417260', 'user_name': '星'},
  161. {'profile_id': '51411909', 'profile_mid': '801020924', 'user_name': '相思鸟'},
  162. {'profile_id': '51525860', 'profile_mid': '1160417222', 'user_name': '老来得福'},
  163. {'profile_id': '51540902', 'profile_mid': '1163003217', 'user_name': '银发中年'},
  164. {'profile_id': '51526760', 'profile_mid': '295640510', 'user_name': '阿中'},
  165. {'profile_id': '51529247', 'profile_mid': '674650199', 'user_name': '张恩培'},
  166. {'profile_id': '51540032', 'profile_mid': '18103420', 'user_name': '吴希孟'},
  167. {'profile_id': '51491818', 'profile_mid': '1162991035', 'user_name': '幸福每一天'},
  168. {'profile_id': '51539843', 'profile_mid': '210721352', 'user_name': '掉地人参果'},
  169. {'profile_id': '51521237', 'profile_mid': '1160417237', 'user_name': '山野村夫'},
  170. {'profile_id': '51531438', 'profile_mid': '103235841', 'user_name': '开心'},
  171. {'profile_id': '51528239', 'profile_mid': '1160417288', 'user_name': '戴玉亭'},
  172. {'profile_id': '51542817', 'profile_mid': '126121769', 'user_name': '芳芳'},
  173. {'profile_id': '51541108', 'profile_mid': '287637208', 'user_name': '心无旁骛'},
  174. {'profile_id': '51462517', 'profile_mid': '247008734', 'user_name': '华'},
  175. {'profile_id': '51543209', 'profile_mid': '1214482340', 'user_name': '丽'},
  176. {'profile_id': '51509548', 'profile_mid': '1162977406', 'user_name': '绚丽多彩'},
  177. {'profile_id': '51536733', 'profile_mid': '1161593366', 'user_name': '夕阳红'},
  178. {'profile_id': '51519407', 'profile_mid': '1122750580', 'user_name': '幸福满堂'},
  179. {'profile_id': '51459413', 'profile_mid': '348958961', 'user_name': '段平清'},
  180. {'profile_id': '51487820', 'profile_mid': '605290310', 'user_name': '青青'},
  181. {'profile_id': '51469080', 'profile_mid': '131475097', 'user_name': '手宝'},
  182. {'profile_id': '51426171', 'profile_mid': '196597807', 'user_name': '妙'},
  183. {'profile_id': '51470293', 'profile_mid': '78386273', 'user_name': '芳草'},
  184. {'profile_id': '51505305', 'profile_mid': '172977522', 'user_name': '伟星'},
  185. {'profile_id': '51516061', 'profile_mid': '913804992', 'user_name': '荷塘月色'},
  186. {'profile_id': '51541167', 'profile_mid': '1100776170', 'user_name': '闪电⚡️'},
  187. {'profile_id': '51391450', 'profile_mid': '982518039', 'user_name': '吕梁'},
  188. {'profile_id': '51529786', 'profile_mid': '168833501', 'user_name': ':shen. ser'},
  189. {'profile_id': '51416422', 'profile_mid': '376557509', 'user_name': '于孝震'},
  190. {'profile_id': '51342944', 'profile_mid': '556796156', 'user_name': '☀吉祥☀'},
  191. {'profile_id': '51536144', 'profile_mid': '1162956349', 'user_name': '凤舞九天'},
  192. {'profile_id': '51533164', 'profile_mid': '153787914', 'user_name': '红梅'},
  193. {'profile_id': '51450974', 'profile_mid': '20396528', 'user_name': '学友'},
  194. {'profile_id': '51512622', 'profile_mid': '35883962', 'user_name': '珎珎'},
  195. {'profile_id': '51525737', 'profile_mid': '69546032', 'user_name': '欣然'},
  196. {'profile_id': '51414093', 'profile_mid': '1154645646', 'user_name': '老中医'},
  197. {'profile_id': '51372351', 'profile_mid': '154481426', 'user_name': '寻花'},
  198. {'profile_id': '51511869', 'profile_mid': '1162958849', 'user_name': '保持微笑'},
  199. {'profile_id': '51367397', 'profile_mid': '173682337', 'user_name': '莲'},
  200. {'profile_id': '51494361', 'profile_mid': '1161593403', 'user_name': '红叶'},
  201. {'profile_id': '51352260', 'profile_mid': '31040287', 'user_name': '52部队'},
  202. {'profile_id': '51430672', 'profile_mid': '69886497', 'user_name': '张俊宽'},
  203. {'profile_id': '51440076', 'profile_mid': '1186145367', 'user_name': '金光'},
  204. {'profile_id': '51431733', 'profile_mid': '112830331', 'user_name': '幸福安康'},
  205. {'profile_id': '51511957', 'profile_mid': '1214482340', 'user_name': '丽'},
  206. {'profile_id': '51466969', 'profile_mid': '131962346', 'user_name': '丹桂'},
  207. {'profile_id': '51515739', 'profile_mid': '1162950925', 'user_name': '烈火'},
  208. {'profile_id': '51425141', 'profile_mid': '207104529', 'user_name': '雪梅'},
  209. {'profile_id': '51465625', 'profile_mid': '913804992', 'user_name': '荷塘月色'},
  210. {'profile_id': '51470424', 'profile_mid': '690938173', 'user_name': '世界风'},
  211. {'profile_id': '51502065', 'profile_mid': '1105154277', 'user_name': '老周.物流全国.'},
  212. {'profile_id': '51505082', 'profile_mid': '674650199', 'user_name': '张恩培'},
  213. {'profile_id': '51508736', 'profile_mid': '30955754', 'user_name': '硕'},
  214. {'profile_id': '51363432', 'profile_mid': '1160417336', 'user_name': '山里红'},
  215. {'profile_id': '51472849', 'profile_mid': '1160417336', 'user_name': '山里红'},
  216. {'profile_id': '51531002', 'profile_mid': '1149063512', 'user_name': '美食分享'},
  217. {'profile_id': '51369043', 'profile_mid': '1162996019', 'user_name': '你好,朋友'},
  218. {'profile_id': '51488704', 'profile_mid': '1160417201', 'user_name': '伴迪伴你'},
  219. {'profile_id': '51514738', 'profile_mid': '1160417222', 'user_name': '老来得福'},
  220. {'profile_id': '51526888', 'profile_mid': '1160417349', 'user_name': '丽丽'},
  221. {'profile_id': '51513568', 'profile_mid': '1163005063', 'user_name': '峥嵘岁月'},
  222. {'profile_id': '51514871', 'profile_mid': '1161593379', 'user_name': '心无界'},
  223. {'profile_id': '51518737', 'profile_mid': '1160417333', 'user_name': '君子兰'},
  224. {'profile_id': '51525297', 'profile_mid': '1160417115', 'user_name': '绚丽多彩'},
  225. {'profile_id': '51383706', 'profile_mid': '1162953017', 'user_name': '荷塘月色'},
  226. {'profile_id': '51528569', 'profile_mid': '1162977406', 'user_name': '绚丽多彩'},
  227. {'profile_id': '51437102', 'profile_mid': '1162958849', 'user_name': '保持微笑'},
  228. {'profile_id': '51536771', 'profile_mid': '1160417243', 'user_name': '亚萍'},
  229. {'profile_id': '51516969', 'profile_mid': '1161593390', 'user_name': '低调生活'},
  230. {'profile_id': '51535885', 'profile_mid': '1160417243', 'user_name': '亚萍'},
  231. {'profile_id': '51517906', 'profile_mid': '1160417268', 'user_name': '宏蕴'},
  232. {'profile_id': '51530809', 'profile_mid': '1163003217', 'user_name': '银发中年'},
  233. {'profile_id': '51458395', 'profile_mid': '1160417288', 'user_name': '戴玉亭'},
  234. {'profile_id': '51517169', 'profile_mid': '1163601308', 'user_name': '今日祝福'},
  235. {'profile_id': '51515458', 'profile_mid': '1162953017', 'user_name': '荷塘月色'},
  236. {'profile_id': '51498134', 'profile_mid': '1160417262', 'user_name': '快乐每一天'},
  237. {'profile_id': '51436822', 'profile_mid': '1162996019', 'user_name': '你好,朋友'},
  238. {'profile_id': '51466833', 'profile_mid': '1160417349', 'user_name': '丽丽'},
  239. {'profile_id': '51514420', 'profile_mid': '1242534599', 'user_name': '人生如茶'},
  240. {'profile_id': '51329643', 'profile_mid': '1160417133', 'user_name': '福海'},
  241. {'profile_id': '51384888', 'profile_mid': '1160417349', 'user_name': '丽丽'},
  242. {'profile_id': '51382334', 'profile_mid': '1177277385', 'user_name': '丝花'}]
  243. return user_list
  244. # 获取个人主页视频
  245. @classmethod
  246. def get_videoList(cls, log_type, crawler, strategy, p_mid, oss_endpoint, env):
  247. try:
  248. while True:
  249. url = "https://api.xiaoniangao.cn/profile/list_album"
  250. headers = {
  251. # "X-Mid": str(cls.follow_x_mid),
  252. "X-Mid": '1fb47aa7a860d9',
  253. # "X-Token-Id": str(cls.follow_x_token_id),
  254. "X-Token-Id": '9f2cb91f9952c107ecb73642083e1dec-1145266232',
  255. "content-type": "application/json",
  256. # "uuid": str(cls.follow_uid),
  257. "uuid": 'f40c2e7c-3cfb-4804-b513-608c0280268c',
  258. "Accept-Encoding": "gzip,compress,br,deflate",
  259. "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X)"
  260. " AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 "
  261. "MicroMessenger/8.0.20(0x18001435) NetType/WIFI Language/zh_CN",
  262. # "Referer": str(cls.follow_referer)
  263. "Referer": 'https://servicewechat.com/wxd7911e4c177690e4/654/page-frame.html'
  264. }
  265. json_text = {
  266. "visited_mid": str(p_mid),
  267. "start_t": cls.next_t,
  268. "qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!690x385r/crop/690x385/interlace/1/format/jpg",
  269. "h_qs": "imageMogr2/gravity/center/rotate/$/thumbnail/!120x120r/crop/120x120/interlace/1/format/jpg",
  270. "limit": 20,
  271. # "token": str(cls.follow_token),
  272. "token": '54e4c603f7bf3dc009c86b49ed91be36',
  273. # "uid": str(cls.follow_uid),
  274. "uid": 'f40c2e7c-3cfb-4804-b513-608c0280268c',
  275. "proj": "ma",
  276. "wx_ver": "8.0.23",
  277. "code_ver": "3.68.0",
  278. "log_common_params": {
  279. "e": [{
  280. "data": {
  281. "page": "profilePage",
  282. "topic": "public"
  283. }
  284. }],
  285. "ext": {
  286. "brand": "iPhone",
  287. "device": "iPhone 11",
  288. "os": "iOS 14.7.1",
  289. "weixinver": "8.0.23",
  290. "srcver": "2.24.7",
  291. "net": "wifi",
  292. "scene": "1089"
  293. },
  294. "pj": "1",
  295. "pf": "2",
  296. "session_id": "7468cf52-00ea-432e-8505-6ea3ad7ec164"
  297. }
  298. }
  299. urllib3.disable_warnings()
  300. r = requests.post(url=url, headers=headers, json=json_text, proxies=proxies, verify=False)
  301. if 'data' not in r.text or r.status_code != 200:
  302. Common.logger(log_type, crawler).info(f"get_videoList:{r.text}\n")
  303. cls.next_t = None
  304. return
  305. elif 'list' not in r.json()['data']:
  306. Common.logger(log_type, crawler).info(f"get_videoList:{r.json()}\n")
  307. cls.next_t = None
  308. return
  309. elif len(r.json()['data']['list']) == 0:
  310. Common.logger(log_type, crawler).info(f"没有更多数据啦~\n")
  311. cls.next_t = None
  312. return
  313. else:
  314. cls.next_t = r.json()["data"]["next_t"]
  315. feeds = r.json()["data"]["list"]
  316. for i in range(len(feeds)):
  317. # 标题,表情随机加在片头、片尾,或替代句子中间的标点符号
  318. char_sheet = Feishu.get_values_batch("hour", "xiaoniangao", "BhlbST")
  319. expression_list = []
  320. char_list = []
  321. for q in range(len(char_sheet)):
  322. if char_sheet[q][0] is not None:
  323. expression_list.append(char_sheet[q][0])
  324. if char_sheet[q][1] is not None:
  325. char_list.append(char_sheet[q][1])
  326. befor_video_title = feeds[i]["title"].strip().replace("\n", "") \
  327. .replace("/", "").replace("\r", "").replace("#", "") \
  328. .replace(".", "。").replace("\\", "").replace("&NBSP", "") \
  329. .replace(":", "").replace("*", "").replace("?", "") \
  330. .replace("?", "").replace('"', "").replace("<", "") \
  331. .replace(">", "").replace("|", "").replace(" ", "")
  332. expression = random.choice(expression_list)
  333. expression_title_list = [expression + befor_video_title, befor_video_title + expression]
  334. # 标题,表情随机加在片头
  335. title_list1 = random.choice(expression_title_list)
  336. # 标题,表情随机加在片尾
  337. title_list2 = befor_video_title + random.choice(char_list)
  338. # # 替代句子中间的标点符号
  339. # title_list3 = befor_video_title.replace(
  340. # ",", random.choice(expression_list)).replace(",", random.choice(expression_list))
  341. title_list4 = [title_list1, title_list2]
  342. video_title = random.choice(title_list4)
  343. # 用户名
  344. user_name = feeds[i]["album_user"]["nick"].strip().replace("\n", "") \
  345. .replace("/", "").replace("快手", "").replace(" ", "") \
  346. .replace(" ", "").replace("&NBSP", "").replace("\r", "")
  347. # 视频 ID
  348. if "vid" in feeds[i]:
  349. video_id = feeds[i]["vid"]
  350. else:
  351. video_id = 0
  352. # 播放量
  353. if "play_pv" in feeds[i]:
  354. video_play_cnt = feeds[i]["play_pv"]
  355. else:
  356. video_play_cnt = 0
  357. # 点赞
  358. if "total" in feeds[i]["favor"]:
  359. video_like_cnt = feeds[i]["favor"]["total"]
  360. else:
  361. video_like_cnt = 0
  362. # 评论数
  363. if "comment_count" in feeds[i]:
  364. video_comment_cnt = feeds[i]["comment_count"]
  365. else:
  366. video_comment_cnt = 0
  367. # 分享
  368. if "share" in feeds[i]:
  369. video_share_cnt = feeds[i]["share"]
  370. else:
  371. video_share_cnt = 0
  372. # 时长
  373. if "du" in feeds[i]:
  374. video_duration = int(feeds[i]["du"] / 1000)
  375. else:
  376. video_duration = 0
  377. # 发布时间
  378. if "t" in feeds[i]:
  379. publish_time_stamp = int(feeds[i]["t"] / 1000)
  380. else:
  381. publish_time_stamp = 0
  382. publish_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(publish_time_stamp))
  383. # 宽和高
  384. if "w" in feeds[i] or "h" in feeds[i]:
  385. video_width = feeds[i]["w"]
  386. video_height = feeds[i]["h"]
  387. else:
  388. video_width = 0
  389. video_height = 0
  390. # 头像
  391. if "hurl" in feeds[i]["album_user"]:
  392. head_url = feeds[i]["album_user"]["hurl"]
  393. else:
  394. head_url = 0
  395. # 用户 ID
  396. if "id" in feeds[i]:
  397. profile_id = feeds[i]["id"]
  398. else:
  399. profile_id = 0
  400. # 用户 mid
  401. if "mid" in feeds[i]:
  402. profile_mid = feeds[i]["mid"]
  403. else:
  404. profile_mid = 0
  405. # 封面
  406. if "url" in feeds[i]:
  407. cover_url = feeds[i]["url"]
  408. else:
  409. cover_url = 0
  410. # 视频播放地址
  411. if "v_url" in feeds[i]:
  412. video_url = feeds[i]["v_url"]
  413. else:
  414. video_url = 0
  415. # 过滤无效视频
  416. if video_id == 0 \
  417. or video_title == 0 \
  418. or publish_time_stamp == 0 \
  419. or video_duration == 0 \
  420. or video_url == 0:
  421. Common.logger(log_type, crawler).info("无效视频\n")
  422. elif int(time.time()) - publish_time_stamp > 3600 * 24 * 3:
  423. Common.logger(log_type, crawler).info(f"发布时间超过3天:{publish_time_str}\n")
  424. cls.next_t = None
  425. return
  426. else:
  427. video_dict = {
  428. "video_id": video_id,
  429. "video_title": video_title,
  430. "duration": video_duration,
  431. "play_cnt": video_play_cnt,
  432. "like_cnt": video_like_cnt,
  433. "comment_cnt": video_comment_cnt,
  434. "share_cnt": video_share_cnt,
  435. "user_name": user_name,
  436. "publish_time_stamp": publish_time_stamp,
  437. "publish_time_str": publish_time_str,
  438. "video_width": video_width,
  439. "video_height": video_height,
  440. "avatar_url": head_url,
  441. "profile_id": profile_id,
  442. "profile_mid": profile_mid,
  443. "cover_url": cover_url,
  444. "video_url": video_url,
  445. "session": f"xiaoniangao-follow-{int(time.time())}"
  446. }
  447. for k, v in video_dict.items():
  448. Common.logger(log_type, crawler).info(f"{k}:{v}")
  449. cls.download_publish(log_type=log_type,
  450. crawler=crawler,
  451. strategy=strategy,
  452. video_dict=video_dict,
  453. oss_endpoint=oss_endpoint,
  454. env=env)
  455. except Exception as error:
  456. Common.logger(log_type, crawler).error(f"获取个人主页视频异常:{error}\n")
  457. @classmethod
  458. def repeat_video(cls, log_type, crawler, video_id, env):
  459. sql = f""" select * from crawler_video where platform="小年糕" and out_video_id="{video_id}"; """
  460. repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env)
  461. return len(repeat_video)
  462. # 下载/上传
  463. @classmethod
  464. def download_publish(cls, log_type, crawler, strategy, video_dict, oss_endpoint, env):
  465. try:
  466. if cls.download_rule(video_dict) is False:
  467. Common.logger(log_type, crawler).info("不满足基础门槛\n")
  468. elif cls.repeat_video(log_type, crawler, video_dict['video_id'], env) != 0:
  469. Common.logger(log_type, crawler).info('视频已下载\n')
  470. elif any(str(word) if str(word) in video_dict['video_title'] else False for word in filter_word(log_type, crawler, "小年糕", env)) is True:
  471. Common.logger(log_type, crawler).info("视频已中过滤词\n")
  472. else:
  473. # 下载封面
  474. Common.download_method(log_type=log_type, crawler=crawler, text="cover",
  475. title=video_dict["video_title"], url=video_dict["cover_url"])
  476. # 下载视频
  477. Common.download_method(log_type=log_type, crawler=crawler, text="video",
  478. title=video_dict["video_title"], url=video_dict["video_url"])
  479. # 保存视频信息至 "./videos/{download_video_title}/info.txt"
  480. Common.save_video_info(log_type=log_type, crawler=crawler, video_dict=video_dict)
  481. # 上传视频
  482. Common.logger(log_type, crawler).info("开始上传视频...")
  483. our_video_id = Publish.upload_and_publish(log_type=log_type,
  484. crawler=crawler,
  485. strategy=strategy,
  486. our_uid="follow",
  487. env=env,
  488. oss_endpoint=oss_endpoint)
  489. if env == "dev":
  490. our_video_link = f"https://testadmin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
  491. else:
  492. our_video_link = f"https://admin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
  493. Common.logger(log_type, crawler).info("视频上传完成")
  494. if our_video_id is None:
  495. # 删除视频文件夹
  496. shutil.rmtree(f"./{crawler}/videos/{video_dict['video_title']}")
  497. return
  498. # 视频信息保存数据库
  499. rule_dict = {
  500. "duration": {"min": 40, "max": 100000000},
  501. "play_cnt": {"min": 500}
  502. }
  503. insert_sql = f""" insert into crawler_video(video_id,
  504. out_user_id,
  505. platform,
  506. strategy,
  507. out_video_id,
  508. video_title,
  509. cover_url,
  510. video_url,
  511. duration,
  512. publish_time,
  513. play_cnt,
  514. crawler_rule,
  515. width,
  516. height)
  517. values({our_video_id},
  518. "{video_dict['profile_id']}",
  519. "{cls.platform}",
  520. "定向爬虫策略",
  521. "{video_dict['video_id']}",
  522. "{video_dict['video_title']}",
  523. "{video_dict['cover_url']}",
  524. "{video_dict['video_url']}",
  525. {int(video_dict['duration'])},
  526. "{video_dict['publish_time_str']}",
  527. {int(video_dict['play_cnt'])},
  528. '{json.dumps(rule_dict)}',
  529. {int(video_dict['video_width'])},
  530. {int(video_dict['video_height'])}) """
  531. Common.logger(log_type, crawler).info(f"insert_sql:{insert_sql}")
  532. MysqlHelper.update_values(log_type, crawler, insert_sql, env)
  533. Common.logger(log_type, crawler).info('视频信息插入数据库成功!')
  534. # 视频写入飞书
  535. Feishu.insert_columns(log_type, crawler, "Wu0CeL", "ROWS", 1, 2)
  536. # 视频ID工作表,首行写入数据
  537. upload_time = int(time.time())
  538. values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(upload_time)),
  539. "用户主页",
  540. str(video_dict['video_id']),
  541. str(video_dict['video_title']),
  542. our_video_link,
  543. video_dict['play_cnt'],
  544. video_dict['comment_cnt'],
  545. video_dict['like_cnt'],
  546. video_dict['share_cnt'],
  547. video_dict['duration'],
  548. f"{video_dict['video_width']}*{video_dict['video_height']}",
  549. str(video_dict['publish_time_str']),
  550. str(video_dict['user_name']),
  551. str(video_dict['profile_id']),
  552. str(video_dict['profile_mid']),
  553. str(video_dict['avatar_url']),
  554. str(video_dict['cover_url']),
  555. str(video_dict['video_url'])]]
  556. time.sleep(1)
  557. Feishu.update_values(log_type, crawler, "Wu0CeL", "F2:Z2", values)
  558. Common.logger(log_type, crawler).info('视频信息写入飞书成功\n')
  559. except Exception as e:
  560. Common.logger(log_type, crawler).error("下载/上传异常:{}", e)
  561. # 获取所有关注列表的用户视频
  562. @classmethod
  563. def get_follow_videos(cls, log_type, crawler, strategy, oss_endpoint, env):
  564. try:
  565. # 已关注的用户列表 mids
  566. user_list = cls.get_users()
  567. for user in user_list:
  568. user_name = user['user_name']
  569. profile_mid = user['profile_mid']
  570. Common.logger(log_type, crawler).info(f"获取 {user_name} 主页视频")
  571. cls.get_videoList(log_type=log_type,
  572. crawler=crawler,
  573. strategy=strategy,
  574. p_mid=profile_mid,
  575. oss_endpoint=oss_endpoint,
  576. env=env)
  577. cls.next_t = None
  578. time.sleep(1)
  579. except Exception as e:
  580. Common.logger(log_type, crawler).error(f"get_follow_videos:{e}\n")
  581. if __name__ == "__main__":
  582. # print(XiaoniangaoFollow.repeat_video("follow", "xiaoniangao", "4919087666", "prod", "aliyun"))
  583. # print(XiaoniangaoFollow.repeat_video("follow", "xiaoniangao", "4919087666", "dev"))
  584. print(XiaoniangaoFollow.get_users())
  585. pass