Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #56 from eNkru/feature/fix_deprecated
Browse files Browse the repository at this point in the history
fix: replace deprecated method
  • Loading branch information
eNkru authored Nov 9, 2018
2 parents d2d4ea3 + 17a952c commit 1af5906
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/controller/player-controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {app, BrowserWindow, Notification, ipcMain, TouchBar, nativeImage} = require('electron');
const {app, BrowserWindow, session, ipcMain, TouchBar, nativeImage} = require('electron');
const {TouchBarButton} = TouchBar
const urlLib = require('url');
const https = require('https');
Expand All @@ -13,8 +13,8 @@ const timeFormat = require('hh-mm-ss');
const UpdateController = require('./update-controller');

const playerUrl = 'https://www.xiami.com/play';
const playlistUrl = 'https://www.xiami.com/song/playlist';
const getLyricUrl = 'https://img.xiami.net/lyric/';
const playlistUrlPrefix = 'https://www.xiami.com/song/playlist*';
const getLyricUrlPrefix = 'https://img.xiami.net/lyric/*';

const language = fs.existsSync(`${app.getPath('userData')}/Settings`) ? settings.get('language', 'en') : 'en';
const Locale = language === 'en' ? require('../locale/locale_en') : require('../locale/locale_sc');
Expand Down Expand Up @@ -127,7 +127,7 @@ class XiamiPlayer {
});

// intercept the ajax call response
this.window.webContents.on('did-get-response-details', ((event, status, newURL, originalURL) => this.handleResponse(originalURL)));
session.defaultSession.webRequest.onCompleted({urls: [playlistUrlPrefix, getLyricUrlPrefix]}, (details) => this.handleResponse(details))

ipcMain.on('playtime', (event, value) => {
const timeline = this.lyrics.select(timeFormat.toS(value));
Expand Down Expand Up @@ -230,19 +230,20 @@ class XiamiPlayer {

/**
* Handle the received response after the web content make a request.
* @param {*} requestUrl the request URL for the event
* @param {*} details the response details
*/
handleResponse(requestUrl) {
requestUrl.startsWith(playlistUrl) && this.updatePlaylist(requestUrl);
handleResponse(details) {
const url = details.url
RegExp(playlistUrlPrefix).test(url) && this.updatePlaylist(url);

if (requestUrl.startsWith(getLyricUrl)) {
if (RegExp(getLyricUrlPrefix).test(url)) {
// Load Lyrics.
this.loadLyrics(requestUrl);
this.loadLyrics(url);

// Load track change notification.
const showNotification = settings.get('showNotification', 'check');
if ('check' === showNotification) {
const lyricPath = urlLib.parse(requestUrl).pathname;
const lyricPath = urlLib.parse(url).pathname;
const songId = lyricPath.match(/\/(\d*)_/)[1];
this.notifyTrackChange(songId);
}
Expand All @@ -251,7 +252,7 @@ class XiamiPlayer {

/**
* Update the playlist if the request URL is for playlist update.
* @param {*} requestUrl the request URL for the event
* @param {string} requestUrl the request URL for the event
*/
updatePlaylist(requestUrl) {
let urlWithPath = urlLib.parse(requestUrl, false);
Expand Down Expand Up @@ -289,9 +290,27 @@ class XiamiPlayer {
});
}

/**
* Load the lyrics into the application
* @param {string} url the lyrics url
*/
loadLyrics(url) {
https.get(url, (response) => {
let lyricContent = '';

response.on('data', (chunk) => {
lyricContent += chunk;
});

response.on('end', () => {
this.lyrics.load(lyricContent)
});
})
}

/**
* Handle the track changed.
* @param {*} songId the changed song ID
* @param {string} songId the changed song ID
*/
notifyTrackChange(songId) {
// console.log(songId)
Expand All @@ -315,10 +334,6 @@ ${Locale.NOTIFICATION_ALBUM}: ${trackInfo.album_name}`;
}
});
}

loadLyrics(url) {
download(url).then(buffer => this.lyrics.load(buffer));
}
}

module.exports = XiamiPlayer;

0 comments on commit 1af5906

Please sign in to comment.