# Fetch exchange strings behind factoriobin/pastebin links gathered from reddit/forums. argv: links tsv files import sys; sys.path.insert(0, __import__('os').path.dirname(__file__)) from common import * import html as _html from concurrent.futures import ThreadPoolExecutor links = {} for p in sys.argv[1:]: for l in open(p): k, _, v = l.rstrip("\n").partition("\t") links.setdefault(k, v) TRIED = os.environ.get("CACHE", "cache") + "/paste_tried.txt" tried = set(open(TRIED).read().split()) if os.path.exists(TRIED) else set() tf = open(TRIED, "a"); tlock = threading.Lock() def mark(link): with tlock: tf.write(link + "\n"); tf.flush() W = {"factoriobin": Writer("factoriobin"), "pastebin": Writer("pastebin")} def strings(text): out = extract_bps(text) return out or extract_bps(re.sub(r"\s+", "", text)) def one(item): link, via = item if link in tried: return try: fetch(link, via) finally: mark(link) def fetch(link, via): if "factoriobin.com" in link: pid = link.rsplit("/", 1)[1]; w = W["factoriobin"] if pid in w.seen: return page = get(f"https://factoriobin.com/post/{pid}") if not page: return page = page.decode("utf-8", "replace") cdn = re.search(r'https://cdn\.factoriobin\.com/[^"]+-0\.txt', page) or re.search(r'https://cdn\.factoriobin\.com/[^"]+\.txt', page) t = get(cdn.group(0)) if cdn else None m = re.search(r"(.*?)", page, re.S) for s in strings(t.decode("utf-8", "replace")) if t else []: w.write({"id": pid, "url": f"https://factoriobin.com/post/{pid}", "title": m and _html.unescape(m.group(1)).strip(), "via": via, "string": s}); break elif "pastebin.com" in link: pid = link.rsplit("/", 1)[1]; w = W["pastebin"] if f"{pid}-0" in w.seen: return t = get(f"https://pastebin.com/raw/{pid}") time.sleep(1) for k, s in enumerate(strings(t.decode("utf-8", "replace")) if t else []): w.write({"id": f"{pid}-{k}", "url": f"https://pastebin.com/{pid}", "via": via, "string": s}) t0 = time.time() with ThreadPoolExecutor(4) as ex: list(ex.map(one, links.items())) for w in W.values(): w.close() print("DONE", {k: (w.n, len(w.seen)) for k, w in W.items()}, "links", len(links), round(time.time() - t0), "s", flush=True)