147 lines
6.0 KiB
Python
147 lines
6.0 KiB
Python
import datetime
|
|
import logging
|
|
import random
|
|
import threading
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from typing import Union
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
from src.db.mirgration.migration_tools import migre_accepted_appointment
|
|
from src.db.mongo_manager import MONGO_STORE_MANAGER
|
|
from src.definitions import LOG_SUBJECT_EVENT, TYPE_EVENT_CHECK_RESULTS
|
|
from src.logs.LogSender import LogSender
|
|
from src.notification.AcceptedResultPojo import get_accepted_result_from
|
|
from src.notification.mailer import Mailer
|
|
from src.pojo.ReserveResultPojo import ReserveResultPojo
|
|
from src.pojo.ResultEnum import ResultEnum
|
|
from src.proxy.proxy_type import ProxyType
|
|
from src import definitions, params
|
|
from src.workers.TlsPlaywright import TlsPlaywright
|
|
|
|
SORRY_SENTENCE_FR = "nous sommes sincèrement désolés de n'avoir pu vous satisfaire cette fois-ci"
|
|
SORRY_SENTENCE_EN = "we are extremely sorry that we were not able to fulfill"
|
|
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."
|
|
URL_TO_VALID_SENTENCE = "Nous avons envoyé un lien par e-mail."
|
|
PENDING_SENTENCE_EN = "This evening between 20:00 and 20:30 you will receive a response by email."
|
|
CAPTCHA_URL = "https://geo.captcha-delivery.com"
|
|
# URLs to ignore during checking results
|
|
BLANK_URL = "about:blank"
|
|
WELCOME_URL = "https://rendezvousparis.hermes.com/client/welcome"
|
|
|
|
mailer = Mailer()
|
|
oracle_log_sender = LogSender()
|
|
|
|
|
|
class ResultChecker:
|
|
tls = TlsPlaywright()
|
|
|
|
def __init__(self):
|
|
self.logger = logging.getLogger("Worker")
|
|
|
|
def load_page(self, playwright, proxy, url, device, headless) -> Union[str, None]:
|
|
try:
|
|
self.browser = playwright.webkit.launch(headless=headless, timeout=90000, proxy=proxy)
|
|
pixel_2 = self.tls.playwright.devices[device]
|
|
context = self.browser.new_context(**pixel_2, locale='fr-FR')
|
|
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)
|
|
self.logger.info("will close browser")
|
|
self.browser.close()
|
|
return None
|
|
|
|
def run(self, reserve_pojo: ReserveResultPojo, firestore_collection, headless=False, need_send_email=False):
|
|
url = reserve_pojo.url
|
|
print("url is " + url)
|
|
content = None
|
|
proxy = params.get_proxy(ProxyType.OXYLABS)
|
|
device = random.choice(params.DEVICES)
|
|
self.logger.info("模拟设备: " + device)
|
|
while content is None:
|
|
content = self.load_page(self.tls.playwright, proxy, url, device, headless)
|
|
proxy = params.get_proxy(ProxyType.OXYLABS)
|
|
print(content)
|
|
print("Stopped worker in ", threading.current_thread().name)
|
|
if SORRY_SENTENCE_FR in content:
|
|
print("status is REFUSED")
|
|
status = ResultEnum.REFUSED
|
|
elif SORRY_SENTENCE_EN in content:
|
|
print("status is REFUSED")
|
|
status = ResultEnum.REFUSED
|
|
elif PENDING_SENTENCE in content:
|
|
print("status is PENDING")
|
|
status = ResultEnum.PENDING
|
|
elif URL_TO_VALID_SENTENCE in content:
|
|
print("status is REFUSED")
|
|
status = ResultEnum.REFUSED
|
|
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
|
|
elif CAPTCHA_URL in content:
|
|
print("status is BLOCKED")
|
|
self.browser.close()
|
|
else:
|
|
print("status is ACCEPTED")
|
|
status = ResultEnum.ACCEPTED
|
|
# send email
|
|
try:
|
|
mailer.send_email(get_accepted_result_from(reserve_pojo), to_all=need_send_email)
|
|
except Exception as err:
|
|
print(err)
|
|
reserve_pojo.accepted = status
|
|
MONGO_STORE_MANAGER.update_reserve_result(reserve_pojo.id, status)
|
|
self.browser.close()
|
|
|
|
|
|
def check_results(headless=False):
|
|
# get the list
|
|
oracle_log_sender.send_log(msg="开始检查约会结果", subject=LOG_SUBJECT_EVENT, type=TYPE_EVENT_CHECK_RESULTS)
|
|
db_manager = definitions.firebase_store_manager
|
|
firestore_collection = db_manager.get_all_successful_items()
|
|
reserve_list = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
|
|
print("size is " + str(len(reserve_list)))
|
|
start_check(reserve_list, firestore_collection, headless, need_send_email=False)
|
|
reserve_list = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
|
|
start_check(reserve_list, firestore_collection, headless, need_send_email=True)
|
|
# copy the accepted info to the accepted collection
|
|
migre_accepted_appointment(str(datetime.date.today()))
|
|
|
|
|
|
def start_check(reserve_list, firestore_collection, headless: bool, need_send_email: bool):
|
|
count = 0
|
|
with ThreadPoolExecutor(max_workers=20) as executor:
|
|
for reserve in reserve_list:
|
|
count = count + 1
|
|
if reserve.accepted is None or ResultEnum.ACCEPTED.value == reserve.accepted:
|
|
print("will check result")
|
|
if "hotmail" in reserve.email:
|
|
if reserve.url != BLANK_URL:
|
|
if reserve.url != WELCOME_URL:
|
|
executor.submit(ResultChecker().run, reserve, firestore_collection, headless,
|
|
need_send_email)
|
|
else:
|
|
print("status is " + reserve.accepted)
|
|
|
|
print(count)
|
|
|
|
|
|
# need to start at 21h00
|
|
if __name__ == '__main__':
|
|
check_results()
|