-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.ts
More file actions
326 lines (281 loc) · 8.93 KB
/
cli.ts
File metadata and controls
326 lines (281 loc) · 8.93 KB
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
import 'dotenv/config'
import { program } from 'commander'
import { publishToClaimableAccount } from './src/claimable/createUserPublish'
import { cleanupFiles } from './src/cleanupFiles'
import { releaseRepo, userRepo } from './src/db'
import { pgMigrate } from './src/db/migrations'
import { parseDelivery } from './src/parseDelivery'
import {
DEFAULT_ALBUM_DEAL,
DEFAULT_TRACK_DEAL,
prepareTrackMetadatas,
publishValidPendingReleases,
} from './src/publishRelease'
import { clmReport } from './src/reporting/clm_report'
import { pollForNewLSRFiles } from './src/reporting/lsr_reader'
import { pollS3 } from './src/s3poller'
import { sync } from './src/s3sync'
import { getSdk } from './src/sdk'
import { startServer } from './src/server'
import { sources } from './src/sources'
import { startUsersPoller } from './src/usersPoller'
import { sleep } from './src/util'
sources.load()
program
.name('ddexer')
.description('CLI to process ddex files')
.version('0.1')
.option('-d, --debug', 'output extra debugging')
program
.command('parse')
.description('Parse DDEX xml and print results')
.argument('<source>', 'source name to use')
.argument('<path>', 'path to ddex xml file')
.action(async (source, p) => {
const releases = await parseDelivery(source, p)
console.log(JSON.stringify(releases, undefined, 2))
})
program
.command('publish-to-user')
.description('Publish a single release to a user')
.argument('<releaseId>', 'release ID')
.argument('<userId>', 'encoded user ID to publish to')
.option(
'--prepend-artist',
'Prepend artist name: <artist> - <title>. Useful for label accounts'
)
.action(async (releaseId, userId, opts) => {
const releaseRow = await releaseRepo.get(releaseId)
const release = releaseRow!
if (!releaseRow || !release) {
throw new Error(`release not found: ${releaseId}`)
}
const source = sources.findByName(releaseRow.source)
const user = await userRepo.findById(userId)
if (!user) {
throw new Error(`connected user not found: ${userId}`)
}
release.audiusUser = userId
const artistName = release.artists[0].name
if (opts.prependArtist) {
if (!release.title.startsWith(artistName)) {
release.title = `${artistName} - ${release.title}`
}
for (const s of release.soundRecordings) {
if (!s.title.startsWith(artistName)) {
s.title = `${artistName} - ${s.title}`
}
}
}
if (opts.useDefaultDeal) {
release.deals.push(
release.soundRecordings.length > 1
? DEFAULT_ALBUM_DEAL
: DEFAULT_TRACK_DEAL
)
}
console.log(JSON.stringify(release, undefined, 2))
console.log('publishing in 5s...')
await sleep(5_000)
await releaseRepo.upsert(
releaseRow.source,
releaseRow.xmlUrl,
releaseRow.messageTimestamp,
release
)
await publishValidPendingReleases()
process.exit(0) // sdk client doesn't know when to quit
})
program
.command('publish-to-claimable-account')
.description('Publish a single release to a user, create user if not exists')
.argument('<releaseId>', 'release ID')
.action(async (releaseId) => {
await publishToClaimableAccount(releaseId)
})
program
.command('sync-s3')
.description('Sync target directory from S3')
.argument('<path>', 'path after s3:// to sync')
.action(async (p) => {
await sync(p)
})
program
.command('poll-s3')
.description('Pull down assets from S3 and process')
.option('--reset', 'reset cursor and re-detect bucket structure')
.option('--bucket <name>', 'only poll this bucket (e.g. ddex-prod-onchainmusic-raw)')
.action(async (opts) => {
await pollS3(opts.reset, opts.bucket ? { bucket: opts.bucket } : undefined)
})
program
.command('server')
.description('start server without background processes, useful for dev')
.action(async () => {
startServer()
})
program
.command('worker')
.description('start background processes, useful for dev')
.action(async () => {
startWorker()
})
program
.command('start')
.description('Start both server + background processes')
.action(async () => {
startServer()
startWorker()
})
program
.command('delete')
.description('Delete a release... USE CAUTION')
.argument('<release_id>', 'release ID to delete')
.action(async (releaseId) => {
const releaseRow = await releaseRepo.get(releaseId)
if (!releaseRow) {
console.warn(`no release for id: ${releaseId}`)
process.exit(1)
}
const release = releaseRow
const userId = release.audiusUser
if (!releaseRow.entityId) {
console.warn(`release id ${releaseId} has no entityId`)
process.exit(1)
}
if (!userId) {
console.warn(`release id ${releaseId} has no audiusUser`)
process.exit(1)
}
const sourceConfig = sources.findByName(releaseRow.source)!
const sdk = getSdk(sourceConfig)
console.warn(
`deleting ${releaseRow.entityType} ${releaseId}: ${release.title}`
)
let result: any
if (releaseRow.entityType == 'album') {
const IS_PROD = process.env.NODE_ENV == 'production'
const API_HOST = IS_PROD
? 'https://api.audius.co'
: 'https://api.staging.audius.co'
const albumUrl = `${API_HOST}/v1/full/playlists/${releaseRow.entityId!}`
const sdkAlbums = await fetch(albumUrl).then((r) => r.json())
const sdkAlbum = sdkAlbums.data[0]
// console.log(sdkAlbum)
// console.log(sdkAlbum.tracks)
for (const t of sdkAlbum.tracks) {
console.log('delete track', t.id)
await sdk.tracks.deleteTrack({
trackId: t.id,
userId,
})
}
result = await sdk.albums.deleteAlbum({
albumId: releaseRow.entityId,
userId,
})
} else {
result = await sdk.tracks.deleteTrack({
trackId: releaseRow.entityId,
userId,
})
}
console.warn(`deleted ${releaseId}`, result)
process.exit(0)
})
program
.command('report-clm')
.description(
'Generate CLM report and push to reporting.clm bucket defined in data/sources.json'
)
.action(async () => {
clmReport()
})
program
.command('report-lsr')
.description('Parse LSR files')
.action(async () => {
pollForNewLSRFiles()
})
program
.command('republish-album')
.description('issue sdk updates for all album tracks')
.argument('<release_id>', 'release ID to republish')
.action(async (releaseId) => {
const releaseRow = await releaseRepo.get(releaseId)
if (!releaseRow) {
throw new Error(`Release ID ${releaseId} not found`)
}
if (releaseRow.entityType != 'album') {
throw new Error(`Release ID ${releaseId} must be a published album`)
}
console.log(
'republish',
releaseId,
releaseRow.entityType,
releaseRow.entityId
)
const sourceConfig = sources.findByName(releaseRow.source)!
const sdk = getSdk(sourceConfig)
// await new Promise((r) => setTimeout(r, 1_000))
// const sel = await sdk.services.discoveryNodeSelector.getSelectedEndpoint()
// console.log('selected', sel)
// await new Promise((r) => setTimeout(r, 1_000))
// I want to do this but it hangs forever :shrug:
// const sdkAlbum = await sdk.full.playlists.getPlaylist({
// playlistId: releaseRow.entityId!,
// })
const IS_PROD = process.env.NODE_ENV == 'production'
const API_HOST = IS_PROD
? 'https://api.audius.co'
: 'https://api.staging.audius.co'
const albumUrl = `${API_HOST}/v1/full/playlists/${releaseRow.entityId!}`
const sdkAlbum = await fetch(albumUrl).then((r) => r.json())
const trackUpdates = prepareTrackMetadatas(
sourceConfig,
releaseRow,
releaseRow
)
for (const sdkTrack of sdkAlbum.data![0].tracks) {
let trackUpdate = trackUpdates.find(
(s) => sdkTrack.isrc && s.isrc == sdkTrack.isrc
)
if (!trackUpdate) {
throw new Error(`failed to find track record for: ${sdkTrack.title}`)
}
// this is needed if generatePreview is true
trackUpdate.trackCid = sdkTrack.track_cid
console.log('update track', sdkTrack.id, sdkTrack.title, trackUpdate)
try {
await sdk.tracks.updateTrack({
trackId: sdkTrack.id,
userId: sdkTrack.user.id,
metadata: trackUpdate,
generatePreview: true,
})
} catch (e) {
console.log('track update failed', sdkTrack.id, sdkTrack.title, e)
throw e
}
}
process.exit(0)
})
program.command('cleanup').description('remove temp files').action(cleanupFiles)
async function main() {
await pgMigrate()
program.parse()
}
main()
async function startWorker() {
startUsersPoller().catch(console.error)
// eslint-disable-next-line no-constant-condition
while (true) {
await sleep(3_000)
console.log('polling...')
await pollS3()
await pollForNewLSRFiles()
await clmReport()
await publishValidPendingReleases()
await sleep(5 * 60_000)
}
}