r-pointgtr/app/main.py

125 lines
3.6 KiB
Python
Raw Normal View History

2023-05-31 09:20:37 +09:00
# main.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time,os
# 変数初期化 ********************************************************************
profile_dir = "/tmp/profile"
username = os.environ["RAKUTENID"]
password = os.environ["RAKUTENPW"]
rch_time = int(os.environ["RCH_WATCH_TIME"])
rch_num = int(os.environ["RCH_WATCH_NUM"])
2023-05-31 09:20:37 +09:00
target_url = "https://channel.rakuten.co.jp/"
welcome_btn_css = 'button[data-ratid="welcome_pg1_ok"]'
login_btn_css = 'button[data-ratid="login_button"]'
user_textarea_css = 'input[aria-label="ユーザIDまたはメールアドレス"]'
passwd_textarea_css = 'input[type="password"]'
channel_btn_css = 'button[data-ratid="select_program_now_playing"]'
# ******************************************************************************
def gen_driver():
"""
WebDriver 生成
"""
options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
options.add_argument("--user-data-dir=" + profile_dir )
driver = webdriver.Remote(
command_executor="http://selenium:4444/wd/hub",
options=options
)
return driver
def init_profiles():
"""
初回ログイン等
"""
driver = gen_driver()
# Begin
driver.get(target_url)
# ロード待ち
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
driver.save_screenshot('./ss/ss.png')
# Welcomeメッセージを閉じる
try:
welcom_button = driver.find_element(By.CSS_SELECTOR, welcome_btn_css)
welcom_button.click()
except Exception as e:
pass
# ログイン試行
try:
# ログインボタン押下
login_button = driver.find_element(By.CSS_SELECTOR, login_btn_css)
login_button.click()
time.sleep(10)
# ユーザID入力
user = driver.find_element(By.CSS_SELECTOR, user_textarea_css)
user.send_keys(username)
driver.save_screenshot('./ss/ss_usrname.png')
user.send_keys(Keys.ENTER)
time.sleep(10)
# パスワード入力
passwd = driver.find_element(By.CSS_SELECTOR, passwd_textarea_css)
passwd.send_keys(password)
driver.save_screenshot('./ss/ss_password.png')
passwd.send_keys(Keys.ENTER)
time.sleep(10)
driver.save_screenshot('./ss/ss_password2.png')
except Exception as e:
print(e)
driver.save_screenshot('./ss/ss2.png')
# Coockie 取得
cookies = driver.get_cookies()
# End
driver.quit()
def watching_rch():
# Rch アクセス
driver = gen_driver()
driver.get(target_url)
# ロード待ち
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
# 10秒待機
time.sleep(10)
for i in range(0, rch_num):
print( str( i + 1) + "番目のチャンネルの視聴を始めます。視聴時間は" + str(rch_time) + "秒に設定されています。")
2023-05-31 09:20:37 +09:00
channel_btns = driver.find_elements(By.CSS_SELECTOR, channel_btn_css)
channel_btn = channel_btns[i]
channel_btn.click()
time.sleep(10)
driver.save_screenshot('./ss/channel_' + str(i) + '.png')
time.sleep(rch_time)
2023-05-31 09:20:37 +09:00
driver.quit()
def main():
init_profiles()
watching_rch()
if __name__ == '__main__':
time.sleep(60) # selniumコンテナの起動を待つ待機
2023-05-31 09:20:37 +09:00
main()