and commited on
Commit
1380f39
1 Parent(s): 12c5777
Files changed (2) hide show
  1. Dockerfile +8 -0
  2. uc_server.py +202 -0
Dockerfile ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+
2
+ FROM ghcr.io/flaresolverr/flaresolverr:latest
3
+
4
+ WORKDIR /usr/app
5
+
6
+ COPY . .
7
+
8
+ CMD ["sh", "-c", "pip install undetected-chromedriver xvfbwrapper && pip install Flask && python uc_server.py]
uc_server.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask
2
+ from flask import request
3
+ import undetected_chromedriver as uc
4
+ import re
5
+ import os
6
+ from threading import Timer
7
+
8
+ # /start_browser
9
+ # /stop_browser
10
+ # /text?url=https://api.investing.com/api/financialdata/8849/historical/chart/?interval=PT1M&pointscount=60
11
+ # encoded: https%3A%2F%2Fapi.investing.com%2Fapi%2Ffinancialdata%2F8849%2Fhistorical%2Fchart%2F%3Finterval%3DPT1M%26pointscount%3D60
12
+
13
+ # /fetch?url=https://api.investing.com/api/financialdata/historical/1?start-date=2024-02-15&end-date=2029-02-15&time-frame=Daily&add-missing-rows=false
14
+ # encoded: https%3A%2F%2Fapi.investing.com%2Fapi%2Ffinancialdata%2Fhistorical%2F1%3Fstart-date%3D2024-02-15%26end-date%3D2029-02-15%26time-frame%3DDaily%26add-missing-rows%3Dfalse
15
+
16
+ driver = None
17
+ XVFB_DISPLAY = None
18
+ USER_AGENT = None
19
+ CHROME_EXE_PATH = None
20
+ timer = None
21
+
22
+ app = Flask(__name__)
23
+
24
+ def _start_xvfb_display():
25
+ global XVFB_DISPLAY
26
+ if XVFB_DISPLAY is None:
27
+ from xvfbwrapper import Xvfb
28
+ XVFB_DISPLAY = Xvfb()
29
+ XVFB_DISPLAY.start()
30
+
31
+ def get_chrome_exe_path() -> str:
32
+ global CHROME_EXE_PATH
33
+ if CHROME_EXE_PATH is not None:
34
+ return CHROME_EXE_PATH
35
+ # linux pyinstaller bundle
36
+ chrome_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chrome', "chrome")
37
+ if os.path.exists(chrome_path):
38
+ CHROME_EXE_PATH = chrome_path
39
+ return CHROME_EXE_PATH
40
+ # system
41
+ CHROME_EXE_PATH = uc.find_chrome_executable()
42
+ return CHROME_EXE_PATH
43
+
44
+ def _start_browser():
45
+ global driver, USER_AGENT
46
+
47
+ if driver != None:
48
+ driver.quit()
49
+ driver = None
50
+
51
+ _start_xvfb_display()
52
+
53
+ # https://github.com/FlareSolverr/FlareSolverr/blob/043f18b231b4f409080b2b5c4421ce0f4cac7dec/src/utils.py
54
+ options = uc.ChromeOptions()
55
+ options.add_argument('--no-sandbox')
56
+ options.add_argument('--window-size=1920,1080')
57
+ # todo: this param shows a warning in chrome head-full
58
+ options.add_argument('--disable-setuid-sandbox')
59
+ options.add_argument('--disable-dev-shm-usage')
60
+ # this option removes the zygote sandbox (it seems that the resolution is a bit faster)
61
+ options.add_argument('--no-zygote')
62
+ # attempt to fix Docker ARM32 build
63
+ options.add_argument('--disable-gpu-sandbox')
64
+ options.add_argument('--disable-software-rasterizer')
65
+ options.add_argument('--ignore-certificate-errors')
66
+ options.add_argument('--ignore-ssl-errors')
67
+ # fix GL errors in ASUSTOR NAS
68
+ # https://github.com/FlareSolverr/FlareSolverr/issues/782
69
+ # https://github.com/microsoft/vscode/issues/127800#issuecomment-873342069
70
+ # https://peter.sh/experiments/chromium-command-line-switches/#use-gl
71
+ options.add_argument('--use-gl=swiftshader')
72
+ #options.add_argument("--headless=new")
73
+ if USER_AGENT is not None:
74
+ options.add_argument('--user-agent=%s' % USER_AGENT)
75
+ language = os.environ.get('LANG', None)
76
+ if language is not None:
77
+ options.add_argument('--lang=%s' % language)
78
+
79
+ # added by me
80
+ options.add_argument(' --disable-web-security') # allow cross origin
81
+
82
+ driver = uc.Chrome(options=options, headless=False, version_main=None, driver_executable_path="/app/chromedriver", browser_executable_path=get_chrome_exe_path())
83
+
84
+ if USER_AGENT is None:
85
+ USER_AGENT = driver.execute_script("return navigator.userAgent")
86
+ USER_AGENT = re.sub('HEADLESS', '', USER_AGENT, flags=re.IGNORECASE)
87
+ app.logger.info(USER_AGENT)
88
+
89
+ driver.quit()
90
+ #restart with user agent
91
+ options = uc.ChromeOptions()
92
+ options.add_argument('--no-sandbox')
93
+ options.add_argument('--window-size=1920,1080')
94
+ # todo: this param shows a warning in chrome head-full
95
+ options.add_argument('--disable-setuid-sandbox')
96
+ options.add_argument('--disable-dev-shm-usage')
97
+ # this option removes the zygote sandbox (it seems that the resolution is a bit faster)
98
+ options.add_argument('--no-zygote')
99
+ # attempt to fix Docker ARM32 build
100
+ options.add_argument('--disable-gpu-sandbox')
101
+ options.add_argument('--disable-software-rasterizer')
102
+ options.add_argument('--ignore-certificate-errors')
103
+ options.add_argument('--ignore-ssl-errors')
104
+ # fix GL errors in ASUSTOR NAS
105
+ # https://github.com/FlareSolverr/FlareSolverr/issues/782
106
+ # https://github.com/microsoft/vscode/issues/127800#issuecomment-873342069
107
+ # https://peter.sh/experiments/chromium-command-line-switches/#use-gl
108
+ options.add_argument('--use-gl=swiftshader')
109
+ if USER_AGENT is not None:
110
+ options.add_argument('--user-agent=%s' % USER_AGENT)
111
+ language = os.environ.get('LANG', None)
112
+ if language is not None:
113
+ options.add_argument('--lang=%s' % language)
114
+
115
+ # added by me
116
+ options.add_argument(' --disable-web-security') # allow cross origin
117
+
118
+ driver = uc.Chrome(options=options, headless=False, version_main=None, driver_executable_path="/app/chromedriver", browser_executable_path=get_chrome_exe_path())
119
+
120
+ _reset_stop_timer()
121
+
122
+ app.logger.info("browser started")
123
+
124
+
125
+ def _stop_browser():
126
+ global driver
127
+ driver.quit()
128
+ driver = None
129
+ app.logger.info("browser stopped")
130
+
131
+ def _reset_stop_timer():
132
+ global timer
133
+ if timer is not None:
134
+ timer.cancel()
135
+ timer = Timer(5*60, _stop_browser) # _stop_browser executed in another thread...
136
+ timer.start()
137
+
138
+ @app.route("/")
139
+ def hello_world():
140
+ return "<p>Hello, World!</p>"
141
+
142
+ @app.route("/start_browser")
143
+ def start_browser():
144
+ _start_browser()
145
+ return "ok"
146
+
147
+ @app.route("/close_browser")
148
+ def stop_browser():
149
+ _stop_browser()
150
+ return "ok"
151
+
152
+ @app.route("/text")
153
+ def text():
154
+ global driver
155
+ if driver == None:
156
+ _start_browser()
157
+ else:
158
+ _reset_stop_timer()
159
+
160
+ url = request.args.get('url', '')
161
+ driver.get(url)
162
+ text = driver.page_source
163
+ #driver.close()
164
+ return text
165
+
166
+ @app.route("/screenshot")
167
+ def screenshot():
168
+ return "todo"
169
+
170
+ @app.route("/evaluate")
171
+ def evaluate():
172
+ return "todo"
173
+
174
+ @app.route("/fetch")
175
+ def fetch():
176
+ global driver
177
+ if driver == None:
178
+ _start_browser()
179
+ else:
180
+ _reset_stop_timer()
181
+
182
+ url = request.args.get('url', '')
183
+
184
+ #driver.get('https://example.com')
185
+ driver.get('https://i-invdn-com.investing.com/redesign/images/seo/investing_300X300.png')
186
+
187
+ script = """
188
+ var callback = arguments[arguments.length - 1]; // this is the callback to call when you are done
189
+ (async function(){
190
+ try {
191
+ let res = await fetch('%s', {headers:{'domain-id':'www'}});
192
+ let text = await res.text();
193
+ callback(text);
194
+ } catch (e) {
195
+ callback('error: ' + e);
196
+ }
197
+ })()""" % (url)
198
+ result = driver.execute_async_script(script)
199
+ return result
200
+
201
+ if __name__ == '__main__':
202
+ app.run(host="0.0.0.0", port=7860, debug=True)