import os, re, json, urllib.parse
D = "/app/data/tasks/29/i29cq9s"
SHELLS = os.path.join(D, "shells")
BASE = "https://h5cq.cqgame.games"
refs = {}
urls = set()
for fn in sorted(os.listdir(SHELLS)):
    code = fn[:-5]
    b = open(os.path.join(SHELLS, fn), "rb").read().decode("utf-8", "replace")
    page = "%s/h5/%s/index.html" % (BASE, code)
    lst = []
    for m in re.findall(r'(?:src|href)="([^"]+)"', b):
        u = urllib.parse.urljoin(page, m)
        if not u.startswith(BASE):
            continue
        lst.append(u)
        urls.add(u)
    refs[code] = lst
json.dump(refs, open(os.path.join(D, "refs.json"), "w"))
open(os.path.join(D, "urls.txt"), "w").write("\n".join(sorted(urls)))
print("codes", len(refs), "total refs", sum(len(v) for v in refs.values()), "uniq urls", len(urls))
import collections
print(collections.Counter(urllib.parse.urlparse(u).path.split("/")[2] for u in urls))
print(collections.Counter(os.path.splitext(urllib.parse.urlparse(u).path)[1] for u in urls).most_common(15))
