Skip to content

Commit

Permalink
♻️ refactor youtube integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Vondee committed Jul 26, 2024
1 parent 1e7db14 commit 84dad63
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
7 changes: 6 additions & 1 deletion packages/server/src/services/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,12 @@ export default class SessionService {
(e) => e.type == data.type && e._id == data.socialId,
);
if (data.type == 'youtube') {
data.token = await refreshAccessToken(token.refreshToken);
data.token = {
secret: await refreshAccessToken(token.refreshToken),
};
}
if (data.type == 'twitter') {
data.token = { key: token.accessToken, secret: token.refreshToken };
}
const queue = 'videos';
const channel = await (await connection).createChannel();
Expand Down
45 changes: 22 additions & 23 deletions packages/video-uploader/src/utils/youtube.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { youtube_v3, google } from "googleapis";
import { createReadStream, createWriteStream, unlinkSync } from "fs";
import https from "https";
import { youtube_v3, google } from 'googleapis';
import { createReadStream, createWriteStream, unlinkSync } from 'fs';
import https from 'https';

function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand All @@ -11,7 +11,7 @@ async function getYoutubeClient(accessToken: string) {
access_token: accessToken,
});
return google.youtube({
version: "v3",
version: 'v3',
auth: oauth2Client,
});
}
Expand All @@ -22,10 +22,9 @@ async function checkVideoProcessingStatus(
): Promise<string> {
const response = await youtube.videos.list({
id: [videoId],
part: ["processingDetails"],
part: ['processingDetails'],
});

return response.data.items[0].processingDetails.processingStatus;
return response.data.items[0].processingDetails.processingStatus ?? '';
}

async function downloadImage(
Expand All @@ -39,21 +38,21 @@ async function downloadImage(
const fileStream = createWriteStream(filePath);
response.pipe(fileStream);

fileStream.on("finish", () => {
fileStream.on('finish', () => {
fileStream.close();
console.log("Image download completed:", filePath);
console.log('Image download completed:', filePath);
resolve({ success: true });
});
} else {
console.error("Image download failed:", response.statusCode);
console.error('Image download failed:', response.statusCode);
resolve({
success: false,
message: `Failed with status code: ${response.statusCode}`,
});
}
})
.on("error", (error) => {
console.error("Error downloading image:", error.message);
.on('error', (error) => {
console.error('Error downloading image:', error.message);
resolve({ success: false, message: error.message });
});
});
Expand All @@ -71,9 +70,9 @@ async function setThumbnail(
body: createReadStream(filePath),
},
});
console.log("Thumbnail set successfully:", response.data);
console.log('Thumbnail set successfully:', response.data);
} catch (error) {
console.error("Error setting thumbnail:", error);
console.error('Error setting thumbnail:', error);
}
}

Expand All @@ -91,16 +90,16 @@ export async function uploadToYouTube(
try {
const youtube = await getYoutubeClient(accessToken);
const insertResponse = await youtube.videos.insert({
part: ["status", "snippet"],
part: ['status', 'snippet'],
requestBody: {
snippet: {
title: session.name,
description: session.description,
defaultLanguage: "en",
defaultAudioLanguage: "en",
defaultLanguage: 'en',
defaultAudioLanguage: 'en',
},
status: {
privacyStatus: session.published ? "public" : "unlisted",
privacyStatus: session.published ? 'public' : 'unlisted',
selfDeclaredMadeForKids: false,
madeForKids: false,
},
Expand All @@ -110,15 +109,15 @@ export async function uploadToYouTube(
},
});

let processingStatus = "processing";
while (processingStatus === "processing") {
let processingStatus = 'processing';
while (processingStatus === 'processing') {
processingStatus = await checkVideoProcessingStatus(
insertResponse.data.id,
youtube
);
if (processingStatus === "processing") {
if (processingStatus === 'processing') {
await delay(180000); // Delay for 3 minutes
} else if (processingStatus === "error") {
} else if (processingStatus === 'error') {
return;
}
}
Expand All @@ -131,7 +130,7 @@ export async function uploadToYouTube(
}
return;
} catch (error) {
console.error("An error occurred:", error);
console.error('An error occurred:', error);
return;
}
}

0 comments on commit 84dad63

Please sign in to comment.