Skip to content

Commit

Permalink
Add getWebsiteContent
Browse files Browse the repository at this point in the history
  • Loading branch information
MetaCorp committed Feb 5, 2024
1 parent be1316c commit f552c11
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 51 deletions.
30 changes: 30 additions & 0 deletions src/AugmentedCanvasPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { insertSystemPrompt } from "./actions/commands/insertSystemPrompt";
import { runPromptFolder } from "./actions/commands/runPromptFolder";
import { InputModal } from "./modals/InputModal";
import { runYoutubeCaptions } from "./actions/commands/youtubeCaptions";
import { insertWebsiteContent } from "./actions/commands/websiteContent";

export default class AugmentedCanvasPlugin extends Plugin {
triggerByPlugin: boolean = false;
Expand Down Expand Up @@ -312,6 +313,35 @@ export default class AugmentedCanvasPlugin extends Plugin {
addCommands() {
const app = this.app;

// * Website to MD
// this.addCommand({
// id: "insert-website-content",
// name: "Insert the content of a website as markdown",
// checkCallback: (checking: boolean) => {
// if (checking) {
// // console.log({ checkCallback: checking });
// if (!getActiveCanvas(app)) return false;

// return true;
// }

// new InputModal(
// app,
// {
// label: "Enter a website url",
// buttonLabel: "Get website content",
// },
// (videoUrl: string) => {
// new Notice(`Scraping website content`);

// insertWebsiteContent(app, this.settings, videoUrl);
// }
// ).open();
// },
// // callback: () => {},
// });

// * Youtube captions
// this.addCommand({
// id: "insert-youtube-caption",
// name: "Insert captions of a Youtube video",
Expand Down
11 changes: 11 additions & 0 deletions src/actions/commands/websiteContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { App } from "obsidian";
import { AugmentedCanvasSettings } from "src/settings/AugmentedCanvasSettings";
import { getWebsiteContent } from "src/utils/websiteContentUtils";

export const insertWebsiteContent = (
app: App,
settings: AugmentedCanvasSettings,
url: string
) => {
// const { textContent } = getWebsiteContent(url);
};
95 changes: 44 additions & 51 deletions src/actions/commands/youtubeCaptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,55 @@ async function getVideoSubtitles(
settings: AugmentedCanvasSettings,
videoId: string
): Promise<string[]> {
// TODO Convert to Oauth
const youtube = google.youtube({
version: "v3",
auth: settings.youtubeApiKey, // Replace with your API key
});

try {
const response = await youtube.captions.list({
part: ["snippet"],
videoId: videoId,
});
console.log({ response });

const items = response.data.items;
if (items) {
const subtitles = [];
for await (const caption of items) {
console.log({ caption });
try {
const response = await youtube.captions.download(
{
id: caption.id!,
tfmt: "ttml", // This specifies the format of the captions file. Options are 'ttml' or 'vtt' for example.
},
{
responseType: "text",
}
);

// The caption content will be in the response body as a string
subtitles.push(response.data as string);
} catch (error) {
console.error("Error downloading caption:", error);
}
}

return subtitles;
}

return [];
} catch (error) {
console.error("Error fetching video captions:", error);

return [];
}
// // TODO Convert to Oauth
// const youtube = google.youtube({
// version: "v3",
// auth: settings.youtubeApiKey, // Replace with your API key
// });
// try {
// const response = await youtube.captions.list({
// part: ["snippet"],
// videoId: videoId,
// });
// console.log({ response });
// const items = response.data.items;
// if (items) {
// const subtitles = [];
// for await (const caption of items) {
// console.log({ caption });
// try {
// const response = await youtube.captions.download(
// {
// id: caption.id!,
// tfmt: "ttml", // This specifies the format of the captions file. Options are 'ttml' or 'vtt' for example.
// },
// {
// responseType: "text",
// }
// );
// // The caption content will be in the response body as a string
// subtitles.push(response.data as string);
// } catch (error) {
// console.error("Error downloading caption:", error);
// }
// }
// return subtitles;
// }
// return [];
// } catch (error) {
// console.error("Error fetching video captions:", error);
// return [];
// }
}

export const runYoutubeCaptions = async (
app: App,
settings: AugmentedCanvasSettings,
videoUrl: string
) => {
const videoId = getYouTubeVideoId(videoUrl);
console.log({ videoId });
if (!videoId) return;

const subtitles = await getVideoSubtitles(settings, videoId);
console.log({ subtitles });
// const videoId = getYouTubeVideoId(videoUrl);
// console.log({ videoId });
// if (!videoId) return;
// const subtitles = await getVideoSubtitles(settings, videoId);
// console.log({ subtitles });
};
96 changes: 96 additions & 0 deletions src/utils/websiteContentUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
export const getWebsiteContent = async (url: string) => {
// // console.log({ getWebsiteContent2: true })
// const content = await fetch(url, {
// // mode: "no-cors",
// });
// console.log({ content, body: content.body });
// return {};
// const getMDForTagName = (tagName: string) => {
// if (tagName === "h1") {
// return "#";
// } else if (tagName === "h2") {
// return "##";
// } else if (tagName === "h3") {
// return "###";
// } else if (tagName === "h4") {
// return "####";
// } else if (tagName === "h5") {
// return "#####";
// } else if (tagName === "h6") {
// return "######";
// }
// };
// // let count = 0;
// let textContent = "";
// // const selectors = [];
// // function fullPath(el) {
// // var names = [];
// // while (el.parentNode) {
// // if (el.id) {
// // names.unshift("#" + el.id);
// // break;
// // } else {
// // if (el == el.ownerDocument.documentElement)
// // names.unshift(el.tagName);
// // else {
// // for (
// // var c = 1, e = el;
// // e.previousElementSibling;
// // e = e.previousElementSibling, c++
// // );
// // names.unshift(el.tagName + ":nth-child(" + c + ")");
// // }
// // el = el.parentNode;
// // }
// // }
// // return names.join(" > ");
// // }
// // Function to traverse all elements in the DOM
// function traverseDOM(element: Element): void {
// // Process the current element
// // console.log(element.tagName);
// const includedTags = ["p", "h1", "h2", "h3", "h4", "h5", "h6"];
// // const excludedTags = ["script", "button"]
// if (
// includedTags.includes(element.tagName.toLowerCase())
// // &&
// // element.textContent.split(" ").length > 5
// ) {
// const text = element.textContent
// ?.replace(/\n/g, " ")
// .replace(/\\n/g, "")
// .replace(/\t/g, "")
// .replace(/\\t/g, "")
// .trim();
// // console.log({ text, tagName: element.tagName })
// // * Example: 1. ### Title
// textContent +=
// "\n\n" +
// // `${count}.` +
// (element.tagName.toLowerCase() !== "p"
// ? getMDForTagName(element.tagName.toLowerCase()) + " "
// : "") +
// text;
// // count++;
// // const path = fullPath(element);
// // selectors.push(path);
// // document.querySelector(path).scrollIntoView()
// }
// // Recursively traverse child elements
// Array.from(element.children).forEach((child) => {
// traverseDOM(child);
// });
// }
// // Example usage
// // document.addEventListener('DOMContentLoaded', () => {
// traverseDOM(document.documentElement);
// // });
// console.log({
// // selectors,
// textContent,
// });
// return {
// textContent,
// // selectors,
// };
};

0 comments on commit f552c11

Please sign in to comment.