Skip to content

Commit

Permalink
youtube connector
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrehault committed Feb 22, 2023
1 parent 9f977d5 commit 98fbeab
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
5 changes: 4 additions & 1 deletion HOWTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Provide Nuclia current status + feature flagging

This repository allows to broadcast important warning and/or info messages to all dashboard users. Messages are shown at loading time, and only once.

It also stores the list of features when want to hide/show on prod.
Other purposes:

- Feature flagging: it stores the list of features when want to hide/show on prod.
- Desktop connectors: it stores some pluggable connectors for Desktop app.

## Usage

Expand Down
4 changes: 1 addition & 3 deletions connectors.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
[
"http://127.0.0.1:4201/youtube.js"
]
["https://nuclia.github.io/status/connectors/youtube.js"]
81 changes: 81 additions & 0 deletions connectors/youtube.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class YoutubeConnector {
isExternal = true;

getParameters() {
return [
{
id: 'key',
label: 'API key',
type: 'text',
required: true,
},
{
id: 'channel',
label: 'YouTube channel ID',
type: 'text',
},
{
id: 'account',
label: 'YouTube account name (without @ prefix)',
type: 'text',
},
];
}

handleParameters(params) {
localStorage.setItem('YOUTUBE_API_KEY', params.key);
if (params.account && !params.channel) {
fetch(
`https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=${params.account}&key=${params.key}`,
)
.then((response) => response.json())
.then((response) => {
if (response.items.length > 0) {
localStorage.setItem('YOUTUBE_CHANNEL', response.items[0].id);
} else {
throw new Error('No channel found');
}
});
} else {
localStorage.setItem('YOUTUBE_CHANNEL', params.channel);
}
}

getFiles(query, pageSize, nextPageToken) {
const apiKey = localStorage.getItem('YOUTUBE_API_KEY');
const channel = localStorage.getItem('YOUTUBE_CHANNEL');
let url = `https://www.googleapis.com/youtube/v3/search?part=snippet,id&order=date&maxResults=${
pageSize || 50
}&channelId=${channel}&q=${encodeURIComponent(query)}&key=${apiKey}`;
if (nextPageToken) {
url += `&pageToken=${nextPageToken}`;
}
return fetch(url)
.then((response) => response.json())
.then((response) => {
return {
items: response.items
.filter((video) => video.id.kind === 'youtube#video')
.map((item) => ({
uuid: item.id.videoId,
title: item.snippet.title,
originalId: item.id.videoId,
})),
nextPage: response.nextPageToken ? () => this.getFiles(query, pageSize, response.nextPageToken) : undefined,
};
});
}

getLink(item) {
return Promise.resolve({ uri: `https://www.youtube.com/watch?v=${item.originalId}` });
}
}

const YOUTUBE_CONNECTOR = {
id: 'youtube',
title: 'YouTube',
logo: 'https://nuclia.github.io/status/connectors/youtube.svg',
description: 'Online video sharing',
factory: () => new YoutubeConnector(),
};
registerConnector(YOUTUBE_CONNECTOR);
1 change: 1 addition & 0 deletions connectors/youtube.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 98fbeab

Please sign in to comment.