-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
122 lines (96 loc) · 2.73 KB
/
build.js
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
import anchor from 'anchor-markdown-header'
import got from 'got'
import { createWriteStream } from 'node:fs'
import { parallelMap, pipeline, writeToStream } from 'streaming-iterables'
import write from 'stream-write'
const newlineRegex = /\r?\n|\r/g
build({
serverUrl: 'https://tube.arthack.nz',
channelName: 'intertwingled',
filePath: './README.md',
chunkSize: 10,
})
async function build(options) {
const file = getFileWriteStream(options)
await write(file, await getChannelTitle(options))
await pipeline(
() => getChannelVideos(options),
mapChannelVideosToText(options),
writeToStream(file),
)
file.end()
}
function getFileWriteStream({ filePath }) {
return createWriteStream(new URL(filePath, import.meta.url))
}
async function getChannelTitle(options) {
const { channelName, serverUrl } = options
const { displayName, description } = await got({
prefixUrl: serverUrl,
url: `api/v1/video-channels/${channelName}`,
}).json()
const description2 = description.replace(newlineRegex, '\n')
const description3 = rewriteVideoLinks(options, description2)
const text = [
`# [${displayName}](${serverUrl}/c/${channelName}/)`,
``,
`![](./banner.jpg)`,
``,
description3,
].join('\n')
return text + '\n\n'
}
async function* getChannelVideos(options, position = {}) {
const { channelName, serverUrl, chunkSize } = options
const { start = 0, count = chunkSize } = position
console.log(start)
const { data } = await got(
`api/v1/video-channels/${channelName}/videos`,
{
prefixUrl: serverUrl,
searchParams: {
count,
start,
sort: "-publishedAt",
skipCount: "true",
},
},
).json()
for (const video of data) {
yield video
}
if (!(data.length < count)) {
yield* getChannelVideos(options, { start: start + data.length, count })
}
}
function mapChannelVideosToText(options) {
const { serverUrl, chunkSize } = options
return parallelMap(chunkSize, async (video) => {
const { id, name, thumbnailPath, url } = video
const { description } = await got(
`api/v1/videos/${id}/description`,
{
prefixUrl: serverUrl,
},
).json()
const shortDescription = description
.split(newlineRegex)
.slice(0, 3)
.join('\n')
const text = [
`## [${name}](${url})`,
``,
`[![${name}](${serverUrl}${thumbnailPath})](${url})`,
``,
shortDescription,
].join('\n')
return text + '\n\n'
})
}
function rewriteVideoLinks(options, text) {
const { serverUrl } = options
const videoLinkRegex = new RegExp(`\\[(.*?)\\]\\(${serverUrl}/w/[a-zA-Z0-9]+\\)`, 'g')
return text.replaceAll(videoLinkRegex, (_, label) => {
return anchor(label)
})
}