|
16 | 16 | import sys |
17 | 17 | from pathlib import Path |
18 | 18 |
|
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 |
20 | 20 |
|
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 |
28 | 26 |
|
29 | 27 |
|
30 | | -def load_done() -> set: |
| 28 | +def load_ids() -> set: |
31 | 29 | 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"))) |
37 | 32 | return done |
38 | 33 |
|
39 | 34 |
|
40 | 35 | 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") |
44 | 43 | 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) |
46 | 46 | # 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" |
48 | 48 | if posts_path.exists(): |
49 | 49 | posts_path.unlink() |
50 | 50 |
|
@@ -105,8 +105,8 @@ def is_blacklisted_url(url: str) -> bool: |
105 | 105 | def is_content_url(url: str) -> bool: |
106 | 106 | return url.startswith("http") and not is_image_url(url) and not is_video_url(url) and not is_blacklisted_url(url) |
107 | 107 |
|
108 | | - done = load_done() |
109 | | - done_urls = load_done_urls() |
| 108 | + done = load_ids() |
| 109 | + done_urls = load_urls() |
110 | 110 | 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", ""))] |
111 | 111 | print(f"Already processed: {len(eligible) - len(to_process)}, remaining: {len(to_process)}") |
112 | 112 |
|
|
0 commit comments