log to file

This commit is contained in:
2024-03-20 14:18:38 +01:00
parent 53ea721cb6
commit 40c7c7cf96
4 changed files with 63 additions and 75 deletions
+25 -24
View File
@@ -48,10 +48,11 @@ def is_open():
class AppointmentRequestSender(threading.Thread):
def __init__(self, sub_contact_list: list, proxy_to_use_list, cookiesPublisher: CookiesPublisher,
def __init__(self, sub_contact_list: list, proxy_to_use_list, logger, cookiesPublisher: CookiesPublisher,
queue_name=REQUEST_DATA_QUEUE):
super().__init__()
self.connection = None
self.logger = logger
self.cookiesPublisher = cookiesPublisher
self.channel = None
self.valid_csrf = None
@@ -65,22 +66,22 @@ class AppointmentRequestSender(threading.Thread):
self.channel = self.connection.channel()
def listen_to_queue(self, callback):
print("listen to queue {}".format(self.queue_name))
self.logger.info("listen to queue {}".format(self.queue_name))
self.channel.basic_qos(prefetch_count=1)
self.channel.basic_consume(queue=self.queue_name, auto_ack=False, on_message_callback=callback)
self.channel.start_consuming()
def on_message(self, ch, method, properties, body):
_message_count = self.cookiesPublisher.message_count()
print("message count in queue is {}".format(_message_count))
self.logger.info("message count in queue is {}".format(_message_count))
_received_object = body.decode("UTF-8")
print(f" [x] Received {_received_object}")
self.logger.info(f" [x] Received {_received_object}")
_port = random.randint(40001, 49999)
print("generated port is {}".format(_port))
_choosed_proxy = random.choice(self.proxy_to_use_list)
self.logger.info("generated port is {}".format(_port))
_chosen_proxy = random.choice(self.proxy_to_use_list)
_proxy_to_use = {}
_proxy_to_use["http"] = _choosed_proxy["http"].format(_port)
_proxy_to_use["https"] = _choosed_proxy["https"].format(_port)
_proxy_to_use["http"] = _chosen_proxy["http"].format(_port)
_proxy_to_use["https"] = _chosen_proxy["https"].format(_port)
if "glrd" in _received_object:
_received_dict = json.loads(_received_object)
js_data = JsDataPojo(glrd=_received_dict['glrd'], glvd=_received_dict['glvd'], hc=_received_dict['hc'],
@@ -93,16 +94,16 @@ class AppointmentRequestSender(threading.Thread):
rs_w=_received_dict['rs_w'], rs_cd=_received_dict['rs_cd'])
_received_cookies = _received_dict["cookiesStr"]
sender = Sender(_received_cookies, cookiesPublisher=self.cookiesPublisher, received_dict=_received_dict,
proxy_to_use=_proxy_to_use)
proxy_to_use=_proxy_to_use, logger=self.logger)
self.contact_list = filter_contacts(self.contact_list)
# remove already booked contacts
random.shuffle(self.contact_list)
if len(self.contact_list) > 0 and is_open():
captchaResultGetter = CaptchaResultGetter()
print("contact number is {}".format(len(self.contact_list)))
self.logger.info("contact number is {}".format(len(self.contact_list)))
self.contact_list = filter_contacts(self.contact_list)
for con in self.contact_list:
print(con.mail)
self.logger.info(con.mail)
if self.valid_csrf is None:
self.valid_csrf = captchaResultGetter.get_csrf(
proxy_to_use=_proxy_to_use, js_data=js_data,
@@ -110,8 +111,8 @@ class AppointmentRequestSender(threading.Thread):
_new_cookies = captchaResultGetter.get_valid_ch_cookie(sender.proxy_to_use, js_data,
old_valid_cookie=_received_cookies)
if _new_cookies is not None:
print("new cookie is " + _new_cookies)
time.sleep(random.randint(1, 5))
# self.logger.info("new cookie is " + _new_cookies)
time.sleep(random.randint(1, 3))
# m_s_c = f.scroll
m_s_c = random.randint(0, 3)
m_c_c = random.randint(3, 5) # click count
@@ -138,38 +139,38 @@ class AppointmentRequestSender(threading.Thread):
js_le_type_data=js_le_data,
old_valid_cookie=_new_cookies)
if _new_le_cookies is not None:
print("new le type cookie is " + _new_le_cookies)
# self.logger.info("new le type cookie is " + _new_le_cookies)
sender.cookie_str = _new_le_cookies
time.sleep(random.randint(1, 5))
time.sleep(random.randint(1, 3))
can_continue = sender.send_request(HERMES_REGISTER, js_data, con, csrf=self.valid_csrf)
else:
can_continue = RequestResult.COOKIES_ERROR
else:
can_continue = RequestResult.COOKIES_ERROR
if can_continue == RequestResult.BLOCKED:
print("cannot continue, valid_csrf is " + str(self.valid_csrf))
self.logger.info("cannot continue, valid_csrf is " + str(self.valid_csrf))
break
elif can_continue == RequestResult.PROXY_ERROR:
print("PROXY_ERROR, will not reset valid_csrf")
self.logger.info("PROXY_ERROR, will not reset valid_csrf")
elif can_continue == RequestResult.COOKIES_ERROR:
print("COOKIES_ERROR, will not reset valid_csrf")
self.logger.info("COOKIES_ERROR, will not reset valid_csrf")
else:
print("can continue, will reset valid_csrf")
self.logger.info("can continue, will reset valid_csrf")
self.valid_csrf = None
time.sleep(random.randint(1, 2))
print("will ack method.delivery_tag: " + str(method.delivery_tag))
self.logger.info("will ack method.delivery_tag: " + str(method.delivery_tag))
ch.basic_ack(delivery_tag=method.delivery_tag)
else:
print("empty list")
self.logger.info("empty list")
time.sleep(120)
print("will basic_reject method.delivery_tag: " + str(method.delivery_tag))
self.logger.info("will basic_reject method.delivery_tag: " + str(method.delivery_tag))
ch.basic_reject(delivery_tag=method.delivery_tag, requeue=True)
else:
print("not a valid object")
self.logger.info("not a valid object")
ch.basic_ack(delivery_tag=method.delivery_tag)
def run(self):
print(threading.currentThread().name + " starts")
self.logger.info(threading.currentThread().name + " starts")
self.set_up_connection()
self.listen_to_queue(self.on_message)
self.channel.start_consuming()