#!/usr/bin/env python3
"""Grep qualifying tweets for arbitrage / free-credit / discount signals."""
import json, re, sys
date = sys.argv[1]
d = json.load(open(f"/home/kiro/trend-engine/trends/x/raw/scan_results_{date}_FULL.json"))
PAT = re.compile(r"(free|credit|cashback|discount|\boff\b|\d+% ?off|unlimited|referral|promo|"
                 r"giveaway|coupon|trial|no cost|\$0|open[- ]source|open[- ]weight|prize|grant|"
                 r"funding|resets?|expires)", re.I)
seen = set()
for x in sorted(d["qualifying"], key=lambda y: -y["score"]):
    m = PAT.search(x["text"])
    if not m:
        continue
    key = x["text"][:60]
    if key in seen:
        continue
    seen.add(key)
    print(f"@{x['handle']} | {x['date']} | {x['likes']}L {x['views']}V | [{m.group(0)}]")
    print(f"   {x['text'][:250]}")
    print(f"   {x['url']}")
