diff --git a/src/db/mongo_manager.py b/src/db/mongo_manager.py index 01c72f8..7298547 100755 --- a/src/db/mongo_manager.py +++ b/src/db/mongo_manager.py @@ -284,19 +284,35 @@ class MongoDbManager: collection.find_one_and_update({'_id': id}, {"$set": {"url_validated": state, "validated_at": validated_at}}, upsert=False) - def save_links_to_validate(self, link: str, mail_address: str): + def get_all_contacts_to_book(self) -> list: + _collection_name = CONTACT_LIST_TO_BOOK + _cursor = self.db[_collection_name] + _all_contact_list = [] + for document in _cursor.find(): + _all_contact_list.append(ContactPojo.from_firestore_dict(document)) + return _all_contact_list + + def save_links_to_validate(self, link: str, mail_address: str, _all_contact_list: list): collection_to_use = self.db[LINKS_TO_VALIDATE] updated_at = time.strftime("%H:%M:%S", time.localtime()) + _ip_country = "FR" + # find ip_country info + for _contact in _all_contact_list: + if _contact.mail == mail_address: + _ip_country = _contact.ip_country + if len(mail_address) > 0: collection_to_use.replace_one(filter={'_id': mail_address, }, replacement={ u'url': link, u'email': mail_address, + u'ip_country': _ip_country, "updated_at": updated_at }, upsert=True) else: collection_to_use.replace_one(filter={'_id': link, }, replacement={ u'url': link, + u'ip_country': _ip_country, "updated_at": updated_at }, upsert=True) @@ -306,7 +322,9 @@ MONGO_STORE_MANAGER = MongoDbManager() if __name__ == '__main__': db_manager = MongoDbManager() - print(db_manager.get_code_for_email("singleton_teri1991@aol.com")) + for c in db_manager.get_all_contacts_to_book(): + print(c) + # print(db_manager.get_code_for_email("singleton_teri1991@aol.com")) # black_list = db_manager.get_blacklist_contacts() # for contact in black_list: # print(contact) diff --git a/src/mail/mail_reader.py b/src/mail/mail_reader.py index 6c76dec..0e474a6 100755 --- a/src/mail/mail_reader.py +++ b/src/mail/mail_reader.py @@ -255,7 +255,8 @@ def read_mails(): if need_to_check_email(mail.mail, successful_items): mail_reader = MailReader(mail.mail, mail.password) executor.submit(mail_reader.read_emails, mails_messages) - + # get ip_country info + _all_contact_list = MONGO_STORE_MANAGER.get_all_contacts_to_book() with ThreadPoolExecutor(max_workers=10) as executor: for mail in mails_messages: # if mail.isImapClient: @@ -269,7 +270,7 @@ def read_mails(): # else: url = match.group(0) if need_to_valid_url(url, successful_items): - MONGO_STORE_MANAGER.save_links_to_validate(url, mail.to_address) + MONGO_STORE_MANAGER.save_links_to_validate(url, mail.to_address, _all_contact_list) # url_validator = LinkValidator(url) print("need to validate url: " + url) # executor.submit(url_validator.start_page, params.get_proxy(ProxyType.OXYLABS), False) diff --git a/src/pojo/contact_pojo.py b/src/pojo/contact_pojo.py index 80d2963..d3a54be 100644 --- a/src/pojo/contact_pojo.py +++ b/src/pojo/contact_pojo.py @@ -11,15 +11,17 @@ class ContactPojo: first_name: str mail: str note: str + ip_country: str - def __init__(self, phone_number: str, passport_number: str, last_name: str, first_name: str, mail: str): + def __init__(self, phone_number: str, passport_number: str, last_name: str, first_name: str, mail: str, + ip_country="FR"): self.phone = str(phone_number).replace(".0", "") self.passport = passport_number self.last_name = last_name self.first_name = first_name self.mail = mail self.note = "" - self.ip_country = "FR" + self.ip_country = ip_country def to_firestore_dict(self): dest = { @@ -30,7 +32,6 @@ class ContactPojo: u'mail': self.mail, u'ip_country': self.ip_country } - return dest @staticmethod @@ -41,13 +42,15 @@ class ContactPojo: @staticmethod def from_firestore_dict(source): - ccid = source['ccid'] phone = source['phone'] - position = source['position'] passport = source['passport'] email = source['mail'] last_name = source['last_name'] first_name = source['first_name'] + ip_country = "FR" + if source.get('ip_country'): + ip_country = source['ip_country'] result = ContactPojo(phone_number=phone, passport_number=passport, mail=email, last_name=last_name, first_name=first_name) + result.ip_country = ip_country return result