Skip to content

Commit

Permalink
feat(plugin): add invidious plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles committed Jan 22, 2020
1 parent 811534e commit 606ea6f
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 6 deletions.
9 changes: 5 additions & 4 deletions .env
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
ACOUSTIC_ID_KEY=Fivodjxo37
YOUTUBE_API_KEY=AIzaSyCIM4EzNqi1in22f4Z3Ru3iYvLaY8tc3bo
DISCORD_CLIENT_ID=668477264995811338
GITHUB_CLIENT_ID=ac58d6da7d7a74c039b7
GITHUB_SECRET=37d02377a3e9d849e18704c3ec823f9c5787d857
INVIDIOUS_URL=https://invidio.us
JAMENDO_CLIENT_ID=836523a7
LAST_FM_API_KEY=2b75dcb291e2b0c9a2c994aca522ac14
LAST_FM_API_SECRET=2ee49e35f08b837d43b2824198171fc8
SOUNDCLOUD_API_KEY=22e8f71d7ca75e156d6b2f0e0a5172b3
JAMENDO_CLIENT_ID=836523a7
GITHUB_CLIENT_ID=ac58d6da7d7a74c039b7
GITHUB_SECRET=37d02377a3e9d849e18704c3ec823f9c5787d857
YOUTUBE_API_KEY=AIzaSyCIM4EzNqi1in22f4Z3Ru3iYvLaY8tc3bo
1 change: 0 additions & 1 deletion packages/app/app/containers/HttpApiUrl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const HttpApiUrl = ({ settings }) => {
return (
<a
className={styles.http_api_url}
href='javascript:void(0)'
onClick={() => handleClick(settings)}>
<Icon name='linkify'/>
API Docs
Expand Down
1 change: 1 addition & 0 deletions packages/app/app/containers/HttpApiUrl/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
font-size: 1.2rem;
padding: 0.5rem 1.5rem;
border: 1px solid $white;
cursor: pointer;
}
1 change: 1 addition & 0 deletions packages/app/app/globals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
invidiousUrl: process.env.INVIDIOUS_URL,
ytApiKey: process.env.YOUTUBE_API_KEY,
lastfmApiKey: process.env.LAST_FM_API_KEY,
lastfmApiSecret: process.env.LAST_FM_API_SECRET,
Expand Down
65 changes: 65 additions & 0 deletions packages/app/app/plugins/stream/InvidiousPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logger from 'electron-timber';

import StreamProviderPlugin from '../streamProvider';
import * as Invidious from '../../rest/Invidious';

class InvidiousPlugin extends StreamProviderPlugin {
constructor() {
super();
this.name = 'Invidious Plugin';
this.sourceName = 'Invidious';
this.description = 'A plugin allowing Nuclear to search for music and play it from invidious';
}

async search(query) {
const terms = query.artist + ' ' + query.track;
try {
const {
adaptiveFormats,
lengthSeconds,
title,
videoId,
videoThumbnails
} = await Invidious.trackSearch(terms);

return {
source: this.sourceName,
id: videoId,
stream: adaptiveFormats.find(({ type }) => type.includes('audio')).url,
duration: lengthSeconds,
title,
thumbnail: videoThumbnails[3].url
};
} catch (error) {
logger.error(`Error while searching for ${terms} on Invidious`);
logger.error(error);
}
}

async getAlternateStream(query) {
const terms = query.artist + ' ' + query.track;
try {
const {
adaptiveFormats,
lengthSeconds,
title,
videoId,
videoThumbnails
} = await Invidious.trackSearch(terms, true);

return {
source: this.sourceName,
id: videoId,
stream: adaptiveFormats.find(({ type }) => type.includes('audio')).url,
duration: lengthSeconds,
title,
thumbnail: videoThumbnails[3].url
};
} catch (error) {
logger.error(`Error while searching for ${terms} on Invidious`);
logger.error(error);
}
}
}

export default InvidiousPlugin;
1 change: 1 addition & 0 deletions packages/app/app/plugins/stream/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as YoutubePlugin } from './YoutubePlugin';
export { default as SoundcloudPlugin } from './SoundcloudPlugin';
export { default as JamendoPlugin } from './JamendoPlugin';
export { default as InvidiousPlugin } from './InvidiousPlugin';
24 changes: 24 additions & 0 deletions packages/app/app/rest/Invidious.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getOption } from '../persistence/store';

const baseUrl = getOption('invidious.url');

export const trackSearch = async (query, alternate) => {
const response = await fetch(`${baseUrl}/api/v1/search?q=${query}&sortBy=relevance&page=1`);
if (!response.ok) {
throw new Error('invidious search failed');
}
const result = await response.json();

const trackInfo = await getTrackInfo(result[alternate ? 1 : 0].videoId);

return trackInfo;
};

const getTrackInfo = async (videoId) => {
const response = await fetch(`${baseUrl}/api/v1/videos/${videoId}`);
if (!response.ok) {
throw new Error('invidious track info failed');
}

return response.json();
};
7 changes: 6 additions & 1 deletion packages/core/src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ export const settingsConfig: Array<Setting> = [
category: 'youtube',
type: SettingType.STRING,
prettyName: 'yt-api-key'
// default: globals.ytApiKey
},
{
name: 'invidious.url',
category: 'youtube',
type: SettingType.STRING,
prettyName: 'invidious-url'
},
{
name: 'language',
Expand Down
8 changes: 8 additions & 0 deletions packages/i18n/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,15 @@
"track-duration": "Display track duration over the seekbar",
"user": "User:",
"youtube": "Youtube",
<<<<<<< HEAD:packages/i18n/src/locales/en.json
"yt-api-key": "Youtube API Key"
=======
"yt-api-key": "Youtube API Key",
"invidious-url": "Invidious instance url",
"downloads": "Downloads",
"downloads-dir": "Downloads directory",
"downloads-dir-button": "Choose a directory..."
>>>>>>> feat(plugin): add invidious plugin:packages/app/app/locales/en.json
},
"tags": {
"albums": "Top Albums",
Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/services/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config {
icon: string;
macIcon: string;
discordClientId: string;
defaultInvidiousUrl: string;

constructor(
@inject($mainLogger) logger: Logger
Expand Down Expand Up @@ -55,6 +56,7 @@ class Config {

this.defaultYoutubeApiKey = process.env.YOUTUBE_API_KEY;
this.discordClientId = process.env.DISCORD_CLIENT_ID;
this.defaultInvidiousUrl = process.env.INVIDIOUS_URL;
}

private validateEnv(): void {
Expand Down
4 changes: 4 additions & 0 deletions packages/main/src/services/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Store extends ElectronStore {
this.setOption('yt.apiKey', this.config.defaultYoutubeApiKey);
}

if (!this.getOption('invidious.url')) {
this.setOption('invidious.url', this.config.defaultInvidiousUrl);
}

this.logger.log(`Initialized settings store at ${this.path}`);
}

Expand Down
1 change: 1 addition & 0 deletions packages/main/typings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ declare namespace NodeJS {
ACOUSTIC_ID_KEY: string;
YOUTUBE_API_KEY: string;
DISCORD_CLIENT_ID: string;
INVIDIOUS_URL: string;
}
}

0 comments on commit 606ea6f

Please sign in to comment.