can run playwright in multi-threads
This commit is contained in:
+22
-22
@@ -8,7 +8,6 @@ from typing import Union
|
|||||||
from playwright.sync_api import sync_playwright
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
import params
|
import params
|
||||||
from workers.commandor_page import get_random_id_number_for_proxy
|
|
||||||
from logs.LogSender import TYPE_EVENT_CHECK_RESULTS, LOG_SUBJECT_EVENT
|
from logs.LogSender import TYPE_EVENT_CHECK_RESULTS, LOG_SUBJECT_EVENT
|
||||||
from pojo.ReserveResultPojo import ReserveResultPojo, PublishType
|
from pojo.ReserveResultPojo import ReserveResultPojo, PublishType
|
||||||
|
|
||||||
@@ -25,7 +24,7 @@ class ResultEnum(Enum):
|
|||||||
|
|
||||||
class TlsPlaywright(threading.local):
|
class TlsPlaywright(threading.local):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.playwright = params.SINGLE_PLAYWRIGHT_INSTANCE
|
self.playwright = sync_playwright().start()
|
||||||
print("Create playwright instance in Thread", threading.current_thread().name)
|
print("Create playwright instance in Thread", threading.current_thread().name)
|
||||||
|
|
||||||
|
|
||||||
@@ -76,7 +75,7 @@ class ResultChecker:
|
|||||||
}
|
}
|
||||||
while content is None:
|
while content is None:
|
||||||
content = self.load_page(self.tls.playwright, proxy, url)
|
content = self.load_page(self.tls.playwright, proxy, url)
|
||||||
random_id_number = get_random_id_number_for_proxy()
|
random_id_number = params.get_random_id_number_for_proxy()
|
||||||
proxy_username = "panleicim-res-fr-" + random_id_number
|
proxy_username = "panleicim-res-fr-" + random_id_number
|
||||||
print("proxy_username is " + proxy_username)
|
print("proxy_username is " + proxy_username)
|
||||||
proxy = {
|
proxy = {
|
||||||
@@ -110,24 +109,25 @@ if __name__ == '__main__':
|
|||||||
db_manager = params.firebase_store_manager
|
db_manager = params.firebase_store_manager
|
||||||
collection = db_manager.get_all_successful_items_for_day("2022-05-14", "landd")
|
collection = db_manager.get_all_successful_items_for_day("2022-05-14", "landd")
|
||||||
count = 0
|
count = 0
|
||||||
# result_pojo = ReserveResultPojo(type=PublishType.SUCCESS, phone="0649614591", email="panleicim@gmail.com",
|
result_pojo = ReserveResultPojo(type=PublishType.SUCCESS, phone="0649614591", email="panleicim@gmail.com",
|
||||||
# message="SUCCESS", firstName="Lei", lastName="PAN", url='https://api.ipify.org')
|
message="SUCCESS", firstName="Lei", lastName="PAN", url='https://api.ipify.org')
|
||||||
result_list = []
|
result_list = []
|
||||||
for appointment in collection.stream():
|
# for appointment in collection.stream():
|
||||||
reserve_pojo = ReserveResultPojo.from_firestore_dict(appointment.to_dict())
|
# reserve_pojo = ReserveResultPojo.from_firestore_dict(appointment.to_dict())
|
||||||
result_list.append(reserve_pojo)
|
# result_list.append(reserve_pojo)
|
||||||
# result_list.append(result_pojo)
|
result_list.append(result_pojo)
|
||||||
for result in result_list:
|
# for result in result_list:
|
||||||
if result.accepted is None or ResultEnum.ACCEPTED.value == result.accepted:
|
# if result.accepted is None or ResultEnum.PENDING.value == result.accepted:
|
||||||
ResultChecker().run(result, collection)
|
# ResultChecker().run(result, collection)
|
||||||
else:
|
# else:
|
||||||
print("status is " + result.accepted)
|
# print("status is " + result.accepted)
|
||||||
|
|
||||||
# with ThreadPoolExecutor(max_workers=1) as executor:
|
with ThreadPoolExecutor(max_workers=1) as executor:
|
||||||
# for reserve in result_list:
|
for reserve in result_list:
|
||||||
# count = count + 1
|
count = count + 1
|
||||||
# if reserve.accepted:
|
if reserve.accepted is None or ResultEnum.PENDING.value == reserve.accepted:
|
||||||
# print("status is " + reserve.accepted)
|
executor.submit(ResultChecker().run, reserve, collection)
|
||||||
# if reserve.accepted is None or ResultEnum.PENDING.value == reserve.accepted:
|
else:
|
||||||
# executor.submit(ResultChecker().run, reserve, collection)
|
print("status is " + reserve.accepted)
|
||||||
# print(count)
|
|
||||||
|
print(count)
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import configparser
|
import configparser
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
from playwright.sync_api import sync_playwright
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
@@ -11,7 +13,13 @@ oracle_log_sender = LogSender()
|
|||||||
# proxy
|
# proxy
|
||||||
PROXY_SERVER = "http://gw.ntnt.io:5959"
|
PROXY_SERVER = "http://gw.ntnt.io:5959"
|
||||||
PROXY_PASSWORD = "94sY7zwBG13i"
|
PROXY_PASSWORD = "94sY7zwBG13i"
|
||||||
SINGLE_PLAYWRIGHT_INSTANCE = sync_playwright().start()
|
|
||||||
|
def get_random_id_number_for_proxy() -> str:
|
||||||
|
S = 8 # number of characters in the string.
|
||||||
|
ran = ''.join(random.choices(string.digits, k=S))
|
||||||
|
id_number = str(ran)
|
||||||
|
print("The randomly generated string is : " + str(ran)) # print the random data
|
||||||
|
return id_number
|
||||||
|
|
||||||
DEVICES = ['iPad (gen 6)', 'iPad (gen 6) landscape', 'iPad (gen 7)', 'iPad (gen 7) landscape', 'iPad Mini',
|
DEVICES = ['iPad (gen 6)', 'iPad (gen 6) landscape', 'iPad (gen 7)', 'iPad (gen 7) landscape', 'iPad Mini',
|
||||||
'iPad Mini landscape', 'iPad Pro 11', 'iPad Pro 11 landscape', 'iPhone 6', 'iPhone 6 landscape',
|
'iPad Mini landscape', 'iPad Pro 11', 'iPad Pro 11 landscape', 'iPhone 6', 'iPhone 6 landscape',
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import threading
|
|||||||
import time
|
import time
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
import params
|
import params
|
||||||
from params import PROXY_SERVER, PROXY_PASSWORD
|
from params import PROXY_SERVER, PROXY_PASSWORD
|
||||||
from pojo.ReserveResultPojo import ReserveResultPojo, PublishType
|
from pojo.ReserveResultPojo import ReserveResultPojo, PublishType
|
||||||
@@ -31,7 +33,7 @@ def get_random_wait_time() -> float:
|
|||||||
|
|
||||||
class Tls(threading.local):
|
class Tls(threading.local):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.playwright = params.SINGLE_PLAYWRIGHT_INSTANCE
|
self.playwright = sync_playwright().start()
|
||||||
|
|
||||||
|
|
||||||
class CommandorPage:
|
class CommandorPage:
|
||||||
@@ -74,7 +76,7 @@ class CommandorPage:
|
|||||||
first_page = None
|
first_page = None
|
||||||
while first_page is None:
|
while first_page is None:
|
||||||
first_page = self.start_browser(proxy, self.tls.playwright, devices)
|
first_page = self.start_browser(proxy, self.tls.playwright, devices)
|
||||||
proxy_username = "panleicim-res-fr-" + get_random_id_number_for_proxy()
|
proxy_username = "panleicim-res-fr-" + params.get_random_id_number_for_proxy()
|
||||||
self.logger.info("proxy_username is " + proxy_username)
|
self.logger.info("proxy_username is " + proxy_username)
|
||||||
proxy = {
|
proxy = {
|
||||||
"server": params.PROXY_SERVER,
|
"server": params.PROXY_SERVER,
|
||||||
@@ -264,14 +266,6 @@ class CommandorPage:
|
|||||||
self.browser.close()
|
self.browser.close()
|
||||||
|
|
||||||
|
|
||||||
def get_random_id_number_for_proxy() -> str:
|
|
||||||
S = 8 # number of characters in the string.
|
|
||||||
ran = ''.join(random.choices(string.digits, k=S))
|
|
||||||
id_number = str(ran)
|
|
||||||
print("The randomly generated string is : " + str(ran)) # print the random data
|
|
||||||
return id_number
|
|
||||||
|
|
||||||
|
|
||||||
def get_random_id_number() -> str:
|
def get_random_id_number() -> str:
|
||||||
S = 8 # number of characters in the string.
|
S = 8 # number of characters in the string.
|
||||||
ran = ''.join(random.choices(string.digits, k=S))
|
ran = ''.join(random.choices(string.digits, k=S))
|
||||||
@@ -285,7 +279,7 @@ def on_success(result: ReserveResultPojo):
|
|||||||
|
|
||||||
|
|
||||||
def launch_page():
|
def launch_page():
|
||||||
PROXY_USERNAME = "panleicim-res-fr-" + get_random_id_number_for_proxy()
|
PROXY_USERNAME = "panleicim-res-fr-" + params.get_random_id_number_for_proxy()
|
||||||
print("proxy_username is " + PROXY_USERNAME)
|
print("proxy_username is " + PROXY_USERNAME)
|
||||||
proxy = {
|
proxy = {
|
||||||
"server": PROXY_SERVER,
|
"server": PROXY_SERVER,
|
||||||
|
|||||||
Reference in New Issue
Block a user