Azaya89 commited on
Commit
7932915
·
1 Parent(s): 939e56e

added check for new data updates

Browse files
Files changed (1) hide show
  1. update_data.py +18 -2
update_data.py CHANGED
@@ -1,5 +1,6 @@
1
  import gspread
2
  import logging
 
3
  import pandas as pd
4
  import requests
5
  import subprocess
@@ -8,6 +9,15 @@ from oauth2client.service_account import ServiceAccountCredentials
8
  # Set up logging
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
 
 
 
 
 
 
 
 
 
 
11
  def download_sheet(sheet_id, range_name):
12
  """Downloads data from Google Sheets and returns a DataFrame."""
13
  try:
@@ -72,8 +82,14 @@ if __name__ == "__main__":
72
  RANGE_NAME = 'data'
73
  FILE_PATH = 'omoku_data.csv'
74
 
75
- df = download_sheet(SHEET_ID, RANGE_NAME)
76
- save_to_csv(df, FILE_PATH)
 
 
 
 
 
 
77
  git_commit_push()
78
  except Exception as e:
79
  logging.critical(f"An unexpected error occurred: {e}")
 
1
  import gspread
2
  import logging
3
+ import os
4
  import pandas as pd
5
  import requests
6
  import subprocess
 
9
  # Set up logging
10
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
11
 
12
+ def read_existing_data(file_path):
13
+ """Reads existing data from a CSV file to compare with newly fetched data."""
14
+ if os.path.exists(file_path):
15
+ logging.info("Existing data file found. Reading data...")
16
+ return pd.read_csv(file_path)
17
+ else:
18
+ logging.info("No existing data file found.")
19
+ return pd.DataFrame() # Return an empty DataFrame if file does not exist
20
+
21
  def download_sheet(sheet_id, range_name):
22
  """Downloads data from Google Sheets and returns a DataFrame."""
23
  try:
 
82
  RANGE_NAME = 'data'
83
  FILE_PATH = 'omoku_data.csv'
84
 
85
+ new_data = download_sheet(SHEET_ID, RANGE_NAME)
86
+ existing_data = read_existing_data(FILE_PATH)
87
+
88
+ if not new_data.empty and new_data.equals(existing_data):
89
+ logging.info("No new data to update.")
90
+ exit(0)
91
+
92
+ save_to_csv(new_data, FILE_PATH)
93
  git_commit_push()
94
  except Exception as e:
95
  logging.critical(f"An unexpected error occurred: {e}")