Files
Bike-Tinder/app/deck.py

66 lines
1.9 KiB
Python

"""Read-only proof CLI: print the ranked deck and DB counts.
python3 -m app.deck --db data/bike-tinder.sqlite3 --top 15
"""
import argparse
import sys
from . import db
DECK_QUERY = """
SELECT * FROM listings
WHERE status = 'active'
AND id NOT IN (SELECT listing_id FROM swipes WHERE action = 'nope')
AND id NOT IN (SELECT listing_id FROM swipes WHERE action IN ('like','maybe')
AND (action = 'like' OR updated_at > datetime('now','-7 days')))
ORDER BY score DESC, first_seen DESC
LIMIT ?
"""
def fmt_price(p):
return p if p else '-'
def fmt_km(k):
return ('%d km' % k) if k is not None else '-'
def main(argv=None):
ap = argparse.ArgumentParser(description='Bike-Tinder deck (read-only)')
ap.add_argument('--db', default='data/bike-tinder.sqlite3')
ap.add_argument('--top', type=int, default=15)
args = ap.parse_args(argv)
conn = db.connect(args.db)
db.init_db(conn)
rows = conn.execute(DECK_QUERY, (args.top,)).fetchall()
print('=== Bike-Tinder deck (top %d) ===' % len(rows))
for i, r in enumerate(rows, 1):
print('%3d. %s · %s · %s · %s · %d' % (
i, r['title'], fmt_price(r['price_display']), fmt_km(r['km']),
r['region'], r['score']))
counts = {}
for status in ('active', 'gone', 'dup'):
counts[status] = conn.execute(
'SELECT COUNT(*) AS n FROM listings WHERE status=?', (status,)).fetchone()['n']
total = conn.execute('SELECT COUNT(*) AS n FROM listings').fetchone()['n']
last = conn.execute(
'SELECT new FROM import_runs ORDER BY id DESC LIMIT 1').fetchone()
new = last['new'] if last else 0
print('=== counts ===')
print('total: %d' % total)
print('active: %d' % counts['active'])
print('gone: %d' % counts['gone'])
print('dup: %d' % counts['dup'])
print('new: %d' % new)
conn.close()
return 0
if __name__ == '__main__':
sys.exit(main())