-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch-update-omny-podcast.js
107 lines (88 loc) · 3.57 KB
/
batch-update-omny-podcast.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
const request = require('request');
const promiseSerial = require('promise-serial');
require('dotenv').config();
const MEGAPHONE_API_PODCAST_URL = process.env.MEGAPHONE_API_PODCAST_URL || "";
const MEGAPHONE_API_TOKEN = process.env.MEGAPHONE_API_TOKEN || "";
const OMNYSTUDIO_MANAGEMENT_API_PODCAST_URL = process.env.OMNYSTUDIO_MANAGEMENT_API_PODCAST_URL || "";
const OMNYSTUDIO_MANAGEMENT_API_TOKEN = process.env.OMNYSTUDIO_MANAGEMENT_API_TOKEN || "";
const megaphoneApi = request.defaults({
headers: {
'Authorization': `Token token=${MEGAPHONE_API_TOKEN}`
}
});
const omnyStudioManagementApi = request.defaults({
headers: {
'Authorization': `OmnyToken ${OMNYSTUDIO_MANAGEMENT_API_TOKEN}`
}
});
// Get all episoes of a program from Omny Studio
const omnyStudioEpisodes = require('./filmweek-omnystudio.json');
console.log(omnyStudioEpisodes.Clips.length);
// Get all episodes of a program from Megaphone
const megaphoneEpisodes = require('./filmweek-megaphone.json');
console.log(megaphoneEpisodes.length);
// Filter to only the episodes that have midroll
const megaphoneEpisodesWithMidroll = megaphoneEpisodes.filter(episode => episode.insertionPoints.length > 0);
console.log(megaphoneEpisodesWithMidroll.length);
// For every episode, find the equivalent episode in Omny
const matchedEpisodes = [];
const unmatchedEpisodes = [];
megaphoneEpisodesWithMidroll.forEach(megaphoneEpisode => {
const matchingOmnyEpisode = omnyStudioEpisodes.Clips.filter(episode => {
if (megaphoneEpisode.title.includes(episode.Title) && episode.Monetization.MidRolls.length === 0) {
return true;
} else {
return false;
}
});
if (matchingOmnyEpisode.length > 0) {
matchedEpisodes.push({ megaphoneEpisode: megaphoneEpisode, omnyEpisode: matchingOmnyEpisode[0] });
} else {
unmatchedEpisodes.push(megaphoneEpisode);
}
});
console.log({ matchedEpisodesLength: matchedEpisodes.length });
console.log({ unmatchedEpisodesLength: unmatchedEpisodes.length });
// Perform a put request
const updateOmnyEpisodeWithMidrolls = (clipId, midrolls, title) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log({ clipId, midrolls, title });
omnyStudioManagementApi.post({
url: `https://api.omnystudio.com/v0/clips/${clipId}`,
body: {
"Monetization": {
"PreRoll": true,
"PostRoll": true,
"MidRolls": midrolls
}
},
json: true
}, (err, response, body) => {
if (err) {
reject(err);
console.warn(err);
return;
}
console.log(body);
resolve(body);
});
}, 2000);
});
}
// Transform the midrolls from seconds to HH:MM:SS
const transformMidrolls = (midrollArray) => {
const transformedMidrolls = midrollArray.map((insertionPoint) => {
return new Date(insertionPoint * 1000).toISOString().substr(11, 8);
});
return transformedMidrolls;
}
const testEpisodeList = matchedEpisodes;
const promises = testEpisodeList.map((episode) => {
const midrolls = episode.megaphoneEpisode.insertionPoints;
const transformedMidrolls = transformMidrolls(midrolls);
const clipId = episode.omnyEpisode.Id;
const title = episode.omnyEpisode.Title;
return updateOmnyEpisodeWithMidrolls.bind(this, clipId, transformedMidrolls, title);
});
promiseSerial(promises);