Skip to content
This repository was archived by the owner on Jun 26, 2024. It is now read-only.

Commit 55d570d

Browse files
author
trazyn
authored
Merge pull request #228 from trazyn/dev
Feature #223 #205
2 parents 0db8195 + a2d9f4d commit 55d570d

File tree

5 files changed

+121
-6
lines changed

5 files changed

+121
-6
lines changed

main.js

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11

2+
import fs from 'fs';
23
import path from 'path';
3-
import { app, BrowserWindow, Menu, Tray, globalShortcut, ipcMain, shell, powerMonitor, dialog } from 'electron';
4+
import { app, BrowserWindow, Menu, Tray, globalShortcut, ipcMain, shell, powerMonitor, dialog, Notification } from 'electron';
45
import windowStateKeeper from 'electron-window-state';
56
import storage from 'electron-json-storage';
67
import { autoUpdater } from 'electron-updater';
78
import axios from 'axios';
9+
import nodeID3 from 'node-id3';
10+
import tmp from 'tmp-promise';
11+
import mkdirp from 'mkdirp';
12+
import rp from 'request-promise-native';
813
import _debug from 'debug';
914

1015
import pkg from './package.json';
@@ -13,6 +18,7 @@ import api from './server/api';
1318
import usocket from './server/usocket';
1419

1520
const _PLATFORM = process.platform;
21+
const _DOWNLOAD_DIR = path.join(app.getPath('music'), pkg.name);
1622

1723
let debug = _debug('dev:main');
1824
let error = _debug('dev:main:error');
@@ -462,6 +468,104 @@ function registerGlobalShortcut() {
462468
});
463469
}
464470

471+
async function getCookies() {
472+
return new Promise((resolve, reject) => {
473+
mainWindow.webContents.session.cookies.get(
474+
{},
475+
(err, cookies) => {
476+
if (err) {
477+
return resolve();
478+
}
479+
480+
resolve(cookies.map(e => `${e.name}=${e.value}`).join('; '));
481+
}
482+
);
483+
});
484+
}
485+
486+
async function writeFile(url, filepath) {
487+
var cookies = await getCookies();
488+
var file = fs.createWriteStream(filepath);
489+
490+
return new Promise((resolve, reject) => {
491+
try {
492+
rp({
493+
url,
494+
headers: {
495+
'Cookie': cookies,
496+
'Origin': 'http://music.163.com',
497+
'Referer': 'http://music.163.com/',
498+
}
499+
}).pipe(file);
500+
501+
file.on('finish', () => {
502+
file.end();
503+
resolve();
504+
});
505+
file.on('error', err => {
506+
throw err;
507+
});
508+
} catch (ex) {
509+
reject(ex);
510+
}
511+
});
512+
}
513+
514+
async function download(song) {
515+
var src = song.data.src;
516+
var imagefile = (await tmp.file()).path;
517+
var trackfile = path.join(
518+
_DOWNLOAD_DIR,
519+
`${song.artists.map(e => e.name).join()} - ${song.name}.${src.match(/^http.*\.(.*)$/)[1]}`
520+
);
521+
var notificationOptions = {
522+
subtitle: song.name,
523+
body: song.artists.map(e => e.name).join(),
524+
closeButtonText: 'Done'
525+
};
526+
527+
try {
528+
// Make sure the download directory already exists
529+
if (fs.existsSync(_DOWNLOAD_DIR) === false) {
530+
mkdirp.sync(_DOWNLOAD_DIR);
531+
}
532+
533+
await writeFile(src, trackfile);
534+
await writeFile(song.album.cover.replace(/\?.*/, ''), imagefile);
535+
536+
let tags = {
537+
title: song.name,
538+
artist: song.artists.map(e => e.name).join(),
539+
album: song.album.name,
540+
image: imagefile,
541+
};
542+
let success = nodeID3.write(tags, trackfile);
543+
544+
if (!success) {
545+
throw Error('Failed to write ID3 tags: \'%s\'', trackfile);
546+
}
547+
548+
let notification = new Notification({
549+
title: '🍉 Download Success~',
550+
...notificationOptions,
551+
});
552+
553+
notification.on('click', () => {
554+
shell.showItemInFolder(_DOWNLOAD_DIR);
555+
});
556+
notification.show();
557+
} catch (ex) {
558+
error(ex);
559+
fs.unlink(trackfile);
560+
fs.unlink(imagefile);
561+
562+
new Notification({
563+
title: '😕 Download Failed~',
564+
...notificationOptions,
565+
}).show();
566+
}
567+
}
568+
465569
const goodbye = () => {
466570
forceQuit = true;
467571
app.quit();
@@ -603,6 +707,11 @@ const createMainWindow = () => {
603707
updateTray(args.playing);
604708
});
605709

710+
// Download track
711+
ipcMain.on('download', (event, args) => {
712+
download(JSON.parse(args.song));
713+
});
714+
606715
// Show the main window
607716
ipcMain.on('show', event => {
608717
mainWindow.show();
@@ -655,8 +764,6 @@ app.on('activate', e => {
655764
app.on('before-quit', e => {
656765
e.preventDefault();
657766

658-
console.log('Before quit...');
659-
660767
if (quitting) {
661768
return;
662769
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
"mobx": "^5.0.3",
141141
"mobx-react": "^5.2.3",
142142
"moment": "^2.22.2",
143+
"node-id3": "^0.1.5",
144+
"node-mkdirp": "0.0.1",
143145
"perdido": "^2.0.1",
144146
"prop-types": "^15.6.2",
145147
"react": "^16.4.1",
@@ -152,6 +154,7 @@
152154
"request": "^2.87.0",
153155
"request-promise-native": "^1.0.5",
154156
"simple-lastfm": "^1.0.5",
157+
"tmp-promise": "^1.0.5",
155158
"webpack-cli": "^3.0.8"
156159
}
157160
}

server/search/Kuwo.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ export default async(request, keyword, artists) => {
3737
e => artists.findIndex(artist => e.ARTIST.indexOf(artist) !== -1) > -1
3838
);
3939

40-
console.log(payload);
41-
4240
if (!payload) {
4341
error(chalk.black.bgRed('🚧 Nothing.'));
4442
return Promise.reject(Error(404));

server/search/MiGu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default async(request, keyword, artists) => {
1010

1111
try {
1212
var response = await request({
13-
uri: 'http://m.10086.cn/migu/remoting/scr_search_tag',
13+
uri: 'http://m.music.migu.cn/migu/remoting/scr_search_tag',
1414
qs: {
1515
keyword: [keyword].concat(artists.split(',')).join('+'),
1616
type: 2,

src/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ class App extends Component {
173173
fm.ban(controller.song.id);
174174
}
175175
},
176+
{
177+
label: 'Download 🍭',
178+
enabled: logined,
179+
click: () => {
180+
ipcRenderer.send('download', { song: JSON.stringify(controller.song) });
181+
}
182+
},
176183
{
177184
type: 'separator',
178185
},

0 commit comments

Comments
 (0)