-
Notifications
You must be signed in to change notification settings - Fork 2
/
retrieve_data.py
168 lines (132 loc) · 4.78 KB
/
retrieve_data.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import argparse
import json
import subprocess
import time
from pathlib import Path
from typing import NamedTuple, Optional, Union
import firebase_admin # type: ignore
from firebase_admin import credentials, firestore # type: ignore
class FirestoreEncoder(json.JSONEncoder):
def default(self, obj):
# Check if the object is an instance of Firestore's datetime type
if hasattr(obj, "isoformat"):
return obj.isoformat()
return super(FirestoreEncoder, self).default(obj)
def load_creds(
file: str,
dmg: Optional[Union[Path, str]] = None,
mountpoint: Optional[Union[Path, str]] = None,
):
if dmg is None:
dmg = "~/Documents/api_credentials.sparsebundle"
if mountpoint is None:
mountpoint = "/Volumes/creds"
cred_archive = Path(dmg).expanduser().resolve()
# pathlib thinks that sparsebundles are directories, not files
assert cred_archive.exists(), f"Sparsebundle does not exist at {cred_archive}"
cred_file = Path(mountpoint).expanduser().resolve() / file
assert cred_file.suffix == ".json", f"File {cred_file} does not have .json suffix"
timeout = 10 # seconds
delay = 0.5
if not cred_file.parent.is_dir():
clout = subprocess.run(
["open", str(cred_archive)], encoding="utf-8", check=True
)
if clout.returncode != 0:
print(f"Error opening {cred_archive}\n\n{clout.stdout}\n\n{clout.stderr}")
raise Exception(f"Error opening {cred_archive}")
elapsed = 0
while not cred_file.parent.is_dir():
time.sleep(delay)
elapsed += delay
assert (
elapsed < timeout
), f"Timed out waiting for {cred_file.parent} to be mounted"
elapsed = 0
while not cred_file.is_file():
time.sleep(delay)
elapsed += delay
assert (
elapsed < timeout
), f"Timed out waiting for {cred_file.parent} to be mounted"
assert cred_file.is_file(), f"File {cred_file} does not exist"
with open(cred_file, "r", encoding="utf-8") as f:
contents = json.load(f)
Cred = NamedTuple("Cred", data=dict[str, str], path=Path)
return Cred(contents, cred_file)
def authorize(credpath: str, encrypted=False):
if encrypted:
cred = load_creds(credpath)
cred_path = cred.path
else:
cred_path = Path(credpath).expanduser()
if not cred_path.is_absolute():
cred_path = (Path(__file__).parent / cred_path).resolve()
assert cred_path.is_file(), f"File {cred_path} does not exist"
cred = credentials.Certificate(cred_path)
firebase_admin.initialize_app(cred)
return firestore.client()
def get_data(
credpath: str, exportpath: str, collections: list[str], encrypted: bool = False
):
export_path = Path(exportpath).expanduser()
if not export_path.is_absolute():
export_path = (Path(__file__).parent / export_path).absolute()
if export_path.exists():
raise Exception(
f"Path {export_path} already exists. Aborting to prevent overwriting data."
)
export_path.mkdir(parents=True, exist_ok=True)
db = authorize(credpath, encrypted)
for collection_name in collections:
docs = db.collection(collection_name).stream()
data = {doc.id: doc.to_dict() for doc in docs}
data_path = export_path / f"{collection_name}.json"
with open(data_path, "w", encoding="utf-8") as json_file:
json.dump(data, json_file, cls=FirestoreEncoder, indent=2)
def _cli():
"""Retrieve data from Firestore and save it locally."""
parser = argparse.ArgumentParser(
prog="Data Retriever",
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
# argument_default=argparse.SUPPRESS
# epilog="Text at the bottom of help"
)
parser.add_argument(
"--cred",
dest="credpath",
required=True,
help="Path to Firebase credentials JSON file",
)
parser.add_argument(
"--out", dest="exportpath", required=True, help="Output directory path"
)
parser.add_argument(
"--collection",
dest="collections",
nargs="+",
required=True,
help="Name of the Firestore collections to retrieve",
)
parser.add_argument(
"--encrypted",
action="store_true",
help="Specify if the credentials file is stored in an encrypted sparseimage",
)
args = parser.parse_args()
return vars(args)
def main(
credpath: str,
exportpath: str,
collections: list[str],
encrypted: bool = False,
):
get_data(
credpath=credpath,
exportpath=exportpath,
collections=collections,
encrypted=encrypted,
)
if __name__ == "__main__":
main(**_cli())