wangkun пре 2 година
родитељ
комит
27ae340d2d

+ 3 - 4
README.MD

@@ -3,10 +3,9 @@
 #### 文档
 1. git: https://git.yishihui.com/Server/crawler_ganggangdouchuan.git
 2. 飞书: https://w42nne6hzg.feishu.cn/sheets/shtcnTuJgeZU2bc7VaesAqk3QJx
-3. 需求: 
 
 #### 软件架构
-1. 见 requirements.txt
+1. 见 requirements.txt
 
 #### 使用说明
 1. cd ./crawler_ganggangdouchuan
@@ -14,5 +13,5 @@
 
 #### 需求
 1. 全抓
-2. 运行时间: 
-3. 站内账号: 
+2. 运行时间: 08:00 - 11:59:59
+3. 站内账号: [26117661, 26117662, 26117663]

+ 31 - 0
ganggangdouchuan.sh

@@ -0,0 +1,31 @@
+#!/bin/bash
+echo "开始"
+echo "$(date "+%Y-%m-%d %H:%M:%S") 更新环境变量..."
+cd ~ && source .base_profile
+echo "$(date "+%Y-%m-%d %H:%M:%S") 更新环境变量完成!"
+
+echo "$(date "+%Y-%m-%d %H:%M:%S") 正在杀爬虫进程..."
+# shellcheck disable=SC2009
+ps aux | grep run_ganggangdouchuan.py | grep -v grep | awk '{print $2}' | xargs kill -9
+echo "$(date "+%Y-%m-%d %H:%M:%S") 进程已杀死!"
+
+echo "$(date "+%Y-%m-%d %H:%M:%S") 正在更新代码..."
+cd /Users/lieyunye/Desktop/crawler/crawler_ganggangdouchuan/ && git pull origin master --force && rm -f nohup.log
+echo "$(date "+%Y-%m-%d %H:%M:%S") 代码更新完成!"
+
+echo "$(date "+%Y-%m-%d %H:%M:%S") 正在重启Appium..."
+# shellcheck disable=SC2009
+ps aux | grep Appium.app | grep -v grep | awk '{print $2}' | xargs kill -9
+nohup /usr/local/bin/node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js >>./nohup.log 2>&1 &
+echo "$(date "+%Y-%m-%d %H:%M:%S") 重启Appium完毕!"
+
+echo "$(date "+%Y-%m-%d %H:%M:%S") 正在重启adb..."
+adb kill-server
+adb start-server
+echo "$(date "+%Y-%m-%d %H:%M:%S") 重启adb完毕!"
+
+echo "$(date "+%Y-%m-%d %H:%M:%S") 正在重启爬虫服务..."
+nohup python3 -u main/run_ganggangdouchuan.py >>./nohup.log 2>&1 &
+echo "$(date "+%Y-%m-%d %H:%M:%S") 爬虫服务重启完毕!"
+
+exit 0

+ 10 - 0
git_push.sh

@@ -0,0 +1,10 @@
+#!/bin/bash
+
+echo "开始"
+echo "$(date "+%Y-%m-%d %H:%M:%S") 正在 push..."
+git add .
+git commit -m "update"
+git push origin master
+echo "$(date "+%Y-%m-%d %H:%M:%S") git push 完成!"
+
+exit 0

+ 3 - 0
main/__init__.py

@@ -0,0 +1,3 @@
+# -*- coding: utf-8 -*-
+# @Author: wangkun
+# @Time: 2023/1/9

+ 15 - 0
main/demo.py

@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+# @Author: wangkun
+# @Time: 2023/1/9
+from main.feishu_lib import Feishu
+
+
+class Demo:
+    @classmethod
+    def get_sheet(cls, logt_ype, crawler, sheetid):
+        sheet = Feishu.get_values_batch(logt_ype, crawler, sheetid)
+        print(sheet)
+
+
+if __name__ == '__main__':
+    Demo.get_sheet('demo', 'ggdc', '070a67')

+ 300 - 0
main/feishu_lib.py

@@ -0,0 +1,300 @@
+# -*- coding: utf-8 -*-
+# @Author: wangkun
+# @Time: 2023/1/9
+import json
+import requests
+import urllib3
+from main.ganggangdouchuan_common import Common
+proxies = {"http": None, "https": None}
+
+
+class Feishu:
+    """
+    编辑飞书云文档
+    """
+    # 刚刚都传
+    crawler_ganggangdouchuan = 'https://w42nne6hzg.feishu.cn/sheets/shtcnTuJgeZU2bc7VaesAqk3QJx'
+
+    # 飞书路径token
+    @classmethod
+    def spreadsheettoken(cls, crawler):
+        """
+        :param crawler: 哪个爬虫
+        """
+        if crawler == "kanyikan":
+            return "shtcngRPoDYAi24x52j2nDuHMih"
+        elif crawler == "kuaishou":
+            return "shtcnp4SaJt37q6OOOrYzPMjQkg"
+        elif crawler == "weishi":
+            return "shtcn5YSWg91JfVGzj0SFZIRRPh"
+        elif crawler == "xiaoniangao":
+            return "shtcnYxiyQ1wLklo1W5Kdqc9cGh"
+        elif crawler == "monitor":
+            return "shtcnlZWYazInhf7Z60jkbLRJyd"
+        elif crawler == "bszf":
+            return "shtcnGh2rrsPYM4iVNEBO7OqWrb"
+        elif crawler == "ssyy":
+            return "shtcnz1ymxHL1u8WHblfqfys7qe"
+        elif crawler == "ggdc":
+            return "shtcnTuJgeZU2bc7VaesAqk3QJx"
+
+    # 获取飞书api token
+    @classmethod
+    def get_token(cls, log_type):
+        """
+        获取飞书api token
+        :return:
+        """
+        url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
+        post_data = {"app_id": "cli_a13ad2afa438d00b",  # 这里账号密码是发布应用的后台账号及密码
+                     "app_secret": "4tK9LY9VbiQlY5umhE42dclBFo6t4p5O"}
+
+        try:
+            urllib3.disable_warnings()
+            response = requests.post(url=url, data=post_data, proxies=proxies, verify=False)
+            tenant_access_token = response.json()["tenant_access_token"]
+            return tenant_access_token
+        except Exception as e:
+            Common.logger(log_type).error("获取飞书 api token 异常:{}", e)
+
+    # 获取表格元数据
+    @classmethod
+    def get_metainfo(cls, log_type, crawler):
+        """
+        获取表格元数据
+        :return:
+        """
+        get_metainfo_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                           + cls.spreadsheettoken(crawler) + "/metainfo"
+
+        headers = {
+            "Authorization": "Bearer " + cls.get_token(log_type),
+            "Content-Type": "application/json; charset=utf-8"
+        }
+        params = {
+            "extFields": "protectedRange",  # 额外返回的字段,extFields=protectedRange时返回保护行列信息
+            "user_id_type": "open_id"  # 返回的用户id类型,可选open_id,union_id
+        }
+        try:
+            urllib3.disable_warnings()
+            r = requests.get(url=get_metainfo_url, headers=headers, params=params, proxies=proxies, verify=False)
+            response = json.loads(r.content.decode("utf8"))
+            return response
+        except Exception as e:
+            Common.logger(log_type).error("获取表格元数据异常:{}", e)
+
+    # 读取工作表中所有数据
+    @classmethod
+    def get_values_batch(cls, log_type, crawler, sheetid):
+        """
+        读取工作表中所有数据
+        :param log_type: 启用哪个 log
+        :param crawler: 哪个爬虫
+        :param sheetid: 哪张表
+        :return: 所有数据
+        """
+        get_values_batch_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                               + cls.spreadsheettoken(crawler) + "/values_batch_get"
+        headers = {
+            "Authorization": "Bearer " + cls.get_token(log_type),
+            "Content-Type": "application/json; charset=utf-8"
+        }
+        params = {
+            # 多个查询范围 如 url?ranges=range1,range2 ,其中 range 包含 sheetId 与单元格范围两部分
+            "ranges": sheetid,
+
+            # valueRenderOption=ToString 可返回纯文本的值(数值类型除外);
+            # valueRenderOption=FormattedValue 计算并格式化单元格;
+            # valueRenderOption=Formula单元格中含有公式时返回公式本身;
+            # valueRenderOption=UnformattedValue计算但不对单元格进行格式化
+            "valueRenderOption": "ToString",
+
+            # dateTimeRenderOption=FormattedString 计算并将时间日期按照其格式进行格式化,但不会对数字进行格式化,返回格式化后的字符串。
+            "dateTimeRenderOption": "",
+
+            # 返回的用户id类型,可选open_id,union_id
+            "user_id_type": "open_id"
+        }
+        try:
+            urllib3.disable_warnings()
+            r = requests.get(url=get_values_batch_url, headers=headers, params=params, proxies=proxies, verify=False)
+            # print(r.text)
+            response = json.loads(r.content.decode("utf8"))
+            values = response["data"]["valueRanges"][0]["values"]
+            return values
+        except Exception as e:
+            Common.logger(log_type).error("读取工作表所有数据异常:{}", e)
+
+    # 工作表,插入行或列
+    @classmethod
+    def insert_columns(cls, log_type, crawler, sheetid, majordimension, startindex, endindex):
+        """
+        工作表插入行或列
+        :param log_type: 日志路径
+        :param crawler: 哪个爬虫的云文档
+        :param sheetid:哪张工作表
+        :param majordimension:行或者列, ROWS、COLUMNS
+        :param startindex:开始位置
+        :param endindex:结束位置
+        """
+        insert_columns_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                             + cls.spreadsheettoken(crawler) + "/insert_dimension_range"
+        headers = {
+            "Authorization": "Bearer " + cls.get_token(log_type),
+            "Content-Type": "application/json; charset=utf-8"
+        }
+        body = {
+            "dimension": {
+                "sheetId": sheetid,
+                "majorDimension": majordimension,  # 默认 ROWS ,可选 ROWS、COLUMNS
+                "startIndex": startindex,  # 开始的位置
+                "endIndex": endindex  # 结束的位置
+            },
+            "inheritStyle": "AFTER"  # BEFORE 或 AFTER,不填为不继承 style
+        }
+        try:
+            urllib3.disable_warnings()
+            r = requests.post(url=insert_columns_url, headers=headers, json=body, proxies=proxies, verify=False)
+            Common.logger(log_type).info("插入行或列:{}", r.json()["msg"])
+        except Exception as e:
+            Common.logger(log_type).error("插入行或列异常:{}", e)
+
+    # 写入数据
+    @classmethod
+    def update_values(cls, log_type, crawler, sheetid, ranges, values):
+        """
+        写入数据
+        :param log_type: 日志路径
+        :param crawler: 哪个爬虫的云文档
+        :param sheetid:哪张工作表
+        :param ranges:单元格范围
+        :param values:写入的具体数据,list
+        """
+        update_values_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                            + cls.spreadsheettoken(crawler) + "/values_batch_update"
+        headers = {
+            "Authorization": "Bearer " + cls.get_token(log_type),
+            "Content-Type": "application/json; charset=utf-8"
+        }
+        body = {
+            "valueRanges": [
+                {
+                    "range": sheetid + "!" + ranges,
+                    "values": values
+                },
+            ],
+        }
+
+        try:
+            urllib3.disable_warnings()
+            r = requests.post(url=update_values_url, headers=headers, json=body, proxies=proxies, verify=False)
+            Common.logger(log_type).info("写入数据:{}", r.json()["msg"])
+        except Exception as e:
+            Common.logger(log_type).error("写入数据异常:{}", e)
+
+    # 合并单元格
+    @classmethod
+    def merge_cells(cls, log_type, crawler, sheetid, ranges):
+        """
+        合并单元格
+        :param log_type: 日志路径
+        :param crawler: 哪个爬虫
+        :param sheetid:哪张工作表
+        :param ranges:需要合并的单元格范围
+        """
+        merge_cells_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                          + cls.spreadsheettoken(crawler) + "/merge_cells"
+        headers = {
+            "Authorization": "Bearer " + cls.get_token(log_type),
+            "Content-Type": "application/json; charset=utf-8"
+        }
+
+        body = {
+            "range": sheetid + "!" + ranges,
+            "mergeType": "MERGE_ROWS"
+        }
+
+        try:
+            urllib3.disable_warnings()
+            r = requests.post(url=merge_cells_url, headers=headers, json=body, proxies=proxies, verify=False)
+            Common.logger(log_type).info("合并单元格:{}", r.json()["msg"])
+        except Exception as e:
+            Common.logger(log_type).error("合并单元格异常:{}", e)
+
+    # 读取单元格数据
+    @classmethod
+    def get_range_value(cls, log_type, crawler, sheetid, cell):
+        """
+        读取单元格内容
+        :param log_type: 日志路径
+        :param crawler: 哪个爬虫
+        :param sheetid: 哪张工作表
+        :param cell: 哪个单元格
+        :return: 单元格内容
+        """
+        get_range_value_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                              + cls.spreadsheettoken(crawler) + "/values/" + sheetid + "!" + cell
+        headers = {
+            "Authorization": "Bearer " + cls.get_token(log_type),
+            "Content-Type": "application/json; charset=utf-8"
+        }
+        params = {
+            # valueRenderOption=ToString 可返回纯文本的值(数值类型除外);
+            # valueRenderOption=FormattedValue 计算并格式化单元格;
+            # valueRenderOption=Formula 单元格中含有公式时返回公式本身;
+            # valueRenderOption=UnformattedValue 计算但不对单元格进行格式化。
+            "valueRenderOption": "FormattedValue",
+
+            # dateTimeRenderOption=FormattedString 计算并对时间日期按照其格式进行格式化,但不会对数字进行格式化,返回格式化后的字符串。
+            "dateTimeRenderOption": "",
+
+            # 返回的用户id类型,可选open_id,union_id
+            "user_id_type": "open_id"
+        }
+        try:
+            urllib3.disable_warnings()
+            r = requests.get(url=get_range_value_url, headers=headers, params=params, proxies=proxies, verify=False)
+            # print(r.text)
+            return r.json()["data"]["valueRange"]["values"][0]
+        except Exception as e:
+            Common.logger(log_type).error("读取单元格数据异常:{}", e)
+
+    # 删除行或列,可选 ROWS、COLUMNS
+    @classmethod
+    def dimension_range(cls, log_type, crawler, sheetid, major_dimension, startindex, endindex):
+        """
+        删除行或列
+        :param log_type: 日志路径
+        :param crawler: 哪个爬虫
+        :param sheetid:工作表
+        :param major_dimension:默认 ROWS ,可选 ROWS、COLUMNS
+        :param startindex:开始的位置
+        :param endindex:结束的位置
+        :return:
+        """
+        dimension_range_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                              + cls.spreadsheettoken(crawler) + "/dimension_range"
+        headers = {
+            "Authorization": "Bearer " + cls.get_token(log_type),
+            "Content-Type": "application/json; charset=utf-8"
+        }
+        body = {
+            "dimension": {
+                "sheetId": sheetid,
+                "majorDimension": major_dimension,
+                "startIndex": startindex,
+                "endIndex": endindex
+                }
+            }
+        try:
+            urllib3.disable_warnings()
+            r = requests.delete(url=dimension_range_url, headers=headers, json=body, proxies=proxies, verify=False)
+            Common.logger(log_type).info("删除视频数据:{}", r.json()["msg"])
+        except Exception as e:
+            Common.logger(log_type).error("删除视频数据异常:{}", e)
+
+
+if __name__ == "__main__":
+    feishu = Feishu()
+
+    pass

+ 297 - 0
main/ganggangdouchuan.py

@@ -0,0 +1,297 @@
+# -*- coding: utf-8 -*-
+# @Author: wangkun
+# @Time: 2023/1/9
+import difflib
+import os
+import sys
+import time
+
+import ffmpeg
+from appium import webdriver
+from appium.webdriver.extensions.android.nativekey import AndroidKey
+from appium.webdriver.webdriver import WebDriver
+from selenium.common import NoSuchElementException
+from selenium.webdriver.common.by import By
+
+sys.path.append(os.getcwd())
+from main.ganggangdouchuan_common import Common
+from main.feishu_lib import Feishu
+from main.ganggangdouchuan_publish import Publish
+
+
+class Recommend:
+    i = 0
+
+    @classmethod
+    def start_wechat(cls, log_type, crawler, oss_endpoint, env, job):
+        try:
+            Common.logger(log_type).info('启动微信')
+            caps = {
+                "platformName": "Android",  # 手机操作系统 Android / iOS
+                "deviceName": "a0a65126",  # 连接的设备名(模拟器或真机),安卓可以随便写
+                "platforVersion": "11",  # 手机对应的系统版本(Android 11)
+                "appPackage": "com.tencent.mm",  # 被测APP的包名,乐活圈 Android
+                "appActivity": ".ui.LauncherUI",  # 启动的Activity名
+                "autoGrantPermissions": "true",  # 让 appium 自动授权 base 权限,
+                # 如果 noReset 为 True,则该条不生效(该参数为 Android 独有),对应的值为 True 或 False
+                "unicodekeyboard": True,  # 使用自带输入法,输入中文时填True
+                "resetkeyboard": True,  # 执行完程序恢复原来输入法
+                "noReset": True,  # 不重置APP
+                "printPageSourceOnFailure": True,  # 找不到元素时,appium log 会完整记录当前页面的 pagesource
+                "newCommandTimeout": 6000,  # 初始等待时间
+                "automationName": "UiAutomator2",  # 使用引擎,默认为 Appium,
+                # 其中 Appium、UiAutomator2、Selendroid、Espresso 用于 Android,XCUITest 用于 iOS
+                "showChromedriverLog": True,
+                'enableWebviewDetailsCollection': True,
+                'setWebContentsDebuggingEnabled': True,
+                'recreateChromeDriverSessions': True,
+                'chromedriverExecutable': '/Users/wangkun/Downloads/chromedriver/chromedriver_v86/chromedriver',
+                # 'chromedriverExecutable': '/Users/piaoquan/Downloads/chromedriver',
+                "chromeOptions": {"androidProcess": "com.tencent.mm:appbrand0"},
+                'browserName': ''
+            }
+            driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
+            driver.implicitly_wait(20)
+            # 向下滑动页面,展示出小程序选择面板
+            for i in range(120):
+                try:
+                    # 发现微信消息 TAB,代表微信已启动成功
+                    if driver.find_elements(By.ID, 'com.tencent.mm:id/f2s'):
+                        break
+                    # 发现并关闭系统菜单栏
+                    elif driver.find_element(By.ID, 'com.android.systemui:id/dismiss_view'):
+                        Common.logger(log_type).info('发现并关闭系统下拉菜单栏')
+                        driver.find_element(By.ID, 'com.android.systemui:id/dismiss_view').click()
+                    else:
+                        pass
+                except NoSuchElementException:
+                    time.sleep(1)
+            Common.logger(log_type).info('下滑,展示小程序选择面板')
+            size = driver.get_window_size()
+            driver.swipe(int(size['width'] * 0.5), int(size['height'] * 0.2),
+                         int(size['width'] * 0.5), int(size['height'] * 0.8), 200)
+            # 打开小程序"刚刚都传"
+            time.sleep(3)
+            Common.logger(log_type).info('打开小程序"刚刚都传"')
+            driver.find_elements(By.XPATH, '//*[@text="刚刚都传"]')[-1].click()
+            cls.get_recommend(log_type, crawler, oss_endpoint, env, job, driver)
+            cls.quit(log_type, driver)
+        except Exception as e:
+            Common.logger(log_type).error(f'start_wechat异常:{e}\n')
+
+    @classmethod
+    def quit(cls, log_type, driver: WebDriver):
+        Common.logger(log_type).info('开始退出微信')
+        driver.quit()
+        Common.logger(log_type).info('退出微信成功\n')
+
+    @classmethod
+    def filter_words(cls, log_type):
+        try:
+            filter_words_sheet = Feishu.get_values_batch(log_type, 'ggdc', 'B4SWsw')
+            filter_words_list = []
+            for x in filter_words_sheet:
+                for y in x:
+                    if y is None:
+                        pass
+                    else:
+                        filter_words_list.append(y)
+            return filter_words_list
+        except Exception as e:
+            Common.logger(log_type).error(f'filter_words异常:{e}')
+
+    @classmethod
+    def get_video_info_from_local(cls, log_type, video_path):
+        probe = ffmpeg.probe(video_path)
+        video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
+        if video_stream is None:
+            Common.logger(log_type).info('No video Stream found!')
+            return
+        width = int(video_stream['width'])
+        height = int(video_stream['height'])
+        duration = int(float(video_stream['duration']))
+        return width, height, duration
+
+    @classmethod
+    def search_elements(cls, log_type, driver: WebDriver, element):
+        try:
+            windowHandles = driver.window_handles
+            for handle in windowHandles:
+                driver.switch_to.window(handle)
+                time.sleep(1)
+                if len(driver.find_elements(By.XPATH, element)) != 0:
+                    return driver.find_elements(By.XPATH, element)
+                else:
+                    pass
+        except Exception as e:
+            Common.logger(log_type).error(f'search_element异常:{e}\n')
+
+    @classmethod
+    def get_video_info(cls, log_type, driver: WebDriver, video_element):
+        try:
+            time.sleep(1)
+            # Common.logger(log_type).info('进入视频详情')
+            video_element.click()
+            time.sleep(3)
+            video_url_element = cls.search_elements(log_type, driver, '//wx-video[@id="v_id"]')
+            if video_url_element is None or len(video_url_element) == 0:
+                Common.logger(log_type).info('未获取到视频 URL')
+                return 0
+            else:
+                return video_url_element[0].get_attribute('src')
+        except Exception as e:
+            Common.logger(log_type).error(f'get_video_info异常:{e}\n')
+
+    @classmethod
+    def get_recommend(cls, log_type, crawler, oss_endpoint, env, job, driver: WebDriver):
+        try:
+            driver.implicitly_wait(15)
+            Common.logger(log_type).info('切换到小程序\n')
+            time.sleep(5)
+            webviews = driver.contexts
+            driver.switch_to.context(webviews[1])
+
+            time.sleep(1)
+            cls.search_elements(log_type, driver, '//wx-view[text()="视频"]')[0].click()
+
+            time.sleep(1)
+            index = 0
+
+            while True:
+                if cls.search_elements(log_type, driver, '//wx-view[@class="lists"]') is None:
+                    Common.logger(log_type).info('窗口已销毁\n')
+                else:
+                    Common.logger(log_type).info('获取视频列表\n')
+                    video_elements = cls.search_elements(log_type, driver, '//wx-view[@class="list"]')
+                    if video_elements is None or len(video_elements) == 0:
+                        Common.logger(log_type).info(f'video_elements:{video_elements}')
+                        return
+
+                    video_element_temp = video_elements[index:]
+                    if len(video_element_temp) == 0:
+                        Common.logger(log_type).info('到底啦~~~~~~~~~~~~~\n')
+                        return
+
+                    for i, video_element in enumerate(video_element_temp):
+                        if video_element is None:
+                            Common.logger(log_type).info('到底啦~\n')
+                            return
+                        cls.i += 1
+                        cls.search_elements(log_type, driver, '//wx-view[@class="list"]')
+                        Common.logger(log_type).info('拖动"视频"列表第{}个至屏幕中间', cls.i)
+                        time.sleep(3)
+                        driver.execute_script("arguments[0].scrollIntoView({block:'center',inline:'center'})", video_element)
+
+                        video_title = video_element.find_elements(By.XPATH, '//wx-view[@class="title"]//span[2]')[cls.i - 1].get_attribute('innerHTML')
+                        cover_url = video_element.find_elements(By.XPATH, '//wx-image[@class="poster-img"]')[cls.i - 1].get_attribute('src')
+
+                        Common.logger(log_type).info(f'video_title:{video_title}')
+                        # Common.logger(log_type).info(f'cover_url:{cover_url}')
+
+                        cls.download_publish(log_type, crawler, oss_endpoint, env, job, driver, video_element, video_title, cover_url)
+                        time.sleep(3)
+
+                    Common.logger(log_type).info('已抓取完一组视频,休眠10秒\n')
+                    time.sleep(10)
+                    index = index + len(video_element_temp)
+
+        except Exception as e:
+            Common.logger(log_type).error(f'get_recommend异常,重启APP:{e}\n')
+            cls.i = 0
+            cls.quit(log_type, driver)
+            cls.start_wechat(log_type, crawler, oss_endpoint, env, job)
+
+    @classmethod
+    def title_like(cls, log_type, title):
+        sheet = Feishu.get_values_batch(log_type, 'ggdc', '070a67')
+        for i in range(1, len(sheet)):
+            video_title = sheet[i][7]
+            if video_title is None:
+                pass
+            elif difflib.SequenceMatcher(None, title, video_title).quick_ratio() >= 0.8:
+                return True
+            else:
+                pass
+
+    @classmethod
+    def download_publish(cls, log_type, crawler, oss_endpoint, env, job, driver: WebDriver, video_element, video_title, cover_url):
+        try:
+            if video_title == 0 or cover_url == 0:
+                Common.logger(log_type).info('无效视频\n')
+            elif video_title in [x for y in Feishu.get_values_batch(log_type, 'ggdc', '070a67') for x in y]:
+                Common.logger(log_type).info('视频已下载\n')
+            elif any(word if word in video_title else False for word in cls.filter_words(log_type)) is True:
+                Common.logger(log_type).info('已中过滤词\n')
+            else:
+                video_url = cls.get_video_info(log_type, driver, video_element)
+                if video_url == 0:
+                    Common.logger(log_type).info('video_url:未获取到\n')
+                elif video_url in [x for y in Feishu.get_values_batch(log_type, 'ggdc', '070a67') for x in y]:
+                    Common.logger(log_type).info('视频已下载\n')
+                else:
+                    Common.logger(log_type).info(f'cover_url:{cover_url}')
+                    Common.logger(log_type).info(f'video_url:{video_url}')
+
+                    # 下载视频
+                    Common.download_method(log_type, 'video', video_title, video_url)
+                    # 获取视频时长
+                    video_info = cls.get_video_info_from_local(log_type, "./videos/" + video_title + "/video.mp4")
+                    video_width = str(video_info[0])
+                    video_height = str(video_info[1])
+                    duration = video_info[2]
+                    # 下载封面
+                    Common.download_method(log_type, 'cover', video_title, cover_url)
+                    # 保存视频信息至 "./videos/{download_video_title}/info.txt"
+                    with open("./videos/" + video_title
+                              + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
+                        f_a.write("ggdc" + str(int(time.time())) + "\n" +
+                                  str(video_title) + "\n" +
+                                  str(int(float(duration))) + "\n" +
+                                  '100000' + "\n" +
+                                  '100000' + "\n" +
+                                  '100000' + "\n" +
+                                  '100000' + "\n" +
+                                  str(video_width) + '*' + str(video_height) + "\n" +
+                                  str(int(time.time())) + "\n" +
+                                  '刚刚都传小程序' + "\n" +
+                                  str(cover_url) + "\n" +
+                                  str(video_url) + "\n" +
+                                  str(cover_url) + "\n" +
+                                  "ganggangdouchuan" + str(int(time.time())))
+                    Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
+
+                    # 上传视频
+                    Common.logger(log_type).info(f"开始上传视频:{video_title}")
+                    if env == 'dev':
+                        our_video_id = Publish.upload_and_publish(log_type, crawler, oss_endpoint, env, job)
+                        our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
+                    else:
+                        our_video_id = Publish.upload_and_publish(log_type, crawler, oss_endpoint, env, job)
+                        our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(our_video_id) + "/info"
+                    Common.logger(log_type).info("视频上传完成")
+
+                    # 视频信息保存至飞书
+                    Feishu.insert_columns(log_type, "ggdc", "070a67", "ROWS", 1, 2)
+                    # 视频ID工作表,首行写入数据
+                    upload_time = int(time.time())
+                    values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
+                               "推荐榜",
+                               video_title,
+                               our_video_link,
+                               int(duration),
+                               str(video_width) + '*' + str(video_height),
+                               cover_url,
+                               video_url]]
+                    time.sleep(1)
+                    Feishu.update_values(log_type, "ggdc", "070a67", "F2:V2", values)
+                    driver.press_keycode(AndroidKey.BACK)
+                    Common.logger(log_type).info(f"视频:{video_title},下载/上传成功\n")
+        except Exception as e:
+            Common.logger(log_type).error(f'download_publish异常:{e}\n')
+
+
+if __name__ == '__main__':
+
+    Recommend.start_wechat('recommend', 'ggdc', 'out', 'dev', 'recommend')
+
+    pass

+ 135 - 0
main/ganggangdouchuan_common.py

@@ -0,0 +1,135 @@
+# -*- coding: utf-8 -*-
+# @Author: wangkun
+# @Time: 2023/1/9
+"""
+公共方法,包含:生成log / 删除log / 下载方法 / 读取文件 / 统计下载数
+"""
+from datetime import date, timedelta
+from loguru import logger
+import datetime
+import os
+import time
+import requests
+import urllib3
+proxies = {"http": None, "https": None}
+
+
+class Common:
+    # 统一获取当前时间 <class 'datetime.datetime'>  2022-04-14 20:13:51.244472
+    now = datetime.datetime.now()
+    # 昨天 <class 'str'>  2022-04-13
+    yesterday = (date.today() + timedelta(days=-1)).strftime("%Y-%m-%d")
+    # 今天 <class 'datetime.date'>  2022-04-14
+    today = date.today()
+    # 明天 <class 'str'>  2022-04-15
+    tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d")
+
+    # 使用 logger 模块生成日志
+    @staticmethod
+    def logger(log_type):
+        """
+        使用 logger 模块生成日志
+        """
+        # 日志路径
+        log_dir = "./logs/"
+        log_path = os.getcwd() + os.sep + log_dir
+        if not os.path.isdir(log_path):
+            os.makedirs(log_path)
+
+        # 日志文件名
+        log_name = time.strftime("%Y-%m-%d", time.localtime(time.time())) + '-ganggangdouchuan-'+str(log_type)+'.log'
+
+        # 日志不打印到控制台
+        logger.remove(handler_id=None)
+
+        # rotation="500 MB",实现每 500MB 存储一个文件
+        # rotation="12:00",实现每天 12:00 创建一个文件
+        # rotation="1 week",每周创建一个文件
+        # retention="10 days",每隔10天之后就会清理旧的日志
+        # 初始化日志
+        logger.add(log_dir + log_name, level="INFO", rotation='00:00')
+        return logger
+
+    # 清除日志,保留最近 10 个文件
+    @classmethod
+    def del_logs(cls, log_type):
+        """
+        清除冗余日志文件
+        :d_dir: 需要删除的 log 地址
+        :return: 保留最近 10 个日志
+        """
+        logs_dir = "./logs/"
+        if not os.path.exists(logs_dir):
+            os.mkdir(logs_dir)
+
+        all_files = sorted(os.listdir(logs_dir))
+        all_logs = []
+        for log in all_files:
+            name = os.path.splitext(log)[-1]
+            if name == ".log":
+                all_logs.append(log)
+
+        if len(all_logs) <= 10:
+            pass
+        else:
+            for file in all_logs[:len(all_logs) - 10]:
+                os.remove(logs_dir + file)
+        cls.logger(log_type).info("清除冗余日志成功")
+
+    # 封装下载视频或封面的方法
+    @classmethod
+    def download_method(cls, log_type, text, d_name, d_url):
+        """
+        下载封面:text == "cover" ; 下载视频:text == "video"
+        需要下载的视频标题:d_title
+        视频封面,或视频播放地址:d_url
+        下载保存路径:"./files/{d_title}/"
+        """
+        videos_dir = "./videos/"
+        if not os.path.exists(videos_dir):
+            os.mkdir(videos_dir)
+        # 首先创建一个保存该视频相关信息的文件夹
+        video_dir = "./videos/" + d_name + "/"
+        if not os.path.exists(video_dir):
+            os.mkdir(video_dir)
+
+        # 下载视频
+        if text == "video":
+            # 需要下载的视频地址
+            video_url = d_url
+            # 视频名
+            video_name = "video.mp4"
+
+            # 下载视频
+            urllib3.disable_warnings()
+            response = requests.get(video_url, stream=True, proxies=proxies, verify=False)
+            try:
+                with open(video_dir + video_name, "wb") as f:
+                    for chunk in response.iter_content(chunk_size=10240):
+                        f.write(chunk)
+                cls.logger(log_type).info("==========视频下载完成==========")
+            except Exception as e:
+                cls.logger(log_type).exception("视频下载失败:{}", e)
+
+        # 下载封面
+        elif text == "cover":
+            # 需要下载的封面地址
+            cover_url = d_url
+            # 封面名
+            cover_name = "image.jpg"
+            # # 封面名
+            # cover_name = d_name + ".jpg"
+
+            # 下载封面
+            urllib3.disable_warnings()
+            response = requests.get(cover_url, proxies=proxies, verify=False)
+            try:
+                with open(video_dir + cover_name, "wb") as f:
+                    f.write(response.content)
+                cls.logger(log_type).info("==========封面下载完成==========")
+            except Exception as e:
+                cls.logger(log_type).exception("封面下载失败:{}", e)
+
+
+if __name__ == "__main__":
+    common = Common()

+ 280 - 0
main/ganggangdouchuan_publish.py

@@ -0,0 +1,280 @@
+# -*- coding: utf-8 -*-
+# @Author: wangkun
+# @Time: 2023/1/9
+import json
+import os
+import random
+import shutil
+import time
+import oss2
+import requests
+import urllib3
+from main.ganggangdouchuan_common import Common
+proxies = {"http": None, "https": None}
+
+
+class Publish:
+    @classmethod
+    def publish_video_dev(cls, log_type, request_data):
+        """
+        loginUid  站内uid (随机)
+        appType  默认:888888
+        crawlerSrcId   站外视频ID
+        crawlerSrcCode   渠道(自定义 KYK)
+        crawlerSrcPublishTimestamp  视频原发布时间
+        crawlerTaskTimestamp   爬虫创建时间(可以是当前时间)
+        videoPath  视频oss地址
+        coverImgPath  视频封面oss地址
+        title  标题
+        totalTime  视频时长
+        viewStatus  视频的有效状态 默认1
+        versionCode  版本 默认1
+        :return:
+        """
+        # Common.logger().info('publish request data: {}'.format(request_data))
+        result = cls.request_post('https://videotest.yishihui.com/longvideoapi/crawler/video/send', request_data)
+        # Common.logger(log_type).info('publish result: {}'.format(result))
+        video_id = result["data"]["id"]
+        # Common.logger(log_type).info('video_id: {}'.format(video_id))
+        if result['code'] != 0:
+            Common.logger(log_type).error('pushlish failure msg = {}'.format(result['msg']))
+        else:
+            Common.logger(log_type).info('publish success video_id = : {}'.format(request_data['crawlerSrcId']))
+        return video_id
+
+    @classmethod
+    def publish_video_prod(cls, log_type, request_data):
+        """
+        loginUid  站内uid (随机)
+        appType  默认:888888
+        crawlerSrcId   站外视频ID
+        crawlerSrcCode   渠道(自定义 KYK)
+        crawlerSrcPublishTimestamp  视频原发布时间
+        crawlerTaskTimestamp   爬虫创建时间(可以是当前时间)
+        videoPath  视频oss地址
+        coverImgPath  视频封面oss地址
+        title  标题
+        totalTime  视频时长
+        viewStatus  视频的有效状态 默认1
+        versionCode  版本 默认1
+        :return:
+        """
+        result = cls.request_post('https://longvideoapi.piaoquantv.com/longvideoapi/crawler/video/send', request_data)
+        # Common.logger(log_type).info('publish result: {}'.format(result))
+        video_id = result["data"]["id"]
+        # Common.logger(log_type).info('video_id: {}'.format(video_id))
+        if result['code'] != 0:
+            Common.logger(log_type).error('pushlish failure msg = {}'.format(result['msg']))
+        else:
+            Common.logger(log_type).info('publish success video_id = : {}'.format(request_data['crawlerSrcId']))
+        return video_id
+
+    @classmethod
+    def request_post(cls, request_url, request_data):
+        """
+        post 请求 HTTP接口
+        :param request_url: 接口URL
+        :param request_data: 请求参数
+        :return: res_data json格式
+        """
+        urllib3.disable_warnings()
+        response = requests.post(url=request_url, data=request_data, proxies=proxies, verify=False)
+        if response.status_code == 200:
+            res_data = json.loads(response.text)
+            return res_data
+
+    @classmethod
+    def bucket(cls, oss_endpoint):
+        """
+        创建 bucket
+        :param oss_endpoint: inner:内网;out:外网
+        :return: bucket
+        """
+        # 以下代码展示了基本的文件上传、下载、罗列、删除用法。
+
+        # 首先初始化AccessKeyId、AccessKeySecret、Endpoint等信息。
+        # 通过环境变量获取,或者把诸如“<你的AccessKeyId>”替换成真实的AccessKeyId等。
+        #
+        # 以杭州区域为例,Endpoint可以是:
+        #   http://oss-cn-hangzhou.aliyuncs.com
+        #   https://oss-cn-hangzhou.aliyuncs.com
+        # 分别以HTTP、HTTPS协议访问。
+        access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', 'LTAIP6x1l3DXfSxm')
+        access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', 'KbTaM9ars4OX3PMS6Xm7rtxGr1FLon')
+        bucket_name = os.getenv('OSS_TEST_BUCKET', 'art-pubbucket')
+        # OSS 内网
+        if oss_endpoint == 'inner':
+            endpoint = os.getenv('OSS_TEST_ENDPOINT', 'oss-cn-hangzhou-internal.aliyuncs.com')
+        # OSS 外网
+        elif oss_endpoint == 'out':
+            endpoint = os.getenv('OSS_TEST_ENDPOINT', 'oss-cn-hangzhou.aliyuncs.com')
+        # 默认走外网
+        else:
+            endpoint = os.getenv('OSS_TEST_ENDPOINT', 'oss-cn-hangzhou.aliyuncs.com')
+
+        # 确认上面的参数都填写正确了
+        for param in (access_key_id, access_key_secret, bucket_name, endpoint):
+            assert '<' not in param, '请设置参数:' + param
+
+        # 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
+        bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
+        return bucket
+
+    """
+    处理流程:
+    1. 定时(每天凌晨1点执行一次)循环files文件下的内容 结构:files -> 视频文件夹 -> 视频文件 + 封面图 + 基本信息
+    2. 视频文件和封面上传到oss
+    - 视频文件oss目录  longvideo/crawler_local/video/prod/文件名
+    - 视频封面oss目录  longvideo/crawler_local/image/prod/文件名
+    3. 发布视频
+    - 读取 基本信息 调用发布接口
+    """
+    # env 日期20220225 文件名
+    oss_file_path_video = 'longvideo/crawler_local/video/{}/{}/{}'
+    oss_file_path_image = 'longvideo/crawler_local/image/{}/{}/{}'
+
+    @classmethod
+    def put_file(cls, log_type, oss_endpoint, oss_file, local_file):
+        cls.bucket(oss_endpoint).put_object_from_file(oss_file, local_file)
+        # Common.logger(log_type).info("put oss file = {}, local file = {} success".format(oss_file, local_file))
+
+    # 清除本地文件
+    @classmethod
+    def remove_local_file(cls, log_type, local_file):
+        os.remove(local_file)
+        # Common.logger(log_type).info("remove local file = {} success".format(local_file))
+
+    # 清除本地文件夹
+    @classmethod
+    def remove_local_file_dir(cls, log_type, local_file):
+        os.rmdir(local_file)
+        # Common.logger(log_type).info("remove local file dir = {} success".format(local_file))
+
+    local_file_path = './videos'
+    video_file = 'video'
+    image_file = 'image'
+    info_file = 'info'
+
+    @classmethod
+    def uids(cls, crawler, env, job):
+        """
+        站内 ID
+        :param crawler: 哪款爬虫
+        :param env: 什么环境
+        :param job: 榜单类型,也可以是指定的站内 UID
+        :return: 推荐榜:recommend,定向榜:follow
+        """
+        if env == 'dev':
+            uids_dev = [6267140, 6267141]
+            return random.choice(uids_dev)
+        elif crawler == 'ggdc' and env == 'prod' and job == 'recommend':
+            uids_ggdc_prod_recommend = [26117661, 26117662, 26117663]
+            return random.choice(uids_ggdc_prod_recommend)
+        elif crawler == 'ggdc' and env == 'prod' and job == 'follow':
+            uids_ggdc_prod_follow = [26117661, 26117662, 26117663]
+            return random.choice(uids_ggdc_prod_follow)
+        else:
+            return job
+
+    @classmethod
+    def upload_and_publish(cls, log_type, crawler, oss_endpoint, env, job):
+        """
+        上传视频到 oss
+        :param log_type: 选择的 log
+        :param crawler: 哪款爬虫
+        :param oss_endpoint: OSS网络环境,'inner'为内网,'out'为外网
+        :param env: 测试环境:dev,正式环境:prod
+        :param job: 推荐榜:recommend,定向榜:follow
+        """
+        Common.logger(log_type).info("视频上传开始")
+        today = time.strftime("%Y%m%d", time.localtime())
+        # videos 目录下的所有视频文件夹
+        files = os.listdir(cls.local_file_path)
+        for fv in files:
+            try:
+                # 单个视频文件夹
+                fi_d = os.path.join(cls.local_file_path, fv)
+                # 确认为视频文件夹
+                if os.path.isdir(fi_d):
+                    # Common.logger(log_type).info('dir = {}'.format(fi_d))
+                    # 列出所有视频文件夹
+                    dir_files = os.listdir(fi_d)
+                    data = {'appType': '888888',
+                            'crawlerSrcCode': 'GANGGANGDOUCHUAN',
+                            'viewStatus': '1',
+                            'versionCode': '1'}
+                    now_timestamp = int(round(time.time() * 1000))
+                    data['crawlerTaskTimestamp'] = str(now_timestamp)
+                    data['loginUid'] = cls.uids(crawler, env, job)
+                    # 单个视频文件夹下的所有视频文件
+                    for fi in dir_files:
+                        # 视频文件夹下的所有文件路径
+                        fi_path = fi_d + '/' + fi
+                        # Common.logger(log_type).info('dir fi_path = {}'.format(fi_path))
+                        # 读取 info.txt,赋值给 data
+                        if cls.info_file in fi:
+                            f = open(fi_path, "r", encoding="UTF-8")
+                            # 读取数据 数据准确性写入的时候保证 读取暂不处理
+                            for i in range(14):
+                                line = f.readline()
+                                line = line.replace('\n', '')
+                                if line is not None and len(line) != 0 and not line.isspace():
+                                    # Common.logger(log_type).info("line = {}".format(line))
+                                    if i == 0:
+                                        data['crawlerSrcId'] = line
+                                    elif i == 1:
+                                        data['title'] = line
+                                    elif i == 2:
+                                        data['totalTime'] = line
+                                    elif i == 8:
+                                        data['crawlerSrcPublishTimestamp'] = line
+                                else:
+                                    Common.logger(log_type).warning("{} line is None".format(fi_path))
+                            f.close()
+                            # remove info.txt
+                            cls.remove_local_file(log_type, fi_path)
+                    # 刷新数据
+                    dir_files = os.listdir(fi_d)
+                    for fi in dir_files:
+                        fi_path = fi_d + '/' + fi
+                        # Common.logger(log_type).info('dir fi_path = {}'.format(fi_path))
+                        # 上传oss
+                        if cls.video_file in fi:
+                            global oss_video_file
+                            if env == "dev":
+                                oss_video_file = cls.oss_file_path_video.format("dev", today, data['crawlerSrcId'])
+                            elif env == "prod":
+                                oss_video_file = cls.oss_file_path_video.format("prod", today, data['crawlerSrcId'])
+                            # Common.logger(log_type).info("oss_video_file = {}".format(oss_video_file))
+                            cls.put_file(log_type, oss_endpoint, oss_video_file, fi_path)
+                            data['videoPath'] = oss_video_file
+                            # Common.logger(log_type).info("videoPath = {}".format(oss_video_file))
+                        elif cls.image_file in fi:
+                            global oss_image_file
+                            if env == "dev":
+                                oss_image_file = cls.oss_file_path_image.format("env", today, data['crawlerSrcId'])
+                            elif env == "prod":
+                                oss_image_file = cls.oss_file_path_image.format("prod", today, data['crawlerSrcId'])
+                            # Common.logger(log_type).info("oss_image_file = {}".format(oss_image_file))
+                            cls.put_file(log_type, oss_endpoint, oss_image_file, fi_path)
+                            data['coverImgPath'] = oss_image_file
+                            # Common.logger(log_type).info("coverImgPath = {}".format(oss_image_file))
+                        # 全部remove
+                        cls.remove_local_file(log_type, fi_path)
+
+                    # 发布
+                    if env == "dev":
+                        video_id = cls.publish_video_dev(log_type, data)
+                    elif env == "prod":
+                        video_id = cls.publish_video_prod(log_type, data)
+                    else:
+                        video_id = cls.publish_video_dev(log_type, data)
+                    cls.remove_local_file_dir(log_type, fi_d)
+                    return video_id
+
+                else:
+                    Common.logger(log_type).error('file not a dir = {}'.format(fi_d))
+            except Exception as e:
+                # 删除视频文件夹
+                shutil.rmtree("./videos/" + fv + "/")
+                Common.logger(log_type).exception('upload_and_publish error', e)

+ 38 - 0
main/run_ganggangdouchuan.py

@@ -0,0 +1,38 @@
+# -*- coding: utf-8 -*-
+# @Author: wangkun
+# @Time: 2023/1/9
+import datetime
+import os
+import sys
+import time
+
+sys.path.append(os.getcwd())
+from main.ganggangdouchuan import Recommend
+from main.ganggangdouchuan_common import Common
+
+
+class Main:
+    @classmethod
+    def main(cls, log_type, crawler, oss_endpoint, env, job):
+        """
+        主函数
+        :param log_type: log 类型
+        :param crawler: 哪款爬虫
+        :param oss_endpoint: 阿里云服务器选择:inner ; 其它机器选择:out
+        :param env: 测试环境:dev ; 正式环境:prod
+        :param job: 榜单类型,如 recommend / follow
+        :return: None
+        """
+        while True:
+            if 11 >= datetime.datetime.now().hour >= 8:
+                Common.logger(log_type).info('开始抓取"刚刚都传"小程序')
+                Recommend.start_wechat(log_type, crawler, oss_endpoint, env, job)
+                Common.del_logs(log_type)
+                Recommend.i = 0
+                time.sleep(10)
+            else:
+                pass
+
+
+if __name__ == '__main__':
+    Main.main('recommend', 'ggdc', 'out', 'prod', 'recommend')

+ 7 - 0
requirements.txt

@@ -0,0 +1,7 @@
+Appium_Python_Client==2.7.1
+ffmpeg==1.4
+loguru==0.6.0
+oss2==2.15.0
+requests==2.27.1
+selenium==4.7.2
+urllib3==1.26.9