File size: 2,018 Bytes
816058d 1fea92e 816058d 58f8884 816058d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import firebase_admin
from firebase_admin import credentials, firestore
import pymongo
from datetime import datetime
import os
# Initialize the Firebase app
cred = credentials.Certificate('serviceAccountKey.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
# Connect to MongoDB
db_user = os.environ['DB_USER']
db_pass = os.environ['DB_PASS']
db_uri = f"mongodb+srv://{db_user}:{db_pass}@cluster0.firly.gcp.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
mongo_client = pymongo.MongoClient(db_uri)
mongo_db = mongo_client["chat_support"]
mongo_collection = mongo_db["faq"]
# Fetch the last sync timestamp from MongoDB
# last_sync_doc = mongo_db["log"].find_one({"key": "last_sync_faq_from_firebase"})
# if last_sync_doc:
# last_sync_timestamp = last_sync_doc["value"]["$numberLong"]
# else:
# last_sync_timestamp = 0
last_sync_doc = mongo_db["log"].find_one({"key": "last_sync_faq_from_firebase"})
if last_sync_doc:
last_sync_timestamp = int(last_sync_doc["value"])
else:
last_sync_timestamp = 0
# Fetch data from Firebase Firestore
firestore_collection = db.collection("quick-reply")
docs = firestore_collection.where("timeModified", ">", last_sync_timestamp).stream()
print(f'lastSync: {last_sync_timestamp} | docs: {type(docs)}')
# # Insert/Update data in MongoDB
for doc in docs:
doc_dict = doc.to_dict()
doc_dict["_id"] = doc.id
doc_dict["title"] = doc.get("shortcut")
print(doc.id, doc.get("shortcut"))
mongo_collection.replace_one({"_id": doc.id}, doc_dict, upsert=True)
# Update the last sync timestamp in MongoDB
current_timestamp = datetime.now().timestamp() * 1000
# mongo_db["log"].replace_one(
# {"key": "last_sync_faq_from_firebase"},
# {"key": "last_sync_faq_from_firebase", "value": {"$numberLong": str(int(current_timestamp))}},
# upsert=True
# )
mongo_db["log"].replace_one(
{"key": "last_sync_faq_from_firebase"},
{"key": "last_sync_faq_from_firebase", "value": current_timestamp},
upsert=True
) |