Skip to content

Commit 5384079

Browse files
committed
switch to ids.txt and urls.txt
1 parent 8b0367b commit 5384079

5 files changed

Lines changed: 54 additions & 37 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# Custom
77
.DS_Store
8+
scripts/*.txt
89
all-technologies.json
910
chat*.json
1011
QueryTechnology.json

scripts/process_posts.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,35 @@
1616
import sys
1717
from pathlib import Path
1818

19-
from utils import MIN_HN_POINTS, MIN_REDDIT_POINTS, SCRIPT_DIR, PYTHON
19+
from utils import MIN_HN_POINTS, MIN_REDDIT_POINTS, POSTS_DIR, SCRIPT_DIR, FAILED_DIR, PYTHON, append_to_file, file_set
2020

21-
def load_done_urls() -> set:
22-
urls = set()
23-
for d in ["completed", "failed"]:
24-
urls_path = Path(SCRIPT_DIR) / d / "urls.txt"
25-
if urls_path.exists():
26-
urls.update(line.strip().rstrip('/') for line in urls_path.read_text().splitlines() if line.strip())
27-
return urls
21+
def load_urls() -> set:
22+
done = set()
23+
done.update(file_set(os.path.join(SCRIPT_DIR, "urls_completed.txt")))
24+
done.update(file_set(os.path.join(SCRIPT_DIR, "urls_failed.txt")))
25+
return done
2826

2927

30-
def load_done() -> set:
28+
def load_ids() -> set:
3129
done = set()
32-
for d in ["posts", "completed", "failed"]:
33-
dir_path = Path(SCRIPT_DIR) / d
34-
if dir_path.is_dir():
35-
for f in dir_path.glob("*.json"):
36-
done.add(f.stem)
30+
done.update(file_set(os.path.join(SCRIPT_DIR, "ids_completed.txt")))
31+
done.update(file_set(os.path.join(SCRIPT_DIR, "ids_failed.txt")))
3732
return done
3833

3934

4035
def mark_failed(post: dict, error_msg: str):
41-
failed_dir = Path(SCRIPT_DIR) / "failed"
42-
failed_dir.mkdir(exist_ok=True)
43-
failed_path = failed_dir / f"{post['id']}.json"
36+
post_id = str(post["id"])
37+
append_to_file(os.path.join(SCRIPT_DIR, "ids_failed.txt"), post_id)
38+
post_url = post.get("url", "")
39+
if post_url:
40+
append_to_file(os.path.join(SCRIPT_DIR, "urls_failed.txt"), post_url.rstrip('/'))
41+
42+
failed_path = os.path.join(FAILED_DIR, f"{post_id}.json")
4443
failed_data = {**post, "error": error_msg}
45-
failed_path.write_text(json.dumps(failed_data, indent=2))
44+
with open(failed_path, "w") as f:
45+
json.dump(failed_data, f, indent=2)
4646
# Remove from posts if it exists there
47-
posts_path = Path(SCRIPT_DIR) / "posts" / f"{post['id']}.json"
47+
posts_path = Path(POSTS_DIR) / f"{post_id}.json"
4848
if posts_path.exists():
4949
posts_path.unlink()
5050

@@ -105,8 +105,8 @@ def is_blacklisted_url(url: str) -> bool:
105105
def is_content_url(url: str) -> bool:
106106
return url.startswith("http") and not is_image_url(url) and not is_video_url(url) and not is_blacklisted_url(url)
107107

108-
done = load_done()
109-
done_urls = load_done_urls()
108+
done = load_ids()
109+
done_urls = load_urls()
110110
to_process = [p for p in eligible if str(p["id"]) not in done and p.get("url", "").rstrip('/') not in done_urls and is_content_url(p.get("url", ""))]
111111
print(f"Already processed: {len(eligible) - len(to_process)}, remaining: {len(to_process)}")
112112

scripts/publish_posts.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
from pathlib import Path
1717

1818
import requests
19-
from utils import TECHSTACKS_BASE, SCRIPT_DIR, create_cookie_jar
20-
21-
POSTS_DIR = os.path.join(SCRIPT_DIR, "posts")
22-
COMPLETED_DIR = os.path.join(SCRIPT_DIR, "completed")
23-
FAILED_DIR = os.path.join(SCRIPT_DIR, "failed")
19+
from utils import TECHSTACKS_BASE, SCRIPT_DIR, POSTS_DIR, COMPLETED_DIR, FAILED_DIR, create_cookie_jar, append_to_file
2420

2521
IMPORT_POST_URL = f"{TECHSTACKS_BASE}/api/ImportNewsPost"
2622
SYNC_POST_URL = f"{TECHSTACKS_BASE}/api/SyncStats"
@@ -41,11 +37,18 @@ def import_post(post_file):
4137
verify=False,
4238
)
4339

40+
post_url = post_data.get("url", "")
41+
4442
if resp.ok:
4543
print(f" Success: {resp.status_code}")
4644
os.makedirs(COMPLETED_DIR, exist_ok=True)
4745
dest = os.path.join(COMPLETED_DIR, f"{post_id}.json")
4846
shutil.move(post_file, dest)
47+
48+
append_to_file(os.path.join(SCRIPT_DIR, "ids_completed.txt"), str(post_id))
49+
if post_url:
50+
append_to_file(os.path.join(SCRIPT_DIR, "urls_completed.txt"), post_url.rstrip('/'))
51+
4952
print(f" Moved to {dest}")
5053
return True
5154
else:
@@ -57,6 +60,11 @@ def import_post(post_file):
5760
with open(failed_dest, "w") as f:
5861
json.dump(post_data, f, indent=2)
5962
os.remove(post_file)
63+
64+
append_to_file(os.path.join(SCRIPT_DIR, "ids_failed.txt"), str(post_id))
65+
if post_url:
66+
append_to_file(os.path.join(SCRIPT_DIR, "urls_failed.txt"), post_url.rstrip('/'))
67+
6068
print(f" Moved to {failed_dest}")
6169
return False
6270

scripts/update.sh

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
#!/bin/bash
22

3-
echo "Updating data and indexes..."
4-
pushd completed
5-
./update.sh
6-
popd
7-
8-
echo "Updating failed urls index..."
9-
pushd failed
10-
./update.sh
11-
popd
12-
133
echo "Updating technology data..."
144
pushd data
155
./update.sh
16-
popd
6+
popd

scripts/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
PYTHON = sys.executable
2323
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
24+
POSTS_DIR = os.path.join(SCRIPT_DIR, "posts")
25+
COMPLETED_DIR = os.path.join(SCRIPT_DIR, "done", "completed")
26+
FAILED_DIR = os.path.join(SCRIPT_DIR, "done", "failed")
27+
2428
REPO_ROOT = os.path.dirname(os.path.dirname(SCRIPT_DIR)) # llms repo root
2529
LLMS_SH = shutil.which("llms")
2630
LLMS_MODEL = os.getenv("LLMS_MODEL", "MiniMax-M2.1")
@@ -30,6 +34,20 @@
3034
if not LLMS_SH:
3135
raise RuntimeError("llms command not found in PATH. Please ensure llms is installed and available.")
3236

37+
def file_set(ids_file):
38+
"""Return a set of post IDs from an ids_*.txt file."""
39+
if os.path.exists(ids_file):
40+
with open(ids_file, "r") as f:
41+
return set(line.strip() for line in f)
42+
return set()
43+
44+
def append_to_file(file_path, symbol: str):
45+
"""Append a post ID to ids_*.txt if not already present."""
46+
existing_symbols = file_set(file_path)
47+
if symbol not in existing_symbols:
48+
with open(file_path, "a") as f:
49+
f.write(f"{symbol}\n")
50+
3351
def create_cookie_jar():
3452
parsed = urlparse(TECHSTACKS_BASE)
3553
jar = requests.cookies.RequestsCookieJar()

0 commit comments

Comments
 (0)