-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathapp.py
More file actions
154 lines (121 loc) · 4.37 KB
/
Copy pathapp.py
File metadata and controls
154 lines (121 loc) · 4.37 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2026 ForgeRSS Contributors
# Licensed under AGPL-3.0
"""
ForgeRSS - Main Application Entry
Supports two modes:
1. Single run: python app.py --once
2. Daemon mode: python app.py (default, with scheduler)
"""
import argparse
import logging
import os
import sys
import time
from datetime import datetime
from pathlib import Path
# ??????
from dotenv import load_dotenv
# ?????????? .env
env_path = Path(__file__).parent / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=True)
else:
# ???? .env.example ????????????????
example_path = Path(__file__).parent / ".env.example"
if example_path.exists():
load_dotenv(dotenv_path=example_path, override=False)
# ????
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(
level=getattr(logging, log_level, logging.INFO),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("forgerss")
def get_config():
"""????????????"""
return {
"max_articles": int(os.getenv("MAX_ARTICLES", "50")),
"run_interval": int(os.getenv("RUN_INTERVAL", "3600")), # ??1??
"full_refresh": os.getenv("FULL_REFRESH", "false").lower() == "true",
"use_db": os.getenv("USE_DB", "true").lower() == "true",
}
def run_all_generators(config: dict) -> dict:
"""???????"""
from scripts.run_all import GENERATORS
results = {}
for name, generator_class in GENERATORS.items():
logger.info(f"Running generator: {name}")
try:
generator = generator_class()
success = generator.run(
full_refresh=config["full_refresh"],
max_articles=config["max_articles"],
use_db=config["use_db"]
)
results[name] = "success" if success else "failed"
logger.info(f"Generator {name}: {'SUCCESS' if success else 'FAILED'}")
except Exception as e:
logger.error(f"Generator {name} error: {e}", exc_info=True)
results[name] = "error"
return results
def run_once():
"""??????"""
logger.info("ForgeRSS starting (single run mode)")
config = get_config()
start_time = time.time()
results = run_all_generators(config)
elapsed = time.time() - start_time
# ????
success_count = sum(1 for v in results.values() if v == "success")
total_count = len(results)
logger.info(f"Completed: {success_count}/{total_count} generators succeeded in {elapsed:.1f}s")
return all(v == "success" for v in results.values())
def run_daemon():
"""?????????????"""
logger.info("ForgeRSS starting (daemon mode)")
config = get_config()
interval = config["run_interval"]
logger.info(f"Run interval: {interval} seconds ({interval/3600:.1f} hours)")
while True:
try:
logger.info(f"Starting scheduled run at {datetime.now().isoformat()}")
start_time = time.time()
results = run_all_generators(config)
elapsed = time.time() - start_time
success_count = sum(1 for v in results.values() if v == "success")
total_count = len(results)
logger.info(f"Run completed: {success_count}/{total_count} in {elapsed:.1f}s")
logger.info(f"Next run in {interval} seconds...")
time.sleep(interval)
except KeyboardInterrupt:
logger.info("Received interrupt, shutting down...")
break
except Exception as e:
logger.error(f"Unexpected error: {e}", exc_info=True)
logger.info(f"Retrying in {interval} seconds...")
time.sleep(interval)
def main():
parser = argparse.ArgumentParser(description="ForgeRSS - RSS Feed Generator")
parser.add_argument(
"--once",
action="store_true",
help="Run once and exit (default: daemon mode with scheduler)"
)
parser.add_argument(
"--full",
action="store_true",
help="Full refresh (ignore cache)"
)
args = parser.parse_args()
# ??????
if args.full:
os.environ["FULL_REFRESH"] = "true"
if args.once:
success = run_once()
sys.exit(0 if success else 1)
else:
run_daemon()
if __name__ == "__main__":
main()