-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedPublisher.ts
170 lines (160 loc) · 5.21 KB
/
FeedPublisher.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { RichText } from '@atproto/api'
import { createHash } from 'crypto'
import { partition, sortBy } from 'lodash-es'
import redaxios from 'redaxios'
import { z } from 'zod'
import { getBlueskyClient as getBlueskyAgent } from './bluesky'
import { env } from './env'
import { reconciler } from './Reconciler'
import { Resource, createResourceType } from './Resource'
import { createShortUrl } from './UrlShortener'
const feedItemSchema = z.object({
site: z.string(),
title: z.string(),
url: z.string(),
published: z.string(),
})
const feedSchema = z.array(feedItemSchema)
const ShortenedUrl = createResourceType<{ url: string }, { shortLink: string }>(
async (state, spec) => {
return { shortLink: await createShortUrl(spec.url) }
},
)
const shortenUrl = async (url: string) => {
const urlHash = hashUrl(url)
const state = await reconciler.reconcile(
ShortenedUrl.create({
key: `${urlHash}:shorturl`,
spec: { url },
description: `Short URL for ${url}`,
}),
)
return state.shortLink
}
export class FeedPublisher {
async publish(dryRun: boolean, socials?: string[]) {
const shouldPostTo = (social: string) =>
!socials || socials.includes(social)
const feed = await redaxios.get(
'https://wonderfulsoftware.github.io/webring-site-data/feed.json',
)
let reconciled = false
const MastodonPost = createResourceType<
{ url: string; title: string },
{ id: string; url: string }
>(async (state, spec) => {
if (state) return state
const shortUrl = await shortenUrl(spec.url)
const response = await redaxios.post(
'https://mastodon.in.th/api/v1/statuses',
{ status: spec.title + '\n\n' + shortUrl },
{ headers: { Authorization: `Bearer ${env.MASTODON_ACCESS_TOKEN}` } },
)
const data = response.data
reconciled = true
return { id: data.id, url: data.url }
})
const BlueskyPost = createResourceType<
{ url: string; title: string },
{ uri: string; cid: string }
>(async (state, spec) => {
if (state) return state
const shortUrl = await shortenUrl(spec.url)
const agent = await getBlueskyAgent()
const richText = new RichText({
text: spec.title + '\n\n' + shortUrl,
})
await richText.detectFacets(agent)
const result = await agent.post({
text: richText.text,
facets: richText.facets,
createdAt: new Date().toISOString(),
})
reconciled = true
return { uri: result.uri, cid: result.cid }
})
const FacebookPost = createResourceType<
{ url: string; title: string },
{ id: string; url: string }
>(async (state, spec) => {
if (state) return state
const shortUrl = await shortenUrl(spec.url)
const response = await redaxios.post(
`https://graph.facebook.com/v15.0/me/feed?access_token=${env.FACEBOOK_PAGE_ACCESS_TOKEN}&fields=id,permalink_url`,
{ message: spec.title, link: shortUrl },
)
const data = response.data
reconciled = true
return { id: data.id, url: data.permalink_url }
})
const feedData = feedSchema.parse(feed.data)
const resources = selectAndSortFeed(feedData)
.flatMap((item): (typeof item)[] => {
// Fix malformed URLs in some feeds
item.url = item.url.replace(
/^(https:\/\/microbenz\.in\.th)([^/])/,
'$1/$2',
)
return [item]
})
.flatMap((item): Resource[] => {
const urlHash = hashUrl(item.url)
const out: Resource[] = []
if (shouldPostTo('mastodon')) {
out.push(
MastodonPost.create({
key: `${urlHash}:mastodon`,
spec: {
url: item.url,
title: `${item.title} [${item.site}]`,
},
description: `Mastodon post for ${item.url}`,
}),
)
}
if (shouldPostTo('facebook')) {
out.push(
FacebookPost.create({
key: `${urlHash}:facebook`,
spec: {
url: item.url,
title: `${item.title} [${item.site}]`,
},
description: `Facebook post for ${item.url}`,
}),
)
}
if (shouldPostTo('bluesky') && item.published >= '2024-10-01') {
out.push(
BlueskyPost.create({
key: `${urlHash}:bluesky`,
spec: {
url: item.url,
title: `${item.title} [${item.site}]`,
},
description: `Bluesky post for ${item.url}`,
}),
)
}
return out
})
for (const resource of resources) {
await reconciler.reconcile(resource, dryRun)
if (reconciled) break
}
}
}
function hashUrl(url: string) {
return createHash('md5').update(url).digest('hex')
}
function selectAndSortFeed<X extends { published: string }>(feed: X[]): X[] {
const items = feed.filter((item) => item.published >= '2022-12')
const [before2023, after2023] = partition<X>(
items,
(item) => item.published < '2023',
) as [X[], X[]]
return [
...sortBy(after2023, (item) => item.published),
...sortBy(before2023, (item) => item.published).reverse(),
]
}