Browse Source

add weixinzhishu: search_key

wangkun 2 years ago
parent
commit
af66f8ce39

BIN
.DS_Store


+ 4 - 0
common/feishu.py

@@ -64,6 +64,8 @@ class Feishu:
     crawler_gongzhonghao = 'https://w42nne6hzg.feishu.cn/sheets/shtcna98M2mX7TbivTj9Sb7WKBN?'
     # YouTube
     crawler_youtube = 'https://w42nne6hzg.feishu.cn/sheets/shtcnrLyr1zbYbhhZyqpN7Xrd5f?'
+    # 微信指数
+    weixinzhishu = 'https://w42nne6hzg.feishu.cn/sheets/shtcnqhMRUGunIfGnGXMOBYiy4K?'
 
     # 手机号
     wangkun = "13426262515"
@@ -123,6 +125,8 @@ class Feishu:
             return 'shtcna98M2mX7TbivTj9Sb7WKBN'
         elif crawler == 'youtube':
             return 'shtcnrLyr1zbYbhhZyqpN7Xrd5f'
+        elif crawler == 'weixinzhishu':
+            return 'shtcnqhMRUGunIfGnGXMOBYiy4K'
 
     # 获取飞书api token
     @classmethod

BIN
weixinzhishu/.DS_Store


File diff suppressed because it is too large
+ 0 - 0
weixinzhishu/weixinzhishu_chlsfiles/charles202302102119.chlsj


BIN
weixinzhishu/weixinzhishu_main/.DS_Store


+ 0 - 109
weixinzhishu/weixinzhishu_main/get_search_key.py

@@ -1,109 +0,0 @@
-# -*- coding: utf-8 -*-
-# @Author: wangkun
-# @Time: 2023/2/10
-import json
-import os
-import sys
-import time
-import psutil as psutil
-from appium import webdriver
-from selenium.webdriver.common.by import By
-sys.path.append(os.getcwd())
-from common.common import Common
-
-
-class Searchkey:
-
-    @classmethod
-    def start_wechat(cls, log_type, crawler):
-        try:
-            # Common.logger(log_type, crawler).info('启动"微信"')
-            print('启动"微信"')
-            desired_caps = {'app': r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"}
-            driver = webdriver.Remote(
-                command_executor='http://127.0.0.1:4723',
-                desired_capabilities=desired_caps)
-            driver.implicitly_wait(10)
-
-            # Common.logger(log_type).info('点击"聊天窗口"')
-            # driver.find_element(By.NAME, '聊天').click()
-            #
-            # Common.logger(log_type).info('点击"爬虫群"')
-            # driver.find_elements(By.NAME, '爬虫群')[0].click()
-
-            # Common.logger(log_type, crawler).info('点击微信指数')
-            print('点击微信指数')
-            driver.find_elements(By.NAME, '消息')[-1].click()
-
-            # Common.logger(log_type, crawler).info('休眠 10 秒,退出微信指数')
-            # time.sleep(10)
-            # cls.kill_pid(log_type)
-            print("3秒后退出微信")
-            time.sleep(3)
-            # Common.logger(log_type, crawler).info('退出微信')
-            driver.quit()
-        except Exception as e:
-            Common.logger(log_type, crawler).error('click_video异常:{}', e)
-
-    @classmethod
-    def get_url(cls, log_type, crawler):
-        try:
-            # charles 抓包文件保存目录
-            charles_file_dir = r"./weixinzhishu_chlsfiles/"
-
-            if len(os.listdir(charles_file_dir)) == 0:
-                Common.logger(log_type).info("未找到chlsfile文件,等待2s")
-                time.sleep(2)
-            else:
-                # 目标文件夹下所有文件
-                all_file = sorted(os.listdir(charles_file_dir))
-
-                # 获取到目标文件
-                old_file = all_file[-1]
-
-                # 分离文件名与扩展名
-                new_file = os.path.splitext(old_file)
-
-                # 重命名文件后缀
-                os.rename(os.path.join(charles_file_dir, old_file),
-                          os.path.join(charles_file_dir, new_file[0] + ".txt"))
-
-                with open(charles_file_dir + new_file[0] + ".txt", encoding='utf-8-sig', errors='ignore') as f:
-                    contents = json.load(f, strict=False)
-
-                video_url_list = []
-                cover_url_list = []
-
-                if "finder.video.qq.com" in [text['host'] for text in contents]:
-                    for text in contents:
-                        if text["host"] == "finder.video.qq.com" and text["path"] == "/251/20302/stodownload":
-                            video_url_list.append(text)
-                        elif text["host"] == "finder.video.qq.com" and text["path"] == "/251/20304/stodownload":
-                            cover_url_list.append(text)
-
-                    video_url = video_url_list[0]['host']+video_url_list[0]['path']+'?'+video_url_list[0]['query']
-                    cover_url = cover_url_list[0]['host']+cover_url_list[0]['path']+'?'+cover_url_list[0]['query']
-                    head_url = cover_url
-
-                    # print(f'video_url:{video_url}')
-                    # print(f'cover_url:{cover_url}')
-                    # print(f'head_url:{head_url}')
-
-                    return video_url, cover_url, head_url
-                else:
-                    Common.logger(log_type).info("未找到url")
-                    return '未找到url'
-
-        except Exception as e:
-            Common.logger(log_type).exception("get_url异常:{}\n", e)
-            return None
-
-
-if __name__ == '__main__':
-    Searchkey.start_wechat('weixin', 'weixinzhishu')
-    # while True:
-    #     ShipinhaoWindows.run_get_url('recommend')
-    #     Common.del_logs('recommend')
-    #     time.sleep(1)
-
-    pass

+ 1 - 1
weixinzhishu/weixinzhishu_chlsfiles/__init__.py → weixinzhishu/weixinzhishu_main/run_weixinzhishu.py

@@ -1,3 +1,3 @@
 # -*- coding: utf-8 -*-
 # @Author: wangkun
-# @Time: 2023/2/10
+# @Time: 2023/2/13

+ 128 - 18
weixinzhishu/weixinzhishu_main/search_key.py

@@ -1,27 +1,137 @@
 # -*- coding: utf-8 -*-
 # @Author: wangkun
 # @Time: 2023/2/10
+import json
+import os
+import sys
+import time
 from appium import webdriver
 from selenium.webdriver.common.by import By
+sys.path.append(os.getcwd())
+from common.common import Common
+from common.feishu import Feishu
 
 
 class Searchkey:
     @classmethod
-    def start_weixinzhishu(cls):
-        desired_caps = {'app': 'Root', 'deviceName': 'windowsPC', 'platformName': 'Windows'}
-        driver = webdriver.Remote(
-            command_executor='http://127.0.0.1:4723',
-            desired_capabilities=desired_caps)
-
-        main_win = driver.find_element(By.NAME, '微信指数')
-        print(main_win)
-        hd = hex(int(main_win.get_attribute("NativeWindowHandle")))
-        print(hd)
-        caps = {'appTopLevelWindow': str(hd)}
-        driver = webdriver.Remote(
-            command_executor='http://127.0.0.1:4723',
-            desired_capabilities=caps)
-
-
-if __name__ == "__main__":
-    Searchkey.start_weixinzhishu()
+    def start_wechat(cls, log_type, crawler):
+        try:
+            # Common.logger(log_type, crawler).info('启动"微信"')
+            # print('启动"微信"')
+            desired_caps = {'app': r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"}
+            driver = webdriver.Remote(
+                command_executor='http://127.0.0.1:4723',
+                desired_capabilities=desired_caps)
+            driver.implicitly_wait(10)
+
+            # Common.logger(log_type).info('点击"聊天窗口"')
+            # driver.find_element(By.NAME, '聊天').click()
+            #
+            # Common.logger(log_type).info('点击"爬虫群"')
+            # driver.find_elements(By.NAME, '爬虫群')[0].click()
+
+            # Common.logger(log_type, crawler).info('点击微信指数')
+            driver.find_elements(By.NAME, '消息')[-1].click()
+            # Common.logger(log_type, crawler).info('休眠 3 秒,退出微信')
+            time.sleep(3)
+            driver.quit()
+        except Exception as e:
+            Common.logger(log_type, crawler).error(f'click_video异常:{e}\n')
+
+    @classmethod
+    def get_search_key(cls, log_type, crawler):
+        try:
+            # charles 抓包文件保存目录
+            chlsfile_path = f"./{crawler}/{crawler}_chlsfiles/"
+            # chlsfile_path = f"../weixinzhishu_chlsfiles/"
+            all_file = os.listdir(chlsfile_path)
+            chlsfile_list = []
+            if len(all_file) == 0:
+                Common.logger(log_type, crawler).info("chlsfile文件夹为空,等待2s")
+                time.sleep(2)
+                cls.get_search_key(log_type, crawler)
+            else:
+                for file in all_file:
+                    if file.endswith("chlsj") is False:
+                        os.remove(f"./{crawler}/{crawler}_chlsfiles/{file}")
+                    else:
+                        chlsfile_list.append(file)
+            if len(chlsfile_list) == 0:
+                Common.logger(log_type, crawler).info('未找到chlsfile文件,重新获取')
+                cls.get_search_key(log_type, crawler)
+            else:
+                # 获取最新的 chlsfile
+                chlsfile = chlsfile_list[-1]
+                # 分离文件名与扩展名
+                new_file = os.path.splitext(chlsfile)
+
+                # 重命名文件后缀
+                os.rename(os.path.join(chlsfile_path, chlsfile),
+                          os.path.join(chlsfile_path, new_file[0] + ".txt"))
+
+                with open(f"{chlsfile_path}{new_file[0]}.txt", encoding='utf-8-sig', errors='ignore') as f:
+                    contents = json.load(f, strict=False)
+
+                if "search.weixin.qq.com" not in [text['host'] for text in contents]:
+                    return "未找到search_key"
+                else:
+                    for content in contents:
+                        if content["host"] == "search.weixin.qq.com" and content["path"] == "/cgi-bin/wxaweb/wxindexgetusergroup":
+                            text = content['request']['body']['text']
+                            search_key = json.loads(text)['search_key']
+                            return search_key
+        except Exception as e:
+            Common.logger(log_type, crawler).exception(f"get_search_key异常:{e}\n")
+            return None
+
+    @classmethod
+    def remove_file(cls, log_type, crawler):
+        try:
+            all_file_path = f"./{crawler}/{crawler}_chlsfiles/"
+            if not os.path.exists(all_file_path):
+                os.mkdir(all_file_path)
+            all_file = os.listdir(f"./{crawler}/{crawler}_chlsfiles/")
+            for file in all_file:
+                os.remove(f"./{crawler}/{crawler}_chlsfiles/{file}")
+        except Exception as e:
+            Common.logger(log_type, crawler).error(f"remove_file异常:{e}\n")
+
+    @classmethod
+    def del_search_key_from_feishu(cls, log_type, crawler):
+        try:
+            sheet = Feishu.get_values_batch(log_type, crawler, 'sVL74k')
+            print(len(sheet))
+            if len(sheet) <= 21:
+                # print('<=20行')
+                return
+            else:
+                Feishu.dimension_range(log_type, crawler, 'sVL74k', 'ROWS', 22, 22)
+                cls.del_search_key_from_feishu(log_type, crawler)
+        except Exception as e:
+            Common.logger(log_type, crawler).error(f"del_search_key_from_feishu异常:{e}\n")
+
+    @classmethod
+    def write_search_key_to_feishu(cls, log_type, crawler):
+        Common.logger(log_type, crawler).info('清除 chlsfiles 文件夹')
+        cls.remove_file(log_type, crawler)
+        Common.logger(log_type, crawler).info('启动微信指数小程序')
+        cls.start_wechat(log_type, crawler)
+        Common.logger(log_type, crawler).info('获取 search_key')
+        while True:
+            search_key = cls.get_search_key(log_type, crawler)
+            if search_key is None or search_key == "未找到search_key":
+                time.sleep(3)
+                cls.start_wechat(log_type, crawler)
+                cls.get_search_key(log_type, crawler)
+            else:
+                Feishu.insert_columns(log_type, crawler, 'sVL74k', 'ROWS', 1, 2)
+                time.sleep(1)
+                time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time())))
+                Feishu.update_values(log_type, crawler, 'sVL74k', 'A2:B2', [[time_str, search_key]])
+                cls.del_search_key_from_feishu(log_type, crawler)
+                Common.logger(log_type, crawler).info(f"search_key:{search_key}写入飞书表成功\n")
+                return
+
+if __name__ == '__main__':
+    Searchkey.write_search_key_to_feishu('searchkey', 'weixinzhishu')
+    pass

BIN
youtube/.DS_Store


Some files were not shown because too many files changed in this diff