Files
appointment_request/queue_message/link_validator_receiver.py

96 lines
3.4 KiB
Python

import random
import threading
import time
from http.cookies import SimpleCookie
import pika
from db.mongo_manager import MONGO_STORE_MANAGER
from models.contact_pojo import ContactPojo
from workers.captcha_result_getter import CaptchaResultGetter, HERMES_REGISTER
QUEUE_HOST = "appointment.lpaconsulting.fr"
REQUEST_DATA_QUEUE = 'REQUEST_DATA'
credentials = pika.PlainCredentials('appointment', 'ZyuhJZ2xEYWhElhpJjy7YEpZGZwNYJz2fHIu')
def is_already_sent(contact: ContactPojo) -> bool:
already_sent_contacts = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
for required_contact in already_sent_contacts:
if contact.mail == required_contact.email:
return True
return False
def filter_contacts(_contact_list: list) -> list:
already_sent_contacts = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
_contact_list_to_book = []
for contact in _contact_list:
_to_add = True
for booked in already_sent_contacts:
if contact.mail == booked.email:
_to_add = False
if _to_add:
_contact_list_to_book.append(contact)
return _contact_list_to_book
class LinkValidatorReceiver(threading.Thread):
def __init__(self, linkpojo_list: list):
self.connection = None
self.linkpojo_list = linkpojo_list
self.channel = None
def set_up_connection(self):
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host=QUEUE_HOST, port=5672, credentials=credentials))
self.channel = self.connection.channel()
def listen_to_queue(self, callback):
self.channel.basic_qos(prefetch_count=1)
self.channel.basic_consume(queue=REQUEST_DATA_QUEUE, auto_ack=False, on_message_callback=callback)
self.channel.start_consuming()
def on_message(self, ch, method, properties, body):
print(f" [x] Received {body}")
# remove already booked contacts
# random.shuffle(link_list)
# link = random.choice(link_list)
# _link_validator = LinkValidator(body.decode("UTF-8"), link_to_validate=link)
# _link_validator.send_request()
#
# print(f" [x] Received {body}")
# sender = Sender(body.decode("UTF-8"))
# remove already booked contacts
random.shuffle(self.linkpojo_list)
if len(self.linkpojo_list) > 0:
print("contact number is {}".format(len(self.linkpojo_list)))
for con in self.linkpojo_list:
# if not is_already_sent(con):
print(con.email)
can_continue = self.send_request(HERMES_REGISTER, con, csrf=self.valid_csrf)
if not can_continue:
print("cannot continue, valid_csrf is " + self.valid_csrf)
break
else:
print("can continue, will reset valid_csrf")
self.valid_csrf = None
# else:
# print(con.mail + "--> skip")
ch.basic_ack(delivery_tag=method.delivery_tag)
else:
print("empty list")
def run(self):
print(threading.currentThread().name + " starts")
self.set_up_connection()
self.listen_to_queue(self.on_message)
self.channel.start_consuming()
if __name__ == '__main__':
link_list = MONGO_STORE_MANAGER.get_links_to_validate()
receiver = LinkValidatorReceiver(link_list)
receiver.run()