Skip to content

Commit

Permalink
modified: app/app.py
Browse files Browse the repository at this point in the history
	modified:   app/async_template.py
	modified:   app/requirements.txt
  • Loading branch information
Eloco committed Oct 22, 2022
1 parent 20523b9 commit eb153f6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
33 changes: 19 additions & 14 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os,sys,json
import traceback
import tempfile
from pprint import pprint
import importlib.machinery
import copy

app = Flask(__name__)
Expand All @@ -34,38 +36,41 @@ async def hello_world():
async def playwright():
try:
status_code = 200
script_dir = os.path.dirname(os.path.abspath(__file__))
template_file="async_template.py"
result="Hello Flask POST!"

if_stealth = bool(request.form.get('stealth' , default = False))
if_reindent = bool(request.form.get('reindent' , default = True))
browser_name = str(request.form.get('browser' , default = 'webkit')).strip().lower()
device_name = str(request.form.get('device' , default = '')).strip().lower()

run_code = str(request.form.get('run', default =f'result="{result}"')).strip()

template_file="async_template.py"

try:
run_code=base64.b64decode(run_code).decode('utf-8')
except:pass
run_code = run_code.replace('\r','\n').replace('\n','\n'+" "*4*2)

with tempfile.TemporaryDirectory() as tmpdirname:
#print('created temporary directory', tmpdirname)
os.chdir(sys.path[0])
template=open(template_file).read()
template=open(os.path.join(script_dir,template_file)).read()
template=template.replace('<run_code>',run_code)
sys.path = list(filter(lambda x:os.path.exists(x) and \
"/var/folders/" not in x[:13],sys.path)) ##clean sys.path
sys.path.insert(1, tmpdirname)
file_path=os.path.join(tmpdirname,"template.py")
with open(file_path,"w") as f:f.write(template)
if if_reindent:os.system(f"reindent {file_path} &> /dev/null;autopep8 {file_path} --select=E121,E101,E11 --in-place &> /dev/null;cat {file_path}")
import template
result=await template.main(browser_name=browser_name,if_stealth=if_stealth,device_name=device_name)

dynamic_path=os.path.join(tmpdirname,"dynamic.py")
with open(dynamic_path,"w") as f:
f.write(template)
del template

if if_reindent:os.system(f"python -m reindent {dynamic_path} &> /dev/null; \
python -m autopep8 {dynamic_path} --select=E121,E101,E11 --in-place &> /dev/null; \
cat {dynamic_path}")

loader = importlib.machinery.SourceFileLoader('dynamic', dynamic_path)
handle = loader.load_module('dynamic')
result = await handle.main(browser_name=browser_name,\
if_stealth=if_stealth,\
device_name=device_name))
except Exception as e:
#result=str(e)
result=traceback.format_exc()
traceback.print_exc()
status_code = 500
Expand Down
7 changes: 4 additions & 3 deletions app/async_template.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/usr/bin/env python
# coding=utf-8
from playwright_stealth import stealth_async
import asyncio
import sh
import datetime,time,random
import requests
import base64
Expand All @@ -11,9 +8,12 @@
import traceback
import random
import copy
import sh
import re

from playwright.async_api import async_playwright
from playwright_stealth import stealth_async
import asyncio

async def main(browser_name,if_stealth,device_name):
async with async_playwright() as playwright:
Expand Down Expand Up @@ -44,4 +44,5 @@ async def main(browser_name,if_stealth,device_name):
print("page is stealth!")
stealth_async(page)
<run_code>

return(result)
3 changes: 2 additions & 1 deletion app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sh==1.*
lxml==4.8.0
lxml==4.9.1
html2text==2020.1.16
html5lib==1.1
pandas==1.4.2
PySocks==1.7.1
Expand Down

0 comments on commit eb153f6

Please sign in to comment.