Make project defaults reusable

This commit is contained in:
Your Name
2026-08-03 15:50:45 +05:00
parent 2358e9d660
commit 6ae61b0302
11 changed files with 116 additions and 29 deletions
+9 -6
View File
@@ -42,8 +42,9 @@ def parse_args() -> argparse.Namespace:
group.add_argument("--ids", help="Comma-separated raw_post ids")
group.add_argument("--all-ready", action="store_true", help="Publish every photo-ready post not yet published to site")
group.add_argument("--all-ready-videos", action="store_true", help="Publish video-only ready posts")
parser.add_argument("--ghost-url", default="https://f-n8.ru")
parser.add_argument("--key-file", default="/opt/vk-parser/.site-poster-key")
parser.add_argument("--ghost-url", required=True)
parser.add_argument("--key-file", default="")
parser.add_argument("--fallback-title", default="Новость")
parser.add_argument("--limit", type=int, default=0, help="Maximum posts to publish in this run; 0 means unlimited")
parser.add_argument("--retry-failed", action="store_true", help="Include previous site publish_failed rows")
return parser.parse_args()
@@ -63,11 +64,11 @@ def ghost_token(key: str) -> str:
return f"{header}.{payload}.{signature}"
def split_post_text(value: str) -> tuple[str, str]:
def split_post_text(value: str, fallback_title: str) -> tuple[str, str]:
lines = [line.strip() for line in str(value or "").replace("\r", "").split("\n")]
while lines and not lines[0]:
lines.pop(0)
title = lines.pop(0) if lines else "Новость Forma N8"
title = lines.pop(0) if lines else fallback_title
while lines and (not lines[0] or SEPARATOR_RE.fullmatch(lines[0])):
lines.pop(0)
while lines and (not lines[-1] or HASHTAG_LINE_RE.fullmatch(lines[-1])):
@@ -296,6 +297,8 @@ async def main() -> None:
args = parse_args()
key = os.environ.get("SITE_POSTER_GHOST_ADMIN_KEY", "").strip()
if not key:
if not args.key_file:
raise RuntimeError("SITE_POSTER_GHOST_ADMIN_KEY or --key-file is required")
key = Path(args.key_file).read_text(encoding="utf-8").strip()
pool = await get_pool()
if args.all_ready or args.all_ready_videos:
@@ -452,7 +455,7 @@ async def main() -> None:
feature = uploaded_photos[0] if uploaded_photos else thumbnail
if not feature:
raise RuntimeError("no downloadable site media")
title, body = split_post_text(post.get("final_text") or post.get("rewritten_text") or "")
title, body = split_post_text(post.get("final_text") or post.get("rewritten_text") or "", args.fallback_title)
producer = str(post.get("source_name") or "").strip()
content = gallery_html(uploaded_photos[1:], title) + videos_html(uploaded_videos, title, feature["url"]) + paragraphs(body)
attachments = uploaded_photos + uploaded_videos + ([thumbnail] if thumbnail else [])
@@ -513,7 +516,7 @@ def run_locked() -> None:
if fcntl is None:
asyncio.run(main())
return
with Path("/tmp/forma-n8-site-poster.lock").open("w") as lock_file:
with Path("/tmp/site-poster.lock").open("w") as lock_file:
try:
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError: