import sys; sys.path.insert(0, __import__('os').path.dirname(__file__)) from common import * import html as _html from concurrent.futures import ThreadPoolExecutor H = "https://forums.factorio.com" def search(prefix): topics, start, total = [], 0, None while True: b = get(f"{H}/search.php?keywords={prefix}%2A&sr=topics&sk=t&sd=d&start={start}") if not b: break s = b.decode("utf-8", "replace") if total is None: m = re.search(r"Search found (\d+) match", s); total = int(m.group(1)) if m else 0 ts = re.findall(r"viewtopic\.php\?t=(\d+)", s) if not ts: break topics += ts; start += 25 if start >= total: break time.sleep(1) print(prefix, total, len(set(topics)), flush=True) return topics TP = os.environ.get("CACHE", "cache") + "/forum_topics.json" if os.path.exists(TP): topics = json.load(open(TP)) else: topics = list(dict.fromkeys(t for p in ["0eN", "0eJ", "0eF", "0eA"] for t in search(p))) json.dump(topics, open(TP, "w")) w = Writer("factorio-forums") done_topics = {i.split("-")[0] for i in w.seen} todo = [t for t in topics if t not in done_topics] print(len(topics), "topics", len(todo), "todo", flush=True) POST_RE = re.compile(r'
]*>(.*?)', s); title = _html.unescape(m.group(1)) if m else None posts = [(pid, body) for pid, body in POST_RE.findall(s) if pid not in seen_posts] if not posts: break for pid, body in posts: seen_posts.add(pid) au = re.search(r'class="username(?:-coloured)?"[^>]*>([^<]+)', body) tm = re.search(r'