import datetime import firebase_admin import xlsxwriter as xlsxwriter from firebase_admin import credentials, firestore import params from pojo.ReserveResultPojo import ReserveResultPojo, PublishType from pojo.SimInfoPojo import SimInfoPojo from pojo.contact_pojo import ContactPojo from utils.excel_reader import ExcelHelper ERROR_COLLECTION_NAME = "error_items" CONTACT_COLLECTION_NAME = "contact_list" SIM_INFOS = "sim_infos" TIMEOUT = "timeout_items" class DataManager: batch_size = 20 def __init__(self): cred = credentials.Certificate("appointment.json") self._app = firebase_admin.initialize_app(cred) self._db = firestore.client() contact_collection = self._db.collection(CONTACT_COLLECTION_NAME) self._contact_list = [] for contact in contact_collection.stream(): contact_pojo = ContactPojo.from_firestore_dict(contact.to_dict()) self._contact_list.append(contact_pojo) def get_all_error_items(self): pass def get_all_sim_infos(self): sim_info_collection = self._db.collection(SIM_INFOS) return sim_info_collection def get_all_successful_items(self): doc_ref = self._db.collection(str(datetime.date.today())) return doc_ref def save_sim_info(self, simInfoPojo: SimInfoPojo): doc_ref = self._db.collection(SIM_INFOS).document(simInfoPojo.phone) doc_ref.set(simInfoPojo.to_firestore_dict()) contact_found = [contact for contact in self._contact_list if contact.position == simInfoPojo.position] if len(contact_found) > 0: # link the phone number with contact contact = contact_found[0] phone = simInfoPojo.phone[2:len(simInfoPojo.phone)] ccid = simInfoPojo.ccid self._db.collection(CONTACT_COLLECTION_NAME).document(contact.passport).update( {'phone': phone, 'ccid': ccid}) else: print("error, contact not found") def save(self, result: ReserveResultPojo): if result.type == PublishType.SUCCESS: # get id id = result.url.split("/")[-1] result.id = id document_name = str(datetime.date.today()) doc_ref = self._db.collection(document_name).document(result.id) doc_ref.set(result.to_firestore_dict()) else: if len(result.phone) > 0: doc_ref = self._db.collection(ERROR_COLLECTION_NAME).document(result.phone) doc_ref.set(result.to_firestore_dict()) def save_timeout_contact(self, contact: ContactPojo): doc_ref = self._db.collection(TIMEOUT).document(str(contact.phone)) doc_ref.set(contact.to_firestore_dict()) def _delete_collection(self, coll_ref, batch_size): docs = coll_ref.limit(batch_size).stream() deleted = 0 for doc in docs: print(f'Deleting doc {doc.id} => {doc.to_dict()}') doc.reference.delete() deleted = deleted + 1 if deleted >= batch_size: return self._delete_collection(coll_ref, batch_size) def clear_all_sim_info(self): coll_ref = self._db.collection(SIM_INFOS) self._delete_collection(coll_ref, self.batch_size) def save_to_excel(self): # Start from the first cell. Rows and columns are zero indexed. sim_info_list = [] for sim in self.get_all_sim_infos().stream(): print(sim) sim_pojo = SimInfoPojo.from_firestore_dict(sim.to_dict()) sim_info_list.append(sim_pojo) row = 0 col = 0 # Create a workbook and add a worksheet. workbook = xlsxwriter.Workbook('sim_infos.xlsx') worksheet = workbook.add_worksheet() for info in sim_info_list: # Iterate over the data and write it out row by row. worksheet.write(row, col, info.phone[2:len(info.phone)]) worksheet.write(row, col + 1, info.ccid) worksheet.write(row, col + 2, info.position) row += 1 workbook.close() def upload_contact_list_to_cloud(self): excel_reader = ExcelHelper() contacts = excel_reader.read_contacts() collections = self._db.collection(CONTACT_COLLECTION_NAME) for contact in contacts: new_contact = collections.document(contact.passport) new_contact.set(contact.to_firestore_dict()) def read_contacts_from_db(self) -> list: contact_collection = self._db.collection(CONTACT_COLLECTION_NAME) return contact_collection if __name__ == '__main__': params.firebase_store_manager.upload_contact_list_to_cloud() # params.firebase_store_manager.save_to_excel() # params.firebase_store_manager.clear_all_sim_info()