Spaces:
Sleeping
Sleeping
added logging to file
Browse files- update_data.py +24 -25
update_data.py
CHANGED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
import gspread
|
| 3 |
-
|
|
|
|
| 4 |
import requests
|
| 5 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def download_sheet(sheet_id, range_name):
|
| 8 |
"""Downloads data from Google Sheets and returns a DataFrame."""
|
|
@@ -27,24 +32,24 @@ def download_sheet(sheet_id, range_name):
|
|
| 27 |
|
| 28 |
# Convert to DataFrame
|
| 29 |
df = pd.DataFrame(data)
|
| 30 |
-
|
| 31 |
return df
|
| 32 |
except gspread.exceptions.APIError as e:
|
| 33 |
-
|
| 34 |
-
raise
|
| 35 |
except requests.exceptions.RequestException as e:
|
| 36 |
-
|
| 37 |
-
raise
|
| 38 |
|
| 39 |
def save_to_csv(df, file_path):
|
| 40 |
if df.empty:
|
| 41 |
-
|
|
|
|
| 42 |
try:
|
| 43 |
df.to_csv(file_path, index=False)
|
| 44 |
-
|
| 45 |
except Exception as e:
|
| 46 |
-
|
| 47 |
-
raise
|
| 48 |
|
| 49 |
def git_commit_push():
|
| 50 |
"""Sets git configurations, commits, and pushes updated CSV file to GitHub."""
|
|
@@ -52,24 +57,18 @@ def git_commit_push():
|
|
| 52 |
subprocess.run(['git', 'config', '--global', 'user.name', 'github-actions'], check=True)
|
| 53 |
subprocess.run(['git', 'config', '--global', 'user.email', 'github-actions@github.com'], check=True)
|
| 54 |
subprocess.run(['git', 'add', 'omoku_data.csv'], check=True)
|
| 55 |
-
except subprocess.CalledProcessError as e:
|
| 56 |
-
print(f"Failed to commit or push changes: {e}")
|
| 57 |
-
if "nothing to commit" in str(e.stderr):
|
| 58 |
-
print("No changes to commit.")
|
| 59 |
-
else:
|
| 60 |
-
raise SystemExit(e)
|
| 61 |
-
|
| 62 |
-
try:
|
| 63 |
subprocess.run(['git', 'commit', '-m', 'Update dataset'], check=True)
|
| 64 |
subprocess.run(['git', 'push'], check=True)
|
|
|
|
| 65 |
except subprocess.CalledProcessError as e:
|
| 66 |
-
if "nothing to commit" in str(e):
|
| 67 |
-
|
| 68 |
else:
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
try:
|
| 73 |
SHEET_ID = '1dVa6SGm1j-z20NUDUlWSJgQffXzvJZ_a33wT_O5EOUk'
|
| 74 |
RANGE_NAME = 'data'
|
| 75 |
FILE_PATH = 'omoku_data.csv'
|
|
@@ -78,5 +77,5 @@ if __name__ == "__main__":
|
|
| 78 |
save_to_csv(df, FILE_PATH)
|
| 79 |
git_commit_push()
|
| 80 |
except Exception as e:
|
| 81 |
-
|
| 82 |
-
raise
|
|
|
|
|
|
|
| 1 |
import gspread
|
| 2 |
+
import logging
|
| 3 |
+
import pandas as pd
|
| 4 |
import requests
|
| 5 |
import subprocess
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
| 8 |
+
|
| 9 |
+
# Set up logging
|
| 10 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 11 |
|
| 12 |
def download_sheet(sheet_id, range_name):
|
| 13 |
"""Downloads data from Google Sheets and returns a DataFrame."""
|
|
|
|
| 32 |
|
| 33 |
# Convert to DataFrame
|
| 34 |
df = pd.DataFrame(data)
|
|
|
|
| 35 |
return df
|
| 36 |
except gspread.exceptions.APIError as e:
|
| 37 |
+
logging.error(f"API error occurred: {e}")
|
| 38 |
+
raise
|
| 39 |
except requests.exceptions.RequestException as e:
|
| 40 |
+
logging.error(f"Request failed: {e}")
|
| 41 |
+
raise
|
| 42 |
|
| 43 |
def save_to_csv(df, file_path):
|
| 44 |
if df.empty:
|
| 45 |
+
logging.error("No data to save: DataFrame is empty.")
|
| 46 |
+
raise ValueError("Empty DataFrame")
|
| 47 |
try:
|
| 48 |
df.to_csv(file_path, index=False)
|
| 49 |
+
logging.info(f"Data saved successfully at {datetime.now().strftime("%I:%M:%S %p")}.")
|
| 50 |
except Exception as e:
|
| 51 |
+
logging.error(f"Failed to save data: {e}")
|
| 52 |
+
raise
|
| 53 |
|
| 54 |
def git_commit_push():
|
| 55 |
"""Sets git configurations, commits, and pushes updated CSV file to GitHub."""
|
|
|
|
| 57 |
subprocess.run(['git', 'config', '--global', 'user.name', 'github-actions'], check=True)
|
| 58 |
subprocess.run(['git', 'config', '--global', 'user.email', 'github-actions@github.com'], check=True)
|
| 59 |
subprocess.run(['git', 'add', 'omoku_data.csv'], check=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
subprocess.run(['git', 'commit', '-m', 'Update dataset'], check=True)
|
| 61 |
subprocess.run(['git', 'push'], check=True)
|
| 62 |
+
logging.info(f"Data updated successfully at {datetime.now().strftime("%I:%M:%S %p")}.")
|
| 63 |
except subprocess.CalledProcessError as e:
|
| 64 |
+
if "nothing to commit" in str(e.stderr):
|
| 65 |
+
logging.info("No changes to commit.")
|
| 66 |
else:
|
| 67 |
+
logging.error(f"Failed to commit or push changes: {e}")
|
| 68 |
+
raise
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
try:
|
| 72 |
SHEET_ID = '1dVa6SGm1j-z20NUDUlWSJgQffXzvJZ_a33wT_O5EOUk'
|
| 73 |
RANGE_NAME = 'data'
|
| 74 |
FILE_PATH = 'omoku_data.csv'
|
|
|
|
| 77 |
save_to_csv(df, FILE_PATH)
|
| 78 |
git_commit_push()
|
| 79 |
except Exception as e:
|
| 80 |
+
logging.critical(f"An unexpected error occurred: {e}")
|
| 81 |
+
raise
|