70 lines
2.1 KiB
Python
Executable File
70 lines
2.1 KiB
Python
Executable File
import yaml
|
|
import re
|
|
import asyncio
|
|
import sys
|
|
from nio import AsyncClient, LoginResponse, RoomSendResponse, RoomMessageText, SyncResponse
|
|
import time
|
|
from post import Post
|
|
from room import Room
|
|
import dbutil
|
|
|
|
|
|
def get_last_getvotes_timestamp(room):
|
|
"""Get the timestamp of the last !getvotes command for this room."""
|
|
conn = dbutil.connect(room.db_path)
|
|
c = conn.cursor()
|
|
# Check if getvotes table exists
|
|
c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='getvotes'")
|
|
if not c.fetchone():
|
|
conn.close()
|
|
return None
|
|
|
|
c.execute('SELECT MAX(timestamp) FROM getvotes')
|
|
row = c.fetchone()
|
|
conn.close()
|
|
if row and row[0]:
|
|
return row[0]
|
|
return None
|
|
|
|
async def main():
|
|
config_path = sys.argv[1] if len(sys.argv) > 1 else "configs/config.yaml"
|
|
room = Room(config_path)
|
|
|
|
if not await room.login_to_matrix():
|
|
print("Failed to login")
|
|
return
|
|
|
|
# Initial sync establishes the next-batch; after this, listen from "now"
|
|
await room.client.sync(timeout=300)
|
|
room.write_room_slugs()
|
|
|
|
try:
|
|
while True:
|
|
listen_since_ms = int(time.time() * 1000)
|
|
|
|
# Get the timestamp of the last !getvotes command
|
|
last_getvotes = get_last_getvotes_timestamp(room)
|
|
if last_getvotes:
|
|
print(f"Last !getvotes was at UNIX timestamp {last_getvotes}")
|
|
else:
|
|
print("No previous !getvotes found - will show all unposted suggestions")
|
|
|
|
# Wait for !gettitles before posting
|
|
await room.wait_for_gettitles(since_ms=listen_since_ms)
|
|
|
|
# Then read and post suggestions created since last !getvotes
|
|
room.read_db(since_timestamp=last_getvotes, only_unposted=True)
|
|
if room.posts:
|
|
await room.post_suggestions_individually()
|
|
room.update_last_post()
|
|
else:
|
|
await room.alert_post("No new unposted suggestions found.")
|
|
await asyncio.sleep(1)
|
|
finally:
|
|
await room.close()
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|
|
|
|
|