Files
hpos-web/scripts/UserImageDownload.py
2023-08-10 16:50:32 +00:00

130 lines
4.7 KiB
Python

import os
import firebase_admin
from firebase_admin import credentials, firestore
import pandas as pd
from urllib.request import urlopen
import datetime
import tkinter as tk
from tkinter import filedialog
# Initialize Firebase Admin SDK
cred = credentials.Certificate(r'C:\Users\smila\PycharmProjects\pythonProject\hpos-prod-firebase-adminsdk-bionp-5486f7becd.json') # Replace with your own service account key path
firebase_admin.initialize_app(cred)
# Get a reference to the Firestore database
db = firestore.client()
# Specify the collections
patient_collection = db.collection("patientData")
test_collection = db.collection("testData")
# Create a tkinter root window (hidden)
root = tk.Tk()
root.withdraw()
# Ask the user to select the base folder where images and data will be saved
base_output_folder = filedialog.askdirectory(title="Select Base Output Folder")
# Get user-defined date ranges
start_date = "2023-08-01"
end_date = "2023-08-10"
# Parse the date range to get a list of days
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)]
# Iterate through each day
for date in date_range:
current_date = date.strftime("%Y-%m-%d")
output_folder = os.path.join(base_output_folder, current_date)
os.makedirs(output_folder, exist_ok=True)
print(f"Processing data for {current_date}...")
# Query Firestore for patient data
query = patient_collection.where("createdAt", ">=", current_date).where("createdAt", "<", (date + datetime.timedelta(days=1)).strftime("%Y-%m-%d"))
patient_docs = query.stream()
# Initialize a list to hold the combined data
data = []
# Iterate through patient documents
for patient_doc in patient_docs:
patient_data = patient_doc.to_dict()
patient_id = patient_data["_id"]
# Query the document from testData collection based on the common _id
test_docs = test_collection.where("_id", "==", patient_id).stream()
# Iterate through test documents
for test_doc in test_docs:
test_data = test_doc.to_dict()
# Combine the data from both collections into a single dictionary
combined_data = {**patient_data, **test_data}
# Access specific attributes from the combined data
csv_path = combined_data.get("csvPath", "")
if csv_path and not csv_path.startswith("http"):
# Perform the desired action
print("CSV path does not start with 'http'.")
# Append the combined_data dictionary to the list
data.append(combined_data)
# Download images separately and store them in the specified folder
user_image_url = combined_data.get("userImageURL", "")
if user_image_url and user_image_url.startswith("http"):
image_data = urlopen(user_image_url).read()
image_filename = f"{patient_id}.jpg" # Use only patient ID as the filename
image_path = os.path.join(output_folder, image_filename)
with open(image_path, "wb") as image_file:
image_file.write(image_data)
# Convert the data to a DataFrame
df = pd.DataFrame(data)
# Rename columns
df.rename(columns={
"name": "Name",
"abhaId": "ABHA ID",
"birthYear": "Age",
"gender": "Gender",
"category": "Category",
"maritalStatus": "Marital Status",
"careOf": "Care-of",
"city": "Address",
"district": "District",
"state": "State",
"pinCode": "Pincode",
"phoneNumber": "Mobile Number",
"bloodGroup": "Blood group",
"result": "Test Result"
}, inplace=True)
# Select specific columns
selected_columns = [
"Name", "ABHA ID", "Age", "Gender", "Category", "Marital Status",
"Care-of", "Address", "District", "State", "Pincode", "Mobile Number",
"Blood group", "Test Result"
]
# Check if selected columns are present in the DataFrame
missing_columns = [col for col in selected_columns if col not in df.columns]
if missing_columns:
print(f"Missing columns: {missing_columns}")
else:
# Create a new DataFrame with selected columns
df = df[selected_columns]
# Save the DataFrame to a CSV file
output_filename = f"data_{current_date}.csv"
output_path = os.path.join(output_folder, output_filename)
df.to_csv(output_path, index=False)
print(f"Data and images for {current_date} saved to '{output_folder}'")
# Close the Firebase Admin SDK
firebase_admin.delete_app(firebase_admin.get_app())