Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

merge #297

Merged
merged 6 commits into from
Oct 26, 2021
Merged

merge #297

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
6 changes: 4 additions & 2 deletions resources/functions/app-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ module.exports = () => {
startupPage: "browse",
discordRPC: "ame-title",
discordClearActivityOnPause: true,
lastfmEnabled: false,
lastfm: false,
lastfmRemoveFeaturingArtists: true,
analyticsEnabled: true
lastfmNowPlaying: true,
analyticsEnabled: true,
lastfmScrobbleDelay: 30
},
visual: {
theme: "default",
Expand Down
7 changes: 6 additions & 1 deletion resources/functions/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const handler = {
app.ame.win.SetTrayTooltip(a)
app.ame.discord.updateActivity(a)
app.ame.lastfm.scrobbleSong(a)
app.ame.lastfm.updateNowPlayingSong(a)
app.ame.mpris.updateState(a)
});
},
Expand All @@ -88,6 +89,7 @@ const handler = {
app.ame.win.SetTrayTooltip(a)
app.ame.discord.updateActivity(a)
app.ame.lastfm.scrobbleSong(a)
app.ame.lastfm.updateNowPlayingSong(a)
app.ame.mpris.updateState(a)
}
});
Expand Down Expand Up @@ -285,7 +287,7 @@ const handler = {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Individually Handled Configuration Options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
handledConfigs.push('advanced.devToolsOnStartup', 'general.storefront') // Stuff for the restart to just ignore
handledConfigs.push('advanced.devToolsOnStartup', 'general.storefront', 'tokens.lastfm') // Stuff for the restart to just ignore

// Theme Changes
handledConfigs.push('visual.theme');
Expand Down Expand Up @@ -545,7 +547,10 @@ const handler = {
let authURI = String(startArgs).split('/auth/')[1]
if (authURI.startsWith('lastfm')) { // If we wanted more auth options
const authKey = authURI.split('lastfm?token=')[1];
app.cfg.set('general.lastfm', true);
app.cfg.set('tokens.lastfm', authKey);
app.win.webContents.send('LastfmAuthenticated', authKey);
app.ame.lastfm.authenticate()
}
} else {
if (!app.isAuthorized) return
Expand Down
81 changes: 60 additions & 21 deletions resources/functions/media/lastfm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const lfm = {
},

authenticate: function () {
if (!app.cfg.get('general.lastfmEnabled') || !app.cfg.get('tokens.lastfm')) {
app.cfg.set('general.lastfmEnabled', false)
if (!app.cfg.get('general.lastfm') || !app.cfg.get('tokens.lastfm')) {
app.cfg.set('general.lastfm', false)
return
}

Expand All @@ -26,7 +26,7 @@ const lfm = {
'secret': apiCredentials.secret
});

app.lastfm = Object.assign(lfmAPI, {cachedAttributes: false})
app.lastfm = Object.assign(lfmAPI, {cachedAttributes: false, cachedNowPlayingAttributes: false});

fs.stat(sessionPath, function (err) {
if (err) {
Expand Down Expand Up @@ -58,7 +58,10 @@ const lfm = {
})
},

scrobbleSong: function (attributes) {
scrobbleSong: async function (attributes) {
await new Promise(resolve => setTimeout(resolve, app.cfg.get('general.lastfmScrobbleDelay') * 1000));
currentAttributes = app.media;

if (!app.lastfm || app.lastfm.cachedAttributes === attributes || app.cfg.get('general.incognitoMode')) {
return
}
Expand All @@ -67,26 +70,30 @@ const lfm = {
if (app.lastfm.cachedAttributes.playParams.id === attributes.playParams.id) return;
}

if (fs.existsSync(sessionPath)) {
// Scrobble playing song.
if (attributes.status === true) {
app.lastfm.track.scrobble({
'artist': lfm.filterArtistName(attributes.artistName),
'track': attributes.name,
'album': attributes.albumName,
'albumArtist': this.filterArtistName(attributes.artistName),
'timestamp': new Date().getTime() / 1000
}, function (err, scrobbled) {
if (err) {
return console.error('[LastFM] An error occurred while scrobbling', err);
}
if (currentAttributes.status && currentAttributes.playParams.catalogId === attributes.playParams.catalogId) {
if (fs.existsSync(sessionPath)) {
// Scrobble playing song.
if (attributes.status === true) {
app.lastfm.track.scrobble({
'artist': lfm.filterArtistName(attributes.artistName),
'track': attributes.name,
'album': attributes.albumName,
'albumArtist': this.filterArtistName(attributes.artistName),
'timestamp': new Date().getTime() / 1000
}, function (err, scrobbled) {
if (err) {
return console.error('[LastFM] An error occurred while scrobbling', err);
}

console.verbose('[LastFM] Successfully scrobbled: ', scrobbled);
});
app.lastfm.cachedAttributes = attributes
console.verbose('[LastFM] Successfully scrobbled: ', scrobbled);
});
app.lastfm.cachedAttributes = attributes
}
} else {
this.authenticate();
}
} else {
this.authenticate()
return console.verbose('[LastFM] Did not add ', attributes.name , '-' , lfm.filterArtistName(attributes.artistName), 'because now playing a other song.');
}
},

Expand All @@ -106,6 +113,38 @@ const lfm = {
artist = artist[0]
}
return artist.charAt(0).toUpperCase() + artist.slice(1);
},

updateNowPlayingSong: function (attributes) {
if (!app.lastfm ||app.lastfm.cachedNowPlayingAttributes === attributes || app.cfg.get('general.incognitoMode') || !app.cfg.get('general.lastfmNowPlaying')) {
return
}

if (app.lastfm.cachedNowPlayingAttributes) {
if (app.lastfm.cachedNowPlayingAttributes.playParams.id === attributes.playParams.id) return;
}

if (fs.existsSync(sessionPath)) {
// update Now Playing
if (attributes.status === true) {
app.lastfm.track.updateNowPlaying({
'artist': lfm.filterArtistName(attributes.artistName),
'track': attributes.name,
'album': attributes.albumName,
'albumArtist': this.filterArtistName(attributes.artistName)
}, function (err, nowPlaying) {
if (err) {
return console.error('[LastFM] An error occurred while updating nowPlayingSong', err);
}

console.verbose('[LastFM] Successfully updated nowPlayingSong', nowPlaying);
});
app.lastfm.cachedNowPlayingAttributes = attributes
}

} else {
this.authenticate()
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion resources/html/preferences-main.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ <h2 class="shelf-title">LastFM</h2>
<span class="app-prefs-help typography-title-3-tall">Enabling this will remove the featuring artists
from the scrobbled data, meaning on LastFM only the primary artist will be shown.</span>
</li>
<li class="app-prefs-toggle">
<span class="typography-title-3-tall">Enable now playing</span>
<label class="toggle-element list-element">
<input checked id="lastfmNowPlaying" type="checkbox">
<span class="slider"></span>
</label>
<span class="app-prefs-help typography-title-3-tall">Enabling this will display what song you are
playing on LastFM.</span>
</li>
<li class="app-prefs-dropdown">
<label class="typography-title-3-tall" for="lastfmScrobbleDelay">Scrobble delay</label>
<input class="form-dropdown-select list-element" id="lastfmScrobbleDelay" name="lastfmScrobbleDelay" type="number" />
<span class="app-prefs-help typography-title-3-tall">Sets the wait time before current song is added to scrobble list.</span>
</li>
</ul>
</div>
<div class="app-prefs-section visual">
Expand Down Expand Up @@ -551,7 +565,7 @@ <h2 class="shelf-title">Miscellaneous Options</h2>
</li>
<li class="app-prefs-dropdown">
<label class="typography-title-3-tall" for="scaling">Interface Scaling Multiplier</label>
<input class="form-dropdown-select list-element" type="number" id="scaling" name="scaling"/>
<input class="form-dropdown-select list-element" type="number" id="scaling" name="scaling" min="0.1" max="4"/>
<span class="app-prefs-help typography-title-3-tall">Set the scale that you would like the interface sized at.</span>
</li>
</ul>
Expand Down
19 changes: 9 additions & 10 deletions resources/js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,18 +841,18 @@ try {
function volumeChange(event) {
if (checkScrollDirectionIsUp(event)) {
if (MusicKit.getInstance().volume <= 1) {
if ((MusicKit.getInstance().volume + 0.04) > 1) {
if ((MusicKit.getInstance().volume + 0.05) > 1) {
MusicKit.getInstance().volume = 1
} else {
MusicKit.getInstance().volume = MusicKit.getInstance().volume + 0.04;
MusicKit.getInstance().volume += 0.05;
}
}
} else {
if (MusicKit.getInstance().volume >= 0) {
if ((MusicKit.getInstance().volume - 0.04) < 0) {
if ((MusicKit.getInstance().volume - 0.05) < 0) {
MusicKit.getInstance().volume = 0;
} else {
MusicKit.getInstance().volume = MusicKit.getInstance().volume - 0.04;
MusicKit.getInstance().volume -= 0.05;
}
}
}
Expand Down Expand Up @@ -948,9 +948,8 @@ try {

lastfm: {
LastFMDeauthorize: () => {
preferences.general.lastfmAuthKey = 'Put your Auth Key here.';
preferences.general.lastfmEnabled = false;
ipcRenderer.sendSync('setStore', preferences);
ipcRenderer.invoke('setStoreValue', 'general.lastfm', false).catch((e) => console.error(e));
ipcRenderer.invoke('setStoreValue', 'tokens.lastfm', '').catch((e) => console.error(e));
const element = document.getElementById('lfmConnect');
element.innerHTML = 'Connect';
element.onclick = AMSettings.lastfm.LastFMAuthenticate;
Expand All @@ -969,11 +968,8 @@ try {
}, 20000);

ipcRenderer.on('LastfmAuthenticated', function (_event, lfmAuthKey) {
preferences.general.lastfmEnabled = true;
preferences.general.lastfmAuthKey = lfmAuthKey;
element.innerHTML = `Disconnect\n<p style="font-size: 8px"><i>(Authed: ${lfmAuthKey})</i></p>`;
element.onclick = AMSettings.lastfm.LastFMDeauthorize;
ipcRenderer.sendSync('setStore', preferences);
});
}
},
Expand Down Expand Up @@ -1087,6 +1083,8 @@ try {
AMSettings.HandleField('discordClearActivityOnPause');
AMSettings.HandleField('lfmConnect');
AMSettings.HandleField('lastfmRemoveFeaturingArtists');
AMSettings.HandleField('lastfmNowPlaying');
AMSettings.HandleField('lastfmScrobbleDelay');

/* Visual Settings */
AMSettings.HandleField('theme');
Expand All @@ -1108,6 +1106,7 @@ try {
/* Audio Settings */
AMSettings.HandleField('audioQuality');
AMSettings.HandleField('seemlessAudioTransitions');
AMSettings.HandleField('volume');

/* Window Settings */
AMSettings.HandleField('appStartupBehavior');
Expand Down