-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (67 loc) · 2.22 KB
/
Copy pathmain.py
File metadata and controls
78 lines (67 loc) · 2.22 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import argparse
import asyncio
import sys
from datetime import date
from pathlib import Path
from infrastructure.content_extractor import NewspaperExtractor
from infrastructure.hn_client import HNClient
from infrastructure.ollama_client import OllamaClient, check_ollama_installed
from interface.cli import run_cli, run_markdown
from usecases.summarize_article import SummarizeArticle
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="hacker-digest",
description="Fetch top Hacker News stories and summarize them with Ollama",
)
parser.add_argument(
"--limit",
type=int,
default=10,
help="Number of stories to fetch (default: 10, 30 for markdown)",
)
parser.add_argument(
"--model",
type=str,
default="gemma2:2b",
help="Ollama model to use (default: gemma2:2b)",
)
parser.add_argument(
"--markdown",
action="store_true",
help="Output in markdown format to a file with today's date",
)
parser.add_argument(
"--output-dir",
type=str,
default="digests",
help="Directory to save markdown files (default: digests)",
)
return parser.parse_args()
async def main() -> None:
args = parse_args()
check_ollama_installed()
limit = args.limit if args.limit != 10 else 30 if args.markdown else 10
hn_client = HNClient()
ollama_client = OllamaClient(model=args.model)
await ollama_client.ensure_model()
content_extractor = NewspaperExtractor()
summarize_article = SummarizeArticle(
hn_client, ollama_client, content_extractor
)
try:
if args.markdown:
output_dir = Path(args.output_dir)
output_dir.mkdir(exist_ok=True)
filename = output_dir / f"hacker-digest-{date.today()}.md"
await run_markdown(summarize_article, limit, str(filename))
print(f"Saved to {filename}")
else:
await run_cli(summarize_article, limit)
except KeyboardInterrupt:
print("\n\nInterrupted by user")
sys.exit(1)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())