Skip to content

Commit

Permalink
Spotify no longer needs to be running on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tma02 committed Mar 19, 2016
1 parent dffab7a commit 9fb8638
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script src="index.js"></script>
</head>
<body>
<div id="loading">Retrieving album data...</div>
<div id="loading">Waiting for Spotify...</div>
<img id="background"><img id="cover">
</body>
</html>
8 changes: 6 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require('ipc').on('coverUrl', function(url) {
var ipc = require('ipc');
ipc.on('coverUrl', function(url) {
document.getElementById('background').src = url;
document.getElementById('cover').src = url;
});
ipc.on('loadingText', function(text) {
document.getElementById('loading').innerHTML = text;
});
document.onkeydown = function(e) {
e = e || window.event;
if (e.keyCode == 27) {
require('remote').getCurrentWindow().close();
}
};
};
69 changes: 47 additions & 22 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const electron = require('electron');
// Module to control application life.
const app = electron.app;
const ipc = require('ipc');
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

Expand Down Expand Up @@ -53,6 +52,9 @@ app.on('activate', function () {
});

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
process.on('uncaughtException', function (error) {
console.log(error);
});

const https = require('https');

Expand All @@ -68,6 +70,8 @@ const DEFAULT_HTTPS_CONFIG = {

let config;
let version;
version = {};
version.running = false;
let csrf;
let oauth;
let albumId;
Expand Down Expand Up @@ -141,28 +145,49 @@ function getAlbumCover(id) {
});
}

config = copyConfig();
config.host = generateLocalHostname();
config.path = '/service/version.json?service=remote';
getJson(config, function(data) { version = data; });
config.host = generateLocalHostname();
config.path = '/simplecsrf/token.json';
getJson(config, function(data) { csrf = data.token; });
config.host = 'open.spotify.com';
config.path = '/token';
config.port = 443;
getJson(config, function(data) { oauth = data.t; });
let updateTrackCover;
let waitForRequest = setInterval(function() {
if (typeof version !== 'undefined' && typeof csrf !== 'undefined' && typeof oauth !== 'undefined') {
clearInterval(waitForRequest);
console.log('done.');
console.log(version);
console.log(csrf);
console.log(oauth);
updateTrackCover = setInterval(getCurrentAlbumId, UPDATE_INTERVAL);
function grabTokens() {
if (mainWindow !== null) {
mainWindow.webContents.send('loadingText', 'Connecting to Spotify...');
}
config.host = generateLocalHostname();
config.path = '/simplecsrf/token.json';
getJson(config, function(data) { csrf = data.token; });
config.host = 'open.spotify.com';
config.path = '/token';
config.port = 443;
getJson(config, function(data) { oauth = data.t; });
let updateTrackCover;
let waitForRequest = setInterval(function() {
if (typeof version !== 'undefined' && typeof csrf !== 'undefined' && typeof oauth !== 'undefined') {
clearInterval(waitForRequest);
console.log('done.');
console.log(version);
console.log(csrf);
console.log(oauth);
updateTrackCover = setInterval(getCurrentAlbumId, UPDATE_INTERVAL);
}
else {
console.log('waiting for authentication...');
}
}, 500);
}

let waitForSpotify = setInterval(function() {
if (typeof version !== 'undefined' && version.running) {
clearInterval(waitForSpotify);
grabTokens();
}
else {
console.log('waiting...');
config = copyConfig();
config.host = generateLocalHostname();
config.path = '/service/version.json?service=remote';
getJson(config, function(data) {
if (!('running' in data)) {
data.running = true;
}
version = data;
console.log(version);
});
console.log('waiting for spotify...');
}
}, 500);

0 comments on commit 9fb8638

Please sign in to comment.