Skip to content

Commit

Permalink
Merge pull request #58 from buhe/master
Browse files Browse the repository at this point in the history
增加显示歌词的功能
  • Loading branch information
guo-yu committed Jan 24, 2014
2 parents f7c08d5 + 1411fed commit c29b189
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
1 change: 1 addition & 0 deletions libs/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var actions = {
"[l] -> 添加到红心列表或者删除红心 (LOVE)",
"[s] -> 分享当前歌曲到新浪微博 (SHARE)",
"[g] -> 跳转到当前播放歌曲的专辑页面 (GOTO)",
"[r] -> 切换歌词显示 (LRC)",
"[q] -> 退出豆瓣电台 (QUIT)",
""
].join('\n')
Expand Down
63 changes: 62 additions & 1 deletion libs/fm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var fs = require('fs'),
params = require('paramrule'),
consoler = require('consoler'),
exeq = require('exeq'),
Lrc = require('lrc').Lrc,
sys = require('../package'),
sdk = require('./sdk'),
utils = require('./utils'),
Expand All @@ -38,13 +39,15 @@ var shorthands = {
'l': 'loving',
'n': 'next',
'q': 'quit',
's': 'share'
's': 'share',
'r': 'showLrc'
};

var Fm = function(params) {
this.home = params && params.home ? params.home : path.join(utils.home(), 'douban.fm');
this.love = path.join(this.home, 'love');
this.shorthands = shorthands;
this.isShowLrc = true;
};

Fm.prototype.play = function(channel, user) {
Expand Down Expand Up @@ -94,6 +97,7 @@ Fm.prototype.play = function(channel, user) {
self.player.on('playing', function(song) {
self.status = 'playing';
self.label(-1, color.yellow('>>'));
self.playLrc(song);
self.update(
channel.index,
printf(
Expand Down Expand Up @@ -227,6 +231,60 @@ Fm.prototype.go = function(channel, user, link) {
]).run();
}

Fm.prototype.playLrc = function(song) {
var self = this;
var title = song.title;
var author = song.artist;
if(self.lrc){
self.lrc.stop();
}
sdk.lrc(title,author,function(data){
if(!data){
self.printLrc('没找到歌词');
}else{
self.printLrc('正在拼命加载歌词....');
self.lrc = new Lrc(data.toString(),function(line,extra){
self.printLrc(line);
});
self.lrc.play(0);
}

});
}

Fm.prototype.printLrc = function(lrc) {
if(this.isShowLrc){
var currentMenu = this.currentMenu;
this.menu.remove(this.menuIndex);
this.menu.add(this.menuIndex,'歌词: '+lrc);
if(currentMenu){
this.menu.select(currentMenu);
}
this.menu.draw();
}
}

Fm.prototype.showLrc = function(lrc) {
if(this.isShowLrc){
var currentMenu = this.currentMenu;
this.isShowLrc = false;
this.menu.remove(this.menuIndex);
if(currentMenu){
this.menu.select(currentMenu);
}
this.menu.draw();
}else{
this.isShowLrc = true;
var currentMenu = this.currentMenu;
this.menu.add(this.menuIndex,'歌词开启');
if(currentMenu){
this.menu.select(currentMenu);
}
this.menu.draw();
}
}


Fm.prototype.share = function(channel, user) {
if (!this.player) return false;
if (!this.player.playing) return false;
Expand Down Expand Up @@ -271,6 +329,7 @@ Fm.prototype.share = function(channel, user) {
Fm.prototype.createMenu = function(callback) {
var self = this;
var shorthands = self.shorthands;
self.menuIndex = 0;
sdk.channels(function(err, list) {
if (err) return consoler.error('获取豆瓣电台频道出错,请稍后再试');
self.configs(function(err, user) {
Expand All @@ -297,6 +356,7 @@ Fm.prototype.createMenu = function(callback) {
channel.index = index;
self.menu.add(index, channel.name);
self.channels[index] = channel;
self.menuIndex++;
});
// start menu
self.menu.start();
Expand All @@ -305,6 +365,7 @@ Fm.prototype.createMenu = function(callback) {
self.menu.on('keypress', function(key, index) {
if (!shorthands[key.name]) return false;
if (index < 0 && key.name != 'q') return exeq(['open ' + sys.repository.url]).run();
self.currentMenu = index;
return self[shorthands[key.name]](self.channels[index], user);
});
self.menu.on('empty', function() {
Expand Down
17 changes: 17 additions & 0 deletions libs/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,20 @@ exports.channels = function(callback) {
return callback(null, [privateHz].concat(result.channels));
});
};

exports.lrc = function(title,artist,callback){
api.get('http://geci.me/api/lyric/'+title+'/' + artist, {}, function(err, result) {
if (err) return callback(err);
var result = result.body;
if (result.count > 0){
var lrc = result.result[0].lrc;
api.get(lrc, {}, function(err, result) {
if (err) return callback(err);
var result = result.body;
callback(result);
});
}else{
callback(result.err);
}
});
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"sprintf": "*",
"paramrule": "*",
"term-list": "*",
"underscore": "*"
"underscore": "*",
"lrc": "*"
},
"devDependencies": {
"mocha": "*",
Expand Down

0 comments on commit c29b189

Please sign in to comment.