-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
417 lines (340 loc) · 13.7 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
411
412
413
414
415
416
417
var OIP_SEO = function () {
// Should we load the current LibraryD database initially? Possibily if under 5mb total?
};
// Method Name: OIP_SEO.generateTags
// Description: Generates all meta tags.
// Paramaters:
// - oipArtifact: The OIP Artifact we will be generating the meta tags for.
// - url: The Permalink back to this page
// - domain: The domain
// Returns: Returns meta tags.
OIP_SEO.prototype.generateTags = function(oipArtifact, url, domain){
var metaTags = '';
metaTags += this.generateBasicTags(oipArtifact, url, domain);
metaTags += this.generateOGTags(oipArtifact, url, domain);
metaTags += this.generateTCTags(oipArtifact, url, domain);
return metaTags;
}
// Method Name: OIP_SEO.generateBasicTags
// Description: Generates basic SEO meta tags.
// Paramaters:
// - oipArtifact: The OIP Artifact we will be generating the meta tags for.
// - url: The Permalink back to this page
// Returns: Returns basic seo meta tags.
OIP_SEO.prototype.generateBasicTags = function(oipArtifact, url, domain){
var artifact = '';
var oip = false;
// alexandria-media
if (oipArtifact['media-data']){
artifact = oipArtifact['media-data']['alexandria-media'];
} else { //OIP
artifact = conformOIP(oipArtifact)['media-data']['alexandria-media'];
}
var metaTags = '';
var IPFS_URL = 'https://ipfs.alexandria.io/ipfs/';
var artifact_link = "https://alexandria.io/browser/" + url.split('/')[url.split('/').length -1];
metaTags += '<title>'+ artifact.info.title.replace(/"/g,'\"') + ' | ΛLΞXΛNDRIΛ</title>';
metaTags += tagGen('description', artifact.info.description.replace(/(")/g,'\"').slice(0,140), true);
return metaTags;
}
// Method Name: OIP_SEO.generateOGTags
// Description: Generates Open Graph meta tags.
// Paramaters:
// - oipArtifact: The OIP Artifact we will be generating the meta tags for.
// - url: The Permalink back to this page
// Returns: Returns OpenGraph compliant meta tags.
OIP_SEO.prototype.generateOGTags = function(oipArtifact, url, domain){
var artifact = '';
var oip = false;
// alexandria-media
if (oipArtifact['media-data']){
artifact = oipArtifact['media-data']['alexandria-media'];
} else { //OIP
artifact = conformOIP(oipArtifact)['media-data']['alexandria-media'];
}
var metaTags = '';
var IPFS_URL = 'https://ipfs.alexandria.io/ipfs/'
var artifact_link = "https://alexandria.io/browser/" + url.split('/')[url.split('/').length -1];
metaTags += tagGen('og:site_name', "Alexandria.io");
metaTags += tagGen('og:url', artifact_link);
metaTags += tagGen('og:title', artifact.info.title.replace(/"/g,'\"'));
metaTags += tagGen('og:description', artifact.info.description.replace(/(")/g,'\"').slice(0,140));
var coverfname = '';
var files = '';
if (artifact.storage && artifact.storage.files){
files = artifact.storage.files;
} else if (artifact.info['extra-info'] && artifact.info['extra-info'].files){
files = artifact.info['extra-info'].files;
}
for (var file in files){
if (files[file].type == "coverArt" || files[file].type == "preview"){
coverfname = files[file].fname;
}
}
if (typeof files[0] === 'object')
var mainURL = IPFS_URL + (oip ? artifact.storage.location : artifact.torrent) + '/' + files[0].fname;
else if (artifact.info['extra-info'] && artifact.info['extra-info'].filename && typeof artifact.info['extra-info'].filename == 'string')
var mainURL = IPFS_URL + artifact.torrent + '/' + artifact.info['extra-info'].filename;
else if (oip)
var mainURL = IPFS_URL + artifact.storage.location + '/' + files[0].fname;
else
var mainURL = '';
var imageURL = IPFS_URL + (oip ? artifact.storage.location : artifact.torrent) + '/' + coverfname;
mainURL = encodeURI(mainURL);
imageURL = encodeURI(imageURL);
if (!imageURL || imageURL == '' || !coverfname)
imageURL = "https://" + domain + '/img/cover_placeholder.jpg';
if (imageURL){
metaTags += tagGen('og:image', imageURL);
}
if (typeof files[0] === 'object')
var cost = files[0].sugPlay;
else if (typeof artifact.payment.sug_tip == 'string')
var cost = artifact.payment.sug_tip.split(',')[0];
else
var cost = '';
if (artifact.type == "music"){
//################################
// MUSIC
//################################
metaTags += tagGen('og:type', 'video.other');
if (!cost){
// Music URL tags
metaTags += tagGen('og:audio', mainURL);
metaTags += tagGen('og:audio:secure_url', mainURL);
metaTags += tagGen('og:audio:type', 'audio/mpeg');
// Optional Tags
if (files[0])
if(files[0].duration)
metaTags += tagGen('music:duration', parseInt(files[0].duration));
}
} else if (artifact.type == "video"){
//################################
// VIDEO
//################################
metaTags += tagGen('og:type', "video.other");
// Video URL tags
if (!cost){
metaTags += tagGen('og:video', mainURL);
metaTags += tagGen('og:video:secure_url', mainURL);
metaTags += tagGen('og:video:type', "video/mp4");
// Optional Tags
if (files[0])
metaTags += tagGen('og:video:duration', parseInt(files[0].duration));
}
} else if (artifact.type == "podcast"){
//################################
// PODCAST
//################################
metaTags += tagGen('og:type', "music.radio_station");
} else if (artifact.type == "pdf"){
//################################
// PDF
//################################
metaTags += tagGen('og:type', "book");
} else if (artifact.type == "movie"){
//################################
// MOVIE
//################################
metaTags += tagGen('og:type', "video.movie");
// Video URL tags
if (!cost){
metaTags += tagGen('og:video', mainURL);
metaTags += tagGen('og:video:secure_url', mainURL);
metaTags += tagGen('og:video:type', "video/mp4");
}
// Optional Tags
if (files[0])
metaTags += tagGen('og:video:duration', parseInt(files[0].duration));
} else if (artifact.type == "thing"){
//################################
// THING
//################################
metaTags += tagGen('og:type', "article");
} else if (artifact.type == "html"){
//################################
// HTML
//################################
metaTags += tagGen('og:type', "article");
}
return metaTags;
}
// Method Name: OIP_SEO.generateTCTags
// Description: Generates Twitter Card meta tags.
// Paramaters:
// - oipArtifact: The OIP Artifact we will be generating the meta tags for.
// - url: The Permalink back to this page
// Returns: Returns Twitter Card compliant meta tags.
OIP_SEO.prototype.generateTCTags = function(oipArtifact, url, domain){
var artifact = '';
var oip = false;
// alexandria-media
if (oipArtifact['media-data']){
artifact = oipArtifact['media-data']['alexandria-media'];
} else { //OIP
artifact = conformOIP(oipArtifact)['media-data']['alexandria-media'];
}
var metaTags = '';
var IPFS_URL = 'https://ipfs.alexandria.io/ipfs/';
var artifact_link = "https://alexandria.io/browser/" + url.split('/')[url.split('/').length -1];
var coverfname = '';
var files = '';
if (artifact.storage && artifact.storage.files){
files = artifact.storage.files;
} else if (artifact.info['extra-info'] && artifact.info['extra-info'].files){
files = artifact.info['extra-info'].files;
}
for (var file in files){
if (files[file].type == "coverArt" || files[file].type == "preview"){
coverfname = files[file].fname;
}
}
if (files[0] == 'object')
var mainURL = IPFS_URL + (oip ? artifact.storage.location : artifact.torrent) + '/' + files[0].fname;
else if (artifact.info['extra-info'] && artifact.info['extra-info'].filename && typeof artifact.info['extra-info'].filename == 'string')
var mainURL = IPFS_URL + artifact.torrent + '/' + artifact.info['extra-info'].filename;
else if (oip)
var mainURL = IPFS_URL + artifact.storage.location + '/' + files[0].fname;
else
var mainURL = '';
var imageURL = IPFS_URL + (oip ? artifact.storage.location : artifact.torrent) + '/' + coverfname;
mainURL = encodeURI(mainURL);
imageURL = encodeURI(imageURL);
if ( (artifact.type != "music") && (artifact.type != "video") ){
//################################
// NON AUDIO/VIDEO ARTIFACTS
//################################
if (!coverfname) {
metaTags += tagGen('twitter:card', "summary", true);
} else {
/* Need cover image dimensions saved with artifact metadata for large image summary cards
if ( (imgHeight >= 157) && (imgWidth >= 300) && (aspectRatio >= .5) && (aspectRatio <= 1) ) {
metaTags += tagGen('twitter:card', "summary_large_image", true);
} else {
metaTags += tagGen('twitter:card', "summary", true);
}
*/
// In the meantime, just use the regular summary card
metaTags += tagGen('twitter:card', "summary", true);
}
}
metaTags += tagGen('twitter:site', "@alexandria");
metaTags += tagGen('twitter:title', artifact.info.title.replace(/"/g,'\"'));
metaTags += tagGen('twitter:description', artifact.info.description.replace(/(")/g,'\"').slice(0,140));
if (!imageURL || imageURL == '' || !coverfname)
imageURL = "https://" + domain + '/img/cover_placeholder.jpg';
if (imageURL){
metaTags += tagGen('twitter:image', imageURL);
}
if (typeof files == 'object')
var cost = files[0].sugPlay;
else if (typeof artifact.payment.sug_tip == 'string')
var cost = artifact.payment.sug_tip.split(',')[0];
else
var cost = '';
if ( (artifact.type == "music") || (artifact.type == "video") ){
//################################
// AUDIO/VIDEO
//################################
var artist = (oip ? artifact.info.extraInfo.artist : artifact.info['extra-info'].artist);
var priceStr = (cost ? '$' + cost : "Free!");
if (files == 'object')
var durStr = secondsToHms(files[0].duration);
else
var durStr = 0;
if (!cost){
var playerURL = artifact_link.slice(0, -(artifact_link.split('/')[artifact_link.split('/').length - 1].length)) + 'player/' + artifact_link.split('/')[artifact_link.split('/').length - 1];
metaTags += tagGen('twitter:card', "player", true);
metaTags += tagGen('twitter:player', playerURL, true);
metaTags += tagGen('twitter:player:stream', mainURL, true);
if (artifact.type == "video") {
metaTags += tagGen('twitter:player:content_type', "video/mp4", true);
metaTags += tagGen('twitter:player:width', "821", true);
metaTags += tagGen('twitter:player:height', "461", true);
} else {
metaTags += tagGen('twitter:player:content_type', "audio/mpeg", true);
metaTags += tagGen('twitter:player:width', "403", true);
metaTags += tagGen('twitter:player:height', "190", true);
}
} else {
metaTags += tagGen('twitter:card', "summary_large_image", true);
metaTags += tagGen('twitter:label1', "Artist", true);
metaTags += tagGen('twitter:data1', artist, true);
metaTags += tagGen('twitter:label2', "Cost/Length", true);
metaTags += tagGen('twitter:data2', priceStr + ' / ' + durStr, true);
}
}
return metaTags;
}
// Method Name: tagGen
// Description: Generate a single meta tag.
// Paramaters:
// - prop: The meta proprety tag.
// - content: The meta content tag
// - nameFlag: If this is true, instead of using proprety as the tag, name will be used.
// Returns: Returns OpenGraph compliant meta tags.
var tagGen = function(prop, content, nameFlag){
return '<meta ' + (nameFlag ? 'name' : 'property') + '="' + prop + '" content="' + content + '" />';
}
function secondsToHms(d) {
d = Number(d);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
var hDisplay = h > 0 ? h + "h " : "";
var mDisplay = m > 0 ? m + "m " : "";
var sDisplay = s > 0 ? s + "s" : "";
return hDisplay + mDisplay + sDisplay;
}
function conformOIP(oipObject){
// Pull out of casing
var oip = oipObject["oip-041"];
var alexandriaObject = {
"media-data":{
"alexandria-media":{
"torrent": oip.artifact.storage.location,
"publisher": oip.artifact.publisher,
"timestamp": oip.artifact.timestamp*1000,
"type": oip.artifact.type,
"info": {
"title": oip.artifact.info.title,
"description":oip.artifact.info.description,
"year": oip.artifact.info.year,
"extra-info": oip.artifact.info.extraInfo ? oip.artifact.info.extraInfo : oip.artifact.info['extra-info']
},
"payment":oip.artifact.payment
},
"signature":oip.signature
},
"txid": oipObject.txid,
"block": oipObject.block
}
alexandriaObject["media-data"]["alexandria-media"]["info"]["extra-info"]["DHT Hash"] = oip.artifact.storage.location;
// Add artist name if it exists to the "publisher-name" for now. This is a hack as oip-041 standards do not include a publisher name. This might need to be updated in LibraryD to be included.
if (oip.artifact.info.extraInfo && oip.artifact.info.extraInfo.artist){
alexandriaObject['publisher-name'] = oip.artifact.info.extraInfo.artist;
}
if(oip.artifact.info['extra-info'] && oip.artifact.info['extra-info'].artist){
alexandriaObject['publisher-name'] = oip.artifact.info['extra-info'].artist;
}
// Conform each file to be fixed.
// Add files.
if (oip.artifact.storage.files){
var files = oip.artifact.storage.files;
for (var i = 0; i < files.length; i++) {
if (files[i].filename && !files[i].fname){
files[i].fname = files[i].filename;
delete files[i].filename;
}
if (files[i].displayname && !files[i].dname){
files[i].dname = files[i].displayname;
delete files[i].displayname;
}
}
alexandriaObject["media-data"]["alexandria-media"]["info"]["extra-info"].files = [];
alexandriaObject["media-data"]["alexandria-media"]["info"]["extra-info"].files = files;
}
return alexandriaObject;
}
// Expose OIP_SEO and all methods.
module.exports = new OIP_SEO();