Files
mixtape/zero.Backoffice.UI/app/utils/videoparser.ts
T

124 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-02-03 14:15:15 +01:00
export const YOUTUBE_REGEX = /youtu(?:\.be|be\.com)\/(?:.*v(?:\/|=)|(?:.*\/)?)([a-zA-Z0-9-_]+)/gim;
export const VIMEO_REGEX = /vimeo\.com\/(\d+)($|\/)/gim;
2021-12-06 15:06:38 +01:00
let youTubeApiKey = "xxx"; // TODO vue use from config
2021-02-03 14:15:15 +01:00
/**
* Get video ID from an URL.
* This currently works for YouTube and Vimeo links.
* @param {string} url
*/
2021-12-06 15:06:38 +01:00
export function getVideoId(url: string): string
2021-02-03 14:15:15 +01:00
{
if (!url)
{
return null;
}
if (url.indexOf('vimeo.com') > -1)
{
let matches = VIMEO_REGEX.exec(url);
return matches && matches[1];
}
else if (url.indexOf('youtu') > -1)
{
let matches = YOUTUBE_REGEX.exec(url);
return matches && matches[1];
}
return null;
};
2021-12-06 15:06:38 +01:00
export interface VideoMetadata
{
id: string,
url: string,
success: boolean,
data: object,
image: string,
title: string,
description: string
}
2021-02-03 14:15:15 +01:00
/**
* Get metadata for a vimeo ID.
* @param {string} id
*/
2021-12-06 15:06:38 +01:00
export async function getVimeoMetadata(id: string): Promise<VideoMetadata>
2021-02-03 14:15:15 +01:00
{
let result = {
id,
url: `https://vimeo.com/${id}`,
2021-12-06 15:06:38 +01:00
success: false,
data: null,
image: null,
title: null,
description: null
2021-02-03 14:15:15 +01:00
};
let response = await fetch(`https://vimeo.com/api/v2/video/${id}.json`);
if (response.ok)
{
let data = await response.json();
result.data = data[0];
result.image = result.data.thumbnail_large;
result.title = result.data.title;
result.description = result.data.description;
result.success = true;
}
return result;
};
/**
* Get metdata for a YouTube ID.
* @param {string} url
*/
2021-12-06 15:06:38 +01:00
export async function getYoutubeMetadata(id: string): Promise<VideoMetadata>
2021-02-03 14:15:15 +01:00
{
let result = {
id,
image: `https://i3.ytimg.com/vi/${id}/maxresdefault.jpg`,
url: `https://www.youtube.com/watch?v=${id}`,
2021-12-06 15:06:38 +01:00
success: false,
data: null,
title: null,
description: null
2021-02-03 14:15:15 +01:00
};
2021-12-06 15:06:38 +01:00
if (youTubeApiKey)
2021-02-03 14:15:15 +01:00
{
result.success = false;
2021-12-06 15:06:38 +01:00
let response = await fetch(`https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${id}&key=${youTubeApiKey}`);
2021-02-03 14:15:15 +01:00
if (response.ok)
{
let data = await response.json();
if (data && data.items && data.items.length)
{
result.data = data.items[0].snippet;
let thumbs = Object.values(result.data.thumbnails);
let thumb = thumbs[thumbs.length - 1];
result.image = thumb.url;
result.title = result.data.title;
result.description = result.data.description;
result.success = true;
}
2021-12-06 15:06:38 +01:00
}
2021-02-03 14:15:15 +01:00
}
else
{
2021-12-06 15:06:38 +01:00
result.success = true;
2021-02-03 14:15:15 +01:00
}
2021-12-06 15:06:38 +01:00
return result;
2021-02-03 14:15:15 +01:00
};