/
DTUtils.js
256 lines (183 loc) · 6.59 KB
/
DTUtils.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
var DTUtils = (function() {
var utils = {};
utils.self = this;
utils.init = function() {
return true
}
utils.about = function() {
var ui = DocumentApp.getUi();
var app = HtmlService.createHtmlOutputFromFile('about.html').setHeight(400).setWidth(750);
ui.showModalDialog(app, 'DocuTube')
}
utils.buildEmbedUrl = function(resourceType, id) {
var url;
if(resourceType === "channel") {
url = 'https://youtube.com/channel/' + id;
} else if(resourceType === "playlist") {
url = 'https://youtube.com/playlist?list=' + id;
} else if(resourceType === "video") {
url = 'https://youtu.be/' + id;
}
return url;
}
// Find and return a cached item
utils.getCachedResource = function(cacheKey, resourceId, resourceType) {
cacheKey = cacheKey || null;
resourceId = resourceId || null;
resourceType = resourceType || null;
var cache;
cache = DTCache.get(cacheKey);
var resource = cache.items.find(function(el) {
if(resourceType === "channel") {
return el.id.channelId === resourceId;
} else if(resourceType === "playlist") {
return el.id.playlistId === resourceId;
} else if(resourceType === "video") {
return el.id.videoId === resourceId;
}
});
return resource;
}
utils.getResourcePreview = function(args, resource, resourceType) {
var preview;
if(resourceType === "video" || resourceType === undefined) {
preview = YouTube.Videos.list("player", args);
DTCache.store(preview.items[0].id, preview, 3600);
} else if(resourceType === "playlist") {
preview = YouTube.Playlists.list("player", args);
// Playlists come back linked via http. Convert to https before returning.
preview.items[0].player.embedHtml = utils.secureHttp(preview.items[0].player.embedHtml);
DTCache.store(preview.items[0].id, resource, 3600);
} else if(resourceType === "channel") {
// These can be pulled from the cache
preview = resource.snippet.thumbnails;
DTCache.store(resource.snippet.channelId, resource, 3600);
}
return preview;
}
utils.getTypeFromUrl = function(url) {
// Check the URL for the type keys
var re = /(v|playlist|channel)/
if(url.match(re) && url.match(re)[0] === "playlist") {
return "playlist";
} else if(url.match(re) && url.match(re)[0] === "channel") {
return "channel";
} else {
return "video";
}
}
// Videos won't embed if an http scheme is used. This forces any
// http string to https.
utils.secureHttp = function(httpUrl) {
var re = /http/gm;
var httpsUrl = httpUrl.replace(re, 'https');
return httpsUrl;
}
utils.shorten = function(string) {
return Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, string));
}
/*********************** GET ALL LINKS ************************/
// Loop through all elements in the Doc and return all links
// This is a modified script from StackOverflow http://stackoverflow.com/a/18731628/2278429
// Recursive search through elements of a document
/**************************************************************/
utils.buildUrl = function(textObj) {
var curUrl, url;
var inUrl = false;
var text = textObj.getText();
// Check individual characters in the string
for (var ch=0; ch < text.length; ch++) {
url = textObj.getLinkUrl(ch);
if (url != null) {
return url;
}
else {
if (inUrl) {
// Not any more, we're not.
inUrl = false;
curUrl = {};
}
}
}
}
utils.getBodyVideos = function(element) {
var curUrl, url;
var links = [];
element = element || DocumentApp.getActiveDocument().getBody();
Logger.log(element)
// Extract a link from a TEXT element
if (element.getType() === DocumentApp.ElementType.TEXT) {
var textObj = element.editAsText();
if(this.buildUrl(textObj)) {
links.push(this.buildUrl(textObj));
}
}
// Handle an INLINE_IMAGE with a link attached.
else if(element.getType() === DocumentApp.ElementType.INLINE_IMAGE) {
url = element.getLinkUrl();
if(url != null) {
url = String(url);
links.push(url);
}
}
else if(element.getType() === DocumentApp.ElementType.INLINE_DRAWING) {
url = element.getLinkUrl();
if(url != null) {
url = String(url);
links.push(url);
}
}
else if(element.getType() === DocumentApp.ElementType.FOOTNOTE) {
var footnote = element.getFootnoteContents().editAsText();
if(this.buildUrl(footnote)) {
links.push(this.buildUrl(footnote));
}
}
// None of these elements can have a link attached. Return an empty object.
else if(element.getType() === DocumentApp.ElementType.HORIZONTAL_RULE) {
curUrl = {};
}
else if(element.getType() === DocumentApp.ElementType.EQUATION) {
curUrl = {};
}
else if(element.getType() === DocumentApp.ElementType.PAGE_BREAK) {
curUrl = {};
}
else {
var numChildren = element.getNumChildren();
for (var i=0; i<numChildren; i++) {
links = links.concat(this.getBodyVideos(element.getChild(i)));
}
}
// Return an array of link objects
return links;
}
utils.getCommentVideos = function() {
// Get the comments
const docId = DocumentApp.getActiveDocument().getId();
Logger.log(docId)
var comments = Drive.Comments.list(docId);
var commentFrames = [];
for(var i=0; i<comments.items.length; i++) {
var comment = comments.items[i].content;
// Do some regex checking
// https://stackoverflow.com/a/31711517/2278429
var re = /(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?\/?.*(?:watch|embed)?(?:.*v=|v\/|\/)([\w\-_]+)\&?/;
if(comment.match(re)) {
commentFrames.push(comment.match(re)[0]);
}
}
return commentFrames;
}
/********************* DISPLAY LINKS *************************/
// Pull all the links in the Document.
// For testing purposes only. Not used in client view.
/*************************************************************/
utils.displayLinks = function() {
var links = this.getAllLinks;
for(var i=0;i<links.length;i++) {
Logger.log(links[i].url);
}
}
return utils;
})();