Files
appointment_tool/check_results.py
T

139 lines
5.5 KiB
Python

import logging
import random
import threading
from concurrent.futures import ThreadPoolExecutor
from enum import Enum
from typing import Union
from playwright.sync_api import sync_playwright
import params
from logs.LogSender import TYPE_EVENT_CHECK_RESULTS, LOG_SUBJECT_EVENT
from pojo.ReserveResultPojo import ReserveResultPojo, PublishType
SORRY_SENTENCE = "nous sommes sincèrement désolés de n'avoir pu vous satisfaire cette fois-ci"
NOT_AVAILABLE_CONTENT = "For more than 130 years, our House has offered its full expertise to satisfy"
PENDING_SENTENCE = "Ce soir, entre 20:00 et 20:30, vous obtiendrez une réponse par e-mail."
PENDING_SENTENCE_EN = "This evening between 20:00 and 20:30 you will receive a response by email."
class ResultEnum(Enum):
ACCEPTED = "ACCEPTED"
REFUSED = "REFUSED"
PENDING = "PENDING"
class TlsPlaywright(threading.local):
def __init__(self) -> None:
self.playwright = sync_playwright().start()
print("Create playwright instance in Thread", threading.current_thread().name)
class ResultChecker:
tls = TlsPlaywright()
def __init__(self):
self.logger = logging.getLogger("Worker")
def load_page(self, playwright, proxy, url) -> Union[str, None]:
try:
self.browser = playwright.webkit.launch(headless=False, timeout=90000, proxy=proxy)
device = random.choice(params.DEVICES)
self.logger.info("模拟设备: " + device)
pixel_2 = self.tls.playwright.devices[device]
context = self.browser.new_context(**pixel_2, locale='en-GB')
self.page = context.new_page()
# hide webdriver information
self.page.add_init_script("""() => {
Object.defineProperty(navigator,'webdriver',{get: () => undefined});
Object.defineProperty(navigator, 'platform', {
get: () => {
return "iPhone";
}});
}
""")
self.page.goto(url, timeout=90000)
return self.page.content()
except Exception as error:
print(error)
return None
def run(self, reserve_pojo: ReserveResultPojo, collection):
print("Launched worker in ", threading.current_thread().name)
url = reserve_pojo.url
phone_number = reserve_pojo.phone
# url_to_check = url.replace("register/", "")
# url_to_check = url + "?lang=fr"
print("url is " + url)
content = None
random_id_number = str(phone_number)[1:len(str(phone_number))]
proxy_username = "panleicim-res-fr-" + random_id_number
print("proxy_username is " + proxy_username)
proxy = {
"server": params.PROXY_SERVER,
"username": proxy_username,
"password": params.PROXY_PASSWORD
}
while content is None:
content = self.load_page(self.tls.playwright, proxy, url)
random_id_number = params.get_random_id_number_for_proxy()
proxy_username = "panleicim-res-fr-" + random_id_number
print("proxy_username is " + proxy_username)
proxy = {
"server": params.PROXY_SERVER,
"username": proxy_username,
"password": params.PROXY_PASSWORD
}
print(content)
self.browser.close()
print("Stopped worker in ", threading.current_thread().name)
status = None
if SORRY_SENTENCE in content:
print("status is REFUSED")
status = ResultEnum.REFUSED
elif PENDING_SENTENCE in content:
print("status is PENDING")
status = ResultEnum.PENDING
elif PENDING_SENTENCE_EN in content:
print("status is PENDING")
status = ResultEnum.PENDING
elif NOT_AVAILABLE_CONTENT in content:
print("status is REFUSED")
status = ResultEnum.REFUSED
else:
print("status is ACCEPTED")
status = ResultEnum.ACCEPTED
collection.document(reserve_pojo.id).update({u'accepted': status.name})
# need to start at 21h00
if __name__ == '__main__':
# get the list
params.oracle_log_sender.send_log(msg="开始检查约会结果", subject=LOG_SUBJECT_EVENT, type=TYPE_EVENT_CHECK_RESULTS)
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()
count = 0
# result_pojo = ReserveResultPojo(type=PublishType.SUCCESS, phone="0649614591", email="panleicim@gmail.com",
# message="SUCCESS", firstName="Lei", lastName="PAN", url='https://api.ipify.org')
result_list = []
for appointment in collection.stream():
reserve_pojo = ReserveResultPojo.from_firestore_dict(appointment.to_dict())
result_list.append(reserve_pojo)
# result_list.append(result_pojo)
# for result in result_list:
# if result.accepted is None or ResultEnum.PENDING.value == result.accepted:
# ResultChecker().run(result, collection)
# else:
# print("status is " + result.accepted)
with ThreadPoolExecutor(max_workers=5) as executor:
for reserve in result_list:
count = count + 1
if reserve.accepted is None or ResultEnum.PENDING.value == reserve.accepted:
executor.submit(ResultChecker().run, reserve, collection)
else:
print("status is " + reserve.accepted)
print(count)