m8chaa commited on
Commit
a8126f9
·
verified ·
1 Parent(s): be8e1ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py CHANGED
@@ -39,6 +39,27 @@ def get_secret(secret_name):
39
  dezgo_api_key = get_secret('dezgo_api_key')
40
  appsScriptsURL = get_secret('appsscript_url')
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  class ImageData(BaseModel):
44
  sheetID: str
@@ -92,6 +113,16 @@ async def post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url):
92
 
93
  async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL):
94
  sheetID = sheeturl.split('/')[-2]
 
 
 
 
 
 
 
 
 
 
95
  image_data = {
96
  'sheetID': sheetID,
97
  'fileName': filename,
@@ -112,6 +143,11 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
112
  await record_failure(s3_orginal_url, 'No valid receipts found.', sheetID)
113
  return {"message": "No valid receipts found."}
114
  else:
 
 
 
 
 
115
  for i, base64_str in enumerate(cut_images, start=1):
116
  image_bytes = base64.b64decode(base64_str)
117
  object_name = f"{object_name}_processed_{i}.png"
 
39
  dezgo_api_key = get_secret('dezgo_api_key')
40
  appsScriptsURL = get_secret('appsscript_url')
41
 
42
+ firebase_config = {
43
+ "type": "service_account",
44
+ "project_id": "resheet-k6x6vh",
45
+ "private_key_id": get_secret('fb_priv_key_id'),
46
+ "private_key": get_secret('fb_priv_key'),
47
+ "client_email": get_secret('client_email'),
48
+ "client_id": get_secret('client_id'),
49
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
50
+ "token_uri": "https://oauth2.googleapis.com/token",
51
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
52
+ "client_x509_cert_url": get_secret('client_x509_cert_url'),
53
+ "universe_domain": "googleapis.com"
54
+ }
55
+
56
+ cred = credentials.Certificate(firebase_config)
57
+ firebase_admin.initialize_app(cred, {
58
+ 'projectId': firebase_config['projectId'],
59
+ })
60
+
61
+ db = firestore.client()
62
+ users_ref = db.collection('users')
63
 
64
  class ImageData(BaseModel):
65
  sheetID: str
 
113
 
114
  async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL):
115
  sheetID = sheeturl.split('/')[-2]
116
+
117
+ user_doc = users_ref.document(sheetID)
118
+ # get the 'total_uploaded' field from the user's document
119
+ user_data = user_doc.get().to_dict()
120
+ if user_data is None:
121
+ await record_failure(s3_orginal_url, 'User not found.', sheetID)
122
+ return {"message": "User not found."}
123
+ total_uploaded = user_data.get('total_uploaded', 0)
124
+
125
+
126
  image_data = {
127
  'sheetID': sheetID,
128
  'fileName': filename,
 
143
  await record_failure(s3_orginal_url, 'No valid receipts found.', sheetID)
144
  return {"message": "No valid receipts found."}
145
  else:
146
+ # get the number of total cut photos
147
+ receipt_count = cut_images.__len__()
148
+ # set the user's total_uploaded field to the current total_uploaded + receipt_count
149
+ user_doc.update({'total_uploaded': total_uploaded + receipt_count})
150
+
151
  for i, base64_str in enumerate(cut_images, start=1):
152
  image_bytes = base64.b64decode(base64_str)
153
  object_name = f"{object_name}_processed_{i}.png"