def fetch_data(cid: int, my_cursor: Cursor, mongo_db: Database): ret = {} # customer my_cursor.execute("select * from channel_customer where id = %s", cid) customer = my_cursor.fetchone() if not customer: return None name = customer['company'] my_cursor.execute( "select * from channel_contact where customer_id = %s limit 1", cid) # email contact = my_cursor.fetchone() buyer_in_mongo = None if contact: email = contact['email'] else: email = None if not email: collection = mongo_db[BUYERS_COLLECTION] buyer_in_mongo = collection.find_one({'full_name': name}) email = fetch_email_from_mongo(mongo_db, buyer_in_mongo) if not email: return None # product my_cursor.execute( "select product_content from channel_product where customer_id = %s", cid) db_products = my_cursor.fetchall() if db_products: products = [x['product_content'] for x in db_products] else: if not buyer_in_mongo: collection = mongo_db[BUYERS_COLLECTION] buyer_in_mongo = collection.find_one({'full_name': name}) if buyer_in_mongo: products = fetch_products_from_mongo(mongo_db, buyer_in_mongo) else: products = None ret['customer_id'] = customer['id'] ret['country'] = customer['country'] ret['name'] = name ret['industry'] = customer['industry'] ret['email'] = email ret['flag6c_grade'] = customer['flag6c_grade'] if products: products = list(set(products)) elif products is None: products = [] ret['products'] = products ret['today_max'] = 0 ret['life_max'] = 0 ret['replied'] = 0 ret['unsubscribe'] = 0 ret['email_valid'] = 0 return ret
def get_cached_entities(url: str, cursor: Cursor = cursor) -> Set[str]: hash = hashlib.sha256() hash.update(url.encode("utf-8")) hash_result = hash.hexdigest() get_sql = """SELECT hash, entities FROM opendigitalworld.article WHERE hash='{}'""".format( hash_result) cursor.execute(get_sql) result = cursor.fetchone() if result is not None: entities = result[1] return set(entities.split("|"))