# -*- coding: utf-8 -*-
import re, io, sys, glob

url = sys.argv[1]
kw = sys.argv[2:] or ['']
# 找缓存文件（r349- 前缀，含 URL 关键片段）
frag = url.rstrip('/').split('/')[-1][:40]
files = glob.glob('cache/r349-*' + '*')
hit = None
for f in files:
    try:
        head = io.open(f, encoding='utf-8', errors='ignore').read(200000)
    except Exception:
        continue
    if url in head or url.replace('https://', '') in head:
        hit = f
if not hit:
    print('no cache for', url)
    sys.exit(1)
h = io.open(hit, encoding='utf-8', errors='ignore').read()
ls = re.findall(r'href="([^"]+)"', h)
seen = set()
for u in ls:
    if any(k and k in u for k in kw):
        if u not in seen:
            seen.add(u)
            print(u)
print('cache', hit)
