File: //proc/thread-self/root/workspace/delete_extra_from_file.py
#!/usr/bin/env python3
from __future__ import annotations
import json
import subprocess
import time
from pathlib import Path
WRAPPER = "/root/clawd/skills/x-twitter/scripts/x_twitter.py"
PROFILE = "datathrop"
IN = Path("/workspace/extra_missing.jsonl")
PROGRESS = Path("/workspace/datathrop_progress.json")
if not IN.exists():
print("No extra file")
raise SystemExit(0)
rows = []
for line in IN.read_text(encoding="utf-8").splitlines():
try:
rows.append(json.loads(line))
except Exception:
continue
print(f"Loaded {len(rows)} candidate rows")
progress = json.loads(PROGRESS.read_text(encoding="utf-8")) if PROGRESS.exists() else {"found":0,"deleted":0,"failed":0,"seen_ids":[]}
ok = 0
fail = 0
fails = []
for idx, t in enumerate(rows, 1):
tid = t.get("id")
text = t.get("text", "")
if not tid:
fail += 1
fails.append("<missing-id>")
continue
cmd = "unretweet" if str(text).startswith("RT @") else "delete"
url = f"https://x.com/datathrop/status/{tid}"
attempt = 0
success = False
while attempt < 3 and not success:
attempt += 1
p = subprocess.run(["python3", WRAPPER, "--profile-key", PROFILE, cmd, url], capture_output=True, text=True, timeout=120)
if p.returncode != 0:
err = (p.stdout + p.stderr).lower()
if "429" in err:
print(f"429 on {tid}, attempt {attempt}/3, wait 60s")
time.sleep(60)
continue
if "not found" in err or "does not exist" in err:
success = True
break
success = False
break
try:
data = json.loads(p.stdout)
if isinstance(data, dict) and data.get("ok"):
success = True
break
except Exception:
if '"status_code": 200' in p.stdout:
success = True
break
if p.returncode == 0:
success = True
break
if success:
ok += 1
print(f"{idx}/{len(rows)} OK {tid}")
else:
fail += 1
fails.append(tid)
print(f"{idx}/{len(rows)} FAIL {tid}")
time.sleep(1)
progress["deleted"] = progress.get("deleted",0) + ok
progress["failed"] = progress.get("failed",0) + fail
PROGRESS.write_text(json.dumps(progress), encoding="utf-8")
print(f"=== delete_from_file done: ok={ok} fail={fail} ===")
if fails:
Path('/workspace/extra_missing_failed.txt').write_text('\n'.join(fails)+"\n", encoding='utf-8')