m8chaa commited on
Commit
25a911e
·
verified ·
1 Parent(s): f0e73a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +425 -557
app.py CHANGED
@@ -66,10 +66,9 @@ firebase_admin.initialize_app(firebase_credentials, {
66
  })
67
  firestore_db = firestore.client()
68
  users_collection_ref = firestore_db.collection('users')
69
-
70
  transactions_collection_ref = firestore_db.collection('transactions')
71
 
72
- SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/datastore']
73
 
74
  with open('/app/secrets/openai_key.txt', 'r') as f:
75
  OPENAI_KEY = f.read()
@@ -80,6 +79,29 @@ APICredential = service_account.Credentials.from_service_account_file(
80
  sheet_service = build('sheets', 'v4', credentials=APICredential)
81
  drive_service = build('drive', 'v3', credentials=APICredential)
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  # first three are "등록일시", "썸네일", "바로가기",
84
  receipt_sheet_headers = ['발행일', '상호', '사업자번호', '합계금액', '내역', '카드번호', '카드종류', '승인번호', '기타']
85
  # at index 1 '등록 일시', at last index '명함보기'
@@ -126,71 +148,168 @@ def convert_dicts_to_list(dicts: List[Dict], headers: List[str], add_link: bool
126
  rows.append(row)
127
  # print(rows)
128
  return rows
129
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  async def request_gpt4o_completion(image_id: str, user_credit: int):
131
- openai_url = "https://api.openai.com/v1/chat/completions"
132
- headers = {"Authorization": f"Bearer {OPENAI_KEY}", "Content-Type": "application/json"}
133
- image_url = f"https://drive.usercontent.google.com/u/0/uc?id={image_id}&export=download"
134
- body = {
135
- "model": "gpt-4o-mini",
136
- "response_format": { "type": "json_object" },
137
- "messages": [
138
- {
139
- "role": "user",
140
- "content": [
141
- {
142
- "type": "text",
143
- "text": f'''
144
- 아래의 순서대로 실행해주세요:
145
- 1. {image_url} 이 링크에서 파일 다운로드가 완료될 때까지 기다리세요.
146
- 2. 이미지의 모든 영수증과 명함을 식별하고, 각각의 정보를 추출하세요.
147
- 3. 작성할 때 영수증과 명함 개수의 합이 {user_credit}을 넘지 않도록 하세요.
148
- 4. 추출한 정보를 {receipt_sheet_headers}란이 있는 영수증 시트 또는 {business_card_sheet_headers}란이 있는 명함 시트에 넣을 수 있도록 JSON으로 정리하세요.
149
- json.receipts.forEach() 또는 json.busi_cards.forEach()
150
- (item) => insert row where header field is item.[fieldName]
151
- '''
152
- },
153
- {
154
- "type": "image_url",
155
- "image_url": {
156
- "url": image_url,
157
- "detail": "high"
158
- }
159
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  ]
161
- }
162
- ],
163
- "max_tokens": 4096
164
- }
165
 
166
- logging.info(f"Request URL: {openai_url}")
167
- logging.info(f"Request Headers: {headers}")
168
- logging.info(f"Request Body: {json.dumps(body, indent=2)}")
 
 
 
 
 
 
169
 
170
- try:
171
- async with httpx.AsyncClient(follow_redirects=True, timeout=300.0) as client:
172
- response = await client.post(openai_url, headers=headers, json=body)
173
- response.raise_for_status() # Raise an error for bad status codes
174
- except httpx.RequestError as exc:
175
- logging.error(f"An error occurred while requesting {exc.request.url!r}.")
176
- logging.error(f"Exception: {str(exc)}")
177
- return None
178
- except httpx.HTTPStatusError as exc:
179
- logging.error(f"HTTP error occurred: {str(exc)}")
180
- logging.error(f"Response: {exc.response.text}")
181
- return None
182
 
183
- logging.info(f"Response Status Code: {response.status_code}")
184
- logging.info(f"Response Body: {response.text}")
185
-
186
- completion = response.json()
187
- if 'choices' in completion and len(completion['choices']) > 0:
188
- message_content = completion['choices'][0]['message']['content']
189
- print(message_content)
190
- return message_content
191
- else:
192
- print("No response received")
193
- return None
194
 
195
  async def move_file_to_folder(file_id, current_parents, new_parents):
196
  try:
@@ -228,98 +347,122 @@ async def move_file_to_folder(file_id, current_parents, new_parents):
228
  except Exception as e:
229
  logging.error(f"Failed to move file {file_id} to new folders: {e}")
230
 
231
-
232
- # Update the update_user_sheets_and_folders function for better error handling
233
- async def update_user_sheets_and_folders(user_data, transaction_ref, transaction_data, image_id):
234
- try:
235
- parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
236
-
237
- # Request GPT-4 completion
238
- dataJson = await request_gpt4o_completion(image_id, transaction_data['purchased_credit']['image_detection'])
239
- if dataJson is None:
240
- raise Exception("Failed to process the image with GPT-4")
241
-
242
- data = json.loads(dataJson)
243
-
244
- # Process receipts and business cards
245
- found_receipt_no = len(data.get('receipts', []))
246
- found_business_cards_no = len(data.get('busi_cards', []))
247
-
248
- new_folder = None
249
- secondary_folder = None
250
- timestamp = datetime.now(kst).strftime("%Y-%m-%d %H:%M:%S")
251
-
252
- # Process receipts
253
- if found_receipt_no > 0:
254
- new_folder = user_data['gDrive_metadata']['receipts']
255
- receipts_data = convert_dicts_to_list(
256
- data['receipts'],
257
- receipt_sheet_headers,
258
- add_link=True,
259
- image_id=image_id,
260
- link_text="바로가기",
261
- link_position=1
262
- )
263
- try:
264
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
265
- except HttpError as e:
266
- logging.error(f"Failed to update receipt sheet: {str(e)}")
267
- raise Exception("Failed to update receipt data in spreadsheet")
268
-
269
- # Process business cards
270
- if found_business_cards_no > 0:
271
- if new_folder is None:
272
- new_folder = user_data['gDrive_metadata']['business_cards']
273
- else:
274
- secondary_folder = user_data['gDrive_metadata']['business_cards']
275
- business_cards_data = convert_dicts_to_list(
276
- data['busi_cards'],
277
- business_card_sheet_headers,
278
- add_link=True,
279
- image_id=image_id,
280
- link_text="명함보기",
281
- link_position=-1
282
- )
283
- try:
284
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "명함", business_cards_data)
285
- except HttpError as e:
286
- logging.error(f"Failed to update business card sheet: {str(e)}")
287
- raise Exception("Failed to update business card data in spreadsheet")
288
-
289
- # Update Firebase transaction
290
- status = await update_fb_transaction(
291
- transaction_ref,
292
- no_receipts=found_receipt_no,
293
- no_business_cards=found_business_cards_no
294
- )
295
 
296
- if status['status'] != "Success":
297
- logging.error(f"Transaction update failed with status: {status['status']}")
298
- raise Exception("Failed to update transaction in Firebase")
 
 
 
 
 
 
 
 
 
 
 
 
299
 
300
- # Move file to appropriate folder
301
- if new_folder:
302
- try:
303
- await move_file_to_folder(image_id, parent_folders, [new_folder])
304
- if secondary_folder:
305
- await create_shortcut(image_id, secondary_folder, f"Shortcut to {image_id}")
306
- except HttpError as e:
307
- logging.error(f"Failed to move file or create shortcut: {str(e)}")
308
- raise Exception("Failed to organize processed image in Google Drive")
 
 
 
 
 
 
 
309
 
310
- logging.info(f"Successfully processed image {image_id} for user {user_data['uid']}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
 
312
- except Exception as e:
313
- logging.error(f"Error processing image {image_id} for user {user_data['uid']}: {str(e)}")
314
- try:
315
- # Move the image file to the error folder
316
- await move_file_to_folder(image_id, parent_folders, [user_data['gDrive_metadata']['error']])
317
- logging.info(f"Moved image {image_id} to error folder for user {user_data['uid']}")
318
- except Exception as move_error:
319
- logging.error(f"Failed to move image {image_id} to error folder: {str(move_error)}")
320
-
321
- # You might want to update the transaction or user document to indicate the error
322
- # This depends on your error handling strategy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
 
324
  async def copy_sms_sheet(phone_no: str, spreadsheet_id: str):
325
  requests = [
@@ -403,13 +546,7 @@ template_sheet_id = '1i5mrmlTs5sPWtx2mBtc_f-zm1D3x21r1T_77guki8_8'
403
  async def edit_spreadsheet(target_sheet_id: str = Form(...)):
404
  try:
405
  # Retrieve the target spreadsheet's metadata
406
- try:
407
- target_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=target_sheet_id).execute()
408
- except HttpError as e:
409
- if e.resp.status == 404:
410
- raise HTTPException(status_code=404, detail="Target spreadsheet not found")
411
- else:
412
- raise HTTPException(status_code=e.resp.status, detail=f"Error accessing target spreadsheet: {e.error_details}")
413
 
414
  # Collect all sheet IDs in the target spreadsheet
415
  target_sheet_ids = [sheet['properties']['sheetId'] for sheet in target_spreadsheet.get('sheets', [])]
@@ -424,76 +561,59 @@ async def edit_spreadsheet(target_sheet_id: str = Form(...)):
424
  })
425
 
426
  # Retrieve the template spreadsheet's metadata
427
- try:
428
- template_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=template_sheet_id).execute()
429
- except HttpError as e:
430
- if e.resp.status == 404:
431
- raise HTTPException(status_code=404, detail="Template spreadsheet not found")
432
- else:
433
- raise HTTPException(status_code=e.resp.status, detail=f"Error accessing template spreadsheet: {e.error_details}")
434
 
435
  # Iterate through each sheet in the template spreadsheet
436
  for sheet in template_spreadsheet.get('sheets', []):
437
  sheet_id = sheet['properties']['sheetId']
438
 
439
- try:
440
- # Use the copyTo method to copy the sheet to the target spreadsheet
441
- copy_request = sheet_service.spreadsheets().sheets().copyTo(
442
- spreadsheetId=template_sheet_id,
443
- sheetId=sheet_id,
444
- body={"destinationSpreadsheetId": target_sheet_id}
445
- )
446
- copy_response = copy_request.execute()
447
-
448
- # Get the copied sheet ID
449
- copied_sheet_id = copy_response['sheetId']
450
-
451
- # Rename the copied sheet to remove "Copy of"
452
- copied_sheet_title = sheet['properties']['title'].replace("Copy of ", "")
453
- rename_request = {
454
- "updateSheetProperties": {
455
- "properties": {
456
- "sheetId": copied_sheet_id,
457
- "title": copied_sheet_title
458
- },
459
- "fields": "title"
460
- }
461
  }
 
462
 
463
- # Execute rename request
464
- sheet_service.spreadsheets().batchUpdate(
465
- spreadsheetId=target_sheet_id,
466
- body={"requests": [rename_request]}
467
- ).execute()
468
 
469
- logging.info(f"Sheet {sheet['properties']['title']} copied and renamed to {copied_sheet_title} in {target_sheet_id}.")
470
- except HttpError as e:
471
- logging.error(f"Error copying or renaming sheet {sheet['properties']['title']}: {e}")
472
- raise HTTPException(status_code=e.resp.status, detail=f"Error copying or renaming sheet: {e.error_details}")
473
 
474
  if requests:
475
- try:
476
- # Execute batch update to delete sheets
477
- batch_update_request = {"requests": requests}
478
- sheet_service.spreadsheets().batchUpdate(
479
- spreadsheetId=target_sheet_id,
480
- body=batch_update_request
481
- ).execute()
482
- logging.info(f"Deleted existing sheets in target spreadsheet {target_sheet_id}.")
483
- except HttpError as e:
484
- logging.error(f"Error deleting existing sheets: {e}")
485
- raise HTTPException(status_code=e.resp.status, detail=f"Error deleting existing sheets: {e.error_details}")
486
 
487
  logging.info(f"All sheets from template {template_sheet_id} copied to {target_sheet_id}.")
488
  return {"message": "Spreadsheet copied successfully"}
489
 
490
- except HTTPException as he:
491
- # Re-raise HTTP exceptions
492
- raise he
493
  except Exception as e:
494
- logging.error(f"Unexpected error in edit_spreadsheet: {e}")
495
- raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
496
-
497
  async def create_shortcut(file_id, parent_folder_id, name):
498
  try:
499
  file_metadata = {
@@ -542,7 +662,7 @@ async def update_user_sheets_and_folders(user_data, transaction_ref, transaction
542
  link_position=1 # Link third
543
  )
544
  print(receipts_data)
545
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
546
 
547
  if found_business_cards_no > 0:
548
  if new_folder is None:
@@ -557,7 +677,7 @@ async def update_user_sheets_and_folders(user_data, transaction_ref, transaction
557
  link_text="명함보기",
558
  link_position=-1 # Link at the end
559
  )
560
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "명함", business_cards_data)
561
 
562
  print(f"{found_receipt_no}, {found_business_cards_no}")
563
 
@@ -584,325 +704,117 @@ async def update_user_sheets_and_folders(user_data, transaction_ref, transaction
584
 
585
  @app.post("/process-image")
586
  async def process_photo(background_tasks: BackgroundTasks, image_id: str = Form(...), user_id: str = Form(...)):
587
- try:
588
- # Validate input
589
- if not image_id or not user_id:
590
- raise HTTPException(status_code=400, detail="Both image_id and user_id are required")
591
-
592
- # Get transaction data
593
- transaction_ref = transactions_collection_ref.document(user_id)
 
 
 
 
 
 
 
594
  transaction = transaction_ref.get()
595
-
596
- if not transaction.exists:
597
- # Initialize new transaction if it doesn't exist
598
- try:
599
- transaction_ref.set({
600
- "no_sms": 0,
601
- "no_contacts": 0,
602
- "no_receipts": 0,
603
- "no_business_cards": 0,
604
- "purchased_credit": {
605
- "image_detection": 100,
606
- "sync_data": 500,
607
- }
608
- })
609
- transaction = transaction_ref.get()
610
- except Exception as e:
611
- logging.error(f"Failed to initialize new transaction for user {user_id}: {str(e)}")
612
- raise HTTPException(status_code=500, detail="Failed to initialize user transaction")
613
-
614
- transaction_data = transaction.to_dict()
615
-
616
- # Check credit limit
617
- if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
618
- raise HTTPException(status_code=403, detail="You have reached the limit of the number of items you can process. Please upgrade your plan.")
619
-
620
- # Get user data
621
- user_ref = users_collection_ref.document(user_id)
622
- user = user_ref.get()
623
- if not user.exists:
624
- raise HTTPException(status_code=404, detail="User not found")
625
- user_data = user.to_dict()
626
-
627
- # Validate user data
628
- if 'gDrive_metadata' not in user_data:
629
- raise HTTPException(status_code=400, detail="User Google Drive metadata not found")
630
-
631
- # Add task to background
632
- try:
633
- background_tasks.add_task(update_user_sheets_and_folders, user_data, transaction_ref, transaction_data, image_id)
634
- except Exception as e:
635
- logging.error(f"Failed to add background task for user {user_id}, image {image_id}: {str(e)}")
636
- raise HTTPException(status_code=500, detail="Failed to start image processing")
637
-
638
- return {"success": True, "message": "Image processing started"}
639
-
640
- except HTTPException as he:
641
- # Re-raise HTTP exceptions
642
- raise he
643
- except Exception as e:
644
- logging.error(f"Unexpected error in process_photo for user {user_id}, image {image_id}: {str(e)}")
645
- raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
646
-
647
-
648
- async def update_user_sheet(spreadsheet_id: str, sheet_id: int, sheet_name: str, data: List[List[str]], is_reset: Optional[bool] = False):
649
- try:
650
- logging.info(f"Updating sheet '{sheet_name}' in spreadsheet {spreadsheet_id}")
651
-
652
- if not spreadsheet_id or not sheet_id or not sheet_name or not data:
653
- raise ValueError("Missing required parameters for updating user sheet")
654
-
655
- no_of_rows = len(data)
656
- no_of_cols = len(data[0]) if data else 0
657
-
658
- if is_reset:
659
- sheet_name = f"{sheet_name} {datetime.now(kst).strftime('%Y-%m-%d %H:%M')}"
660
- try:
661
- duplicate_requests = [{
662
- "duplicateSheet": {
663
- "sourceSheetId": contacts_ss,
664
- "newSheetName": sheet_name
665
- }
666
- }]
667
- duplicate_response = sheet_service.spreadsheets().batchUpdate(
668
- spreadsheetId=spreadsheet_id,
669
- body={
670
- 'requests': duplicate_requests
671
- }
672
- ).execute()
673
- sheet_id = duplicate_response['replies'][0]['duplicateSheet']['properties']['sheetId']
674
- logging.info(f"Created new sheet '{sheet_name}' with ID {sheet_id}")
675
- except HttpError as e:
676
- logging.error(f"Error duplicating sheet: {e}")
677
- raise HTTPException(status_code=500, detail=f"Failed to create new sheet: {str(e)}")
678
-
679
- requests = [
680
- {
681
- "updateSheetProperties": {
682
- "properties": {
683
- "sheetId": sheet_id,
684
- "hidden": False
685
- },
686
- "fields": "hidden"
687
- }
688
- },
689
- {
690
- "insertDimension": {
691
- "range": {
692
- "sheetId": sheet_id,
693
- "dimension": "ROWS",
694
- "startIndex": 1,
695
- "endIndex": no_of_rows + 1
696
- },
697
- "inheritFromBefore": False,
698
- }
699
  }
700
- ]
701
-
702
- try:
703
- sheet_service.spreadsheets().batchUpdate(
704
- spreadsheetId=spreadsheet_id,
705
- body={
706
- 'requests': requests
707
- }
708
- ).execute()
709
- logging.info(f"Inserted {no_of_rows} rows in sheet '{sheet_name}'")
710
- except HttpError as e:
711
- logging.error(f"Error updating sheet properties or inserting rows: {e}")
712
- raise HTTPException(status_code=500, detail=f"Failed to prepare sheet for data update: {str(e)}")
713
-
714
- update_body = {
715
- 'values': data
716
- }
717
- start_column = 'A'
718
- end_column = chr(ord(start_column) + no_of_cols - 1)
719
- range_to_update = f'{sheet_name}!{start_column}2:{end_column}{no_of_rows + 1}'
720
-
721
- try:
722
- result = sheet_service.spreadsheets().values().update(
723
- spreadsheetId=spreadsheet_id,
724
- range=range_to_update,
725
- valueInputOption='USER_ENTERED',
726
- body=update_body
727
- ).execute()
728
- logging.info(f"Updated {result.get('updatedCells')} cells in sheet '{sheet_name}'")
729
- except HttpError as e:
730
- logging.error(f"Error updating cell values: {e}")
731
- raise HTTPException(status_code=500, detail=f"Failed to update sheet data: {str(e)}")
732
-
733
- # Format the cells
734
- for row_index, row in enumerate(data):
735
- for col_index, cell_value in enumerate(row):
736
- if cell_value.startswith('=HYPERLINK'):
737
- color = {"red": 0, "green": 0, "blue": 1} # Blue for hyperlinks
738
- else:
739
- color = {"red": 0, "green": 0, "blue": 0} # Black for other text
740
-
741
- requests = [
742
- {
743
- "repeatCell": {
744
- "range": {
745
- "sheetId": sheet_id,
746
- "startRowIndex": row_index + 1,
747
- "endRowIndex": row_index + 2,
748
- "startColumnIndex": col_index,
749
- "endColumnIndex": col_index + 1
750
- },
751
- "cell": {
752
- "userEnteredFormat": {
753
- "textFormat": {
754
- "foregroundColor": color
755
- }
756
- }
757
- },
758
- "fields": "userEnteredFormat.textFormat.foregroundColor"
759
- }
760
- }
761
- ]
762
-
763
- try:
764
- sheet_service.spreadsheets().batchUpdate(
765
- spreadsheetId=spreadsheet_id,
766
- body={
767
- 'requests': requests
768
- }
769
- ).execute()
770
- except HttpError as e:
771
- logging.error(f"Error formatting cells: {e}")
772
- # We'll continue even if formatting fails, as it's not critical
773
- logging.warning("Cell formatting failed, but data was updated successfully")
774
-
775
- logging.info(f"Successfully updated and formatted sheet '{sheet_name}'")
776
- return True
777
-
778
- except ValueError as ve:
779
- logging.error(f"Value error in update_user_sheet: {ve}")
780
- raise HTTPException(status_code=400, detail=str(ve))
781
- except HttpError as he:
782
- logging.error(f"Google Sheets API error in update_user_sheet: {he}")
783
- raise HTTPException(status_code=500, detail=f"Google Sheets API error: {str(he)}")
784
- except Exception as e:
785
- logging.error(f"Unexpected error in update_user_sheet: {e}")
786
- raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
787
 
788
- @app.post("/sync-data")
789
- async def sync_data(data_json: str = Form(...), user_id: str = Form(...)):
790
- try:
791
- # Validate input
792
- if not data_json or not user_id:
793
- raise HTTPException(status_code=400, detail="Both data_json and user_id are required")
794
-
795
- # Parse JSON data
796
- try:
797
- data: Dict[str, Any] = json.loads(data_json)
798
- except json.JSONDecodeError as e:
799
- logging.error(f"Invalid JSON data for user {user_id}: {str(e)}")
800
- raise HTTPException(status_code=400, detail="Invalid JSON data")
801
-
802
- # Get transaction data
803
- transaction_ref = transactions_collection_ref.document(user_id)
804
- try:
805
- transaction = transaction_ref.get()
806
- if not transaction.exists:
807
- transaction_ref.set({
808
- "no_sms": 0,
809
- "no_contacts": 0,
810
- "no_receipts": 0,
811
- "no_business_cards": 0,
812
- "purchased_credit": {
813
- "image_detection": 100,
814
- "sync_data": 500,
815
- }
816
- })
817
- transaction = transaction_ref.get()
818
- except GoogleAPICallError as e:
819
- logging.error(f"Firebase error for user {user_id}: {str(e)}")
820
- raise HTTPException(status_code=500, detail="Error accessing user transaction data")
821
 
822
- transaction_data = transaction.to_dict()
 
823
 
824
- # Check credit limit
825
- if transaction_data['no_contacts'] + transaction_data['no_sms'] >= transaction_data['purchased_credit']['sync_data']:
826
- raise HTTPException(status_code=403, detail="You have reached the limit of the number of items you can process. Please upgrade your plan.")
827
 
828
- # Get user data
829
- user_ref = users_collection_ref.document(user_id)
830
- try:
831
- user = user_ref.get()
832
- if not user.exists:
833
- raise HTTPException(status_code=404, detail="User not found")
834
- user_data = user.to_dict()
835
- except GoogleAPICallError as e:
836
- logging.error(f"Firebase error fetching user data for user {user_id}: {str(e)}")
837
- raise HTTPException(status_code=500, detail="Error accessing user data")
838
-
839
- # Process contacts
840
- if data.get('isContacts', False):
841
- try:
842
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], contacts_ss, "연락처", data['data'], True)
843
- new_contacts_count = len(data['data'])
844
- transaction_data['no_contacts'] += new_contacts_count
845
- transaction_ref.update({'no_contacts': transaction_data['no_contacts']})
846
- logging.info(f"Updated {new_contacts_count} contacts for user {user_id}")
847
- except HttpError as e:
848
- logging.error(f"Google Sheets API error updating contacts for user {user_id}: {str(e)}")
849
- raise HTTPException(status_code=500, detail="Error updating contact data")
850
- except KeyError as e:
851
- logging.error(f"Missing key in user data for user {user_id}: {str(e)}")
852
- raise HTTPException(status_code=500, detail="Error accessing user Google Drive data")
853
-
854
- # Process SMS
855
  else:
856
  if 'sms_ids' not in user_data:
 
857
  user_data['sms_ids'] = {}
858
 
859
  existing_phones = set(user_data.get('sms_ids', {}).keys())
860
  new_phones = [phone['address'] for phone in data['data'] if phone['address'] not in existing_phones]
861
-
 
 
862
  for phone_address in new_phones:
863
- try:
864
- new_sheet_id = await copy_sms_sheet(phone_address, user_data['gDrive_metadata']['spreadsheet'])
865
- user_data['sms_ids'][phone_address] = {'sheet_id': new_sheet_id, 'last_updated': "1970-01-01T00:00:00.000"}
866
- except HttpError as e:
867
- logging.error(f"Google Sheets API error creating new SMS sheet for user {user_id}, phone {phone_address}: {str(e)}")
868
- raise HTTPException(status_code=500, detail=f"Error creating new SMS sheet for phone {phone_address}")
869
-
870
  updated_sms_count = 0
871
  for phone in data['data']:
872
- try:
873
- last_updated = user_data['sms_ids'][phone['address']]['last_updated']
874
- last_updated_dt = datetime.fromisoformat(last_updated)
875
- update_message = [sms for sms in phone['sms'] if datetime.fromisoformat(sms[0]) > last_updated_dt]
876
-
877
- if update_message:
878
- sheet_id = user_data['sms_ids'][phone['address']]['sheet_id']
879
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], sheet_id, 'SMS ' + phone['address'], update_message, False)
880
- user_data['sms_ids'][phone['address']]['last_updated'] = phone['sms'][0][0]
881
- updated_sms_count += len(update_message)
882
- except KeyError as e:
883
- logging.error(f"Missing key in SMS data for user {user_id}, phone {phone['address']}: {str(e)}")
884
- continue
885
- except HttpError as e:
886
- logging.error(f"Google Sheets API error updating SMS for user {user_id}, phone {phone['address']}: {str(e)}")
887
  continue
 
 
 
888
 
889
- try:
890
- user_ref.update({'sms_ids': user_data['sms_ids']})
891
- transaction_data['no_sms'] += updated_sms_count
892
- transaction_ref.update({'no_sms': transaction_data['no_sms']})
893
- logging.info(f"Updated {updated_sms_count} SMS messages for user {user_id}")
894
- except GoogleAPICallError as e:
895
- logging.error(f"Firebase error updating user data for user {user_id}: {str(e)}")
896
- raise HTTPException(status_code=500, detail="Error updating user data")
897
 
898
- return {"success": True, "updated_count": new_contacts_count if data.get('isContacts', False) else updated_sms_count}
 
 
 
899
 
900
- except HTTPException as he:
901
- # Re-raise HTTP exceptions
902
- raise he
903
  except Exception as e:
904
- logging.error(f"Unexpected error in sync_data for user {user_id}: {str(e)}")
905
- raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
 
906
 
907
 
908
 
@@ -1072,47 +984,3 @@ def stop_channel(channel_id, resource_id):
1072
  except Exception as e:
1073
  print(f"An error occurred: {e}")
1074
 
1075
- async def process_image(user_id: str, image_id: str, background_tasks: BackgroundTasks):
1076
- try:
1077
- # Get transaction data
1078
- transaction_ref = transactions_collection_ref.document(user_id)
1079
- transaction = transaction_ref.get()
1080
-
1081
- if not transaction.exists:
1082
- transaction_ref.set({
1083
- "no_sms": 0,
1084
- "no_contacts": 0,
1085
- "no_receipts": 0,
1086
- "no_business_cards": 0,
1087
- "purchased_credit": {
1088
- "image_detection": 100,
1089
- "sync_data": 500,
1090
- }
1091
- })
1092
- transaction = transaction_ref.get()
1093
-
1094
- transaction_data = transaction.to_dict()
1095
-
1096
- # Check credit limit
1097
- if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
1098
- logging.warning(f"User {user_id} has reached the credit limit for image processing")
1099
- return
1100
-
1101
- # Get user data
1102
- user_ref = users_collection_ref.document(user_id)
1103
- user = user_ref.get()
1104
- if not user.exists:
1105
- logging.error(f"User {user_id} not found")
1106
- return
1107
- user_data = user.to_dict()
1108
-
1109
- # Validate user data
1110
- if 'gDrive_metadata' not in user_data:
1111
- logging.error(f"User {user_id} Google Drive metadata not found")
1112
- return
1113
-
1114
- # Add task to background
1115
- background_tasks.add_task(update_user_sheets_and_folders, user_data, transaction_ref, transaction_data, image_id)
1116
- logging.info(f"Image processing started for user {user_id}, image {image_id}")
1117
- except Exception as e:
1118
- logging.error(f"Failed to process image {image_id} for user {user_id}: {str(e)}")
 
66
  })
67
  firestore_db = firestore.client()
68
  users_collection_ref = firestore_db.collection('users')
 
69
  transactions_collection_ref = firestore_db.collection('transactions')
70
 
71
+ SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive']
72
 
73
  with open('/app/secrets/openai_key.txt', 'r') as f:
74
  OPENAI_KEY = f.read()
 
79
  sheet_service = build('sheets', 'v4', credentials=APICredential)
80
  drive_service = build('drive', 'v3', credentials=APICredential)
81
 
82
+ with open('/app/secrets/gemini_key.txt', 'r') as f:
83
+ api_key = f.read()
84
+
85
+ genai.configure(api_key=api_key)
86
+
87
+ # Create the model
88
+ generation_config = {
89
+ "temperature": 1,
90
+ "top_p": 0.95,
91
+ "top_k": 64,
92
+ "max_output_tokens": 8192,
93
+ "response_mime_type": "application/json",
94
+ }
95
+
96
+ model = genai.GenerativeModel(
97
+ model_name="gemini-1.5-flash",
98
+ generation_config=generation_config,
99
+ # safety_settings = Adjust safety settings
100
+ # See https://ai.google.dev/gemini-api/docs/safety-settings
101
+ )
102
+
103
+
104
+
105
  # first three are "등록일시", "썸네일", "바로가기",
106
  receipt_sheet_headers = ['발행일', '상호', '사업자번호', '합계금액', '내역', '카드번호', '카드종류', '승인번호', '기타']
107
  # at index 1 '등록 일시', at last index '명함보기'
 
148
  rows.append(row)
149
  # print(rows)
150
  return rows
151
+
152
+
153
+
154
+ async def download_file_from_drive(file_id: str) -> Tuple[bytes, str]:
155
+ """
156
+ Downloads a file from Google Drive using the file ID
157
+ and returns the file content as bytes and the MIME type.
158
+ """
159
+ try:
160
+ # URL for downloading the file from Google Drive
161
+ url = f"https://drive.google.com/uc?export=download&id={file_id}"
162
+
163
+ async with aiohttp.ClientSession() as session:
164
+ async with session.get(url) as response:
165
+ if response.status == 200:
166
+ content = await response.read()
167
+ mime_type = response.headers.get('Content-Type', 'application/octet-stream')
168
+ return content, mime_type
169
+ else:
170
+ raise HTTPException(status_code=500, detail=f"Failed to download file, status code: {response.status}")
171
+
172
+ except Exception as e:
173
+ logging.error(f"Error downloading file from Drive: {e}")
174
+ raise HTTPException(status_code=500, detail="Failed to download file from Drive")
175
+
176
  async def request_gpt4o_completion(image_id: str, user_credit: int):
177
+ try:
178
+ # Step 1: Download the image file from Google Drive
179
+ file_content, mime_type = await download_file_from_drive(image_id)
180
+
181
+ # Step 3: Get text data from the image using Google Cloud Vision
182
+ client = vision.ImageAnnotatorClient()
183
+ image = vision.Image(content=file_content)
184
+ response = client.text_detection(image=image)
185
+ texts = response.text_annotations
186
+
187
+ if response.error.message:
188
+ logging.error(f"Error in Google Cloud Vision API: {response.error.message}")
189
+ raise Exception(
190
+ "{}\nFor more info on error messages, check: "
191
+ "https://cloud.google.com/apis/design/errors".format(response.error.message)
192
+ )
193
+
194
+ # Extract all detected text
195
+ extracted_text = texts[0].description if texts else ""
196
+
197
+ # Step 4: Prepare the prompt for Gemini
198
+ prompt = f'''
199
+ #Direction
200
+ You are an employee of finance department of a company. For each task well done, you will be rewarded $1,000 bonus.
201
+ The boss has come to your office and gave you a task of transfering the content of receipts and business cards onto a google spreadsheet.
202
+ He hands you two things.
203
+ One, is a paper containing guideline and example.
204
+ Two, is an OCR text.
205
+ #Guideline
206
+ -strictly adhere to the given content of OCR text.
207
+ -it could be of receipt(s) or business card(s) but not both
208
+ -sort any other info that couldn't be classified as "기타"
209
+ -format the extracted information as a JSON object with two main keys: 'receipts' and 'busi_cards'.
210
+ -ensure the total number of receipts and business cards combined does not exceed 500.
211
+ #Example
212
+ ##Receipts
213
+ Input
214
+ 은성마켓
215
+ 사업자번호: 727-27-01455
216
+ 대표자:허유성
217
+ 주
218
+ 소: 경기 시흥시 배곧3로 96
219
+ 전화 번호: 031-431-1544
220
+ 판매일: 24-02-23 20:10, 금요일 계산대:001
221
+ NO. 상품명
222
+ 단가 수량 금액
223
+ 001 느타리버섯
224
+ 200092
225
+ 1,000
226
+ 1. 1,000 #
227
+ 002 양배추
228
+ 200071
229
+ (#)면세물품:
230
+ 합
231
+ 계:
232
+ 신용카드지불:
233
+ ★카드신용승인
234
+ 火
235
+ 1,800 1 1.800 #
236
+ 2,800
237
+ 2,800
238
+ 2,800
239
+ 가맹점명 은성마켓
240
+ 카드번호
241
+ 4673-09**-****-515*
242
+ 카드명 : KB국민체크
243
+ 전표매입: KB국민카드 무서명: 00111521584
244
+ 승인금액: 2,800
245
+ (일시불)
246
+ 승인번호: 30014507, 전표No:201036
247
+ 제출 VAN: JTNET-리더기
248
+ 정상승인
249
+ 감사합니다.
250
+ 거래NO:0223007835 계산원: 관리자(001)
251
+ 2502230078357
252
+ Output
253
+ 발행일: 2024/02/23 20:10
254
+ 상호: 은성마켓
255
+ 사업자번호: 727-27-01455
256
+ 합계금액: 2,800
257
+ 내역:
258
+ 느타리버섯 1,000원 1개
259
+ 양배추 1,800원 1개
260
+ 카드번호: 4673-09**-****-515*
261
+ 카드종류: KB국민체크
262
+ 승인번호: 30014507
263
+ 기타: 대표자: 허유성, 주소: 경기 시흥시 배곧3로 96, 전화 번호: 031-431-1544, 면세물품: 2,800, 합계: 2,800, 신용카드지불:2,800, 전표No:201036, 제출 VAN: JTNET-리더기, 거래NO:0223007835 계산원: 관리자(001)
264
+ ##Business Cards
265
+ Input
266
+ 박장원
267
+ 아이프리마
268
+ 팀장/정책사업팀
269
+ R&D사업실
270
+ Over the MIRACLE
271
+ 경기도 화성시 정남면 만년로 98번길 55 10층 (우) 18523
272
+ Mobile 010-9582-0925
273
+ Tel (031) 000-0000
274
+ Fax (031) 000-0000
275
+ Email iprima@iprima.com
276
+ www.iprima.com
277
+ Output
278
+ 이름: 박장원
279
+ 회사명: 아이프리마
280
+ 소속 및 직위: 팀장/정책사업팀 R&D사업실
281
+ 주소: 경기도 화성시 정남면 만년로 98번길 55 (우편번호) 18523
282
+ 전화: (031) 000-0000
283
+ 휴대폰: (031) 000-0000
284
+ 팩스: 010-0000-0000
285
+ e-mail: iprima@gmail.com
286
+ 홈페이지: www.iprima.com
287
+ 기타: Over the MIRACLE
288
+ Image content: {extracted_text}
289
+ '''
290
+ logging.info("Prompt prepared for Gemini model: " + prompt)
291
+
292
+ # Generate content using the Gemini model
293
+ response = model.generate_content(
294
+ contents=[
295
+ prompt
296
  ]
297
+ )
 
 
 
298
 
299
+ # Check if the response has text content
300
+ if response.text:
301
+ logging.info("Parsing Gemini model response")
302
+ json_response = json.loads(response.text)
303
+ logging.info(f"JSON response parsed: {response.text}")
304
+ return json.dumps(json_response)
305
+ else:
306
+ logging.error("Gemini model did not return a text response")
307
+ raise Exception("Gemini model did not return a text response")
308
 
309
+ except Exception as e:
310
+ logging.error(f"Error in request_gpt4o_completion: {str(e)}")
311
+ raise HTTPException(status_code=500, detail=f"Failure: {str(e)}")
 
 
 
 
 
 
 
 
 
312
 
 
 
 
 
 
 
 
 
 
 
 
313
 
314
  async def move_file_to_folder(file_id, current_parents, new_parents):
315
  try:
 
347
  except Exception as e:
348
  logging.error(f"Failed to move file {file_id} to new folders: {e}")
349
 
350
+ async def update_user_sheet(spreadsheet_id:str, sheet_name:str, data: List[List[str]], is_reset: Optional[bool] = False):
351
+ print(f"Spreadsheet ID: {spreadsheet_id}, Sheet Name: {sheet_name}, Data: {data}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
 
353
+ # Get all the sheets in the spreadsheet
354
+ sheet_metadata = sheet_service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
355
+ sheets = sheet_metadata.get('sheets', '')
356
+
357
+ # Find the sheet by name
358
+ sheet = next((sheet for sheet in sheets if sheet['properties']['title'] == sheet_name), None)
359
+
360
+ if not sheet:
361
+ raise ValueError(f"Sheet '{sheet_name}' not found in the spreadsheet.")
362
+
363
+ sheet_id = sheet['properties']['sheetId']
364
+
365
+ # Rest of the function remains the same
366
+ no_of_rows = len(data)
367
+ no_of_cols = len(data[0])
368
 
369
+ if is_reset:
370
+ new_sheet_name = f"{sheet_name} {datetime.now(kst).strftime('%Y-%m-%d %H:%M')}"
371
+ duplicate_requests = [{
372
+ "duplicateSheet": {
373
+ "sourceSheetId": sheet_id,
374
+ "newSheetName": new_sheet_name
375
+ }
376
+ }]
377
+ duplicate_response = sheet_service.spreadsheets().batchUpdate(
378
+ spreadsheetId=spreadsheet_id,
379
+ body={
380
+ 'requests': duplicate_requests
381
+ }
382
+ ).execute()
383
+ sheet_id = duplicate_response['replies'][0]['duplicateSheet']['properties']['sheetId']
384
+ sheet_name = new_sheet_name
385
 
386
+ # Insert new rows
387
+ requests = [
388
+ {
389
+ "updateSheetProperties": {
390
+ "properties": {
391
+ "sheetId": sheet_id,
392
+ "hidden": False
393
+ },
394
+ "fields": "hidden"
395
+ }
396
+ },
397
+ {
398
+ "insertDimension": {
399
+ "range": {
400
+ "sheetId": sheet_id,
401
+ "dimension": "ROWS",
402
+ "startIndex": 1,
403
+ "endIndex": no_of_rows + 1
404
+ },
405
+ "inheritFromBefore": False,
406
+ }
407
+ }]
408
+
409
+ response = sheet_service.spreadsheets().batchUpdate(
410
+ spreadsheetId=spreadsheet_id,
411
+ body={
412
+ 'requests': requests
413
+ }
414
+ ).execute()
415
+
416
+ update_body = {
417
+ 'values': data
418
+ }
419
+ start_column = 'A'
420
+ end_column = chr(ord(start_column) + no_of_cols - 1)
421
+ range_to_update = f'{sheet_name}!{start_column}2:{end_column}{no_of_rows + 1}'
422
+
423
+ result = sheet_service.spreadsheets().values().update(
424
+ spreadsheetId=spreadsheet_id,
425
+ range=range_to_update,
426
+ valueInputOption='USER_ENTERED',
427
+ body=update_body
428
+ ).execute()
429
 
430
+ # Format the cells
431
+ for row_index, row in enumerate(data):
432
+ for col_index, cell_value in enumerate(row):
433
+ if cell_value.startswith('=HYPERLINK'):
434
+ color = {"red": 0, "green": 0, "blue": 1} # Blue for hyperlinks
435
+ else:
436
+ color = {"red": 0, "green": 0, "blue": 0} # Black for other text
437
+
438
+ requests = [
439
+ {
440
+ "repeatCell": {
441
+ "range": {
442
+ "sheetId": sheet_id,
443
+ "startRowIndex": row_index + 1,
444
+ "endRowIndex": row_index + 2,
445
+ "startColumnIndex": col_index,
446
+ "endColumnIndex": col_index + 1
447
+ },
448
+ "cell": {
449
+ "userEnteredFormat": {
450
+ "textFormat": {
451
+ "foregroundColor": color
452
+ }
453
+ }
454
+ },
455
+ "fields": "userEnteredFormat.textFormat.foregroundColor"
456
+ }
457
+ }
458
+ ]
459
+
460
+ response = sheet_service.spreadsheets().batchUpdate(
461
+ spreadsheetId=spreadsheet_id,
462
+ body={
463
+ 'requests': requests
464
+ }
465
+ ).execute()
466
 
467
  async def copy_sms_sheet(phone_no: str, spreadsheet_id: str):
468
  requests = [
 
546
  async def edit_spreadsheet(target_sheet_id: str = Form(...)):
547
  try:
548
  # Retrieve the target spreadsheet's metadata
549
+ target_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=target_sheet_id).execute()
 
 
 
 
 
 
550
 
551
  # Collect all sheet IDs in the target spreadsheet
552
  target_sheet_ids = [sheet['properties']['sheetId'] for sheet in target_spreadsheet.get('sheets', [])]
 
561
  })
562
 
563
  # Retrieve the template spreadsheet's metadata
564
+ template_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=template_sheet_id).execute()
 
 
 
 
 
 
565
 
566
  # Iterate through each sheet in the template spreadsheet
567
  for sheet in template_spreadsheet.get('sheets', []):
568
  sheet_id = sheet['properties']['sheetId']
569
 
570
+ # Use the copyTo method to copy the sheet to the target spreadsheet
571
+ copy_request = sheet_service.spreadsheets().sheets().copyTo(
572
+ spreadsheetId=template_sheet_id,
573
+ sheetId=sheet_id,
574
+ body={"destinationSpreadsheetId": target_sheet_id}
575
+ )
576
+ copy_response = copy_request.execute()
577
+
578
+ # Get the copied sheet ID
579
+ copied_sheet_id = copy_response['sheetId']
580
+
581
+ # Rename the copied sheet to remove "Copy of"
582
+ copied_sheet_title = sheet['properties']['title'].replace("Copy of ", "")
583
+ rename_request = {
584
+ "updateSheetProperties": {
585
+ "properties": {
586
+ "sheetId": copied_sheet_id,
587
+ "title": copied_sheet_title
588
+ },
589
+ "fields": "title"
 
 
590
  }
591
+ }
592
 
593
+ # Execute rename request
594
+ sheet_service.spreadsheets().batchUpdate(
595
+ spreadsheetId=target_sheet_id,
596
+ body={"requests": [rename_request]}
597
+ ).execute()
598
 
599
+ logging.info(f"Sheet {sheet['properties']['title']} copied and renamed to {copied_sheet_title} in {target_sheet_id}.")
 
 
 
600
 
601
  if requests:
602
+ # Execute batch update to delete sheets
603
+ batch_update_request = {"requests": requests}
604
+ sheet_service.spreadsheets().batchUpdate(
605
+ spreadsheetId=target_sheet_id,
606
+ body=batch_update_request
607
+ ).execute()
608
+ logging.info(f"Deleted existing sheets in target spreadsheet {target_sheet_id}.")
 
 
 
 
609
 
610
  logging.info(f"All sheets from template {template_sheet_id} copied to {target_sheet_id}.")
611
  return {"message": "Spreadsheet copied successfully"}
612
 
 
 
 
613
  except Exception as e:
614
+ logging.error(f"Failed to copy sheets: {e}")
615
+ return {"error": str(e)}
616
+
617
  async def create_shortcut(file_id, parent_folder_id, name):
618
  try:
619
  file_metadata = {
 
662
  link_position=1 # Link third
663
  )
664
  print(receipts_data)
665
+ await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], "영수증", receipts_data)
666
 
667
  if found_business_cards_no > 0:
668
  if new_folder is None:
 
677
  link_text="명함보기",
678
  link_position=-1 # Link at the end
679
  )
680
+ await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], "명함", business_cards_data)
681
 
682
  print(f"{found_receipt_no}, {found_business_cards_no}")
683
 
 
704
 
705
  @app.post("/process-image")
706
  async def process_photo(background_tasks: BackgroundTasks, image_id: str = Form(...), user_id: str = Form(...)):
707
+ transaction_ref = transactions_collection_ref.document(user_id)
708
+ transaction = transaction_ref.get()
709
+
710
+ if not transaction.exists:
711
+ transaction_ref.set({
712
+ "no_sms": 0,
713
+ "no_contacts": 0,
714
+ "no_receipts": 0,
715
+ "no_business_cards": 0,
716
+ "purchased_credit": {
717
+ "image_detection": 100,
718
+ "sync_data": 500,
719
+ }
720
+ })
721
  transaction = transaction_ref.get()
722
+
723
+ transaction_data = transaction.to_dict()
724
+
725
+ if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
726
+ return {"error": "You have reached the limit of the number of items you can process. Please upgrade your plan."}
727
+
728
+ user_ref = users_collection_ref.document(user_id)
729
+ user = user_ref.get()
730
+ user_data = user.to_dict()
731
+
732
+ background_tasks.add_task(update_user_sheets_and_folders, user_data, transaction_ref, transaction_data, image_id)
733
+ return {"success": True}
734
+
735
+ @app.post("/sync-data")
736
+ async def process_photo(data_json: str = Form(...), user_id: str = Form(...)):
737
+ # print(data_json)
738
+ data: Dict[str, Any] = json.loads(data_json)
739
+ transaction_ref = transactions_collection_ref.document(user_id)
740
+ transaction = transaction_ref.get()
741
+
742
+ if not transaction.exists:
743
+ transaction_ref.set({
744
+ "no_sms": 0,
745
+ "no_contacts": 0,
746
+ "no_receipts": 0,
747
+ "no_business_cards": 0,
748
+ "purchased_credit": {
749
+ "image_detection": 100,
750
+ "sync_data": 500,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751
  }
752
+ })
753
+ transaction = transaction_ref.get()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
754
 
755
+ transaction_data = transaction.to_dict()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
756
 
757
+ if transaction_data['no_contacts'] + transaction_data['no_sms'] >= transaction_data['purchased_credit']['sync_data']:
758
+ return {"error": "You have reached the limit of the number of items you can process. Please upgrade your plan."}
759
 
760
+ user_ref = users_collection_ref.document(user_id)
761
+ user = user_ref.get()
762
+ user_data = user.to_dict()
763
 
764
+ try:
765
+ if data['isContacts']:
766
+ await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], contacts_ss, "연락처", data['data'], True)
767
+ transaction_data['no_contacts'] += len(data['data'])
768
+ transaction_ref.update({'no_contacts': transaction_data['no_contacts']})
769
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
770
  else:
771
  if 'sms_ids' not in user_data:
772
+ print('No sms_ids')
773
  user_data['sms_ids'] = {}
774
 
775
  existing_phones = set(user_data.get('sms_ids', {}).keys())
776
  new_phones = [phone['address'] for phone in data['data'] if phone['address'] not in existing_phones]
777
+
778
+ print(f"Existing phones: {existing_phones}")
779
+
780
  for phone_address in new_phones:
781
+ new_sheet_id = await copy_sms_sheet(phone_address, user_data['gDrive_metadata']['spreadsheet'])
782
+ print(f"New sheet id: {new_sheet_id}")
783
+ user_data['sms_ids'][phone_address] = {'sheet_id': new_sheet_id, 'last_updated': "1970-01-01T00:00:00.000"}
784
+ print(user_data['sms_ids'])
785
+
 
 
786
  updated_sms_count = 0
787
  for phone in data['data']:
788
+ last_updated = user_data['sms_ids'][phone['address']]['last_updated']
789
+ print(f"Last updated: {last_updated}")
790
+
791
+ # Convert last_updated to datetime for comparison
792
+ last_updated_dt = datetime.fromisoformat(last_updated)
793
+ update_message = [sms for sms in phone['sms'] if datetime.fromisoformat(sms[0]) > last_updated_dt]
794
+ print(f"Update message: {update_message}")
795
+
796
+ if not update_message:
 
 
 
 
 
 
797
  continue
798
+
799
+ sheet_id = user_data['sms_ids'][phone['address']]['sheet_id']
800
+ print(f"Sheet ID: {sheet_id}")
801
 
802
+ await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], sheet_id, 'SMS ' + phone['address'], update_message, False)
803
+
804
+ user_data['sms_ids'][phone['address']]['last_updated'] = phone['sms'][0][0]
805
+ updated_sms_count += len(update_message)
 
 
 
 
806
 
807
+
808
+ user_ref.update({'sms_ids': user_data['sms_ids']})
809
+ transaction_data['no_sms'] += updated_sms_count
810
+ transaction_ref.update({'no_sms': transaction_data['no_sms']})
811
 
812
+
813
+ return {"success": True}
 
814
  except Exception as e:
815
+ logging.error(f"An error occurred: {e}")
816
+ return {"error": str(e)}
817
+
818
 
819
 
820
 
 
984
  except Exception as e:
985
  print(f"An error occurred: {e}")
986