|
import os |
|
import time |
|
import requests |
|
from dotenv import load_dotenv |
|
import globs |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
api_key_goo = os.getenv('api_key_g') |
|
spreadsheet_id = os.getenv('spreadsheet') |
|
sheet_name = "Sheet1" |
|
|
|
def send_data_to_google_sheets(): |
|
try: |
|
|
|
dey_graf = globs.dey |
|
wek_graf = globs.wek |
|
v_hid_graf = globs.v_hid |
|
v_min_graf = globs.v_min |
|
ph_graf = globs.ph |
|
|
|
|
|
new_values = [ |
|
[dey_graf, wek_graf, v_hid_graf, v_min_graf, ph_graf] |
|
] |
|
|
|
|
|
url = f'https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}/values/{sheet_name}:append?key={api_key_goo}' |
|
|
|
|
|
data = { |
|
"range": sheet_name, |
|
"majorDimension": "ROWS", |
|
"values": new_values |
|
} |
|
|
|
|
|
response = requests.post(url, json=data) |
|
|
|
|
|
if response.status_code == 200: |
|
print('Данные успешно добавлены в таблицу.') |
|
else: |
|
print('Ошибка при добавлении данных в таблицу:', response.status_code, response.text) |
|
except Exception as e: |
|
print(f"Произошла ошибка при отправке данных: {e}") |
|
|
|
def start_scheduler(): |
|
from apscheduler.schedulers.background import BackgroundScheduler |
|
scheduler = BackgroundScheduler() |
|
scheduler.add_job(func=send_data_to_google_sheets, trigger="interval", hours=1) |
|
scheduler.start() |
|
return scheduler |
|
|