forked from amu-kuroneko/K-AutoBook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathk_auto_book.py
executable file
·186 lines (158 loc) · 6.99 KB
/
k_auto_book.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# --- coding: utf-8 ---
import os
import re
import sys
from os import path
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
from config import Config
from runner import AbstractRunner
def _make_directory(directory):
if not path.isdir(directory):
try:
os.makedirs(directory)
except OSError as exception:
print(f"Directory creation failed ({directory})")
raise exception
def _initialize_driver(config, profile="chrome"):
# TODO out source (best: configuration, better: class)
log_name = path.join(config.log_directory, 'driver.log')
if profile == 'chrome':
chrome_options = ChromeOptions()
if config.chrome_binary:
print(config.chrome_binary)
chrome_options.binary_location = config.chrome_binary
chrome_options.add_argument('high-dpi-support=1')
chrome_options.add_argument('device-scale-factor=1')
chrome_options.add_argument('force-device-scale-factor=1')
chrome_options.add_argument('disable-gpu')
if config.headless:
print(f"headless: {config.headless}")
chrome_options.headless = True
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=960,1222')
if config.profile_directory:
print(f"use profile: {config.profile_directory}")
chrome_options.add_argument(f'--user-data-dir={config.profile_directory}')
if config.user_agent:
chrome_options.add_argument(f'user-agent={config.user_agent}')
# https://stackoverflow.com/a/59111770
chrome_options.add_argument('disable-web-security')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
service = Service(options=chrome_options, service_args=["--verbose", f"--log-path={log_name}"])
driver = webdriver.Chrome(service=service)
elif profile == 'chrome_headless':
chrome_options = ChromeOptions()
if config.chrome_binary:
print(config.chrome_binary)
chrome_options.binary_location = config.chrome_binary
chrome_options.add_argument('high-dpi-support=1')
chrome_options.add_argument('device-scale-factor=1')
chrome_options.add_argument('force-device-scale-factor=1')
chrome_options.add_argument('disable-gpu')
chrome_options.add_argument('--app=https://www.google.com')
chrome_options.add_argument(f'--user-data-dir={config.profile_directory}')
chrome_options.add_argument('--window-size=847,1228')
# TODO headless doesn't work at amazon
if config.user_agent:
chrome_options.add_argument(f'user-agent={config.user_agent}')
# https://stackoverflow.com/a/59111770
chrome_options.add_argument('disable-web-security')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
service = Service(options=chrome_options, service_args=["--verbose", f"--log-path={log_name}"])
driver = webdriver.Chrome(service=service)
elif profile == 'chrome_existing':
chrome_options = ChromeOptions()
if config.chrome_binary:
print(config.chrome_binary)
chrome_options.binary_location = config.chrome_binary
chrome_options.add_argument('high-dpi-support=1')
chrome_options.add_argument('device-scale-factor=1')
chrome_options.add_argument('force-device-scale-factor=1')
chrome_options.add_argument('disable-gpu')
if config.headless:
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=960,1222')
if config.user_agent:
chrome_options.add_argument(f'user-agent={config.user_agent}')
# https://stackoverflow.com/a/59111770
chrome_options.add_argument('disable-web-security')
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9014")
service = Service(options=chrome_options, service_args=["--verbose", f"--log-path={log_name}"])
driver = webdriver.Chrome(service=service)
else:
service = Service(service_args=["--verbose", f"--log-path={log_name}"])
driver = webdriver.Chrome(service=service)
return driver
def _reset_driver(driver, plugin):
driver.close()
print('recreate driver')
return _initialize_driver(plugin.config, plugin.sub_config.driver)
def _main():
"""
$ python k_auto_book.py url [option]
or
$ python k_auto_book.py '?python script'
"""
config = Config()
_make_directory(config.log_directory)
_make_directory(config.base_directory)
driver = _initialize_driver(config)
profile = config.driver
print(f'driver: {profile}')
stripper = re.compile(r'^\s+')
plugin_classes = AbstractRunner.get_plugins()
# print(f'{plugin_classes}')
plugins = [p(m, driver, config) for m, p in plugin_classes]
input_data = None
if len(sys.argv) > 1:
input_data = str.join(' ', sys.argv[1:])
while True:
if not input_data:
try:
input_data = stripper.sub('', input('Input URL > '))
except EOFError:
print("\nBye.")
break
if input_data == '':
continue
elif input_data == 'exit':
print('Bye.')
break
elif input_data[0:1] == '?':
"""
you can script as python syntax if input strings starts with '?'
for example:
Input URL > ?[f'https://web-ace.jp/youngaceup/contents/1000053/episode/{n}/' for n in range(1124, 1152)]
this creates urls of range 1124 ~ 1152. and downloads the contents of the url automatically.
"""
urls = eval(input_data[1:])
options = None
else:
inputs_data = input_data.split(' ', 1)
urls = [inputs_data[0]]
options = inputs_data[1] if 1 < len(inputs_data) else None
input_data = None
for url in urls:
done = False
print(f'url: {url}')
for plugin in plugins:
# print(plugin)
if plugin.check(url):
if done or (plugin.sub_config.driver and plugin.sub_config.driver != profile):
driver = _reset_driver(driver, plugin)
profile = plugin.sub_config.driver
print(f'driver: {profile}')
plugin.reset(driver)
plugin.init(url, options)
plugin.run()
print('', flush=True)
done = True
if not done:
print('URL is not supported')
if __name__ == '__main__':
_main()