@appeeky/open-slideshow 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +227 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +9 -0
- package/dist/brand.d.ts +55 -0
- package/dist/brand.js +246 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +261 -0
- package/dist/decks.d.ts +44 -0
- package/dist/decks.js +90 -0
- package/dist/fonts.d.ts +45 -0
- package/dist/fonts.js +243 -0
- package/dist/gallery.d.ts +35 -0
- package/dist/gallery.js +109 -0
- package/dist/images.d.ts +49 -0
- package/dist/images.js +223 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +13 -0
- package/dist/lint.d.ts +33 -0
- package/dist/lint.js +181 -0
- package/dist/mcp.d.ts +3 -0
- package/dist/mcp.js +233 -0
- package/dist/publish.d.ts +100 -0
- package/dist/publish.js +401 -0
- package/dist/render.d.ts +73 -0
- package/dist/render.js +515 -0
- package/dist/schedule.d.ts +19 -0
- package/dist/schedule.js +114 -0
- package/dist/schema.d.ts +1004 -0
- package/dist/schema.js +174 -0
- package/dist/shape.d.ts +31 -0
- package/dist/shape.js +169 -0
- package/dist/skill.d.ts +7 -0
- package/dist/skill.js +17 -0
- package/dist/templates.d.ts +44 -0
- package/dist/templates.js +185 -0
- package/dist/text.d.ts +40 -0
- package/dist/text.js +230 -0
- package/dist/themes.d.ts +43 -0
- package/dist/themes.js +202 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/dist/viral.d.ts +12 -0
- package/dist/viral.js +35 -0
- package/dist/workspace.d.ts +80 -0
- package/dist/workspace.js +306 -0
- package/package.json +65 -4
- package/skills/open-slideshow/SKILL.md +52 -0
- package/skills/open-slideshow/references/brand.md +54 -0
- package/skills/open-slideshow/references/channels.md +63 -0
- package/skills/open-slideshow/references/images.md +53 -0
- package/skills/open-slideshow/references/languages.md +41 -0
- package/skills/open-slideshow/references/publishing.md +70 -0
- package/skills/open-slideshow/references/review.md +74 -0
- package/skills/open-slideshow/references/templates.md +59 -0
- package/skills/open-slideshow/references/troubleshooting.md +82 -0
- package/skills/open-slideshow/references/writing.md +76 -0
package/dist/publish.js
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join, relative } from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import YAML from "yaml";
|
|
6
|
+
import { freshRenders } from "./decks.js";
|
|
7
|
+
import { captionText, lintDeck, platformsOf } from "./lint.js";
|
|
8
|
+
import { imageMeta } from "./images.js";
|
|
9
|
+
import { plainText } from "./text.js";
|
|
10
|
+
import { appendHistory, isInside, loadDeck, rendersDir, saveDeck, UserError, writeAtomic } from "./workspace.js";
|
|
11
|
+
const run = promisify(execFile);
|
|
12
|
+
export const APPEEKY_API_URL = "https://api.appeeky.com/v1";
|
|
13
|
+
/** Postiz Cloud. A self-hosted Postiz is https://<your-domain>/api/public/v1, set in POSTIZ_API_URL. */
|
|
14
|
+
export const POSTIZ_API_URL = "https://api.postiz.com/public/v1";
|
|
15
|
+
const PRIVACY = { public: "PUBLIC_TO_EVERYONE", friends: "MUTUAL_FOLLOW_FRIENDS", followers: "FOLLOWER_OF_CREATOR", only_me: "SELF_ONLY" };
|
|
16
|
+
const DISCLOSURE = { none: "none", own_brand: "brand_organic", paid_partnership: "brand_content" };
|
|
17
|
+
function credits(ws, deck) {
|
|
18
|
+
const lines = [];
|
|
19
|
+
deck.slides.forEach((s, i) => {
|
|
20
|
+
if (!s.image)
|
|
21
|
+
return;
|
|
22
|
+
const m = imageMeta(ws, s.image);
|
|
23
|
+
if (!m || m.source === "local" || m.license === "own")
|
|
24
|
+
return;
|
|
25
|
+
lines.push(`Slide ${i + 1}: ${m.attribution ?? `${m.author ?? "unknown author"} (${m.source}, ${m.license})`}${m.landing_url ? ` ${m.landing_url}` : ""}`);
|
|
26
|
+
});
|
|
27
|
+
return lines.join("\n");
|
|
28
|
+
}
|
|
29
|
+
function whenOf(deck, o) {
|
|
30
|
+
const when = o.when ?? deck.when;
|
|
31
|
+
if (!when)
|
|
32
|
+
return undefined;
|
|
33
|
+
const t = Date.parse(when);
|
|
34
|
+
if (Number.isNaN(t))
|
|
35
|
+
throw new UserError(`"${when}" is not a date and time; use ISO, e.g. 2026-12-08T18:30:00+03:00`);
|
|
36
|
+
if (t < Date.now() + 60_000)
|
|
37
|
+
throw new UserError(`${when} is in the past (or less than a minute away); pick a later time or leave it out to post now`);
|
|
38
|
+
if (!/[zZ]|[+-]\d\d:?\d\d$/.test(when))
|
|
39
|
+
throw new UserError(`"${when}" has no time zone; add one, e.g. +03:00 or Z`);
|
|
40
|
+
return new Date(t).toISOString();
|
|
41
|
+
}
|
|
42
|
+
export function tiktokExtra(deck, t) {
|
|
43
|
+
const cover = deck.slides.find((s) => s.kind === "cover") ?? deck.slides[0];
|
|
44
|
+
const title = cover?.title ? plainText(cover.title).slice(0, 90) : undefined;
|
|
45
|
+
const out = {
|
|
46
|
+
privacyLevel: t?.privacy ? PRIVACY[t.privacy] : undefined,
|
|
47
|
+
commercialContentType: t?.disclosure ? DISCLOSURE[t.disclosure] : undefined,
|
|
48
|
+
draft: t?.inbox_draft,
|
|
49
|
+
autoAddMusic: t?.auto_music,
|
|
50
|
+
allowComment: t?.comments,
|
|
51
|
+
title,
|
|
52
|
+
photoCoverIndex: t?.cover_index,
|
|
53
|
+
};
|
|
54
|
+
for (const k of Object.keys(out))
|
|
55
|
+
if (out[k] === undefined)
|
|
56
|
+
delete out[k];
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
function alreadyPublished(deck, provider, hash) {
|
|
60
|
+
return deck.published.find((p) => p.provider === provider && p.render_hash === hash && (p.post_id || p.draft_file));
|
|
61
|
+
}
|
|
62
|
+
export async function publishDeck(ws, id, o) {
|
|
63
|
+
let deck = loadDeck(ws, id);
|
|
64
|
+
const manifest = freshRenders(ws, deck);
|
|
65
|
+
const lint = await lintDeck(ws, deck);
|
|
66
|
+
if (!lint.ok && o.provider !== "export")
|
|
67
|
+
throw new UserError(`fix these first:\n${lint.errors.map((e) => `- ${e.message}`).join("\n")}`);
|
|
68
|
+
const files = manifest.files.map((f) => join(rendersDir(ws, id), f));
|
|
69
|
+
const caption = captionText(deck);
|
|
70
|
+
const credit = credits(ws, deck);
|
|
71
|
+
if (o.provider === "export") {
|
|
72
|
+
const out = o.out ?? join(ws.dir, "exports", id);
|
|
73
|
+
const preview = { folder: out, images: files.length, caption_chars: caption.length };
|
|
74
|
+
if (!o.confirm)
|
|
75
|
+
return { provider: "export", done: false, preview, message: `would copy ${files.length} images, caption.txt and credits.txt to ${out}; call again with confirm` };
|
|
76
|
+
mkdirSync(out, { recursive: true });
|
|
77
|
+
const written = manifest.files.map((f) => {
|
|
78
|
+
copyFileSync(join(rendersDir(ws, id), f), join(out, f));
|
|
79
|
+
return join(out, f);
|
|
80
|
+
});
|
|
81
|
+
writeAtomic(join(out, "caption.txt"), `${caption}\n`);
|
|
82
|
+
if (credit)
|
|
83
|
+
writeAtomic(join(out, "credits.txt"), `${credit}\n`);
|
|
84
|
+
return { provider: "export", done: true, preview, files: written, message: `exported to ${out}` };
|
|
85
|
+
}
|
|
86
|
+
if (o.provider === "photos") {
|
|
87
|
+
if (process.platform !== "darwin")
|
|
88
|
+
throw new UserError("adding to Photos works on macOS only; use export");
|
|
89
|
+
const album = `open-slideshow ${id}`;
|
|
90
|
+
if (!o.confirm)
|
|
91
|
+
return { provider: "photos", done: false, preview: { album, images: files.length }, message: `would add ${files.length} images to the Photos album "${album}"; call again with confirm` };
|
|
92
|
+
// The files become aliases before Photos is told anything: inside the tell block Photos itself would try to
|
|
93
|
+
// read the POSIX file and fail (-1728).
|
|
94
|
+
const script = [
|
|
95
|
+
"on run argv",
|
|
96
|
+
"set albumName to item 1 of argv",
|
|
97
|
+
"set theFiles to {}",
|
|
98
|
+
"repeat with i from 2 to count of argv",
|
|
99
|
+
"set end of theFiles to (POSIX file (item i of argv)) as alias",
|
|
100
|
+
"end repeat",
|
|
101
|
+
'tell application "Photos"',
|
|
102
|
+
"if not (exists album albumName) then make new album named albumName",
|
|
103
|
+
"import theFiles into album albumName skip check duplicates true",
|
|
104
|
+
"end tell",
|
|
105
|
+
"end run",
|
|
106
|
+
].join("\n");
|
|
107
|
+
try {
|
|
108
|
+
await run("osascript", ["-e", script, album, ...files], { timeout: 120_000 });
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
const why = String(e.stderr ?? e.message).trim().split("\n").pop() ?? "";
|
|
112
|
+
if (/-1743|not authori[sz]ed|not allowed/i.test(why))
|
|
113
|
+
throw new UserError("Photos did not allow it: allow your terminal (or agent app) under System Settings → Privacy & Security → Automation → Photos, then try again");
|
|
114
|
+
throw new UserError(`could not add the images to Photos: ${why}`);
|
|
115
|
+
}
|
|
116
|
+
return { provider: "photos", done: true, preview: { album }, message: `added to Photos album "${album}"; open TikTok on your phone and pick them, the caption is in the deck` };
|
|
117
|
+
}
|
|
118
|
+
if (o.provider === "spec-draft" && ws.mode !== "spec")
|
|
119
|
+
throw new UserError("spec-draft works inside an Appeeky Spec repo; here use appeeky or export");
|
|
120
|
+
const prev = alreadyPublished(deck, o.provider, manifest.hash);
|
|
121
|
+
if (prev && !o.again)
|
|
122
|
+
throw new UserError(`this exact deck was already ${o.provider === "spec-draft" ? `drafted (${prev.draft_file})` : `sent (post ${prev.post_id})`} at ${prev.at}; pass again to do it twice`);
|
|
123
|
+
const when = whenOf(deck, o);
|
|
124
|
+
const accounts = o.accounts ?? deck.accounts;
|
|
125
|
+
if (!accounts.length)
|
|
126
|
+
throw new UserError("no accounts: set the deck's accounts, e.g. [\"tiktok\"] or [\"instagram:@yourhandle\"]");
|
|
127
|
+
if (o.provider === "spec-draft") {
|
|
128
|
+
if (ws.mode !== "spec" || !ws.spec)
|
|
129
|
+
throw new UserError("spec-draft works inside an Appeeky Spec repo; here use appeeky or export");
|
|
130
|
+
const file = join(ws.spec.drafts_dir, `${id}.yaml`);
|
|
131
|
+
if (existsSync(file)) {
|
|
132
|
+
const existing = YAML.parse(readFileSync(file, "utf8"));
|
|
133
|
+
if (existing?.status === "sent")
|
|
134
|
+
throw new UserError(`${relative(ws.root, file)} was already sent by Spec; make a new deck`);
|
|
135
|
+
if (!o.again)
|
|
136
|
+
throw new UserError(`${relative(ws.root, file)} already exists; pass again to replace it`);
|
|
137
|
+
}
|
|
138
|
+
const draft = {
|
|
139
|
+
accounts,
|
|
140
|
+
text: caption,
|
|
141
|
+
...(when ? { when } : {}),
|
|
142
|
+
media: files.map((f) => relative(ws.root, f).split("\\").join("/")),
|
|
143
|
+
...(platformsOf({ ...deck, accounts }).has("tiktok") || accounts.some((a) => a.includes("tiktok")) ? { tiktok: { ...deck.tiktok, title: tiktokExtra(deck, deck.tiktok).title } } : {}),
|
|
144
|
+
why: `open-slideshow deck ${id}${deck.topic ? `: ${deck.topic}` : ""}${credit ? `\nImage credits:\n${credit}` : ""}`,
|
|
145
|
+
status: "draft",
|
|
146
|
+
};
|
|
147
|
+
for (const [k, v] of Object.entries(draft.tiktok ?? {}))
|
|
148
|
+
if (v === undefined)
|
|
149
|
+
delete draft.tiktok[k];
|
|
150
|
+
const preview = { file: relative(ws.root, file), ...draft };
|
|
151
|
+
if (!o.confirm)
|
|
152
|
+
return { provider: "spec-draft", done: false, preview, message: `would write ${relative(ws.root, file)}; Spec sends it with \`appeeky-spec social push\` after review. Call again with confirm` };
|
|
153
|
+
if (!isInside(ws.root, file))
|
|
154
|
+
throw new UserError("draft path escapes the repo");
|
|
155
|
+
writeAtomic(file, YAML.stringify(draft, { lineWidth: 0 }));
|
|
156
|
+
deck = { ...deck, status: "published", published: [...deck.published, { provider: "spec-draft", at: new Date().toISOString(), render_hash: manifest.hash, draft_file: relative(ws.root, file), ...(when ? { scheduled_for: when } : {}) }] };
|
|
157
|
+
saveDeck(ws, deck);
|
|
158
|
+
appendHistory(ws, { deck: id, channel: deck.channel, hook: hookOf(deck), topic: deck.topic, at: new Date().toISOString(), provider: "spec-draft", scheduled_for: when });
|
|
159
|
+
return { provider: "spec-draft", done: true, preview, draft_file: relative(ws.root, file), message: `wrote ${relative(ws.root, file)}; open a pull request, and \`appeeky-spec social push\` sends it after merge` };
|
|
160
|
+
}
|
|
161
|
+
// appeeky or postiz: the user's own key, the service's public API.
|
|
162
|
+
const provider = o.provider;
|
|
163
|
+
const service = provider === "postiz" ? "Postiz" : "Appeeky";
|
|
164
|
+
const api = provider === "postiz" ? new PostizClient() : new AppeekyClient();
|
|
165
|
+
const resolved = resolveAccounts(accounts, await api.accounts(), service);
|
|
166
|
+
if (resolved.problems.length)
|
|
167
|
+
throw new UserError(resolved.problems.join("\n"));
|
|
168
|
+
const ids = resolved.accounts.map((a) => a.id);
|
|
169
|
+
const tiktok = resolved.accounts.some((a) => a.platform.toLowerCase() === "tiktok");
|
|
170
|
+
const instagram = resolved.accounts.some((a) => a.platform.toLowerCase() === "instagram");
|
|
171
|
+
if (instagram) {
|
|
172
|
+
const ig = await lintDeck(ws, deck, { platforms: ["instagram"] });
|
|
173
|
+
if (!ig.ok)
|
|
174
|
+
throw new UserError(`for Instagram, fix:\n${ig.errors.map((e) => `- ${e.message}`).join("\n")}`);
|
|
175
|
+
}
|
|
176
|
+
const extra = tiktok ? { tiktok: tiktokExtra(deck, deck.tiktok) } : undefined;
|
|
177
|
+
const preview = { accounts: resolved.accounts.map((a) => `${a.platform} ${a.name}`), images: files.length, caption, scheduled_for: when ?? "now", ...(extra ? { extra } : {}) };
|
|
178
|
+
if (!o.confirm)
|
|
179
|
+
return { provider, done: false, preview, message: `would ${when ? `schedule for ${when}` : "post now"} through ${service} to ${preview.accounts.join(", ")}. Show this to the user and call again with confirm only after they say yes` };
|
|
180
|
+
// Resume uploads after a failure: keep what went up.
|
|
181
|
+
const record = deck.published.find((p) => p.provider === provider && p.render_hash === manifest.hash && !p.post_id) ?? { provider, at: new Date().toISOString(), render_hash: manifest.hash, media: {} };
|
|
182
|
+
if (!deck.published.includes(record))
|
|
183
|
+
deck.published.push(record);
|
|
184
|
+
record.media ??= {};
|
|
185
|
+
for (const f of manifest.files) {
|
|
186
|
+
if (record.media[f])
|
|
187
|
+
continue;
|
|
188
|
+
record.media[f] = await api.upload(join(rendersDir(ws, id), f));
|
|
189
|
+
saveDeck(ws, deck);
|
|
190
|
+
}
|
|
191
|
+
const media = manifest.files.map((f) => record.media[f]);
|
|
192
|
+
const post = api instanceof PostizClient
|
|
193
|
+
? await api.createPost(postizBody(deck, resolved.accounts, caption, media, when))
|
|
194
|
+
: await api.createPost({ content: caption, account_ids: ids, media_ids: media, ...(when ? { scheduled_for: when } : {}), ...(extra ? { extra } : {}) });
|
|
195
|
+
record.post_id = post;
|
|
196
|
+
record.at = new Date().toISOString();
|
|
197
|
+
if (when)
|
|
198
|
+
record.scheduled_for = when;
|
|
199
|
+
deck.status = "published";
|
|
200
|
+
saveDeck(ws, deck);
|
|
201
|
+
appendHistory(ws, { deck: id, channel: deck.channel, hook: hookOf(deck), topic: deck.topic, at: record.at, provider, post_id: post, scheduled_for: when });
|
|
202
|
+
return { provider, done: true, preview, post_id: post, message: when ? `scheduled for ${when} in ${service} (post ${post})` : `sent through ${service} (post ${post})` };
|
|
203
|
+
}
|
|
204
|
+
export function hookOf(deck) {
|
|
205
|
+
const cover = deck.slides.find((s) => s.kind === "cover") ?? deck.slides[0];
|
|
206
|
+
return plainText(cover?.title ?? cover?.body ?? deck.id);
|
|
207
|
+
}
|
|
208
|
+
export class AppeekyClient {
|
|
209
|
+
key;
|
|
210
|
+
base;
|
|
211
|
+
constructor(key = process.env.APPEEKY_API_KEY, base = process.env.APPEEKY_API_URL ?? APPEEKY_API_URL) {
|
|
212
|
+
if (!key)
|
|
213
|
+
throw new UserError("set APPEEKY_API_KEY (your key from appeeky.com) to post through Appeeky; without it, use export or photos");
|
|
214
|
+
this.key = key;
|
|
215
|
+
this.base = base.replace(/\/$/, "");
|
|
216
|
+
}
|
|
217
|
+
async call(path, init = {}) {
|
|
218
|
+
const res = await fetch(`${this.base}${path}`, { ...init, headers: { "x-api-key": this.key, ...(init.headers ?? {}) }, signal: AbortSignal.timeout(60_000) });
|
|
219
|
+
const text = await res.text();
|
|
220
|
+
let body = {};
|
|
221
|
+
try {
|
|
222
|
+
body = text ? JSON.parse(text) : {};
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
/* not JSON */
|
|
226
|
+
}
|
|
227
|
+
if (!res.ok) {
|
|
228
|
+
const msg = typeof body.error === "string" ? body.error : body.error?.message ?? body.message ?? `HTTP ${res.status}`;
|
|
229
|
+
if (res.status === 401 || res.status === 403)
|
|
230
|
+
throw new UserError(`Appeeky refused the key: ${msg}`);
|
|
231
|
+
if (res.status === 402)
|
|
232
|
+
throw new UserError(`Appeeky: ${msg} (check your plan or balance)`);
|
|
233
|
+
throw new UserError(`Appeeky ${path}: ${msg}`);
|
|
234
|
+
}
|
|
235
|
+
return (body.data ?? body);
|
|
236
|
+
}
|
|
237
|
+
/** The most liked TikTok posts for a topic (one search from the Appeeky balance). */
|
|
238
|
+
async viral(query, opts = {}) {
|
|
239
|
+
const q = new URLSearchParams({ q: query, kind: opts.kind ?? "slideshow", since: opts.since ?? "quarter", limit: String(opts.limit ?? 20) });
|
|
240
|
+
return this.call(`/viral/search?${q}`);
|
|
241
|
+
}
|
|
242
|
+
async accounts() {
|
|
243
|
+
const rows = await this.call("/connect/social/accounts");
|
|
244
|
+
return (rows ?? []).map((r) => {
|
|
245
|
+
const problem = r.needsReconnect ? "needs reconnecting" : r.isActive === false ? "turned off" : undefined;
|
|
246
|
+
return { id: r.id, platform: r.platform, name: r.displayName || r.platformUserId || r.platform, active: !problem, ...(problem ? { problem } : {}) };
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
async upload(file) {
|
|
250
|
+
const form = new FormData();
|
|
251
|
+
form.append("file", new Blob([readFileSync(file)], { type: "image/jpeg" }), file.split("/").pop());
|
|
252
|
+
const asset = await this.call("/social/media/upload", { method: "POST", body: form });
|
|
253
|
+
const id = asset.id ?? asset.asset?.id;
|
|
254
|
+
if (!id)
|
|
255
|
+
throw new UserError("Appeeky did not return a media id for the upload");
|
|
256
|
+
return id;
|
|
257
|
+
}
|
|
258
|
+
async createPost(body) {
|
|
259
|
+
const post = await this.call("/social/posts", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
260
|
+
const id = post.id ?? post.post?.id;
|
|
261
|
+
if (!id)
|
|
262
|
+
throw new UserError("Appeeky did not return a post id");
|
|
263
|
+
return id;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// ---------- Postiz public API ----------
|
|
267
|
+
/**
|
|
268
|
+
* Postiz, the open-source scheduler, cloud or self-hosted. Its channels are "integrations"; the
|
|
269
|
+
* slideshow goes up as images, then one request schedules it on every chosen channel. The key is
|
|
270
|
+
* the user's own, from Postiz settings; nothing else is sent.
|
|
271
|
+
*/
|
|
272
|
+
export class PostizClient {
|
|
273
|
+
key;
|
|
274
|
+
base;
|
|
275
|
+
constructor(key = process.env.POSTIZ_API_KEY, base = process.env.POSTIZ_API_URL ?? POSTIZ_API_URL) {
|
|
276
|
+
if (!key)
|
|
277
|
+
throw new UserError("set POSTIZ_API_KEY (Postiz: Settings → Public API) to post through Postiz; for a self-hosted Postiz also set POSTIZ_API_URL to https://<your-domain>/api/public/v1");
|
|
278
|
+
this.key = key;
|
|
279
|
+
this.base = base.replace(/\/$/, "");
|
|
280
|
+
}
|
|
281
|
+
async call(path, init = {}) {
|
|
282
|
+
const res = await fetch(`${this.base}${path}`, { ...init, headers: { Authorization: this.key, ...(init.headers ?? {}) }, signal: AbortSignal.timeout(60_000) });
|
|
283
|
+
const text = await res.text();
|
|
284
|
+
let body = {};
|
|
285
|
+
try {
|
|
286
|
+
body = text ? JSON.parse(text) : {};
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
/* not JSON */
|
|
290
|
+
}
|
|
291
|
+
if (!res.ok) {
|
|
292
|
+
const b = body;
|
|
293
|
+
const msg = (Array.isArray(b.message) ? b.message.join("; ") : b.message) ?? b.error ?? `HTTP ${res.status}`;
|
|
294
|
+
if (res.status === 401 || res.status === 403)
|
|
295
|
+
throw new UserError(`Postiz refused the key: ${msg}`);
|
|
296
|
+
if (res.status === 429)
|
|
297
|
+
throw new UserError("Postiz: too many posts this hour (its public API allows a limited number per hour); try again later");
|
|
298
|
+
throw new UserError(`Postiz ${path}: ${msg}`);
|
|
299
|
+
}
|
|
300
|
+
return body;
|
|
301
|
+
}
|
|
302
|
+
async accounts() {
|
|
303
|
+
const rows = await this.call("/integrations");
|
|
304
|
+
return (rows ?? []).map((r) => {
|
|
305
|
+
// instagram and instagram-standalone are both Instagram to people; the kind is kept for the settings.
|
|
306
|
+
const platform = r.identifier.startsWith("instagram") ? "instagram" : r.identifier;
|
|
307
|
+
const problem = r.refreshNeeded ? "needs reconnecting" : r.disabled ? "turned off" : undefined;
|
|
308
|
+
const name = r.profile ? `@${r.profile.replace(/^@/, "")}` : r.name || r.identifier;
|
|
309
|
+
return { id: r.id, platform, name, active: !problem, kind: r.identifier, ...(problem ? { problem } : {}) };
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
/** Returns the uploaded image as JSON {id, path}, which is what a Postiz post refers to. */
|
|
313
|
+
async upload(file) {
|
|
314
|
+
const form = new FormData();
|
|
315
|
+
form.append("file", new Blob([readFileSync(file)], { type: "image/jpeg" }), file.split("/").pop());
|
|
316
|
+
const up = await this.call("/upload", { method: "POST", body: form });
|
|
317
|
+
if (!up.id || !up.path)
|
|
318
|
+
throw new UserError("Postiz did not return the uploaded image");
|
|
319
|
+
return JSON.stringify({ id: up.id, path: up.path });
|
|
320
|
+
}
|
|
321
|
+
async createPost(body) {
|
|
322
|
+
const rows = await this.call("/posts", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
323
|
+
const ids = (Array.isArray(rows) ? rows : []).map((r) => r.postId).filter(Boolean);
|
|
324
|
+
if (!ids.length)
|
|
325
|
+
throw new UserError("Postiz did not return a post id");
|
|
326
|
+
return ids.join(",");
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
/** One Postiz request: the same slides and caption on every channel, with each platform's settings. */
|
|
330
|
+
export function postizBody(deck, accounts, caption, media, when) {
|
|
331
|
+
const image = media.map((m) => JSON.parse(m));
|
|
332
|
+
const t = deck.tiktok;
|
|
333
|
+
return {
|
|
334
|
+
type: when ? "schedule" : "now",
|
|
335
|
+
date: when ?? new Date().toISOString(),
|
|
336
|
+
shortLink: false,
|
|
337
|
+
tags: [],
|
|
338
|
+
posts: accounts.map((a) => ({
|
|
339
|
+
integration: { id: a.id },
|
|
340
|
+
value: [{ content: caption, image }],
|
|
341
|
+
settings: a.platform.toLowerCase() === "tiktok"
|
|
342
|
+
? {
|
|
343
|
+
__type: "tiktok",
|
|
344
|
+
title: tiktokExtra(deck, t).title ?? "",
|
|
345
|
+
privacy_level: t?.privacy ? PRIVACY[t.privacy] : "PUBLIC_TO_EVERYONE",
|
|
346
|
+
duet: false,
|
|
347
|
+
stitch: false,
|
|
348
|
+
comment: t?.comments ?? true,
|
|
349
|
+
autoAddMusic: t?.auto_music ? "yes" : "no",
|
|
350
|
+
brand_content_toggle: t?.disclosure === "paid_partnership",
|
|
351
|
+
brand_organic_toggle: t?.disclosure === "own_brand",
|
|
352
|
+
content_posting_method: t?.inbox_draft ? "UPLOAD" : "DIRECT_POST",
|
|
353
|
+
}
|
|
354
|
+
: { __type: a.kind ?? "instagram", post_type: "post" },
|
|
355
|
+
})),
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
const handles = (a) => {
|
|
359
|
+
const n = a.name.toLowerCase();
|
|
360
|
+
return [n, ...(n.match(/@[\w.]+/g) ?? []), n.startsWith("@") ? n : `@${n}`];
|
|
361
|
+
};
|
|
362
|
+
/** Account names as people write them: `tiktok`, `tiktok:all`, `@handle`, `instagram:@handle`, or an id. */
|
|
363
|
+
export function resolveAccounts(refs, all, service = "Appeeky") {
|
|
364
|
+
const found = [];
|
|
365
|
+
const problems = [];
|
|
366
|
+
for (const raw of refs) {
|
|
367
|
+
const ref = raw.trim().toLowerCase();
|
|
368
|
+
const colon = ref.indexOf(":");
|
|
369
|
+
const platform = colon > 0 ? ref.slice(0, colon) : undefined;
|
|
370
|
+
const who = colon > 0 ? ref.slice(colon + 1).trim() : ref;
|
|
371
|
+
let match;
|
|
372
|
+
const byId = all.filter((a) => a.id.toLowerCase() === who);
|
|
373
|
+
if (byId.length)
|
|
374
|
+
match = byId;
|
|
375
|
+
else if (platform && who === "all")
|
|
376
|
+
match = all.filter((a) => a.platform.toLowerCase() === platform);
|
|
377
|
+
else if (!platform && all.some((a) => a.platform.toLowerCase() === who)) {
|
|
378
|
+
match = all.filter((a) => a.platform.toLowerCase() === who);
|
|
379
|
+
if (match.filter((a) => a.active).length > 1) {
|
|
380
|
+
problems.push(`"${raw}": you have ${match.length} ${who} accounts (${match.map((a) => a.name).join(", ")}); name one, e.g. ${who}:@handle, or ${who}:all`);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
else
|
|
385
|
+
match = all.filter((a) => (!platform || a.platform.toLowerCase() === platform) && handles(a).includes(who.startsWith("@") ? who : who));
|
|
386
|
+
if (!match.length) {
|
|
387
|
+
problems.push(`"${raw}": no connected account matches; connected: ${all.map((a) => `${a.platform}:${a.name}`).join(", ") || `none (connect one in ${service})`}`);
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
for (const a of match) {
|
|
391
|
+
if (!a.active)
|
|
392
|
+
problems.push(`${a.platform} ${a.name} ${a.problem}; reconnect it in ${service}`);
|
|
393
|
+
else if (!found.includes(a))
|
|
394
|
+
found.push(a);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
for (const a of found)
|
|
398
|
+
if (!["tiktok", "instagram"].includes(a.platform.toLowerCase()))
|
|
399
|
+
problems.push(`${a.platform} ${a.name}: open-slideshow posts photo slideshows to TikTok and Instagram only`);
|
|
400
|
+
return { accounts: found, problems };
|
|
401
|
+
}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type Deck, type Slide, type SizeId } from "./schema.js";
|
|
2
|
+
import { type Theme } from "./themes.js";
|
|
3
|
+
export declare const RENDERER_VERSION = "1";
|
|
4
|
+
type Style = Record<string, string | number | undefined>;
|
|
5
|
+
export interface El {
|
|
6
|
+
type: string;
|
|
7
|
+
props: {
|
|
8
|
+
style?: Style;
|
|
9
|
+
children?: Child | Child[];
|
|
10
|
+
src?: string;
|
|
11
|
+
width?: number;
|
|
12
|
+
height?: number;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
type Child = El | string;
|
|
16
|
+
export interface RenderBrand {
|
|
17
|
+
name?: string;
|
|
18
|
+
handle?: string;
|
|
19
|
+
/** Absolute path of the app icon, when there is one. */
|
|
20
|
+
icon?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface RenderContext {
|
|
23
|
+
theme: Theme;
|
|
24
|
+
size: SizeId;
|
|
25
|
+
brand: RenderBrand;
|
|
26
|
+
/** Absolute path of a slide image, or null when it cannot be found. */
|
|
27
|
+
resolveImage: (ref: string) => string | null;
|
|
28
|
+
/** The deck's language. */
|
|
29
|
+
locale?: string;
|
|
30
|
+
/** Ready images by absolute path (cropped later per box). */
|
|
31
|
+
images?: Map<string, Buffer>;
|
|
32
|
+
}
|
|
33
|
+
export interface SlideIssue {
|
|
34
|
+
slide: number;
|
|
35
|
+
level: "error" | "warning";
|
|
36
|
+
code: string;
|
|
37
|
+
message: string;
|
|
38
|
+
}
|
|
39
|
+
interface Frame {
|
|
40
|
+
W: number;
|
|
41
|
+
H: number;
|
|
42
|
+
pad: number;
|
|
43
|
+
top: number;
|
|
44
|
+
bottom: number;
|
|
45
|
+
}
|
|
46
|
+
export declare function frameFor(size: SizeId): Frame;
|
|
47
|
+
export interface Laid {
|
|
48
|
+
tree: El;
|
|
49
|
+
issues: SlideIssue[];
|
|
50
|
+
}
|
|
51
|
+
interface SlideArgs {
|
|
52
|
+
deck: Pick<Deck, "full_cover" | "text_style" | "show_handle">;
|
|
53
|
+
slide: Slide;
|
|
54
|
+
index: number;
|
|
55
|
+
tipNumber?: number;
|
|
56
|
+
/** Only plan the text (for lint): no images are read. */
|
|
57
|
+
dry?: boolean;
|
|
58
|
+
}
|
|
59
|
+
export declare function tipNumbers(slides: Slide[]): (number | undefined)[];
|
|
60
|
+
export declare function laySlide(ctx: RenderContext, a: SlideArgs): Promise<Laid>;
|
|
61
|
+
export declare function rasterize(tree: El, size: SizeId, quality?: number): Promise<Buffer>;
|
|
62
|
+
export declare function themeFor(deck: Pick<Deck, "theme" | "colors">): Theme;
|
|
63
|
+
export interface RenderResult {
|
|
64
|
+
files: string[];
|
|
65
|
+
sheet: string;
|
|
66
|
+
hash: string;
|
|
67
|
+
issues: SlideIssue[];
|
|
68
|
+
}
|
|
69
|
+
export declare function renderHash(deck: Deck, ctx: RenderContext): string;
|
|
70
|
+
export declare function renderDeck(deck: Deck, ctx: RenderContext, outDir: string): Promise<RenderResult>;
|
|
71
|
+
/** All slides side by side in one image, small, for a quick look. */
|
|
72
|
+
export declare function contactSheet(slides: Buffer[], size: SizeId, columns?: number, labels?: string[]): Promise<Buffer>;
|
|
73
|
+
export {};
|