-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
48 lines (41 loc) · 1.24 KB
/
main.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
import asyncio
from asyncio_helpers import asyncio_run
from flask import Flask, request
from multiprocessing import Process, Manager
import os
from pyppeteer import launch
import sys
from user_dir import USER_DATA_DIR
import urllib.parse
app = Flask(__name__)
async def get_async(d):
url = d['url']
browser = await launch(userDataDir=USER_DATA_DIR, args=['--no-sandbox'])
try:
page = await browser.newPage()
decoded_url = urllib.parse.unquote(url)
await page.goto(decoded_url)
#await page.waitForNavigation({"waitUtil": 'networkidle0', "timeout": 0})
#await asyncio.sleep(5)
html = await page.evaluate("() => document.querySelector('*').outerHTML")
await browser.close()
d['html'] = html
except:
await browser.close()
raise
def get_process(url):
return asyncio_run(get_async(url))
@app.route('/proxy', methods=['GET'])
def get():
with Manager() as manager:
d = manager.dict()
d['url'] = request.args.get('url')
try:
p = Process(target=get_process, args=(d,))
p.start()
p.join()
except:
raise
return d['html']
if __name__ == "__main__":
app.run(host='127.0.0.1')