File size: 1,592 Bytes
02f8da8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # 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/posts/search"
cache, name, q = sys.argv[1], sys.argv[2], sys.argv[3]
path = os.path.join(cache, f"posts_{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,title,selftext,url,author,link_flair_text,num_comments,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)
|