64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
import os
|
|
import time
|
|
|
|
import firebase_admin
|
|
from firebase_admin import credentials, firestore
|
|
import pandas as pd
|
|
from urllib.request import urlopen
|
|
import datetime
|
|
|
|
cred = credentials.Certificate(os.getcwd() + '/' + 'keys/hpos-prod-firebase-adminsdk.json')
|
|
firebase_admin.initialize_app(cred)
|
|
|
|
db = firestore.client()
|
|
|
|
patient_collection = db.collection("patientData")
|
|
test_collection = db.collection("testData")
|
|
|
|
base_output_folder = os.getcwd() + '/data/image_download'
|
|
|
|
start_date = "2023-10-31"
|
|
end_date = "2023-11-02"
|
|
|
|
start_datetime = datetime.datetime.strptime(start_date, "%Y-%m-%d")
|
|
end_datetime = datetime.datetime.strptime(end_date, "%Y-%m-%d")
|
|
date_range = [start_datetime + datetime.timedelta(days=x) for x in range((end_datetime - start_datetime).days + 1)]
|
|
|
|
for date in date_range:
|
|
current_date = date.strftime("%Y-%m-%d")
|
|
formatted_date = date.strftime("%d%b%Y")
|
|
output_folder = os.path.join(base_output_folder, formatted_date)
|
|
os.makedirs(output_folder, exist_ok=True)
|
|
|
|
os.makedirs(output_folder, exist_ok=True)
|
|
|
|
print(f"Processing data for {current_date}...")
|
|
|
|
query = patient_collection.where("createdAt", ">=", current_date).where("createdAt", "<", (
|
|
date + datetime.timedelta(days=1)).strftime("%Y-%m-%d"))
|
|
patient_docs = query.stream()
|
|
|
|
data = []
|
|
for patient_doc in patient_docs:
|
|
patient_data = patient_doc.to_dict()
|
|
patient_id = patient_data["_id"]
|
|
|
|
user_image_url = patient_data.get("userImageURL", "")
|
|
image_filename = f"{patient_id}.jpg"
|
|
image_path = os.path.join(output_folder, image_filename)
|
|
if os.path.exists(image_path):
|
|
continue
|
|
test_docs = test_collection.where("_id", "==", patient_id)
|
|
|
|
if test_docs.count().get()[0][0].value > 0:
|
|
if user_image_url and user_image_url.startswith("http") and not os.path.exists(image_path):
|
|
try:
|
|
image_data = urlopen(user_image_url).read()
|
|
with open(image_path, "wb") as image_file:
|
|
image_file.write(image_data)
|
|
|
|
except Exception as e:
|
|
print(f"An error occurred while downloading image: {str(e)}")
|
|
|
|
firebase_admin.delete_app(firebase_admin.get_app())
|
|
|