-
Notifications
You must be signed in to change notification settings - Fork 0
/
museviewer.js
114 lines (101 loc) · 4.35 KB
/
museviewer.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
function getYouTubeVideoID(url) {
try {
const urlObj = new URL(url);
const hostname = urlObj.hostname;
// Check if the URL is from YouTube
if (hostname === 'www.youtube.com' || hostname === 'youtube.com' || hostname === 'm.youtube.com' || hostname === 'youtu.be') {
// Extract the video ID from the URL
if (hostname === 'youtu.be') {
// For shortened youtu.be URLs
return urlObj.pathname.slice(1);
} else {
// For regular youtube.com URLs
return urlObj.searchParams.get('v');
}
} else {
return null; // Not a YouTube URL
}
} catch (e) {
return null; // Invalid URL
}
}
// Function to process the publication data
function processPublications(data) {
let publications = data["ore:describes"]['publication'];
if (!publications) {
console.error('No publication data found.');
return;
}
if (!Array.isArray(publications)) {
publications = [publications]; // Convert single object to an array
}
publications.forEach(publication => {
let url = publication['publicationURL'];
let youtubeId = getYouTubeVideoID(url);
let urlObject = new URL(url);
let pathname = urlObject.pathname.split('/');
let videoId = pathname[pathname.length - 1];
console.log("URL:", publication);
console.log("YouTube ID:", youtubeId); // Prints YouTube video ID
console.log("Video ID from pathname:", videoId); // Prints the last segment of the path
});
return publications
}
// Function to recursively list all elements in the JSON object
function listElements(obj, indent = '') {
let result = '';
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
result += `${indent}${key}:\n`;
result += listElements(obj[key], indent + ' ');
} else {
result += `${indent}${key}: ${obj[key]}\n`;
}
}
}
return result;
}
const params = new URLSearchParams(window.location.search);
const persistentId = params.get('persistentId');
const host2 = 'https://database.sharemusic.se';
const host = 'https://dev.now.museum';
const fullURL = host + '/api/datasets/export?exporter=OAI_ORE&persistentId=' + persistentId;
const directURL = host + '/dataset.xhtml?persistentId=' + persistentId;
console.log('Persistent ID:', persistentId);
fetch(fullURL)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.then(data => {
// Extract the title from the JSON data
const title = data["ore:describes"]["title"];
// Insert the title into the HTML element
document.getElementById("title").textContent = title;
document.getElementById("description").textContent = data["ore:describes"]["citation:dsDescription"]["citation:dsDescriptionValue"];
document.getElementById('persistentId').innerHTML = '<a href="' + directURL + '">' + persistentId + '</a>';
let url = data["ore:describes"]['publication']['publicationURL']
let urls = processPublications(data);
let videoId = "";
let youtubeId = "";
let urlObject = new URL(url);
youtubeId = getYouTubeVideoID(url);
let pathname = urlObject.pathname.split('/');
videoId = pathname[pathname.length - 1];
console.log('YouTube: ', youtubeId);
console.log(urls);
if (videoId) {
document.getElementById("video").src = "https://player.vimeo.com/video/" + videoId + "?h=97c0e1a853";
} else { document.getElementById("video").src = ""; }
if (youtubeId) {
document.getElementById("video").src = "https://www.youtube.com/embed/" + youtubeId;
} else { document.getElementById("video").src = ""; }
const elementsList = listElements(data);
document.getElementById('jsonElements').textContent = elementsList;
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});