fucked up my commit history and removed node modules folder from tree
This commit is contained in:
@@ -1,5 +1,20 @@
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
import { GOOGLE_API_KEY } from '$app/env/private';
|
||||
import { decode } from 'he';
|
||||
|
||||
export enum ContentType {
|
||||
YOUTUBE_VIDEO,
|
||||
BLOG_POST
|
||||
}
|
||||
|
||||
export interface ContentItem {
|
||||
title: string;
|
||||
description: string;
|
||||
published: Date;
|
||||
image: string | undefined;
|
||||
link: string;
|
||||
type: ContentType;
|
||||
}
|
||||
|
||||
const parser = new XMLParser({
|
||||
ignoreAttributes: false,
|
||||
@@ -8,7 +23,7 @@ const parser = new XMLParser({
|
||||
trimValues: true
|
||||
});
|
||||
|
||||
async function fetchXml(url: string) {
|
||||
async function fetchXml(fetch: typeof globalThis.fetch, url: string) {
|
||||
const response = await fetch(url);
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -20,16 +35,82 @@ async function fetchXml(url: string) {
|
||||
return parser.parse(xmlText);
|
||||
}
|
||||
|
||||
export async function fetchBlogArticles(url: string) {
|
||||
const blogArticles = await fetchXml(url);
|
||||
async function fetchBlogArticles(fetch: typeof globalThis.fetch, url: string) {
|
||||
// Need to make into interface
|
||||
const blogArticles = await fetchXml(fetch, url);
|
||||
|
||||
let items: ContentItem[] = [];
|
||||
for (let article of blogArticles.rss.channel.item) {
|
||||
items.push({
|
||||
title: article.title,
|
||||
description: decode(article.description),
|
||||
published: new Date(article.pubDate),
|
||||
link: article.link,
|
||||
image: undefined,
|
||||
type: ContentType.BLOG_POST
|
||||
});
|
||||
}
|
||||
return items.slice(0, 5);
|
||||
}
|
||||
|
||||
export async function getChannelInfo(channelId: string) {
|
||||
async function getContentPlaylistId(fetch: typeof globalThis.fetch, channelId: string) {
|
||||
const response = await fetch(
|
||||
`https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=${channelId}&key=${GOOGLE_API_KEY}`
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get Channel Information: ${response.status}`);
|
||||
}
|
||||
|
||||
// Channel uploads collection
|
||||
return data.items[0].contentDetails.relatedPlaylists.uploads;
|
||||
}
|
||||
|
||||
async function getLatestContent(
|
||||
fetch: typeof globalThis.fetch,
|
||||
channelId: string
|
||||
): Promise<ContentItem[]> {
|
||||
const playlistId = await getContentPlaylistId(fetch, channelId);
|
||||
|
||||
const response = await fetch(
|
||||
`https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&playlistId=${playlistId}&maxResults=5&key=${GOOGLE_API_KEY}`
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get Channel Content: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
let items: ContentItem[] = [];
|
||||
for (let video of data.items) {
|
||||
const info = video.snippet;
|
||||
|
||||
items.push({
|
||||
title: info.title,
|
||||
description: info.description.split('\n')[0],
|
||||
published: new Date(info.publishedAt),
|
||||
image: info.thumbnails.maxres.url,
|
||||
link: `https://www.youtube.com/watch?v=${info.resourceId.videoId}`,
|
||||
type: ContentType.YOUTUBE_VIDEO
|
||||
});
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
export async function getContentItems(
|
||||
fetch: typeof globalThis.fetch,
|
||||
channelId: string,
|
||||
blogUrl: string
|
||||
): Promise<ContentItem[]> {
|
||||
const latestVideos = await getLatestContent(fetch, channelId);
|
||||
const blogArticles = await fetchBlogArticles(fetch, blogUrl);
|
||||
|
||||
let finalArray = [...latestVideos, ...blogArticles];
|
||||
|
||||
finalArray.sort((a, b) => b.published.getTime() - a.published.getTime());
|
||||
|
||||
return finalArray.slice(0, 5);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user