Skip to content

Commit

Permalink
Improve youtube functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thevops committed Jun 25, 2024
1 parent 1b8e41c commit c8d7333
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/docker-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ name: Build and Push Docker Image
on:
push:
branches:
- 'master'
- "master"
tags:
- 'v*'
- "v*"
pull_request:
branches:
- 'master'
- "master"

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# use the official Bun image
# see all versions at https://hub.docker.com/r/oven/bun/tags
FROM oven/bun:1.1.4-alpine as base
FROM oven/bun:1.1.4-alpine AS base

# install dependencies into temp directory
# this will cache them and speed up future builds
Expand All @@ -12,7 +12,7 @@ RUN cd /temp/prod && bun install --frozen-lockfile --production


# final image
FROM base as release
FROM base AS release

RUN apk add -q --progress --update --no-cache dumb-init

Expand Down
41 changes: 31 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { summaryYouTubeTranscript } from "./langchain-openai.ts";
import { logger, raindropAPI, Config } from "./config.ts";
import { getTranscript, getVideoDetails } from "./youtube.ts";
import {
normalizeYouTubeURL,
getTranscript,
getVideoDetails,
extractVideoId,
} from "./youtube.ts";

async function main() {
// Get link
Expand All @@ -15,37 +20,53 @@ async function main() {
// Remove the link from Raindrop
await raindropAPI.removeItem(object._id.toString());

// Normalize link
const link = await normalizeYouTubeURL(object.link);
if (link === null) {
logger.error(`Invalid YouTube URL: ${object.link}`);
return;
}

// Extract video ID
const videoId = await extractVideoId(link);
if (videoId === null) {
logger.error(`Invalid YouTube URL: ${link}`);
return;
}

// Get details from YouTube
const [title, channel, duration, upload_date] = await getVideoDetails(
object.link,
);
const [title, channel, duration, upload_date] =
await getVideoDetails(videoId);

// Get transcript from YouTube
const transcript = await getTranscript(object.link);
const transcript = await getTranscript(videoId);

// Summarize the transcript if available
let summary = "Podsumowanie: none";
let usage = "";
if (transcript !== "") {
if (transcript === null) {
logger.error(`Transcription unavailable for: ${link}`);
} else {
[summary, usage] = await summaryYouTubeTranscript(transcript);
}

// Send the summary to target collection in Raindrop
const note = `<${channel}> ${title} [${duration}]
---
${object.link} (${upload_date})
${link} (${upload_date})
${summary}
${usage}
`;
const result = await raindropAPI.addItem(
Config.raindrop_target_collection_id,
object.link,
link,
note,
);

if (result) {
logger.info(`Final success: ${object.link} ${title}`);
logger.info(`Final success: ${link} ${title}`);
} else {
logger.error(`Final failure: ${object.link} ${title}`);
logger.error(`Final failure: ${link} ${title}`);
}
}

Expand Down
36 changes: 17 additions & 19 deletions src/youtube.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { Innertube } from "youtubei.js/web";
import { logger } from "./config.ts";

function extractVideoId(videoUrl: string): string | null {
const urlParts = videoUrl.split('v=');
export async function extractVideoId(videoUrl: string): Promise<string | null> {
const urlParts = videoUrl.split("v=");
if (urlParts.length > 1) {
const videoId = urlParts[1].split('&')[0];
return videoId;
const videoId = urlParts[1].split("&")[0];
return videoId;
}
return null;
}

export async function getTranscript(videoUrl: string): Promise<string> {
const videoId = extractVideoId(videoUrl);
export async function normalizeYouTubeURL(
videoUrl: string,
): Promise<string | null> {
const videoId = await extractVideoId(videoUrl);
if (videoId === null) {
logger.error(`Invalid YouTube URL: ${videoUrl}`);
return "";
return null;
}
return `https://www.youtube.com/watch?v=${videoId}`;
}

export async function getTranscript(videoId: string): Promise<string | null> {
const youtube = await Innertube.create();

try {
Expand All @@ -31,18 +34,13 @@ export async function getTranscript(videoUrl: string): Promise<string> {
.join(" ") ?? ""
);
} catch (error) {
logger.error(`Transcription unavailable for: ${videoUrl}`);
return "";
return null;
}
}

export async function getVideoDetails(videoUrl: string): Promise<[string | undefined, string | undefined, string, string]> {
const videoId = extractVideoId(videoUrl);
if (videoId === null) {
logger.error(`Invalid YouTube URL: ${videoUrl}`);
return ["", "", "", ""];
}

export async function getVideoDetails(
videoId: string,
): Promise<[string | undefined, string | undefined, string, string]> {
const youtube = await Innertube.create();

const videoBasicInfo = await youtube.getBasicInfo(videoId);
Expand Down Expand Up @@ -98,5 +96,5 @@ if (import.meta.main) {
Run tests
*/
// await test_getTranscript();
await test_getVideoDetails();
// await test_getVideoDetails();
}

0 comments on commit c8d7333

Please sign in to comment.