@senseihq/emaki-posting 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 +54 -2
- package/dist/access.d.ts +37 -0
- package/dist/access.js +100 -0
- package/dist/api.d.ts +172 -0
- package/dist/api.js +229 -0
- package/dist/cloudflare/index.d.ts +36 -0
- package/dist/cloudflare/index.js +90 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +7 -0
- package/dist/rules.d.ts +112 -0
- package/dist/rules.js +97 -0
- package/dist/store.d.ts +69 -0
- package/dist/store.js +72 -0
- package/dist/sveltekit/index.d.ts +33 -0
- package/dist/sveltekit/index.js +46 -0
- package/migrations/0001_posting.sql +53 -0
- package/migrations/0002_planning.sql +31 -0
- package/package.json +40 -4
- package/poster/post.py +245 -0
- package/poster/sync_schedule.py +24 -0
- package/poster/youtube.py +31 -0
- package/src/access.ts +97 -0
- package/src/api.ts +225 -0
- package/src/cloudflare/index.ts +103 -0
- package/src/index.ts +7 -0
- package/src/rules.ts +117 -0
- package/src/store.ts +98 -0
- package/src/sveltekit/index.ts +59 -0
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@senseihq/emaki-posting",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The motion stories posting desk: stage, approve exactly what was staged, post on schedule. Framework-free core with Cloudflare (D1/R2/Access) and SvelteKit adapters.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "SenecaGlobal",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/sensei-hq/emaki.git",
|
|
11
|
+
"directory": "packages/emaki-posting"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["emaki", "social", "publishing", "approval", "cloudflare", "d1", "r2", "sveltekit"],
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"files": ["dist/**/*.js", "dist/**/*.d.ts", "src", "migrations", "poster", "README.md", "LICENSE"],
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
"./package.json": "./package.json",
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./cloudflare": {
|
|
25
|
+
"types": "./dist/cloudflare/index.d.ts",
|
|
26
|
+
"import": "./dist/cloudflare/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./sveltekit": {
|
|
29
|
+
"types": "./dist/sveltekit/index.d.ts",
|
|
30
|
+
"import": "./dist/sveltekit/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./migrations/*": "./migrations/*"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "rm -rf dist && bunx tsc --project tsconfig.build.json",
|
|
36
|
+
"prepublishOnly": "cp ../../LICENSE . && bun run build",
|
|
37
|
+
"postpublish": "rm -f LICENSE"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/poster/post.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Post what was approved on the desk to YouTube, Instagram and X. Runs in CI (see the package README for a GitHub Actions workflow).
|
|
3
|
+
|
|
4
|
+
python3 post.py [--live] [--only youtube,x]
|
|
5
|
+
|
|
6
|
+
It asks /api/admin/queue for the posts that are approved for exactly the content now staged, due (the article's date
|
|
7
|
+
has come) and not yet posted; the queue is empty while posting is paused. For each post it downloads the media through
|
|
8
|
+
the admin API, checks every file's sha256 against what was approved, posts it, and reports the outcome to
|
|
9
|
+
/api/admin/posted (the timeline and audit log). Without --live (the default, and POST_MODE=dry-run in the workflow)
|
|
10
|
+
it does everything except the posting itself and reports "dry-run".
|
|
11
|
+
|
|
12
|
+
After a real post it writes the link into the piece's `social` in STORIES_INDEX (default content/stories/index.json;
|
|
13
|
+
the workflow commits that, so the next build links and embeds the post).
|
|
14
|
+
|
|
15
|
+
Environment (CI secrets; see the package README):
|
|
16
|
+
ADMIN_ORIGIN https://your-site.example (required)
|
|
17
|
+
POSTER_CLIENT_ID, POSTER_CLIENT_SECRET the poster's Cloudflare Access service token
|
|
18
|
+
YT_CLIENT_ID, YT_CLIENT_SECRET, YT_REFRESH_TOKEN YouTube Data API (scope youtube.upload); YT_PRIVACY=unlisted for tests
|
|
19
|
+
IG_USER_ID, IG_ACCESS_TOKEN, IG_GRAPH_VERSION Instagram Graph API (instagram_content_publish)
|
|
20
|
+
X_API_KEY, X_API_SECRET, X_ACCESS_TOKEN, X_ACCESS_SECRET X API, OAuth 1.0a user context (or X_USER_TOKEN: OAuth 2.0)
|
|
21
|
+
"""
|
|
22
|
+
import base64, hashlib, hmac, json, os, secrets, sys, time, urllib.parse, urllib.request
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
INDEX = Path(os.environ.get('STORIES_INDEX', 'content/stories/index.json'))
|
|
26
|
+
API = os.environ.get('ADMIN_API', '/api/admin').rstrip('/')
|
|
27
|
+
sys.path.insert(0, str(Path(__file__).absolute().parent))
|
|
28
|
+
from youtube import yt_upload_caption, yt_set_thumbnail # noqa: E402
|
|
29
|
+
E = os.environ
|
|
30
|
+
RUN = f"{E.get('GITHUB_SERVER_URL', '')}/{E.get('GITHUB_REPOSITORY', '')}/actions/runs/{E.get('GITHUB_RUN_ID', '')}" if E.get('GITHUB_RUN_ID') else 'local'
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Failed(Exception):
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def http(method, url, body=None, headers=None, timeout=600):
|
|
38
|
+
req = urllib.request.Request(url, data=body, method=method, headers=headers or {})
|
|
39
|
+
try:
|
|
40
|
+
with urllib.request.urlopen(req, timeout=timeout) as r:
|
|
41
|
+
return r.status, dict(r.headers), r.read()
|
|
42
|
+
except urllib.error.HTTPError as err:
|
|
43
|
+
raise Failed(f'{method} {url.split("?")[0]} → {err.code}: {err.read()[:400].decode(errors="replace")}')
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def jhttp(method, url, data=None, headers=None):
|
|
47
|
+
body = json.dumps(data).encode() if data is not None else None
|
|
48
|
+
_, _, b = http(method, url, body, {**({'content-type': 'application/json'} if body else {}), **(headers or {})})
|
|
49
|
+
return json.loads(b or b'{}')
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def admin(method, path, data=None):
|
|
53
|
+
h = {'CF-Access-Client-Id': E['POSTER_CLIENT_ID'], 'CF-Access-Client-Secret': E['POSTER_CLIENT_SECRET']}
|
|
54
|
+
return jhttp(method, E['ADMIN_ORIGIN'].rstrip('/') + path, data, h)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def fetch_media(item):
|
|
58
|
+
"""Every media file of a post, downloaded and checked against the approved sha256."""
|
|
59
|
+
out = []
|
|
60
|
+
for m in item['post']['media']:
|
|
61
|
+
h = {'CF-Access-Client-Id': E['POSTER_CLIENT_ID'], 'CF-Access-Client-Secret': E['POSTER_CLIENT_SECRET']}
|
|
62
|
+
_, _, data = http('GET', E['ADMIN_ORIGIN'].rstrip('/') + API + '/media/' + m['key'], headers=h)
|
|
63
|
+
if hashlib.sha256(data).hexdigest() != m['sha256']:
|
|
64
|
+
raise Failed(f"{m['key']}: the file does not match the approved sha256; not posting")
|
|
65
|
+
out.append({**m, 'data': data})
|
|
66
|
+
return out
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# ── YouTube ──────────────────────────────────────────────────────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
def youtube(item, media, live):
|
|
72
|
+
p = item['post']
|
|
73
|
+
video = next((m for m in media if m['type'].startswith('video')), None)
|
|
74
|
+
if not video:
|
|
75
|
+
raise Failed('no video staged')
|
|
76
|
+
if not live:
|
|
77
|
+
return None, f"would upload {len(video['data']) // 1024} KB as {'a Short' if p.get('short') else 'a video'}: {p.get('title')}"
|
|
78
|
+
tok = json.loads(http('POST', 'https://oauth2.googleapis.com/token', urllib.parse.urlencode({
|
|
79
|
+
'client_id': E['YT_CLIENT_ID'], 'client_secret': E['YT_CLIENT_SECRET'], 'refresh_token': E['YT_REFRESH_TOKEN'], 'grant_type': 'refresh_token'}).encode(),
|
|
80
|
+
{'content-type': 'application/x-www-form-urlencoded'})[2])['access_token']
|
|
81
|
+
meta = {'snippet': {'title': p['title'][:100], 'description': p.get('description', '')[:5000], 'tags': p.get('tags', [])[:30], 'categoryId': '28'},
|
|
82
|
+
'status': {'privacyStatus': E.get('YT_PRIVACY') or 'public', 'selfDeclaredMadeForKids': False}}
|
|
83
|
+
_, h, _ = http('POST', 'https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status', json.dumps(meta).encode(),
|
|
84
|
+
{'authorization': f'Bearer {tok}', 'content-type': 'application/json; charset=UTF-8', 'x-upload-content-type': 'video/mp4', 'x-upload-content-length': str(len(video['data']))})
|
|
85
|
+
loc = h.get('Location') or h.get('location')
|
|
86
|
+
_, _, b = http('PUT', loc, video['data'], {'authorization': f'Bearer {tok}', 'content-type': 'video/mp4'})
|
|
87
|
+
vid = json.loads(b)['id']
|
|
88
|
+
srt = next((m for m in media if m['key'].endswith('.srt')), None)
|
|
89
|
+
thumb = next((m for m in media if m['key'].endswith('-thumb.jpg')), None)
|
|
90
|
+
if srt:
|
|
91
|
+
yt_upload_caption(tok, vid, srt['data'])
|
|
92
|
+
if thumb:
|
|
93
|
+
yt_set_thumbnail(tok, vid, thumb['data'])
|
|
94
|
+
return (f'https://www.youtube.com/shorts/{vid}' if p.get('short') else f'https://www.youtube.com/watch?v={vid}'), vid
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
# ── Instagram ────────────────────────────────────────────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
def graph(method, path, params):
|
|
100
|
+
v = E.get('IG_GRAPH_VERSION', 'v21.0')
|
|
101
|
+
q = urllib.parse.urlencode({**params, 'access_token': E['IG_ACCESS_TOKEN']})
|
|
102
|
+
url = f'https://graph.facebook.com/{v}/{path}'
|
|
103
|
+
_, _, b = http(method, url + ('?' + q if method == 'GET' else ''), q.encode() if method == 'POST' else None,
|
|
104
|
+
{'content-type': 'application/x-www-form-urlencoded'} if method == 'POST' else {})
|
|
105
|
+
return json.loads(b)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def wait_ready(container):
|
|
109
|
+
for _ in range(60):
|
|
110
|
+
st = graph('GET', container, {'fields': 'status_code'}).get('status_code')
|
|
111
|
+
if st == 'FINISHED':
|
|
112
|
+
return
|
|
113
|
+
if st in ('ERROR', 'EXPIRED'):
|
|
114
|
+
raise Failed(f'Instagram could not process the media ({st})')
|
|
115
|
+
time.sleep(10)
|
|
116
|
+
raise Failed('Instagram took too long to process the media')
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def publish(container):
|
|
120
|
+
wait_ready(container)
|
|
121
|
+
mid = graph('POST', f"{E['IG_USER_ID']}/media_publish", {'creation_id': container})['id']
|
|
122
|
+
return graph('GET', mid, {'fields': 'permalink'}).get('permalink'), mid
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def instagram_reel(item, media, live):
|
|
126
|
+
video = next((m for m in item['media'] if m['type'].startswith('video')), None)
|
|
127
|
+
if not video or not video.get('url'):
|
|
128
|
+
raise Failed('no video, or no signed media URL (MEDIA_URL_SECRET unset on the site)')
|
|
129
|
+
if not live:
|
|
130
|
+
return None, f"would post a Reel ({len(media[0]['data']) // 1024} KB) with a {len(item['post'].get('caption', ''))}-character caption"
|
|
131
|
+
params = {'media_type': 'REELS', 'video_url': video['url'], 'caption': item['post'].get('caption', ''), 'share_to_feed': 'true'}
|
|
132
|
+
cover = next((m for m in item['media'] if m['type'].startswith('image') and m.get('url')), None)
|
|
133
|
+
if cover:
|
|
134
|
+
params['cover_url'] = cover['url']
|
|
135
|
+
c = graph('POST', f"{E['IG_USER_ID']}/media", params)['id']
|
|
136
|
+
return publish(c)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def instagram_carousel(item, media, live):
|
|
140
|
+
imgs = [m for m in item['media'] if m['type'].startswith('image')]
|
|
141
|
+
if not 2 <= len(imgs) <= 10 or not all(m.get('url') for m in imgs):
|
|
142
|
+
raise Failed(f'a carousel needs 2–10 images with signed URLs (have {len(imgs)})')
|
|
143
|
+
if not live:
|
|
144
|
+
return None, f'would post a {len(imgs)}-slide carousel'
|
|
145
|
+
kids = [graph('POST', f"{E['IG_USER_ID']}/media", {'image_url': m['url'], 'is_carousel_item': 'true'})['id'] for m in imgs]
|
|
146
|
+
for k in kids:
|
|
147
|
+
wait_ready(k)
|
|
148
|
+
c = graph('POST', f"{E['IG_USER_ID']}/media", {'media_type': 'CAROUSEL', 'children': ','.join(kids), 'caption': item['post'].get('caption', '')})['id']
|
|
149
|
+
return publish(c)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# ── X ────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
def x_auth(method, url, params=None):
|
|
155
|
+
"""Authorization header: OAuth 2.0 user token if given, else OAuth 1.0a (JSON / multipart bodies are not signed)."""
|
|
156
|
+
if E.get('X_USER_TOKEN'):
|
|
157
|
+
return {'authorization': f"Bearer {E['X_USER_TOKEN']}"}
|
|
158
|
+
pct = lambda s: urllib.parse.quote(str(s), safe='~')
|
|
159
|
+
o = {'oauth_consumer_key': E['X_API_KEY'], 'oauth_nonce': secrets.token_hex(16), 'oauth_signature_method': 'HMAC-SHA1',
|
|
160
|
+
'oauth_timestamp': str(int(time.time())), 'oauth_token': E['X_ACCESS_TOKEN'], 'oauth_version': '1.0'}
|
|
161
|
+
base_url, _, query = url.partition('?')
|
|
162
|
+
allp = {**o, **(params or {}), **dict(urllib.parse.parse_qsl(query))}
|
|
163
|
+
base = '&'.join([method, pct(base_url), pct('&'.join(f'{pct(k)}={pct(allp[k])}' for k in sorted(allp)))])
|
|
164
|
+
key = f"{pct(E['X_API_SECRET'])}&{pct(E['X_ACCESS_SECRET'])}"
|
|
165
|
+
o['oauth_signature'] = base64.b64encode(hmac.new(key.encode(), base.encode(), hashlib.sha1).digest()).decode()
|
|
166
|
+
return {'authorization': 'OAuth ' + ', '.join(f'{pct(k)}="{pct(v)}"' for k, v in sorted(o.items()))}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def x_upload(data, mime):
|
|
170
|
+
init = 'https://api.x.com/2/media/upload/initialize'
|
|
171
|
+
mid = jhttp('POST', init, {'media_type': mime, 'total_bytes': len(data), 'media_category': 'tweet_video' if mime.startswith('video') else 'tweet_image'}, x_auth('POST', init))['data']['id']
|
|
172
|
+
chunk = 4 * 1024 * 1024
|
|
173
|
+
for i in range(0, len(data), chunk):
|
|
174
|
+
url = f'https://api.x.com/2/media/upload/{mid}/append'
|
|
175
|
+
boundary = secrets.token_hex(12)
|
|
176
|
+
body = (f'--{boundary}\r\nContent-Disposition: form-data; name="segment_index"\r\n\r\n{i // chunk}\r\n'
|
|
177
|
+
f'--{boundary}\r\nContent-Disposition: form-data; name="media"; filename="chunk"\r\nContent-Type: application/octet-stream\r\n\r\n').encode() + data[i:i + chunk] + f'\r\n--{boundary}--\r\n'.encode()
|
|
178
|
+
http('POST', url, body, {**x_auth('POST', url), 'content-type': f'multipart/form-data; boundary={boundary}'})
|
|
179
|
+
fin = f'https://api.x.com/2/media/upload/{mid}/finalize'
|
|
180
|
+
info = jhttp('POST', fin, None, x_auth('POST', fin)).get('data', {}).get('processing_info')
|
|
181
|
+
while info and info.get('state') in ('pending', 'in_progress'):
|
|
182
|
+
time.sleep(info.get('check_after_secs', 2))
|
|
183
|
+
st = f'https://api.x.com/2/media/upload?command=STATUS&media_id={mid}'
|
|
184
|
+
info = jhttp('GET', st, None, x_auth('GET', st)).get('data', {}).get('processing_info')
|
|
185
|
+
if info and info.get('state') == 'failed':
|
|
186
|
+
raise Failed(f'X could not process the media: {info}')
|
|
187
|
+
return mid
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def x_post(item, media, live):
|
|
191
|
+
p = item['post']
|
|
192
|
+
if not live:
|
|
193
|
+
return None, f"would post with {len(media)} file(s) and a {len(p.get('thread', []))}-reply thread: {p['text'][:80]}"
|
|
194
|
+
ids = [x_upload(m['data'], m['type']) for m in media[:1]]
|
|
195
|
+
tweets = 'https://api.x.com/2/tweets'
|
|
196
|
+
first = jhttp('POST', tweets, {'text': p['text'], **({'media': {'media_ids': ids}} if ids else {})}, x_auth('POST', tweets))['data']['id']
|
|
197
|
+
prev = first
|
|
198
|
+
for t in p.get('thread', []):
|
|
199
|
+
prev = jhttp('POST', tweets, {'text': t, 'reply': {'in_reply_to_tweet_id': prev}}, x_auth('POST', tweets))['data']['id']
|
|
200
|
+
return f'https://x.com/i/web/status/{first}', first
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
POSTERS = {'youtube': youtube, 'instagram_reel': instagram_reel, 'instagram_carousel': instagram_carousel, 'x': x_post}
|
|
204
|
+
SOCIAL = {'youtube': 'youtube', 'instagram_reel': 'instagram', 'x': 'x'}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def record_link(slug, channel, url):
|
|
208
|
+
key = SOCIAL.get(channel)
|
|
209
|
+
if not key or not url:
|
|
210
|
+
return
|
|
211
|
+
index = json.loads(INDEX.read_text())
|
|
212
|
+
for a in index:
|
|
213
|
+
if a['slug'] == slug and not a.setdefault('social', {}).get(key):
|
|
214
|
+
a['social'][key] = url
|
|
215
|
+
INDEX.write_text(json.dumps(index, indent=2, ensure_ascii=False) + '\n')
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def main():
|
|
219
|
+
live = '--live' in sys.argv
|
|
220
|
+
only = sys.argv[sys.argv.index('--only') + 1].split(',') if '--only' in sys.argv else None
|
|
221
|
+
q = admin('GET', API + '/queue')
|
|
222
|
+
print(f"{q['today']}: {'paused' if q['paused'] else 'on'}, {len(q['items'])} approved post(s) due · {'LIVE' if live else 'dry run'}")
|
|
223
|
+
failures = 0
|
|
224
|
+
for item in q['items']:
|
|
225
|
+
ch = item['channel']
|
|
226
|
+
if only and ch not in only:
|
|
227
|
+
continue
|
|
228
|
+
try:
|
|
229
|
+
media = fetch_media(item)
|
|
230
|
+
url, ext = POSTERS[ch](item, media, live)
|
|
231
|
+
status = 'posted' if live else 'dry-run'
|
|
232
|
+
print(f" {item['slug']} · {ch}: {status} {url or ext}")
|
|
233
|
+
admin('POST', API + '/posted', {'slug': item['slug'], 'channel': ch, 'content_sha': item['content_sha'], 'status': status,
|
|
234
|
+
'url': url or '', 'external_id': ext if live else '', 'detail': '' if live else ext, 'run': RUN})
|
|
235
|
+
if live:
|
|
236
|
+
record_link(item['slug'], ch, url)
|
|
237
|
+
except (Failed, KeyError) as err:
|
|
238
|
+
failures += 1
|
|
239
|
+
print(f" {item['slug']} · {ch}: FAILED {err}")
|
|
240
|
+
admin('POST', API + '/posted', {'slug': item['slug'], 'channel': ch, 'content_sha': item['content_sha'], 'status': 'failed', 'detail': str(err)[:1500], 'run': RUN})
|
|
241
|
+
sys.exit(1 if failures else 0)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
if __name__ == '__main__':
|
|
245
|
+
main()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Write the posting order set on the desk ("Make next") into STORIES_INDEX (default content/stories/index.json).
|
|
3
|
+
|
|
4
|
+
ADMIN_ORIGIN=https://your-site.example python3 sync_schedule.py
|
|
5
|
+
|
|
6
|
+
Runs in CI before the morning rebuild, which commits the change; the commit rebuilds the site with the new dates. Uses the poster's Access service token (POSTER_CLIENT_ID / _SECRET).
|
|
7
|
+
"""
|
|
8
|
+
import json, os, sys, urllib.request
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
INDEX = Path(os.environ.get('STORIES_INDEX', 'content/stories/index.json'))
|
|
12
|
+
E = os.environ
|
|
13
|
+
req = urllib.request.Request(E['ADMIN_ORIGIN'].rstrip('/') + os.environ.get('ADMIN_API', '/api/admin').rstrip('/') + '/schedule',
|
|
14
|
+
headers={'CF-Access-Client-Id': E['POSTER_CLIENT_ID'], 'CF-Access-Client-Secret': E['POSTER_CLIENT_SECRET']})
|
|
15
|
+
with urllib.request.urlopen(req, timeout=60) as r:
|
|
16
|
+
changes = {c['slug']: c['publish_date'] for c in json.loads(r.read())['changes']}
|
|
17
|
+
if not changes:
|
|
18
|
+
print('no changes to the posting order'); sys.exit(0)
|
|
19
|
+
index = json.loads(INDEX.read_text())
|
|
20
|
+
for a in index:
|
|
21
|
+
if a['slug'] in changes:
|
|
22
|
+
print(f" {a['slug']}: {a['publish_date']} → {changes[a['slug']]}")
|
|
23
|
+
a['publish_date'] = changes[a['slug']]
|
|
24
|
+
INDEX.write_text(json.dumps(index, indent=2, ensure_ascii=False) + '\n')
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""YouTube extras the poster needs after an upload: the caption track and the custom thumbnail. Stdlib only."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import urllib.request
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def http(method, url, body=None, headers=None, timeout=600):
|
|
9
|
+
req = urllib.request.Request(url, data=body, method=method, headers=headers or {})
|
|
10
|
+
with urllib.request.urlopen(req, timeout=timeout) as r:
|
|
11
|
+
return r.status, dict(r.headers), r.read()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def yt_upload_caption(token, video_id, srt):
|
|
15
|
+
"""English caption track (needs the youtube.force-ssl scope)."""
|
|
16
|
+
boundary = 'cap' + hashlib.sha256(os.urandom(8)).hexdigest()[:16]
|
|
17
|
+
body = (f'--{boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n'
|
|
18
|
+
f'{json.dumps({"snippet": {"videoId": video_id, "language": "en", "name": "English"}})}\r\n'
|
|
19
|
+
f'--{boundary}\r\nContent-Type: application/octet-stream\r\n\r\n').encode() + srt + f'\r\n--{boundary}--\r\n'.encode()
|
|
20
|
+
http('POST', 'https://www.googleapis.com/upload/youtube/v3/captions?part=snippet', body,
|
|
21
|
+
{'authorization': f'Bearer {token}', 'content-type': f'multipart/related; boundary={boundary}'})
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def yt_set_thumbnail(token, video_id, jpg):
|
|
25
|
+
"""Custom thumbnail (needs youtube.force-ssl; the channel must be phone-verified)."""
|
|
26
|
+
boundary = 'thm' + hashlib.sha256(os.urandom(8)).hexdigest()[:16]
|
|
27
|
+
body = (f'--{boundary}\r\nContent-Type: image/jpeg\r\n\r\n').encode() + jpg + f'\r\n--{boundary}--\r\n'.encode()
|
|
28
|
+
http('POST', f'https://www.googleapis.com/upload/youtube/v3/thumbnails/set?videoId={video_id}', body,
|
|
29
|
+
{'authorization': f'Bearer {token}', 'content-type': f'multipart/related; boundary={boundary}'})
|
|
30
|
+
|
|
31
|
+
|
package/src/access.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Who is calling the admin paths. Cloudflare Access sits in front of them (the Access application's policies decide
|
|
2
|
+
// who reaches the site at all); this checks again on the server, and fails closed:
|
|
3
|
+
// - a person: a valid Access token (Cf-Access-Jwt-Assertion) signed by the team's keys, for this app's audience,
|
|
4
|
+
// unexpired, naming an email in ADMIN_EMAILS;
|
|
5
|
+
// - a machine: the same token issued to an Access service token, whose client id is in STAGER_CLIENT_IDS (the Mac
|
|
6
|
+
// that uploads finished media and post text) or POSTER_CLIENT_IDS (the GitHub job that posts approved pieces).
|
|
7
|
+
// Without a team domain + audience the deployed site refuses everything here; only a dev server lets a local admin in.
|
|
8
|
+
// Any host whose identity proxy issues RS256 JWTs with a JWKS can reuse verifyCaller with its own certs URL.
|
|
9
|
+
|
|
10
|
+
export type Role = 'admin' | 'stager' | 'poster';
|
|
11
|
+
export interface Caller { role: Role; id: string }
|
|
12
|
+
export interface AccessConfig { teamDomain?: string; aud?: string; emails?: string; stagers?: string; posters?: string }
|
|
13
|
+
|
|
14
|
+
type Jwk = JsonWebKey & { kid?: string };
|
|
15
|
+
let certs: { url: string; keys: Jwk[]; at: number } | null = null;
|
|
16
|
+
|
|
17
|
+
const b64 = (s: string) => Uint8Array.from(atob(s.replace(/-/g, '+').replace(/_/g, '/') + '==='.slice((s.length + 3) % 4)), (c) => c.charCodeAt(0));
|
|
18
|
+
const part = (s: string) => JSON.parse(new TextDecoder().decode(b64(s)));
|
|
19
|
+
const list = (raw?: string) => (raw ?? '').split(',').map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
20
|
+
|
|
21
|
+
/** Where the desk lives: the admin pages and the admin API. Change them with configurePaths() before handling requests. */
|
|
22
|
+
export const paths = { admin: '/admin', api: '/api/admin' };
|
|
23
|
+
export function configurePaths(p: Partial<typeof paths>) { Object.assign(paths, p); }
|
|
24
|
+
export const isAdminPath = (path: string) => path === paths.admin || path.startsWith(paths.admin + '/') || path.startsWith(paths.api + '/');
|
|
25
|
+
export const accessConfigured = (c: AccessConfig) => Boolean(c.teamDomain && c.aud);
|
|
26
|
+
|
|
27
|
+
async function keys(teamDomain: string, fetchFn: typeof fetch, now: number): Promise<Jwk[]> {
|
|
28
|
+
const url = `https://${teamDomain.replace(/^https?:\/\//, '').replace(/\/$/, '')}/cdn-cgi/access/certs`;
|
|
29
|
+
if (certs && certs.url === url && now - certs.at < 3_600_000) return certs.keys;
|
|
30
|
+
const r = await fetchFn(url);
|
|
31
|
+
if (!r.ok) throw new Error(`Access certs ${r.status}`);
|
|
32
|
+
certs = { url, keys: ((await r.json()) as { keys: Jwk[] }).keys, at: now };
|
|
33
|
+
return certs.keys;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The caller behind a valid Access token for this app, or null. */
|
|
37
|
+
export async function verifyCaller(token: string | null, c: AccessConfig, fetchFn: typeof fetch = fetch, now = Date.now()): Promise<Caller | null> {
|
|
38
|
+
if (!token || !accessConfigured(c)) return null;
|
|
39
|
+
const [h, p, s] = token.split('.');
|
|
40
|
+
if (!h || !p || !s) return null;
|
|
41
|
+
try {
|
|
42
|
+
const head = part(h), claims = part(p);
|
|
43
|
+
if (head.alg !== 'RS256') return null;
|
|
44
|
+
const aud: string[] = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
45
|
+
if (!aud.some((a) => list(c.aud).includes(String(a).toLowerCase()))) return null;
|
|
46
|
+
if (typeof claims.exp !== 'number' || claims.exp * 1000 < now) return null;
|
|
47
|
+
const team = c.teamDomain!.replace(/^https?:\/\//, '').replace(/\/$/, '');
|
|
48
|
+
if (claims.iss && String(claims.iss).replace(/^https?:\/\//, '').replace(/\/$/, '') !== team) return null;
|
|
49
|
+
let caller: Caller | null = null;
|
|
50
|
+
const email = String(claims.email ?? '').toLowerCase();
|
|
51
|
+
const cn = String(claims.common_name ?? '').toLowerCase();
|
|
52
|
+
if (email && list(c.emails).includes(email)) caller = { role: 'admin', id: email };
|
|
53
|
+
else if (!email && cn && list(c.stagers).includes(cn)) caller = { role: 'stager', id: cn };
|
|
54
|
+
else if (!email && cn && list(c.posters).includes(cn)) caller = { role: 'poster', id: cn };
|
|
55
|
+
if (!caller) return null;
|
|
56
|
+
const jwk = (await keys(c.teamDomain!, fetchFn, now)).find((k) => k.kid === head.kid);
|
|
57
|
+
if (!jwk) return null;
|
|
58
|
+
const key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, false, ['verify']);
|
|
59
|
+
const ok = await crypto.subtle.verify('RSASSA-PKCS1-v1_5', key, b64(s), new TextEncoder().encode(`${h}.${p}`));
|
|
60
|
+
return ok ? caller : null;
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Which roles may use a path (and method). People: everything. Machines: only their own endpoints. */
|
|
67
|
+
export function allowed(role: Role, path: string, method: string): boolean {
|
|
68
|
+
if (role === 'admin') return true;
|
|
69
|
+
const a = paths.api;
|
|
70
|
+
// The producing machine: upload media, stage posts, report what is produced, read and answer motion requests.
|
|
71
|
+
if (role === 'stager') return (path.startsWith(`${a}/media/`) && method === 'PUT') || (path === `${a}/stage` && method === 'POST') ||
|
|
72
|
+
(path === `${a}/production` && method === 'POST') || (path === `${a}/requests` && (method === 'GET' || method === 'POST'));
|
|
73
|
+
// The CI poster: read the approved queue and the posting order, download media, report outcomes.
|
|
74
|
+
if (role === 'poster') return (path === `${a}/queue` && method === 'GET') || (path === `${a}/posted` && method === 'POST') ||
|
|
75
|
+
(path.startsWith(`${a}/media/`) && method === 'GET') || (path === `${a}/schedule` && method === 'GET');
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** for tests: forget the cached keys */
|
|
80
|
+
export function resetAccessCache() { certs = null; }
|
|
81
|
+
|
|
82
|
+
/** Access settings from environment variables (CF_ACCESS_TEAM_DOMAIN, CF_ACCESS_AUD, ADMIN_EMAILS, STAGER_CLIENT_IDS,
|
|
83
|
+
* POSTER_CLIENT_IDS). */
|
|
84
|
+
export const accessFromEnv = (env: Record<string, string | undefined>): AccessConfig => ({
|
|
85
|
+
teamDomain: env.CF_ACCESS_TEAM_DOMAIN, aud: env.CF_ACCESS_AUD, emails: env.ADMIN_EMAILS,
|
|
86
|
+
stagers: env.STAGER_CLIENT_IDS, posters: env.POSTER_CLIENT_IDS,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The caller of an admin request, or null to refuse it. `dev` lets a local admin in only when Access is not set up
|
|
91
|
+
* (never pass true in production). Does not check the role against the path; use allowed() for that.
|
|
92
|
+
*/
|
|
93
|
+
export async function callerOf(request: Request, config: AccessConfig, o: { dev?: boolean; fetch?: typeof fetch } = {}): Promise<Caller | null> {
|
|
94
|
+
if (!accessConfigured(config)) return o.dev ? { role: 'admin', id: 'dev@localhost' } : null;
|
|
95
|
+
return verifyCaller(request.headers.get('cf-access-jwt-assertion'), config, o.fetch ?? fetch);
|
|
96
|
+
}
|
|
97
|
+
|