From 94d7e75215f2eb707a3d6611abfd4c0eb5bbbc51 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 31 May 2023 09:20:37 +0900 Subject: [PATCH] initial commit --- .gitignore | 1 + app/main.py | 122 +++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 25 ++++++++++ python3/Dockerfile | 5 ++ 4 files changed, 153 insertions(+) create mode 100644 .gitignore create mode 100644 app/main.py create mode 100644 docker-compose.yml create mode 100644 python3/Dockerfile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..70228e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +app/ss/ \ No newline at end of file diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..3b1f2e5 --- /dev/null +++ b/app/main.py @@ -0,0 +1,122 @@ +# 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"] +waittime = 10 +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,8): + 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(waittime) + + driver.quit() + + +def main(): + init_profiles() + watching_rch() + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b3ae5c1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,25 @@ +version: '3' +services: + selenium: + restart: always + image: selenium/standalone-chrome:latest + container_name: 'selenium' + volumes: + - /dev/shm:/dev/shm + python3: + restart: always + build: ./python3 + container_name: 'rpoint-getter' + working_dir: '/var/app' + tty: true + environment: + - RAKUTENID=${RAKUTENID} + - RAKUTENPW=${RAKUTENPW} + volumes: + - ./app:/var/app + depends_on: + - selenium + entrypoint: + - python3 + command: + - /var/app/main.py diff --git a/python3/Dockerfile b/python3/Dockerfile new file mode 100644 index 0000000..2a6a027 --- /dev/null +++ b/python3/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.11 + +RUN pip3 install --upgrade pip + +RUN pip3 install selenium \ No newline at end of file