-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
410 lines (370 loc) · 13.4 KB
/
index.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const path = require('path')
const debug = require('debug')('source-podlove')
const { get, noop } = require('lodash')
const { map } = require('lodash/fp')
const pLimit = require('p-limit')
const { toPlayerTime } = require('@podlove/utils/time')
const wordsCounter = require('word-counting')
const cache = require('./lib/cache')
const fetch = require('./lib/fetch')
const transformations = require('./transformation')
const show = require('./schema/show')
const statistics = require('./schema/statistics')
const episode = require('./schema/episode')
const chapter = require('./schema/chapter')
const audio = require('./schema/audio')
const transcript = require('./schema/transcript')
const transcriptChunk = require('./schema/transcript-chunk')
const contributor = require('./schema/contributor')
const group = require('./schema/group')
const role = require('./schema/role')
const episodeContributor = require('./schema/episode-contributor')
const timeline = require('./schema/timeline')
const contributorStatistic = require('./schema/contributor-statistics')
const contributorEpisodeStatistics = require('./schema/contributor-statistics-episode')
const socialService = require('./schema/social-service')
const contributorSocialService = require('./schema/contributor-social-service')
class PodloveSource {
static defaultOptions() {
return {
baseUrl: '',
apiBase: 'wp-json',
typeName: 'Podlove',
imageCache: 'src/assets/images',
version: 'v1'
}
}
constructor(api, options) {
this.options = options
this.actions = null
this.collections = {
episode: noop,
contributor: noop,
group: noop,
role: noop,
contributorStatistic: noop,
socialService: noop
}
this.cacheImage = cache.image(path.resolve(this.options.imageCache))
if (!options.typeName) {
throw new Error(`Missing typeName option.`)
}
const endpoint = (...path) => `podlove/${this.options.version}/${path.join('/')}`
this.fetch = fetch(this.options)
this.routes = {
show: () => endpoint('show'),
episodes: () => endpoint('episodes'),
episode: (id) => endpoint('episodes', id),
transcripts: (id) => endpoint('transcripts', id),
contributors: () => endpoint('contributors'),
groups: () => endpoint('contributors', 'groups'),
roles: () => endpoint('contributors', 'roles'),
episodeContributors: (id) => endpoint('contributors', 'episode', id),
socialServices: () => endpoint('social', 'services'),
contributorSocialService: (id) => endpoint('social', 'services', 'contributor', id)
}
api.loadSource(async (actions) => {
this.actions = actions
await this.registerCollections()
await this.loadShow()
await this.loadSocialServices()
await this.loadContributors()
await this.loadGroups()
await this.loadRoles()
await this.loadEpisodes()
await this.calculateContributorStatistic()
await this.calculatePodcastStatistics()
})
}
objectName(name) {
return `${this.options.typeName}${name}`
}
async registerCollections() {
const { addSchemaTypes, schema, addCollection } = this.actions
const Episode = schema.createObjectType(episode.schema(this.options.typeName))
const Chapter = schema.createObjectType(chapter.schema(this.options.typeName))
const Audio = schema.createObjectType(audio.schema(this.options.typeName))
const Transcripts = schema.createObjectType(transcript.schema(this.options.typeName))
const Timeline = schema.createObjectType(timeline.schema(this.options.typeName))
const TranscriptChunk = schema.createObjectType(transcriptChunk.schema(this.options.typeName))
const Contributor = schema.createObjectType(contributor.schema(this.options.typeName))
const Group = schema.createObjectType(group.schema(this.options.typeName))
const Role = schema.createObjectType(role.schema(this.options.typeName))
const EpisodeContributor = schema.createObjectType(
episodeContributor.schema(this.options.typeName)
)
const ContributorStatistic = schema.createObjectType(
contributorStatistic.schema(this.options.typeName)
)
const ContributorEpisodeStatistic = schema.createObjectType(
contributorEpisodeStatistics.schema(this.options.typeName)
)
const ContributorSocialService = schema.createObjectType(
contributorSocialService.schema(this.options.typeName)
)
const SocialServices = schema.createObjectType(socialService.schema(this.options.typeName))
addSchemaTypes([
Chapter,
Audio,
Episode,
Transcripts,
Timeline,
TranscriptChunk,
Contributor,
Group,
Role,
EpisodeContributor,
ContributorStatistic,
ContributorEpisodeStatistic,
SocialServices,
ContributorSocialService
])
this.collections.episode = addCollection(episode.name(this.options.typeName))
this.collections.transcript = addCollection(transcript.name(this.options.typeName))
this.collections.contributor = addCollection(contributor.name(this.options.typeName))
this.collections.group = addCollection(group.name(this.options.typeName))
this.collections.role = addCollection(role.name(this.options.typeName))
this.collections.contributorStatistic = addCollection(
contributorStatistic.name(this.options.typeName)
)
this.collections.socialService = addCollection(socialService.name(this.options.typeName))
}
/**
* Show
*/
async loadShow() {
debug(`Fetching Show from ${this.routes.show()}`)
const data = await this.fetch(this.routes.show(), {})
debug(`Add Show ${data.name}`)
this.actions.addMetadata(show.name(this.options.typeName), {
...show.normalizer(data),
poster: await this.cacheImage(data.poster)
})
}
/**
* Contributors
*/
async loadContributors() {
debug(`Fetching Contributors from ${this.routes.contributors()}`)
const contributors = await this.fetch(this.routes.contributors(), [])
await Promise.all(
contributors.map(async (data) => {
debug(`Add Contributor ${data.name} [${data.id}]`)
const avatar = await this.cacheImage(data.avatar)
const social = await this.fetch(
this.routes.contributorSocialService(data.id) + '?category=social',
[]
).then(
map((item) => ({
...contributorSocialService.normalizer(item),
service: this.actions.createReference(
socialService.name(this.options.typeName),
item.service_id
)
}))
)
const donation = await this.fetch(
this.routes.contributorSocialService(data.id) + '?category=donation',
[]
).then(
map((item) => ({
...contributorSocialService.normalizer(item),
service: this.actions.createReference(
socialService.name(this.options.typeName),
item.service_id
)
}))
)
return this.collections.contributor.addNode({
...contributor.normalizer(data),
avatar,
social,
donation
})
})
)
}
/**
* Groups
*/
async loadGroups() {
debug(`Fetch Groups from ${this.routes.groups()}`)
const groups = await this.fetch(this.routes.groups(), [])
groups.forEach(
(data) =>
debug(`Add Group ${data.title} [${data.id}]`) ||
this.collections.group.addNode(group.normalizer(data))
)
}
/**
* Roles
*/
async loadRoles() {
debug(`Fetch Roles from ${this.routes.roles()}`)
const roles = await this.fetch(this.routes.roles(), [])
roles.forEach((data) => {
debug(`Add Group ${data.title} [${data.id}]`)
this.collections.role.addNode(group.normalizer(data))
})
}
/**
* SocialServices
*/
async loadSocialServices() {
debug(`Fetch Social Services from ${this.routes.socialServices()}`)
await this.fetch(this.routes.socialServices(), [])
.then((services) =>
Promise.all(
services.map(async (data) => ({
...socialService.normalizer(data),
logo: await this.cacheImage(data.logo_url)
}))
)
)
.then((services) =>
services.forEach((service) => this.collections.socialService.addNode(service))
)
}
/**
* Episodes
*/
async loadEpisodes() {
const limiter = pLimit(10)
debug(`Fetch Episodes from ${this.routes.episodes()}`)
const episodes = await this.fetch(this.routes.episodes(), { data: [] }).then((data) =>
get(data, 'results', [])
)
await Promise.all(
episodes.map((data) =>
limiter(() => {
const id = get(data, 'id')
return id && this.addEpisode(id)
})
)
)
}
async addEpisode(id) {
debug(`Fetch Episode ${id} from ${this.routes.episode(id)}`)
const data = await this.fetch(this.routes.episode(id))
const duration = toPlayerTime(data.duration)
debug(`Fetch Transcripts for Episode ${id} from ${this.routes.transcripts(id)}`)
const transcripts = await this.fetch(this.routes.transcripts(id), [])
.then(map(transcript.normalizer))
.then(
map((transcript) => ({
...transcript,
speaker: this.actions.createReference(
contributor.name(this.options.typeName),
transcript.speaker
),
episode: this.actions.createReference(episode.name(this.options.typeName), data.slug)
}))
)
transcripts.forEach((transcript) => this.collections.transcript.addNode(transcript))
debug(`Fetch Contributors for Episode ${id} from ${this.routes.episodeContributors(id)}`)
const contributorList = await this.fetch(this.routes.episodeContributors(id), []).then(
map(episodeContributor.normalizer)
)
const chapters = await Promise.all(
(data.chapters || [])
.map(chapter.normalizer)
.reduce(transformations.chapters(duration), [])
.map(async (chapter) => ({
...chapter,
image: await this.cacheImage(chapter.image)
}))
)
debug(`Add Episode ${data.title} [${id}]`)
this.collections.episode.addNode({
...episode.normalizer(data),
title: data.mnemonic ? data.title.replace(data.mnemonic, '').trim() : data.title,
poster: await this.cacheImage(data.poster),
transcripts: transcripts.map(({ id }) =>
this.actions.createReference(transcript.name(this.options.typeName), id)
),
chapters,
timeline: transformations.timeline({
duration,
chapters,
transcripts: transcripts.map(({ id }) => this.collections.transcript.getNodeById(id))
}),
contributors: contributorList.map((mapping) => ({
details: this.actions.createReference(
contributor.name(this.options.typeName),
mapping.contributorId
),
role: this.actions.createReference(role.name(this.options.typeName), mapping.roleId),
group: this.actions.createReference(group.name(this.options.typeName), mapping.groupId)
})),
...transcripts.reduce(
(result, transcript) => ({
talkTime: result.talkTime + (transcript.end - transcript.start),
words: result.words + transcript.text ? wordsCounter(transcript.text).wordsCount : 0
}),
{ talkTime: 0, words: 0 }
)
})
}
/**
* Statistics
**/
async calculateContributorStatistic() {
const episodes = this.collections.episode.data()
const statistics = transformations.contributorStatistics(
this.collections.contributor.data().map((item) => ({
id: item.id,
contributor: this.actions.createReference(contributor.name(this.options.typeName), item.id)
}))
)
episodes.forEach((data) => {
data.contributors.forEach((contributor) =>
statistics.addEpisode(contributor.details.id, {
episode: this.actions.createReference(episode.name(this.options.typeName), data.id),
role: contributor.role
})
)
data.transcripts
.map(({ id }) => this.collections.transcript.getNodeById(id))
.forEach((transcript) =>
statistics.update(transcript.speaker.id, data.id, {
talkTime: transcript.end - transcript.start,
words: transcript.text ? wordsCounter(transcript.text).wordsCount : 0
})
)
})
statistics
.data()
.forEach((contributorStatistic) =>
this.collections.contributorStatistic.addNode(contributorStatistic)
)
}
async calculatePodcastStatistics() {
const episodes = this.collections.episode.data()
this.actions.addMetadata(
statistics.name(this.options.typeName),
episodes.reduce(
(result, episode) => {
const transcripts = get(episode, 'transcripts', []).map(({ id }) =>
this.collections.transcript.getNodeById(id)
)
const words = transcripts.reduce(
(res, transcript) =>
res + (transcript.text ? wordsCounter(transcript.text).wordsCount : 0),
0
)
const talkTime = transcripts.reduce(
(res, transcript) => res + transcript.end - transcript.start,
0
)
return {
episodes: result.episodes + 1,
talkTime: result.talkTime + talkTime,
words: result.words + words
}
},
{ episodes: 0, talkTime: 0, words: 0 }
)
)
}
}
module.exports = PodloveSource