-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_cookies.py
163 lines (134 loc) · 5.12 KB
/
create_cookies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# coding: utf-8
"""
Name: create_cookies.py
Description: Create cookies for login to the "https://www.ocn.ne.jp/"
Usage: python3 create_cookies.py --userid <user_id> --password <password>
Author: Ryosuke D. Tomita
Created: 2023/08/05
"""
import argparse
from selenium import webdriver
from selenium.webdriver.common.by import By
import pickle
import time
import os
from os.path import join, dirname, abspath, exists
import boto3
def parse_args() -> dict:
"""parse_args.
"""
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--userid", help="docomo userID", type=str)
parser.add_argument("-s", "--password", help="docomo user password", type=str)
parser.add_argument("-p", "--profile", help="aws cli profile", type=str, default="default")
parser.add_argument("-b", "--bucket", help="bucket name", type=str)
p = parser.parse_args()
args = {"userid": p.userid,
"password": p.password,
"profile": p.profile,
"bucket": p.bucket
}
return args
def cookies_file_is_valid(cookies_file_path: str) -> bool:
"""cookies_file_is_valid.
1. cookies.pklがローカルになければ空のファイルを作成し,Falseを返す。
2. cookies.pklが空の場合にはFalseを返す。
3. cookies.pklが存在し,中身がある場合にはtrueを返す。
"""
if not exists(cookies_file_path):
print("create cookies.pkl")
with open(cookies_file_path, 'wb'):
pass
return False
elif os.stat(cookies_file_path).st_size == 0:
print("cookies.pkl is empty")
return False
print("cookies.pkl is valid")
return True
def fetch_driver():
"""fetch_driver.
ヘッドレスモードでドライバを取得する。
"""
options = webdriver.ChromeOptions()
# headlessモードのchromiumを指定
options.binary_location = "./create_selenium_driver_layers/headless/headless-chromium"
options.add_argument("--headless")
options.add_argument('--single-process')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--no-sandbox")
# Layersに配置したものは/opt以下に展開される。
driver = webdriver.Chrome(
# chromedriverのパスを指定
executable_path="./create_selenium_driver_layers/headless/chromedriver",
options=options
)
return driver
def login(driver, user_id, password):
"""login.
Security codeはコンソールに手動で入力する。
$"Type security code: "123456
Args:
driver:
user_id:
password:
"""
login_button1 = driver.find_element(By.XPATH, '//*[@id="va14-vin-2d"]')
login_button1.click()
time.sleep(3)
# input ID
id_textbox = driver.find_element(By.XPATH, '//*[@id="Di_Uid"]')
id_textbox.send_keys(user_id)
next_button = driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[1]/div[4]/div[1]/form/input[4]')
next_button.click()
# input password and security_code
password_textbox = driver.find_element(By.XPATH, '//*[@id="Di_Pass"]')
password_textbox.send_keys(password)
security_code = input("Type security code: ")
security_code_textbox = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div[6]/form/dl[2]/dd[2]/input')
security_code_textbox.send_keys(security_code)
time.sleep(3)
# login
login_button2 = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div[6]/form/input[4]')
login_button2.click()
time.sleep(5)
def save_cookies(driver, cookies_file_path):
cookies = driver.get_cookies()
pickle.dump(cookies, open(cookies_file_path, 'wb'))
def upload_to_s3(bucket_name, cookie, profile):
"""_summary_
boto3は~/.aws/configにあるプロファイルを読み込む仕様のため
場所を変更しない。
aws s3 cp cookies.pkl s3://cookie-for-iceman2 --acl private --profile=default
upload: ./cookies.pkl to s3://cookie-for-iceman2/cookies.pkl
"""
session = boto3.Session(profile_name=profile)
s3 = session.client("s3")
s3.upload_file(cookie, bucket_name, cookie) # (local_file, bucket_name, s3_key)
def main():
"""_summary_
1. コマンドライン引数からid,passwordを取得する。
2. cookies.pklが存在し,有効か確認する。
3. driverを取得する。
4. ログインする。
5. cookies.pklに保存する。
"""
args = parse_args()
user_id = args['userid']
password = args['password']
bucket_name = args['bucket'] # s3 bucket
profile = args['profile'] # aws cli profile
url = "https://www.ocn.ne.jp/"
#cookies_file_path = abspath(join(dirname(__file__), 'cookies.pkl'))
cookies_file_path = "cookies.pkl" # フルパスで指定するとなぜかuploadできない
if cookies_file_is_valid(cookies_file_path):
upload_to_s3(bucket_name, cookies_file_path, profile)
return
driver = fetch_driver()
# login
driver.get(url)
login(driver, user_id, password)
save_cookies(driver, cookies_file_path)
driver.quit()
upload_to_s3(bucket_name, cookies_file_path, profile)
if __name__ == '__main__':
main()