You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1012 B
36 lines
1012 B
import { VideoPreview } from "@/client/typings/trending";
|
|
|
|
import PipedTrending from "./typings/trending";
|
|
|
|
const videoIdRegex = /\/watch\?v=(.+)/;
|
|
const channelIdRegex = /\/channel\/(.+)/;
|
|
|
|
export default class Transformer {
|
|
public static trending(data: PipedTrending[]): VideoPreview[] {
|
|
return data.map((video) => {
|
|
const videoIdMatch = video.url.match(videoIdRegex);
|
|
|
|
const videoId = videoIdMatch !== null ? videoIdMatch[1] : null;
|
|
|
|
if (videoId === null) throw new Error("Piped: Missing trending video id");
|
|
|
|
const channelIdMatch = video.uploaderUrl.match(channelIdRegex);
|
|
|
|
const channelId = channelIdMatch !== null ? channelIdMatch[1] : null;
|
|
|
|
if (channelId === null)
|
|
throw new Error("Piped: Missing trending channelId");
|
|
|
|
return {
|
|
duration: video.duration * 1000,
|
|
views: video.views,
|
|
id: videoId,
|
|
uploaded: new Date(video.uploaded),
|
|
thumbnail: video.thumbnail,
|
|
title: video.title,
|
|
author: { id: channelId, name: video.uploaderName }
|
|
};
|
|
});
|
|
}
|
|
}
|