thedesk/app/main/np.js

71 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-05-19 17:39:30 +10:00
function np(mainWindow) {
2019-04-03 14:59:29 +11:00
const electron = require("electron");
const join = require('path').join;
const app = electron.app;
const fs = require("fs");
var ipc = electron.ipcMain;
2019-04-14 02:33:00 +10:00
ipc.on('itunes', async (e, args) => {
2019-04-03 14:59:29 +11:00
//Verified on Windows
console.log("Access");
2019-05-19 17:39:30 +10:00
if (args[0] == "set") {
var { NowPlaying, PlayerName } = require("nowplaying-node");
var nppath = join(app.getPath("userData"), "nowplaying");
2019-04-03 14:59:29 +11:00
var npProvider;
try {
npProvider = args[1];
} catch (e) {
2019-05-19 17:39:30 +10:00
npProvider = "AIMP";
2019-04-03 14:59:29 +11:00
}
var myAIMP = new NowPlaying({
fetchCover: true,
player: PlayerName[npProvider],
});
fs.writeFileSync(nppath, npProvider);
2019-05-19 17:39:30 +10:00
} else {
var platform = process.platform;
var bit = process.arch;
if (platform == "darwin") {
try {
const nowplaying = require("itunes-nowplaying-mac");
const value = await nowplaying.getRawData();
e.sender.webContents.send('itunes-np', value);
2019-05-19 17:39:30 +10:00
} catch (error) {
// エラーを返す
console.error(error);
e.sender.webContents.send('itunes-np', error);
2019-05-19 17:39:30 +10:00
}
} else {
var { NowPlaying, PlayerName } = require("nowplaying-node");
var nppath = join(app.getPath("userData"), "nowplaying");
2019-04-03 14:59:29 +11:00
var npProvider;
try {
npProvider = fs.readFileSync(nppath, 'utf8');
} catch (e) {
2019-05-19 17:39:30 +10:00
npProvider = "AIMP";
2019-04-03 14:59:29 +11:00
}
var myAIMP = new NowPlaying({
fetchCover: true,
player: PlayerName[npProvider],
});
myAIMP.update();
2019-05-19 17:39:30 +10:00
var path = myAIMP.getCoverPath();
if (path) {
2019-04-03 14:59:29 +11:00
var bin = fs.readFileSync(path, 'base64');
2019-05-19 17:39:30 +10:00
} else {
var bin = false;
2019-04-03 14:59:29 +11:00
}
2019-05-19 17:39:30 +10:00
var value = {
win: true,
name: myAIMP.getTitle(),
artist: myAIMP.getArtist(),
album: myAIMP.getAlbum(),
path: bin
2019-04-03 14:59:29 +11:00
}
e.sender.webContents.send('itunes-np', value);
2019-04-03 14:59:29 +11:00
}
2019-05-19 17:39:30 +10:00
}
2019-04-03 14:59:29 +11:00
});
}
exports.TheDeskNowPlaying = np;