|
@@ -1,129 +1,170 @@
|
|
# -*- coding: utf-8 -*-
|
|
# -*- coding: utf-8 -*-
|
|
# @Author: wangkun
|
|
# @Author: wangkun
|
|
# @Time: 2023/4/13
|
|
# @Time: 2023/4/13
|
|
-import difflib
|
|
|
|
|
|
+import json
|
|
import os
|
|
import os
|
|
|
|
+import shutil
|
|
import sys
|
|
import sys
|
|
import time
|
|
import time
|
|
-import ffmpeg
|
|
|
|
|
|
+from hashlib import md5
|
|
from appium import webdriver
|
|
from appium import webdriver
|
|
from appium.webdriver.extensions.android.nativekey import AndroidKey
|
|
from appium.webdriver.extensions.android.nativekey import AndroidKey
|
|
from appium.webdriver.webdriver import WebDriver
|
|
from appium.webdriver.webdriver import WebDriver
|
|
from selenium.common import NoSuchElementException
|
|
from selenium.common import NoSuchElementException
|
|
from selenium.webdriver.common.by import By
|
|
from selenium.webdriver.common.by import By
|
|
|
|
+
|
|
sys.path.append(os.getcwd())
|
|
sys.path.append(os.getcwd())
|
|
from common.common import Common
|
|
from common.common import Common
|
|
|
|
+from common.feishu import Feishu
|
|
|
|
+from common.publish import Publish
|
|
|
|
+from common.scheduling_db import MysqlHelper
|
|
|
|
|
|
|
|
|
|
class GanggangdouchuanRecommend:
|
|
class GanggangdouchuanRecommend:
|
|
|
|
+ platform = "刚刚都传"
|
|
i = 0
|
|
i = 0
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
def start_wechat(cls, log_type, crawler, oss_endpoint, env):
|
|
def start_wechat(cls, log_type, crawler, oss_endpoint, env):
|
|
- # try:
|
|
|
|
- if env == "dev":
|
|
|
|
- chromedriverExecutable = '/Users/wangkun/Downloads/chromedriver/chromedriver_v107/chromedriver'
|
|
|
|
- else:
|
|
|
|
- chromedriverExecutable = '/Users/piaoquan/Downloads/chromedriver'
|
|
|
|
|
|
+ try:
|
|
|
|
+ if env == "dev":
|
|
|
|
+ chromedriverExecutable = '/Users/wangkun/Downloads/chromedriver/chromedriver_v107/chromedriver'
|
|
|
|
+ else:
|
|
|
|
+ chromedriverExecutable = '/Users/piaoquan/Downloads/chromedriver'
|
|
|
|
|
|
- Common.logger(log_type, crawler).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': chromedriverExecutable,
|
|
|
|
- "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, crawler).info('发现并关闭系统下拉菜单栏')
|
|
|
|
- driver.find_element(By.ID, 'com.android.systemui:id/dismiss_view').click()
|
|
|
|
- else:
|
|
|
|
- pass
|
|
|
|
- except NoSuchElementException:
|
|
|
|
- time.sleep(1)
|
|
|
|
- Common.logger(log_type, crawler).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, crawler).info('打开小程序"刚刚都传"')
|
|
|
|
- driver.find_elements(By.XPATH, '//*[@text="刚刚都传"]')[-1].click()
|
|
|
|
- cls.get_videoList(log_type, crawler, oss_endpoint, env, driver)
|
|
|
|
- driver.quit()
|
|
|
|
- Common.logger(log_type, crawler).info('退出微信成功\n')
|
|
|
|
- # except Exception as e:
|
|
|
|
- # Common.logger(log_type, crawler).error(f'start_wechat异常:{e}\n')
|
|
|
|
- # cmd = "cd ~ && source .bash_profile && adb kill-server && adb start-server"
|
|
|
|
- # os.system(cmd)
|
|
|
|
|
|
+ Common.logger(log_type, crawler).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': chromedriverExecutable,
|
|
|
|
+ "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, crawler).info('发现并关闭系统下拉菜单栏')
|
|
|
|
+ driver.find_element(By.ID, 'com.android.systemui:id/dismiss_view').click()
|
|
|
|
+ else:
|
|
|
|
+ pass
|
|
|
|
+ except NoSuchElementException:
|
|
|
|
+ time.sleep(1)
|
|
|
|
+ Common.logger(log_type, crawler).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, crawler).info('打开小程序"刚刚都传"')
|
|
|
|
+ driver.find_elements(By.XPATH, '//*[@text="刚刚都传"]')[-1].click()
|
|
|
|
+ time.sleep(10)
|
|
|
|
+ cls.get_videoList(log_type, crawler, oss_endpoint, env, driver)
|
|
|
|
+ driver.quit()
|
|
|
|
+ Common.logger(log_type, crawler).info('退出微信成功\n')
|
|
|
|
+ except Exception as e:
|
|
|
|
+ Common.logger(log_type, crawler).error(f'start_wechat异常:{e}\n')
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
- def search_elements(cls, log_type, crawler, driver: WebDriver, element):
|
|
|
|
|
|
+ def get_video_url(cls, log_type, crawler, driver: WebDriver, video_element):
|
|
try:
|
|
try:
|
|
|
|
+ time.sleep(1)
|
|
|
|
+ # Common.logger(log_type, crawler).info('进入视频详情')
|
|
|
|
+ video_element.click()
|
|
|
|
+ time.sleep(5)
|
|
windowHandles = driver.window_handles
|
|
windowHandles = driver.window_handles
|
|
|
|
+ # Common.logger(log_type, crawler).info('windowHandles:{}', windowHandles)
|
|
|
|
+ # 遍历所有的handles,找到当前页面所在的handle:如果pageSource有包含你想要的元素,就是所要找的handle
|
|
|
|
+ # 小程序的页面来回切换也需要:遍历所有的handles,切换到元素所在的handle
|
|
for handle in windowHandles:
|
|
for handle in windowHandles:
|
|
driver.switch_to.window(handle)
|
|
driver.switch_to.window(handle)
|
|
time.sleep(1)
|
|
time.sleep(1)
|
|
- if len(driver.find_elements(By.XPATH, element)) != 0:
|
|
|
|
- return driver.find_elements(By.XPATH, element)
|
|
|
|
- else:
|
|
|
|
- return None
|
|
|
|
|
|
+ try:
|
|
|
|
+ video_url_element = driver.find_element(By.XPATH, '//wx-video[@id="v_id"]')
|
|
|
|
+ video_url = video_url_element.get_attribute("src")
|
|
|
|
+ return video_url
|
|
|
|
+ except NoSuchElementException:
|
|
|
|
+ time.sleep(1)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
- Common.logger(log_type, crawler).error(f'search_element异常:{e}\n')
|
|
|
|
|
|
+ Common.logger(log_type, crawler).error(f'get_video_url异常:{e}\n')
|
|
|
|
|
|
|
|
+ # 切换 Handle
|
|
@classmethod
|
|
@classmethod
|
|
- def get_video_url(cls, log_type, crawler, driver: WebDriver, video_element):
|
|
|
|
|
|
+ def search_elements(cls, log_type, crawler, driver: WebDriver, xpath):
|
|
try:
|
|
try:
|
|
- time.sleep(1)
|
|
|
|
- # Common.logger(log_type, crawler).info('进入视频详情')
|
|
|
|
- video_element.click()
|
|
|
|
- time.sleep(3)
|
|
|
|
- video_url_element = cls.search_elements(log_type, crawler, driver, '//wx-video[@id="v_id"]')
|
|
|
|
- if video_url_element is None or len(video_url_element) == 0:
|
|
|
|
- Common.logger(log_type, crawler).info('未获取到视频 URL')
|
|
|
|
- return 0
|
|
|
|
- else:
|
|
|
|
- return video_url_element[0].get_attribute('src')
|
|
|
|
|
|
+ windowHandles = driver.window_handles
|
|
|
|
+ # Common.logger(log_type, crawler).info('windowHandles:{}', windowHandles)
|
|
|
|
+ # 遍历所有的handles,找到当前页面所在的handle:如果pageSource有包含你想要的元素,就是所要找的handle
|
|
|
|
+ # 小程序的页面来回切换也需要:遍历所有的handles,切换到元素所在的handle
|
|
|
|
+ for handle in windowHandles:
|
|
|
|
+ driver.switch_to.window(handle)
|
|
|
|
+ time.sleep(1)
|
|
|
|
+ try:
|
|
|
|
+ elements = driver.find_elements(By.XPATH, xpath)
|
|
|
|
+ return elements
|
|
|
|
+ except NoSuchElementException:
|
|
|
|
+ pass
|
|
except Exception as e:
|
|
except Exception as e:
|
|
- Common.logger(log_type, crawler).error(f'get_video_url异常:{e}\n')
|
|
|
|
|
|
+ Common.logger(log_type, crawler).warning(f'search_elements异常:{e}\n')
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
- def get_videoList(cls, log_type, crawler, oss_endpoint, env, driver: WebDriver):
|
|
|
|
- # try:
|
|
|
|
- driver.implicitly_wait(15)
|
|
|
|
- Common.logger(log_type, crawler).info('切换到小程序\n')
|
|
|
|
- time.sleep(5)
|
|
|
|
- webviews = driver.contexts
|
|
|
|
- driver.switch_to.context(webviews[1])
|
|
|
|
|
|
+ def check_to_applet(cls, log_type, crawler, driver: WebDriver):
|
|
|
|
+ while True:
|
|
|
|
+ webview = driver.contexts
|
|
|
|
+ # Common.logger(log_type, crawler).info(f"webviews:{webview}")
|
|
|
|
+ driver.switch_to.context(webview[1])
|
|
|
|
+ windowHandles = driver.window_handles
|
|
|
|
+ for handle in windowHandles:
|
|
|
|
+ driver.switch_to.window(handle)
|
|
|
|
+ time.sleep(1)
|
|
|
|
+ try:
|
|
|
|
+ video_list = driver.find_element(By.XPATH, '//wx-view[text()="视频"]')
|
|
|
|
+ video_list.click()
|
|
|
|
+ Common.logger(log_type, crawler).info('切换到小程序视频列表成功\n')
|
|
|
|
+ return
|
|
|
|
+ except NoSuchElementException:
|
|
|
|
+ time.sleep(1)
|
|
|
|
+ Common.logger(log_type, crawler).info("切换到小程序失败\n")
|
|
|
|
+ break
|
|
|
|
|
|
- time.sleep(1)
|
|
|
|
- cls.search_elements(log_type, crawler, driver, '//wx-view[text()="视频"]')[0].click()
|
|
|
|
|
|
+ @classmethod
|
|
|
|
+ def repeat_out_video_id(cls, log_type, crawler, out_video_id, env):
|
|
|
|
+ sql = f""" select * from crawler_video where platform="{cls.platform}" and out_video_id="{out_video_id}"; """
|
|
|
|
+ repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env)
|
|
|
|
+ return len(repeat_video)
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def repeat_video_url(cls, log_type, crawler, video_url, env):
|
|
|
|
+ sql = f""" select * from crawler_video where platform="{cls.platform}" and video_url="{video_url}"; """
|
|
|
|
+ repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env)
|
|
|
|
+ return len(repeat_video)
|
|
|
|
|
|
|
|
+ @classmethod
|
|
|
|
+ def get_videoList(cls, log_type, crawler, oss_endpoint, env, driver: WebDriver):
|
|
|
|
+ # 切换到小程序
|
|
|
|
+ cls.check_to_applet(log_type, crawler, driver)
|
|
time.sleep(1)
|
|
time.sleep(1)
|
|
index = 0
|
|
index = 0
|
|
|
|
|
|
@@ -153,110 +194,142 @@ class GanggangdouchuanRecommend:
|
|
time.sleep(3)
|
|
time.sleep(3)
|
|
driver.execute_script("arguments[0].scrollIntoView({block:'center',inline:'center'})", video_element)
|
|
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')
|
|
|
|
|
|
+ # video_title = video_element.find_elements(By.XPATH, '//wx-view[@class="title"]//span[2]')[cls.i - 1].get_attribute('innerHTML')
|
|
|
|
+ video_title = video_element.find_elements(By.XPATH, '//wx-view[@class="title"]//span[2]')[cls.i - 1].text
|
|
cover_url = video_element.find_elements(By.XPATH, '//wx-image[@class="poster-img"]')[cls.i - 1].get_attribute('src')
|
|
cover_url = video_element.find_elements(By.XPATH, '//wx-image[@class="poster-img"]')[cls.i - 1].get_attribute('src')
|
|
|
|
+ out_video_id = md5(video_title.encode('utf8')).hexdigest()
|
|
|
|
+ video_dict = {
|
|
|
|
+ 'video_title': video_title,
|
|
|
|
+ 'video_id': out_video_id,
|
|
|
|
+ 'play_cnt': 0,
|
|
|
|
+ 'comment_cnt': 0,
|
|
|
|
+ 'like_cnt': 0,
|
|
|
|
+ 'share_cnt': 0,
|
|
|
|
+ 'publish_time_stamp': int(time.time()),
|
|
|
|
+ 'publish_time_str': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))),
|
|
|
|
+ 'user_name': "ganggangdouchuan",
|
|
|
|
+ 'user_id': "ganggangdouchuan",
|
|
|
|
+ 'avatar_url': cover_url,
|
|
|
|
+ 'cover_url': cover_url,
|
|
|
|
+ 'session': f"ganggangdouchuan-{int(time.time())}"
|
|
|
|
+ }
|
|
|
|
+ for k, v in video_dict.items():
|
|
|
|
+ Common.logger(log_type, crawler).info(f"{k}:{v}")
|
|
|
|
|
|
- Common.logger(log_type, crawler).info(f'video_title:{video_title}')
|
|
|
|
- Common.logger(log_type, crawler).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)
|
|
|
|
-
|
|
|
|
|
|
+ if video_title is None or cover_url is None:
|
|
|
|
+ Common.logger(log_type, crawler).info("无效视频\n")
|
|
|
|
+ elif cls.repeat_out_video_id(log_type, crawler, out_video_id, env) != 0:
|
|
|
|
+ Common.logger(log_type, crawler).info('视频已下载\n')
|
|
|
|
+ else:
|
|
|
|
+ video_url = cls.get_video_url(log_type, crawler, driver, video_element)
|
|
|
|
+ if video_url is None:
|
|
|
|
+ Common.logger(log_type, crawler).info("未获取到视频播放地址\n")
|
|
|
|
+ driver.press_keycode(AndroidKey.BACK)
|
|
|
|
+ elif cls.repeat_video_url(log_type, crawler, video_url, env) != 0:
|
|
|
|
+ Common.logger(log_type, crawler).info('视频已下载\n')
|
|
|
|
+ driver.press_keycode(AndroidKey.BACK)
|
|
|
|
+ else:
|
|
|
|
+ video_dict["video_url"]=video_url
|
|
|
|
+ cls.download_publish(log_type, crawler, video_dict, oss_endpoint, env, driver)
|
|
Common.logger(log_type, crawler).info('已抓取完一组视频,休眠10秒\n')
|
|
Common.logger(log_type, crawler).info('已抓取完一组视频,休眠10秒\n')
|
|
time.sleep(10)
|
|
time.sleep(10)
|
|
index = index + len(video_element_temp)
|
|
index = index + len(video_element_temp)
|
|
|
|
|
|
# except Exception as e:
|
|
# except Exception as e:
|
|
# Common.logger(log_type, crawler).error(f'get_recommend异常,重启APP:{e}\n')
|
|
# Common.logger(log_type, crawler).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, crawler).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, crawler).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, crawler).info('已中过滤词\n')
|
|
|
|
- # else:
|
|
|
|
- # video_url = cls.get_video_url(log_type, driver, video_element)
|
|
|
|
- # if video_url == 0:
|
|
|
|
- # Common.logger(log_type, crawler).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, crawler).info('视频已下载\n')
|
|
|
|
- # else:
|
|
|
|
- # Common.logger(log_type, crawler).info(f'cover_url:{cover_url}')
|
|
|
|
- # Common.logger(log_type, crawler).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" +
|
|
|
|
- # '100' + "\n" +
|
|
|
|
- # '100000' + "\n" +
|
|
|
|
- # '100000' + "\n" +
|
|
|
|
- # '100000' + "\n" +
|
|
|
|
- # '100000' + "\n" +
|
|
|
|
- # '1920*1080' + "\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, crawler).info("==========视频信息已保存至info.txt==========")
|
|
|
|
- #
|
|
|
|
- # # 上传视频
|
|
|
|
- # Common.logger(log_type, crawler).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, crawler).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,
|
|
|
|
- # 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, crawler).info(f"视频:{video_title},下载/上传成功\n")
|
|
|
|
- # except Exception as e:
|
|
|
|
- # Common.logger(log_type, crawler).error(f'download_publish异常:{e}\n')
|
|
|
|
|
|
+ @classmethod
|
|
|
|
+ def download_publish(cls, log_type, crawler, video_dict, oss_endpoint, env, driver: WebDriver):
|
|
|
|
+ try:
|
|
|
|
+ # 下载视频
|
|
|
|
+ Common.download_method(log_type=log_type, crawler=crawler, text='video', title=video_dict['video_title'], url=video_dict['video_url'])
|
|
|
|
+ ffmpeg_dict = Common.ffmpeg(log_type, crawler, f"./{crawler}/videos/{video_dict['video_title']}/video.mp4")
|
|
|
|
+ if ffmpeg_dict is None:
|
|
|
|
+ md_title = md5(video_dict['video_title'].encode('utf8')).hexdigest()
|
|
|
|
+ shutil.rmtree(f"./{crawler}/videos/{md_title}/")
|
|
|
|
+ Common.logger(log_type, crawler).info("视频size=0,删除成功\n")
|
|
|
|
+ return
|
|
|
|
+ video_dict["duration"] = ffmpeg_dict["duration"]
|
|
|
|
+ video_dict["video_width"] = ffmpeg_dict["width"]
|
|
|
|
+ video_dict["video_height"] = ffmpeg_dict["height"]
|
|
|
|
+ # 下载封面
|
|
|
|
+ Common.download_method(log_type=log_type, crawler=crawler, text='cover', title=video_dict['video_title'],
|
|
|
|
+ url=video_dict['cover_url'])
|
|
|
|
+ # 保存视频信息至txt
|
|
|
|
+ Common.save_video_info(log_type=log_type, crawler=crawler, video_dict=video_dict)
|
|
|
|
+
|
|
|
|
+ # 上传视频
|
|
|
|
+ Common.logger(log_type, crawler).info("开始上传视频...")
|
|
|
|
+ our_video_id = Publish.upload_and_publish(log_type=log_type,
|
|
|
|
+ crawler=crawler,
|
|
|
|
+ strategy="推荐榜爬虫策略",
|
|
|
|
+ our_uid="recommend",
|
|
|
|
+ env=env,
|
|
|
|
+ oss_endpoint=oss_endpoint)
|
|
|
|
+ if env == 'dev':
|
|
|
|
+ our_video_link = f"https://testadmin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
|
|
|
|
+ else:
|
|
|
|
+ our_video_link = f"https://admin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
|
|
|
|
+ Common.logger(log_type, crawler).info("视频上传完成")
|
|
|
|
+
|
|
|
|
+ if our_video_id is None:
|
|
|
|
+ # 删除视频文件夹
|
|
|
|
+ shutil.rmtree(f"./{crawler}/videos/{video_dict['video_title']}")
|
|
|
|
+ return
|
|
|
|
+
|
|
|
|
+ # 视频信息保存至飞书
|
|
|
|
+ Feishu.insert_columns(log_type, crawler, "070a67", "ROWS", 1, 2)
|
|
|
|
+ # 视频ID工作表,首行写入数据
|
|
|
|
+ upload_time = int(time.time())
|
|
|
|
+ values = [[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(upload_time)),
|
|
|
|
+ "推荐榜爬虫策略",
|
|
|
|
+ video_dict["video_title"],
|
|
|
|
+ video_dict["video_id"],
|
|
|
|
+ video_dict["duration"],
|
|
|
|
+ f'{video_dict["video_width"]}*{video_dict["video_height"]}',
|
|
|
|
+ our_video_link,
|
|
|
|
+ video_dict["cover_url"],
|
|
|
|
+ video_dict["video_url"]]]
|
|
|
|
+ time.sleep(1)
|
|
|
|
+ Feishu.update_values(log_type, crawler, "070a67", "F2:V2", values)
|
|
|
|
+ Common.logger(log_type, crawler).info(f"视频已保存至飞书文档\n")
|
|
|
|
+
|
|
|
|
+ rule_dict = {}
|
|
|
|
+ # 视频信息保存数据库
|
|
|
|
+ insert_sql = f""" insert into crawler_video(video_id,
|
|
|
|
+ out_user_id,
|
|
|
|
+ platform,
|
|
|
|
+ strategy,
|
|
|
|
+ out_video_id,
|
|
|
|
+ video_title,
|
|
|
|
+ cover_url,
|
|
|
|
+ video_url,
|
|
|
|
+ duration,
|
|
|
|
+ publish_time,
|
|
|
|
+ play_cnt,
|
|
|
|
+ crawler_rule,
|
|
|
|
+ width,
|
|
|
|
+ height)
|
|
|
|
+ values({our_video_id},
|
|
|
|
+ "{video_dict['user_id']}",
|
|
|
|
+ "{cls.platform}",
|
|
|
|
+ "推荐榜爬虫策略",
|
|
|
|
+ "{video_dict['video_id']}",
|
|
|
|
+ "{video_dict['video_title']}",
|
|
|
|
+ "{video_dict['cover_url']}",
|
|
|
|
+ "{video_dict['video_url']}",
|
|
|
|
+ {int(video_dict['duration'])},
|
|
|
|
+ "{video_dict['publish_time_str']}",
|
|
|
|
+ {int(video_dict['play_cnt'])},
|
|
|
|
+ '{json.dumps(rule_dict)}',
|
|
|
|
+ {int(video_dict['video_width'])},
|
|
|
|
+ {int(video_dict['video_height'])}) """
|
|
|
|
+ Common.logger(log_type, crawler).info(f"insert_sql:{insert_sql}")
|
|
|
|
+ MysqlHelper.update_values(log_type, crawler, insert_sql, env, action='')
|
|
|
|
+ Common.logger(log_type, crawler).info('视频信息插入数据库成功!\n')
|
|
|
|
+ driver.press_keycode(AndroidKey.BACK)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ Common.logger(log_type, crawler).error(f'download_publish异常:{e}\n')
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|