factorio-blueprints / scripts /blueprints /reddit_comments.py
beyarkay's picture
Blueprints from 11 sites + 31 save files
02f8da8 verified
Raw History Blame Contribute Delete
1.57 kB
# Dump posts (all fields needed for bp extraction) via Arctic Shift. argv: cache_dir name query-params...
import sys; sys.path.insert(0, __import__('os').path.dirname(__file__))
from common import *
A = "https://arctic-shift.photon-reddit.com/api/comments/search"
cache, name, q = sys.argv[1], sys.argv[2], sys.argv[3]
path = os.path.join(cache, f"comments_{name}.jsonl")
after, seen = "2005-01-01", set()
if os.path.exists(path):
for l in open(path):
try: d = json.loads(l)
except Exception: continue
seen.add(d["id"]); after = max(after, d["created_utc"]) if isinstance(after, int) else d["created_utc"]
f = open(path, "a"); t = time.time()
F = "id,created_utc,body,author,link_id,parent_id,subreddit"
while True:
d = None
for i in range(8):
b = get(f"{A}?{q}&limit=auto&sort=asc&after={after}&fields={F}", tries=3, timeout=120, ok404=False)
try: d = json.loads(b) if b else None
except Exception: d = None
if d and d.get("data") is not None: break
time.sleep(5 * (i + 1))
if not d or d.get("data") is None: print("FAILED at", after, flush=True); break
rows = [r for r in d["data"] if r["id"] not in seen]
if not d["data"] or not rows and len(d["data"]) < 100: break
for r in rows: seen.add(r["id"]); f.write(json.dumps(r) + "\n")
f.flush()
after = d["data"][-1]["created_utc"]
if not rows: after += 1
print(len(seen), time.strftime("%Y-%m-%d", time.gmtime(after)), round(time.time() - t), flush=True)
print("DONE", len(seen), round(time.time() - t), flush=True)