Skip to content

Commit

Permalink
Improved error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioastarita committed Oct 12, 2016
1 parent 6f2c719 commit d9477cc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 44 deletions.
13 changes: 8 additions & 5 deletions render/Searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {MusicMatch} from "./plugins/MusicMatch";
import {SpotifyService} from './SpotifyService';
const async = require('async');
const plugins = [MusicMatch, SearchWikia];
const request = require('request');
const request = require('request').defaults({timeout: 5000});

export class Searcher {

Expand All @@ -30,14 +30,17 @@ export class Searcher {
}

syncLyrics(cb) {
console.log('sync lyrics called');
this.getSpotify().getCurrentSong((err, song) => {
if (err) {
this.sendStatus('Current song error: ' + err);
return;
return cb(true);
}
if (this.isLastSong(song)) {
return cb(this.lastSongSync, false);
console.log('is last song nothing to do here');
return cb(null, this.lastSongSync, false);
}
console.log('is not last song searching by title and artist');
this.sendStatus('Current song: ' + song.title);
this.search(song.title, song.artist, (err, lyric) => {
if (err) {
Expand All @@ -47,7 +50,7 @@ export class Searcher {
this.sendStatus('Song result!');
song.lyric = lyric;
this.saveLastSong(song);
cb(song, true);
cb(null, song, true);
});
});
}
Expand Down Expand Up @@ -77,7 +80,7 @@ export class Searcher {
if (!err) {
lyric = result;
}
callback(err, result)
callback(null, result)
})
}, (err) => {
cb(err, lyric);
Expand Down
13 changes: 5 additions & 8 deletions render/SongRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class SongRender {
if (this.timer) {
clearTimeout(this.timer);
}
console.warn('Scheduling ', this.nextCallTime / 1000 , ' seconds');
this.timer = setTimeout(() => {
this.refresh();
}, this.nextCallTime);
Expand All @@ -48,15 +49,11 @@ export class SongRender {
}

refresh() {
this.searcher.syncLyrics((song, changed) => {
this.song = song;
if (changed) {
new Notification(song.title, {
body: `Playing ` + song.title + ` - ` + song.artist,
icon: '../img/icon.png',
silent: true
});
this.searcher.syncLyrics((error, song, changed) => {
if (!error && changed) {
this.song = song;
}

this.scheduleNextCall();
});
}
Expand Down
82 changes: 56 additions & 26 deletions render/SpotifyService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const request = require('request');
const request = require('request').defaults({timeout: 5000});
const async = require('async');
const initialPortTest = 4370;

export class SpotifyService {

protected port = 4370;
protected foundPort = false;
protected port : number;
protected portTries = 15;

protected oAuthToken = {
Expand Down Expand Up @@ -31,20 +33,31 @@ export class SpotifyService {
return cb(null, this.oAuthToken.t);
}
request.get('https://open.spotify.com/token', (err, status, body) => {
let json = JSON.parse(body);
this.oAuthToken.t = json.t;
return cb(null, json.t);
if (err) {
return cb(err);
}
try {
let json = JSON.parse(body);
this.oAuthToken.t = json.t;
return cb(null, json.t);
} catch(e) {
return cb(e);
}
});
}

public detectPort(cb) {
if (!this.foundPort) {
this.port = initialPortTest;
}
async.retry(this.portTries, (finish) => {
this.getCsrfToken((err, token) => {
if (err) {
console.log('FAILED WITH PORT: ', this.port)
this.port++;
console.log('TRYING WITH PORT: ', this.port)
return finish(err);
}
this.foundPort = true;
console.log('VALID PORT', this.port);
finish(err, token)
});
Expand Down Expand Up @@ -91,10 +104,12 @@ export class SpotifyService {
'csrf': tokens.csrf,
};
let url = this.url('/remote/status.json') + '?' + this.encodeData(params);

request(url, {
headers: this.headers(),
'rejectUnauthorized': false,
}, (err, status, body) => {

if (err) {
return cb(err);
}
Expand All @@ -105,16 +120,20 @@ export class SpotifyService {
}

protected getAlbumImages(albumUri:string, cb) {
let id = albumUri.split('spotify:album:')[1];
let url = `https://api.spotify.com/v1/albums/${id}?oauth=${this.oAuthToken.t}`;

request(url, (err, status, body) => {
if (err) {
return cb(err, null)
}
let parsed = JSON.parse(body);
cb(null, parsed.images);
});
async.retry(2, (finish) => {
let id = albumUri.split('spotify:album:')[1];
let url = `https://api.spotify.com/v1/albums/${id}?oauth=${this.oAuthToken.t}`;
request(url, (err, status, body) => {
if (err) {
return finish(err, null)
}
let parsed = JSON.parse(body);
finish(null, parsed.images);
});
}, cb);


}

public pause(pause:boolean, cb) {
Expand Down Expand Up @@ -143,18 +162,29 @@ export class SpotifyService {
public getCurrentSong(cb) {
this.getStatus((err, status)=> {
if (err) return cb(err);
if (status.track) {
return this.getAlbumImages(status.track.album_resource.uri, (err, images) => {
return cb(null, {
playing: status.playing,
artist: status.track.artist_resource.name,
title: status.track.track_resource.name,
album: {
name: status.track.album_resource.name,
images: images
if (status.track && status.track.track_resource) {

let result = {
playing: status.playing,
artist: status.track.artist_resource ? status.track.artist_resource.name : 'Unknown',
title: status.track.track_resource.name,
album: {
name: 'Unknown',
images: null
}
};

if (status.track.album_resource) {
result.album.name = status.track.album_resource.name;
return this.getAlbumImages(status.track.album_resource.uri, (err, images) => {
if (!err) {
result.album.images = images;
}
})
})
return cb(null, result);
});
} else {
return cb(null, result);
}

}
return cb('No song', null)
Expand Down
17 changes: 12 additions & 5 deletions render/views/Song.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

<div v-if="song" class="lyric-view">
<header class="header">
<span class="header__background" :style="{ backgroundImage: 'url(' + song.album.images[0].url + ')' }"></span>
<img class="header__album-art" :src="song.album.images[0].url" alt="Album Art">

<!-- if we have images !-->
<span v-if="song.album.images" class="header__background" :style="{ backgroundImage: 'url(' + song.album.images[0].url + ')' }"></span>
<img v-if="song.album.images" class="header__album-art" :src="song.album.images[0].url" alt="{{song.album.name}}">

<!-- showing default logo !-->
<span v-if="song.album.images == null" class="header__background" style="background-image: url(../img/icon.png);"></span>
<img v-if="song.album.images == null" class="header__album-art" style="padding: 5px; box-sizing: border-box;" src="../img/icon.png" alt="{{song.album.name}}">

<h3>{{ song.title }}</h3>
<h4>{{ song.artist }}</h4>
</header>
Expand All @@ -14,7 +21,7 @@ <h4>{{ song.artist }}</h4>

<div v-else class="not-playing-view">
<img src="../img/waves.svg" alt="Waveform">
<h3>Play a song on Spotify</h3>
<p>Looks like you're not listening to music right now. Is Spotify runnig?</p>
<a href="#" @click.prevent="refresh" class="button">Retry</a>
<h3>Looking for a Song on Spotify</h3>
<p>Is Spotify runnig?</p>
<a href="#" @click.prevent="refresh" class="button">Try Again</a>
</div>

0 comments on commit d9477cc

Please sign in to comment.