Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Discord Rich Presence album art via Last.fm (#341) #817

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@
"imageAspectRatio_description": "if enabled, cover art will be shown using their native aspect ratio. for art that is not 1:1, the remaining space will be empty",
"language": "language",
"language_description": "sets the language for the application ($t(common.restartRequired))",
"lastfmApiKey": "{{lastfm}} API key",
"lastfmApiKey_description": "the API key for {{lastfm}}. required for cover art",
"lyricFetch": "fetch lyrics from the internet",
"lyricFetch_description": "fetch lyrics from various internet sources",
"lyricFetchProvider": "providers to fetch lyrics from",
Expand Down
16 changes: 16 additions & 0 deletions src/renderer/features/discord-rpc/use-discord-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useCurrentSong,
useCurrentStatus,
useDiscordSetttings,
useGeneralSettings,
usePlayerStore,
} from '/@/renderer/store';
import { SetActivity } from '@xhayper/discord-rpc';
Expand All @@ -16,6 +17,7 @@ const discordRpc = isElectron() ? window.electron.discordRpc : null;
export const useDiscordRpc = () => {
const intervalRef = useRef(0);
const discordSettings = useDiscordSetttings();
const generalSettings = useGeneralSettings();
const currentSong = useCurrentSong();
const currentStatus = useCurrentStatus();

Expand Down Expand Up @@ -67,6 +69,19 @@ export const useDiscordRpc = () => {
activity.largeImageKey = song?.imageUrl;
}

if (generalSettings.lastfmApiKey && song?.album && song?.artists.length) {
console.log('Fetching album info for', song.album, song.artists[0].name);
const albumInfo = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=${generalSettings.lastfmApiKey}&artist=${encodeURIComponent(song.artistName)}&album=${encodeURIComponent(song.album)}&format=json`,
);

const albumInfoJson = await albumInfo.json();

if (albumInfoJson.album?.image?.[3]['#text']) {
activity.largeImageKey = albumInfoJson.album.image[3]['#text'];
}
}

// Fall back to default icon if not set
if (!activity.largeImageKey) {
activity.largeImageKey = 'icon';
Expand All @@ -79,6 +94,7 @@ export const useDiscordRpc = () => {
discordSettings.enableIdle,
discordSettings.showAsListening,
discordSettings.showServerImage,
generalSettings.lastfmApiKey,
]);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import {
SettingOption,
SettingsSection,
} from '/@/renderer/features/settings/components/settings-section';
import { useDiscordSetttings, useSettingsStoreActions } from '/@/renderer/store';
import {
useDiscordSetttings,
useSettingsStoreActions,
useGeneralSettings,
} from '/@/renderer/store';
import { useTranslation } from 'react-i18next';

export const DiscordSettings = () => {
const { t } = useTranslation();
const settings = useDiscordSetttings();
const generalSettings = useGeneralSettings();
const { setSettings } = useSettingsStoreActions();

const discordOptions: SettingOption[] = [
Expand Down Expand Up @@ -142,6 +147,31 @@ export const DiscordSettings = () => {
postProcess: 'sentenceCase',
}),
},
{
control: (
<TextInput
defaultValue={generalSettings.lastfmApiKey}
onBlur={(e) => {
setSettings({
general: {
...generalSettings,
lastfmApiKey: e.currentTarget.value,
},
});
}}
/>
),
description: t('setting.lastfmApiKey', {
context: 'description',
lastfm: 'Last.fm',
postProcess: 'sentenceCase',
}),
isHidden: !isElectron(),
title: t('setting.lastfmApiKey', {
lastfm: 'Last.fm',
postProcess: 'sentenceCase',
}),
},
];

return <SettingsSection options={discordOptions} />;
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/store/settings.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export interface SettingsState {
homeFeature: boolean;
homeItems: SortableItem<HomeItem>[];
language: string;
lastfmApiKey: string;
nativeAspectRatio: boolean;
passwordStore?: string;
playButtonBehavior: Play;
Expand Down Expand Up @@ -377,6 +378,7 @@ const initialState: SettingsState = {
homeFeature: true,
homeItems,
language: 'en',
lastfmApiKey: '',
nativeAspectRatio: false,
passwordStore: undefined,
playButtonBehavior: Play.NOW,
Expand Down
Loading