Skip to content

Commit

Permalink
✨ LastFM authorization and Custom Theme
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerluiz committed Dec 5, 2019
1 parent cc4ac77 commit 308ca0f
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 30 deletions.
5 changes: 4 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"LABEL_SETTINGS_TAB_GENERAL_SHINY_TRAY": "Easy Tray Control(For Mac OS only)",
"LABEL_SETTINGS_TAB_READ_CLIPBOARD": "Read clipboard for youtube links",
"LABEL_SETTINGS_SHOW_HIDE_WINDOW": "Show/Hide Main Window",
"LABEL_SETTINGS_RELAUNCH": "Apply and restart"
"LABEL_SETTINGS_RELAUNCH": "Apply and restart",
"LABEL_SETTINGS_TAB_GENERAL_CUSTOM_THEME": "Custom theme",
"LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER": "Last FM (Scrobbler)",
"LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER_AUTHORIZE": "Save and authorize"
}
3 changes: 2 additions & 1 deletion locales/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
"LABEL_SETTINGS_SHOW_HIDE_WINDOW": "Mostrar/Ocultar janela principal",
"LABEL_SETTINGS_RELAUNCH": "Aplicar e reiniciar",
"LABEL_SETTINGS_TAB_GENERAL_CUSTOM_THEME": "Tema customizado",
"LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER": "Last FM (Scrobbler)"
"LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER": "Last FM (Scrobbler)",
"LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER_AUTHORIZE": "Salvar e autorizar"
}
5 changes: 4 additions & 1 deletion locales/scaffold.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"LABEL_SETTINGS_TAB_GENERAL_SHINY_TRAY": "Easy Tray Control(For Mac OS only)",
"LABEL_SETTINGS_TAB_READ_CLIPBOARD": "Read clipboard for youtube links",
"LABEL_SETTINGS_SHOW_HIDE_WINDOW": "Show/Hide Main Window",
"LABEL_SETTINGS_RELAUNCH": "Apply and restart"
"LABEL_SETTINGS_RELAUNCH": "Apply and restart",
"LABEL_SETTINGS_TAB_GENERAL_CUSTOM_THEME": "Custom theme",
"LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER": "Last FM (Scrobbler)",
"LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER_AUTHORIZE": "Save and authorize"
}
30 changes: 27 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,32 @@ function createWindow() {
settings.loadFile(
path.join(app.getAppPath(), "/pages/settings/settings.html")
);
//settings.webContents.openDevTools();
// settings.webContents.openDevTools();
});

ipcMain.on("show-last-fm-login", function() {
const lastfm = new BrowserWindow({
//parent: mainWindow,
modal: false,
frame: false,
center: true,
resizable: true,
backgroundColor: "#232323",
width: 300,
minWidth: 300,
height: 260,
minHeight: 260,
icon: path.join(__dirname, "./assets/favicon.png"),
autoHideMenuBar: false,
skipTaskbar: false,
webPreferences: {
nodeIntegration: true
}
});
lastfm.loadFile(
path.join(app.getAppPath(), "/pages/settings/last-fm-login.html")
);
// lastfm.webContents.openDevTools();
});

ipcMain.on("switch-clipboard-watcher", () => {
Expand Down Expand Up @@ -816,7 +841,6 @@ ipcMain.on("show-companion", function() {
autoHideMenuBar: true
});
settings.loadURL(companionUrl);
// window.open(companionUrl, companionWindowTitle, companionWindowSettings);
});

function createLyricsWindow() {
Expand All @@ -833,7 +857,7 @@ function createLyricsWindow() {
}
});
lyrics.loadFile(path.join(__dirname, "./pages/lyrics/lyrics.html"));
//lyrics.webContents.openDevTools();
// lyrics.webContents.openDevTools();
}

function logDebug(data) {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"electron-updater": "^4.2.0",
"i18n": "^0.8.4",
"is-online": "^8.2.1",
"js-base64": "^2.5.1",
"materialize-css": "^1.0.0",
"npx": "^10.2.0",
"prettier": "^1.19.1",
Expand Down
3 changes: 2 additions & 1 deletion pages/editor/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ body {

.bottom {
position: absolute;
bottom: 0;
bottom: 12px;
right: 12px
}
9 changes: 6 additions & 3 deletions pages/editor/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
</div>

<div class="cmd-btns">
<div class="no-drag pointer cmd-btn green-text" id="btn-save">
<i class="material-icons tiny">save</i>
</div>
<div class="no-drag pointer cmd-btn hover-red" id="btn-close">
<i class="material-icons tiny">close</i>
</div>
</div>
</div>
<div class="content">
<pre id="editor"></pre>

<a
class="btn-floating btn waves-effect waves-light red bottom"
id="btn-save"
><i class="material-icons">save</i></a
>
</div>
</body>

Expand Down
57 changes: 57 additions & 0 deletions pages/settings/last-fm-login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<html>
<head>
<link
type="text/css"
rel="stylesheet"
href="../../node_modules/materialize-css/dist/css/materialize.min.css"
media="screen,projection"
/>
<link href="../../assets/css/material_icons.css" rel="stylesheet" />
<link type="text/css" rel="stylesheet" href="../../assets/css/styles.css" />
</head>

<body>
<div class="bar drag">
<div class="title">
<i class="material-icons tiny left">music_note</i>
<span
i18n="i18n_LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER"
id="i18n_LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER"
></span>
</div>

<div class="cmd-btns">
<div class="no-drag pointer cmd-btn hover-red" id="btn-close">
<i class="material-icons tiny">close</i>
</div>
</div>
</div>
<div class="content center">
<div class="row">
<div class="input-field col s12">
<input id="username" type="text" class="white-text" />
<label for="username">Username</label>
</div>
<div class="input-field col s12">
<input id="password" type="password" class="white-text" />
<label for="password">Password</label>
</div>
</div>
<div class="row">
<button class="btn red" id="btn-save" style="text-transform: none;">
<span
i18n="i18n_LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER_AUTHORIZE"
></span>
</button>
</div>
</div>
</body>
</html>

<script src="../../node_modules/materialize-css/dist/js/materialize.min.js"></script>

<script>
M.updateTextFields();
</script>

<script src="./last-fm-login.js"></script>
22 changes: 22 additions & 0 deletions pages/settings/last-fm-login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const __ = require("../../providers/translateProvider");
const scrobbleProvider = require("../../providers/scrobblerProvider");
__.loadi18n();
const Base64 = require("js-base64").Base64;

document.getElementById("btn-close").addEventListener("click", function() {
window.close();
});

var login = scrobbleProvider.getLogin();
if (login) {
document.getElementById("username").value = login.username;
document.getElementById("password").value = login.password;
}

document.getElementById("btn-save").addEventListener("click", function() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
scrobbleProvider.setLogin(username, password);

scrobbleProvider.getToken();
});
11 changes: 7 additions & 4 deletions pages/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@
i18n="i18n_LABEL_SETTINGS_TAB_GENERAL_CUSTOM_THEME"
></span>
<button
class="waves-effect waves-green btn-flat green-text"
class="waves-effect waves-light btn-flat white-text"
id="btn-editor-custom-theme"
>
<i class="material-icons">color_lens</i>
<i class="material-icons">settings</i>
</button>
</td>
<td class="right">
Expand Down Expand Up @@ -333,8 +333,11 @@
<span
i18n="i18n_LABEL_SETTINGS_TAB_LAST_FM_SCROBBLER"
></span>
<button class="btn-small" id="btn-last-fm-login">
Login
<button
class="waves-effect waves-light btn-flat white-text"
id="btn-last-fm-login"
>
<i class="material-icons">settings</i>
</button>
</td>
<td class="right">
Expand Down
9 changes: 7 additions & 2 deletions pages/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ initElement("settings-clipboard-read", "click", () => {
});
initElement("titlebar-type", "change", showRelaunchButton);
initElement("settings-custom-theme", "click", showRelaunchButton);
initElement("settings-last-fm-scrobbler", "click");
initElement("settings-last-fm-scrobbler", "click", () => {
var login = store.get("last-fm-login");
if (!login) {
ipc.send("show-last-fm-login");
}
});

if (elementSettingsCompanionApp) {
elementSettingsCompanionApp.addEventListener("click", function() {
Expand All @@ -68,7 +73,7 @@ if (elementBtnOpenEditor) {

if (elementBtnLastFmLogin) {
elementBtnLastFmLogin.addEventListener("click", function() {
scrobblerProvider.getToken();
ipc.send("show-last-fm-login");
});
}

Expand Down
77 changes: 63 additions & 14 deletions providers/scrobblerProvider.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,85 @@
const scribble = require("scribble");
const electronStore = require("electron-store");
const store = new electronStore();
const http = require("http");
const Base64 = require("js-base64").Base64;

const apiKey = "9ab417e8b808ed071223a1b4b3c29642";
const apiSecret = "9d8830c167627e65dac63786be101964";

var Scrobbler;

var userLogin = store.get("last-fm-login");
var userLogin = getLogin();

function signIn() {
Scrobbler = new scribble(
apiKey,
apiSecret,
userLogin.user,
userLogin.username,
userLogin.password
);
}

exports.getToken = function() {
var windowToken = window.open(
`http://ws.audioscrobbler.com/2.0/?method=auth.gettoken&api_key=${apiKey}&format=json`
function setLogin(username, password) {
store.set("last-fm-login", {
username: username,
password: Base64.encode(password)
});
}

function getLogin() {
var login = store.get("last-fm-login");
login.password = Base64.decode(login.password);

return login;
}

function getToken() {
http.get(
`http://ws.audioscrobbler.com/2.0/?method=auth.gettoken&api_key=${apiKey}&format=json`,
function(res) {
let rawData = "";
res.on("data", chunk => {
rawData += chunk;
});
res.on("end", () => {
try {
const parsedData = JSON.parse(rawData);
authorize(parsedData.token);
} catch (e) {
console.error(e.message);
}
});
}
);
console.log(windowToken);
};
}

function updateTrackInfo(title, author) {
if (store.get("settings-last-fm-scrobbler")) {
if (Scrobbler === undefined) {
signIn();
}

var song = {
artist: author,
track: title
};
Scrobbler.Scrobble(song, function(_) {});
}
}

exports.updateTrackInfo = function(title, author) {
console.log(Scrobbler);
var song = {
artist: author,
track: title
};
function authorize(token) {
var authorize = window.open(
`https://www.last.fm/api/auth?api_key=${apiKey}&token=${token}`,
"Authorize App",
"frame=true"
);
}

Scrobbler.Scrobble(song, function(_) {});
module.exports = {
getToken: getToken,
updateTrackInfo: updateTrackInfo,
getLogin: getLogin,
setLogin: setLogin,
authorize: authorize
};

0 comments on commit 308ca0f

Please sign in to comment.