#!/usr/bin/env python3 """Check today's football matches""" import sys sys.path.insert(0, 'ai-engine') import psycopg2 from psycopg2.extras import RealDictCursor from datetime import datetime, timezone # Read .env file manually db_url = None with open('.env') as f: for line in f: if line.startswith('DATABASE_URL'): db_url = line.split('=', 1)[1].strip().strip('"').strip("'") break if '?schema=' in db_url: db_url = db_url.split('?schema=')[0] conn = psycopg2.connect(db_url) cur = conn.cursor(cursor_factory=RealDictCursor) # Get matches that have finished (have scores) cur.execute(''' SELECT m.id, m.score_home, m.score_away, m.ht_score_home, m.ht_score_away, m.mst_utc, m.status, m.state, ht.name as home_team_name, at.name as away_team_name, l.name as league_name, l.sport as sport FROM matches m JOIN teams ht ON ht.id = m.home_team_id JOIN teams at ON at.id = m.away_team_id JOIN leagues l ON l.id = m.league_id WHERE l.sport = 'football' AND m.score_home IS NOT NULL ORDER BY m.mst_utc DESC LIMIT 20 ''') matches = cur.fetchall() print('Last 20 finished football matches:') print() for m in matches: ts = m['mst_utc'] / 1000 dt = datetime.fromtimestamp(ts, tz=timezone.utc) score = "HT: {}-{}, FT: {}-{}".format(m['ht_score_home'], m['ht_score_away'], m['score_home'], m['score_away']) league = (m['league_name'] or 'Unknown')[:20] home = (m['home_team_name'] or 'Unknown')[:20] away = (m['away_team_name'] or 'Unknown')[:20] print("{} UTC | {} | {} vs {} | {}".format(dt.strftime('%Y-%m-%d %H:%M'), league.ljust(20), home.ljust(20), away.ljust(20), score)) cur.close() conn.close()