Make project defaults reusable
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import {createHmac} from "node:crypto";
|
||||
import {readFileSync} from "node:fs";
|
||||
|
||||
const baseUrl = "https://f-n8.ru";
|
||||
const [keyId, secret] = readFileSync("/root/ghost-admin-api-key", "utf8").trim().split(":");
|
||||
const baseUrl = (process.env.GHOST_URL || "").replace(/\/$/, "");
|
||||
const keyFile = process.env.GHOST_ADMIN_KEY_FILE || "";
|
||||
if (!baseUrl || !keyFile) {
|
||||
throw new Error("GHOST_URL and GHOST_ADMIN_KEY_FILE are required");
|
||||
}
|
||||
const [keyId, secret] = readFileSync(keyFile, "utf8").trim().split(":");
|
||||
const encode = (value) => Buffer.from(JSON.stringify(value)).toString("base64url");
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const unsigned = `${encode({alg: "HS256", typ: "JWT", kid: keyId})}.${encode({iat: now, exp: now + 300, aud: "/admin/"})}`;
|
||||
|
||||
Reference in New Issue
Block a user