scheduler now working

This commit is contained in:
knoflook 2022-12-10 00:37:56 +01:00
parent 42507fa67c
commit 293a362645
4 changed files with 54 additions and 7 deletions

View File

@ -1,2 +1,2 @@
version="dev"
timeslots = ["09:00", "15:30", "20:45"]
timeslots = ["00:37", "15:20", "20:45"]

13
db.py
View File

@ -92,12 +92,17 @@ def get_transmission_data(uid):
return return_data
# set return_disabled=True to get all transmissions, including those that aren't enabled
def get_transmissions_by_timeslot(date, timeslot, return_disabled=False):
def get_transmissions_by_timeslot(date, timeslot, cursor, return_disabled=False):
if return_disabled:
data = cursor.execute('SELECT * FROM transmissions WHERE instr("timeslots", ?) > 1 AND date=?;', (timeslot,date)).fetchall()
try:
data = cursor.execute('SELECT * FROM transmissions WHERE instr("timeslots", ?) > 1 AND date=?;', (timeslot,date)).fetchall()
except sqlite3.Error as error:
log.error("Coud not execute command to get transmissions by timeslot: " + error)
else:
data = cursor.execute('SELECT * FROM transmissions WHERE instr("timeslots", ?) > 1 AND date=? AND play=1;', (timeslot,date)).fetchall()
print("data: ", data)
try:
data = cursor.execute('SELECT * FROM transmissions WHERE instr("timeslots", ?) > 1 AND date=? AND play=1;', (timeslot,date)).fetchall()
except:
log.error("Coud not execute command to get transmissions by timeslot: " + error)
return_data = []
for row in data:
return_data.append({

43
main.py
View File

@ -3,6 +3,9 @@
import gi
import config
import logging
import schedule
import sqlite3
from threading import Thread
from datetime import datetime
from time import sleep
from random import choice
@ -17,9 +20,47 @@ import db
ttsEngine = pyttsx3.init('espeak')
ttsEngine.setProperty('voice', 'female3')
ttsEngine.setProperty('speed', 75)
ttsEngine.setProperty('speed', 40)
log = logging.getLogger("main")
def play_transmissions(timeslot):
schedulerEngine = pyttsx3.init('espeak')
schedulerEngine.setProperty('voice', 'female3')
schedulerEngine.setProperty('speed', 40)
log = logging.getLogger("main")
try:
schedulerConnection = sqlite3.connect(db.database)
schedulerCursor = schedulerConnection.cursor()
schedulerCursor.execute('select sqlite_version();')
except sqlite3.Error as error:
log.critical('Could not connect scheduler to sqlite. An error occured: ', error)
exit(1)
date = datetime.now().date().isoformat()
toPlay = db.get_transmissions_by_timeslot(date, timeslot, schedulerCursor)
for transmission in toPlay:
message = transmission["message"]
author = transmission["author"]
title = transmission["title"]
text = "Author: %s, Title: %s. Message: %s" % (
author,
title,
message)
schedulerEngine.say(text)
schedulerEngine.runAndWait()
schedulerConnection.close()
for i in range(len(config.timeslots)):
schedule.every().day.at(config.timeslots[i]).do(play_transmissions, i)
def scheduler_loop():
while True:
schedule.run_pending()
sleep(30)
scheduler_thread = Thread(target=scheduler_loop)
scheduler_thread.daemon = True
scheduler_thread.start()
db.create_transmissions_table()

View File

@ -17,4 +17,5 @@ def check(date, inputTime):
print (now.strftime("%Y-%m-%d %H:%M:%S"))
time.sleep(1)
check(data, czas)
check(data, czas)