shipinhao_recommend.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # @Author: wangkun
  2. # @Time: 3月 02, 2022
  3. import os
  4. import sys
  5. import time
  6. from appium import webdriver
  7. from appium.webdriver.webdriver import WebDriver
  8. # from selenium.common import NoSuchElementException
  9. from selenium.common.exceptions import NoSuchElementException
  10. from selenium.webdriver.common.by import By
  11. sys.path.append(os.getcwd())
  12. # from crawler_shipinhao.main.common import Common
  13. # from crawler_shipinhao.main.feishu_lib import Feishu
  14. from main.common import Common
  15. from main.feishu_lib import Feishu
  16. class Recommend:
  17. # 启动微信,并打开视频号
  18. @classmethod
  19. def start_wechat(cls, log_type):
  20. try:
  21. Common.logger(log_type).info('启动微信')
  22. caps = {
  23. "platformName": "Android", # 手机操作系统 Android / iOS
  24. "deviceName": "Android", # 连接的设备名(模拟器或真机),安卓可以随便写
  25. "platforVersion": "11", # 手机对应的系统版本(Android 11)
  26. "appPackage": "com.tencent.mm", # 被测APP的包名,乐活圈 Android
  27. "appActivity": ".ui.LauncherUI", # 启动的Activity名
  28. "autoGrantPermissions": "true", # 让 appium 自动授权 base 权限,
  29. # 如果 noReset 为 True,则该条不生效(该参数为 Android 独有),对应的值为 True 或 False
  30. "unicodekeyboard": True, # 使用自带输入法,输入中文时填True
  31. "resetkeyboard": True, # 执行完程序恢复原来输入法
  32. "noReset": True, # 不重置APP
  33. "printPageSourceOnFailure": True, # 找不到元素时,appium log 会完整记录当前页面的 pagesource
  34. "newCommandTimeout": 6000, # 初始等待时间
  35. "automationName": "UiAutomator2" # 使用引擎,默认为 Appium,
  36. # 其中 Appium、UiAutomator2、Selendroid、Espresso 用于 Android,XCUITest 用于 iOS
  37. }
  38. # global driver
  39. driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
  40. driver.implicitly_wait(10)
  41. Common.logger(log_type).info('点击发现TAB')
  42. driver.find_elements(By.ID, 'com.tencent.mm:id/f2s')[2].click()
  43. Common.logger(log_type).info('点击视频号\n')
  44. driver.find_elements(By.ID, 'com.tencent.mm:id/gv6')[1].click()
  45. time.sleep(5)
  46. cls.get_feeds(log_type, driver)
  47. Common.logger(log_type).info('休眠 3s')
  48. time.sleep(3)
  49. cls.quit(log_type, driver)
  50. except Exception as e:
  51. Common.logger(log_type).error('start_wechat异常', e)
  52. # 退出 APP
  53. @classmethod
  54. def quit(cls, log_type, driver: WebDriver):
  55. driver.quit()
  56. Common.logger(log_type).info('退出 APP 成功\n')
  57. # 下载规则
  58. @staticmethod
  59. def download_rule(duration, like_cnt, share_cnt, favorite_cnt, comment_cnt):
  60. if int(duration) >= 10:
  61. if int(like_cnt) >= 0:
  62. if int(share_cnt) >= 0:
  63. if int(favorite_cnt) >= 0:
  64. if int(comment_cnt) >= 0:
  65. return True
  66. else:
  67. return False
  68. else:
  69. return False
  70. else:
  71. return False
  72. else:
  73. return False
  74. else:
  75. return False
  76. # 操作安卓手机,自己滑动首页视频,并获取视频信息
  77. @classmethod
  78. def get_feeds(cls, log_type, driver: WebDriver):
  79. try:
  80. driver.implicitly_wait(10)
  81. for i in range(5):
  82. # # 关闭页面弹窗
  83. # try:
  84. # driver.find_element(By.XPATH, '//*[@text="我知道了"]').click()
  85. # driver.find_element(By.XPATH, '//*[@text="知道了"]').click()
  86. # Common.logger(log_type).info('已关闭未成年模式弹窗')
  87. # except NoSuchElementException:
  88. # pass
  89. #
  90. # try:
  91. # driver.find_element(By.ID, 'com.tencent.mm:id/dkf')
  92. # Common.logger(log_type).info('直播视频,向上滑动页面\n')
  93. # driver.swipe(500, 1500, 500, 500, 300)
  94. # except NoSuchElementException:
  95. # pass
  96. # 视频标题
  97. try:
  98. title_id = driver.find_element(By.ID, 'com.tencent.mm:id/ki5')
  99. video_title = title_id.get_attribute('name').split('\n')[0].replace('#', '').strip()
  100. except NoSuchElementException:
  101. video_title = ''
  102. driver.swipe(500, 1500, 500, 500, 300)
  103. # 点击播放器,获取视频时长
  104. # Common.logger(log_type).info('暂停播放')
  105. pause_btn = driver.find_element(By.ID, 'com.tencent.mm:id/eh4')
  106. pause_btn.click()
  107. start_time = driver.find_element(By.ID, 'com.tencent.mm:id/l59').get_attribute('name')
  108. start_time = int(start_time.split(':')[0])*60 + int(start_time.split(':')[-1])
  109. try:
  110. end_time = driver.find_element(By.ID, 'com.tencent.mm:id/l7i').get_attribute('name')
  111. except NoSuchElementException:
  112. end_time = driver.find_element(By.ID, 'com.tencent.mm:id/g73').get_attribute('name')
  113. end_time = int(end_time.split(':')[0]) * 60 + int(end_time.split(':')[-1])
  114. duration = start_time + end_time
  115. # 点赞
  116. like_id = driver.find_element(By.ID, 'com.tencent.mm:id/k04')
  117. like_cnt = like_id.get_attribute('name')
  118. if like_cnt == "" or like_cnt == "喜欢":
  119. like_cnt = 0
  120. elif '万' in like_cnt:
  121. like_cnt = float(like_cnt.split('万')[0]) * 10000
  122. elif '万+' in like_cnt:
  123. like_cnt = float(like_cnt.split('万+')[0]) * 10000
  124. else:
  125. like_cnt = float(like_cnt)
  126. # 分享
  127. share_id = driver.find_element(By.ID, 'com.tencent.mm:id/jhv')
  128. share_cnt = share_id.get_attribute('name')
  129. if share_cnt == "" or share_cnt == "转发":
  130. share_cnt = 0
  131. elif '万' in share_cnt:
  132. share_cnt = float(share_cnt.split('万')[0]) * 10000
  133. elif '万+' in share_cnt:
  134. share_cnt = float(share_cnt.split('万+')[0]) * 10000
  135. else:
  136. share_cnt = float(share_cnt)
  137. # 收藏
  138. favorite_id = driver.find_element(By.ID, 'com.tencent.mm:id/fnp')
  139. favorite_cnt = favorite_id.get_attribute('name')
  140. if favorite_cnt == "" or favorite_cnt == "收藏":
  141. favorite_cnt = 0
  142. elif '万' in favorite_cnt:
  143. favorite_cnt = float(favorite_cnt.split('万')[0]) * 10000
  144. elif '万+' in favorite_cnt:
  145. favorite_cnt = float(favorite_cnt.split('万+')[0]) * 10000
  146. else:
  147. favorite_cnt = float(favorite_cnt)
  148. # 评论
  149. comment_id = driver.find_element(By.ID, 'com.tencent.mm:id/bje')
  150. comment_cnt = comment_id.get_attribute('name')
  151. if comment_cnt == "" or comment_cnt == "评论":
  152. comment_cnt = 0
  153. elif '万' in comment_cnt:
  154. comment_cnt = float(comment_cnt.split('万')[0]) * 10000
  155. elif '万+' in comment_cnt:
  156. comment_cnt = float(comment_cnt.split('万+')[0]) * 10000
  157. else:
  158. comment_cnt = float(comment_cnt)
  159. # 用户名
  160. username_id = driver.find_element(By.ID, 'com.tencent.mm:id/hft')
  161. user_name = username_id.get_attribute('name')
  162. Common.logger(log_type).info('video_title:{}', video_title)
  163. Common.logger(log_type).info('duration:{}', duration)
  164. Common.logger(log_type).info('like_cnt:{}', like_cnt)
  165. Common.logger(log_type).info('share_cnt:{}', share_cnt)
  166. Common.logger(log_type).info('favorite_cnt:{}', favorite_cnt)
  167. Common.logger(log_type).info('comment_cnt:{}', comment_cnt)
  168. Common.logger(log_type).info('user_name:{}', user_name)
  169. # 判断无效视频
  170. if video_title == '' or user_name == '':
  171. Common.logger(log_type).info('向上滑动页面')
  172. driver.swipe(500, 1500, 500, 500, 300)
  173. Common.logger(log_type).info('无效视频\n')
  174. # 判断下载规则
  175. elif cls.download_rule(duration, like_cnt, share_cnt, favorite_cnt, comment_cnt) is False:
  176. Common.logger(log_type).info('向上滑动页面')
  177. driver.swipe(500, 1500, 500, 500, 300)
  178. Common.logger(log_type).info('不满足抓取规则\n')
  179. # 已下载表去重
  180. elif str(video_title) in [x for y in Feishu.get_values_batch(log_type, 'shipinhao', 'c77cf9') for x in y]:
  181. Common.logger(log_type).info('向上滑动页面')
  182. driver.swipe(500, 1500, 500, 500, 300)
  183. Common.logger(log_type).info('视频已下载\n')
  184. # feeds 表去重
  185. elif str(video_title) in [x for y in Feishu.get_values_batch(log_type, 'shipinhao', 'FSDlBy') for x in y]:
  186. Common.logger(log_type).info('向上滑动页面')
  187. driver.swipe(500, 1500, 500, 500, 300)
  188. Common.logger(log_type).info('视频已存在\n')
  189. # 分享给 windows 爬虫机
  190. else:
  191. share_id.click()
  192. driver.find_element(By.XPATH, '//*[@text="转发给朋友"]').click()
  193. driver.find_element(By.XPATH, '//*[@text="爬虫群"]').click()
  194. driver.find_element(By.ID, 'com.tencent.mm:id/guw').click()
  195. # Click.click_video(log_type, video_title)
  196. # time.sleep(1)
  197. # urls = GetUrl.get_url(log_type)
  198. # if urls is None:
  199. # Common.logger(log_type).info('未获取到视频 URL')
  200. # else:
  201. # video_url = urls[0]
  202. # cover_url = urls[1]
  203. # 把视频信息写入飞书feeds文档
  204. Feishu.insert_columns(log_type, 'shipinhao', 'FSDlBy', 'ROWS', 1, 2)
  205. get_feeds_time = int(time.time())
  206. values = [[time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(get_feeds_time)),
  207. '推荐榜',
  208. str(video_title),
  209. duration,
  210. like_cnt,
  211. share_cnt,
  212. favorite_cnt,
  213. comment_cnt,
  214. str(user_name)]]
  215. time.sleep(1)
  216. Feishu.update_values(log_type, 'shipinhao', 'FSDlBy', 'A2:Z2', values)
  217. Common.logger(log_type).info('向上滑动页面')
  218. # driver.swipe(500, 1500, 500, 500, 300)
  219. Common.logger(log_type).info('视频信息写入飞书文档成功\n')
  220. while True:
  221. if Feishu.get_values_batch(log_type, 'shipinhao', 'FSDlBy')[1][11] is None:
  222. Common.logger(log_type).info('等待更新 URL 信息')
  223. time.sleep(10)
  224. else:
  225. Common.logger(log_type).info('URL 信息已更新\n')
  226. driver.swipe(500, 1500, 500, 500, 300)
  227. break
  228. except Exception as e:
  229. Common.logger(log_type).error('get_feeds异常', e)
  230. driver.swipe(500, 1500, 500, 500, 300)
  231. if __name__ == '__main__':
  232. Recommend.start_wechat('recommend')
  233. # print(Feishu.get_values_batch('log_type', 'shipinhao', 'FSDlBy')[1][11])
  234. pass