Files
titlebot-ng-ng/post_results.py
T

71 lines
2.1 KiB
Python
Raw Normal View History

2026-08-06 21:34:52 -05:00
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
2026-08-12 18:22:26 -05:00
import dbutil
2026-08-06 21:34:52 -05:00
def get_last_getvotes_timestamp(room):
"""Get the timestamp of the last !getvotes command for this room."""
2026-08-12 18:22:26 -05:00
conn = dbutil.connect(room.db_path)
2026-08-06 21:34:52 -05:00
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"
2026-08-16 12:02:37 -04:00
assert room.client is not None
2026-08-06 21:34:52 -05:00
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())