Skip to content

Commit

Permalink
Sync with sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
thecarrot123 committed Apr 29, 2024
1 parent 19a066b commit 44402f6
Show file tree
Hide file tree
Showing 5 changed files with 460 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ __pycache__/
.pytest_cache/
.venv/
.coverage
.mutmut-cache
.mutmut-cache
sheets_key.json
41 changes: 41 additions & 0 deletions app/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from threading import Event, Thread

from dotenv import load_dotenv
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
from telegram import Bot, Update
from telegram.ext import Application, CallbackContext, CommandHandler

Expand Down Expand Up @@ -383,13 +385,49 @@ def run_notifiers():
shutdown_event.wait(timeout=60)


def sync_with_google_sheets():
# Google Sheets credentials and service setup
SERVICE_ACCOUNT_FILE = 'sheets_key.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
creds = Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=creds)

SPREADSHEET_ID = os.getenv('SPREADSHEET_ID')
RANGE_NAME = 'Sheet1!A1'

# Connect to the SQLite database
conn = sqlite3.connect(DATABASE_URL)
cursor = conn.cursor()
cursor.execute("SELECT * FROM tasks")
rows = cursor.fetchall()
conn.close()

# Format the data for Google Sheets
values = [list(row) for row in rows]

# Update Google Sheet
body = {'values': values}
service.spreadsheets().values().update(
spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME,
valueInputOption='RAW',
body=body).execute()


def run_sheets_sync():
while not shutdown_event.is_set():
sync_with_google_sheets()
shutdown_event.wait(timeout=600)

# Main function


def main():
"""Run bot."""
try:
init_db()
sync_with_google_sheets()
application = Application.builder().token(TOKEN).build()

application.add_handler(CommandHandler("start", start_command))
Expand All @@ -403,6 +441,9 @@ def main():
thread = Thread(target=run_notifiers)
thread.start()

sheets_thread = Thread(target=run_sheets_sync)
sheets_thread.start()

# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)

Expand Down
Loading

0 comments on commit 44402f6

Please sign in to comment.