Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
feat: Add youtubes bookmarking script.
Browse files Browse the repository at this point in the history
  • Loading branch information
fourjuaneight committed Nov 5, 2021
1 parent 4e2075d commit 3e80b55
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bookmark-viemos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ const getVimeoDetails = async (url: string): Promise<BookmarkData> => {
};

/**
* Get podcast details and upload to Airtable.
* Get Vimeo video details and upload to Airtable.
* @function
* @async
*
* @param {string} url podcast url
* @param {string} url video url
* @param {string[]} tags record tags
* @returns {Promise<BookmarkingResponse>} result of record upload
*/
Expand Down
81 changes: 81 additions & 0 deletions bookmark-youtubes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import "https://deno.land/x/[email protected]/load.ts";

import { airtableUpload } from "./airtable-upload.ts";

import {
BookmarkData,
BookmarkingResponse,
YouTubeAPIEndpoint,
YouTubeResponse,
} from "./typings.d.ts";

/**
* Convert video url to API ready endpoint. Extracts youtube ID.
* @function
*
* @param {string} url video url
* @returns {YouTubeAPIEndpoint} API endpoint to fetch video data + bookmarking ready url
*/
const cleanUrl = (url: string): YouTubeAPIEndpoint => {
const extractedID = url
.replace(/(https\:\/\/)(youtu.*)\.(be|com)\/(watch\?v=)?/g, "")
.replace("&feature=share", "");
const endpoint = `https://youtube.googleapis.com/youtube/v3/videos?id=${extractedID}`;
const link = `https://youtu.be/${extractedID}`;

return { endpoint, link };
};

/**
* Get video details via YouTube API.
* Docs: https://developers.google.com/youtube/v3/docs/videos/list
* @function
* @async
*
* @param {string} url video url
* @returns {Promise<BookmarkData>} video title, creator, and url
*/
const getYouTubeDetails = async (url: string): Promise<BookmarkData> => {
try {
const { endpoint, link } = cleanUrl(url);
const request = await fetch(
`${endpoint}&key=${Deno.env.get("YOUTUBE_KEY")}`
);
const response: YouTubeResponse = await request.json();
const video = response.items[0].snippet;

return {
title: video.title,
creator: video.channelTitle,
url: link,
};
} catch (error) {
throw new Error(`Gettingg youtube details: \n ${error}`);
}
};

/**
* Get YouTube video details and upload to Airtable.
* @function
* @async
*
* @param {string} url video url
* @param {string[]} tags record tags
* @returns {Promise<BookmarkingResponse>} result of record upload
*/
export const bookmarkYouTube = async (
url: string,
tags: string[]
): Promise<BookmarkingResponse> => {
try {
const youtTubeData = await getYouTubeDetails(url);
const airtableResp = await airtableUpload("Videos", {
...youtTubeData,
tags,
});

return { success: true, message: airtableResp, source: "bookmarkYouTube" };
} catch (error) {
return { success: false, message: error, source: "bookmarkYouTube" };
}
};
62 changes: 62 additions & 0 deletions typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export interface ParsingPatterns {
[key: string]: ParsingService | RegExp[];
}

export interface YouTubeAPIEndpoint {
endpoint: string;
link: string;
}

export interface TwitterResponse {
data: {
author_id: string;
Expand Down Expand Up @@ -989,6 +994,63 @@ export interface VimeoResponse {
width: number;
}

export interface YouTubeResponse {
kind: string;
etag: string;
items: {
kind: string;
etag: string;
id: string;
snippet: {
publishedAt: string;
channelId: string;
title: string;
description: string;
thumbnails: {
default: {
url: string;
width: number;
height: number;
};
medium: {
url: string;
width: number;
height: number;
};
high: {
url: string;
width: number;
height: number;
};
standard: {
url: string;
width: number;
height: number;
};
maxres: {
url: string;
width: number;
height: number;
};
};
channelTitle: string;
tags: string[];
categoryId: string;
liveBroadcastContent: string;
defaultLanguage: string;
localized: {
title: string;
description: string;
};
defaultAudioLanguage: string;
};
}[];
pageInfo: {
totalResults: number;
resultsPerPage: number;
};
}

export interface RecordData {
title?: string;
tweet?: string;
Expand Down

0 comments on commit 3e80b55

Please sign in to comment.