Skip to content

Commit

Permalink
Merge pull request #91 from gnehs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gnehs authored Oct 17, 2018
2 parents 83aa124 + aa035f7 commit 6ec8a7b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 103 deletions.
7 changes: 6 additions & 1 deletion dataModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ router.get("/", (req, res) => {
router.use((req, res, next) => {
if (req.session.pass != config.PokaPlayer.password && config.PokaPlayer.passwordSwitch)
res.status(403).send("Permission Denied Desu");
else next();
else {
if (req.method.toUpperCase() === "GET" && config.PokaPlayer.debug) {
res.header("Cache-Control", "max-age=7200") //快取 2hr
}
next();
}
});
//-----------------------------> 首頁
// 取得想推薦的東西(?
Expand Down
177 changes: 83 additions & 94 deletions dataModule/netease2.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,22 @@ function isIdName(id) {

var isLoggedin;

const normalOptions = async (url, req = {}) => {
const normalOptions = async(url, req = {}) => {
async function m10() {
return (await m10s)[Math.floor(Math.random() * (await m10s).length)];
}
return {
method: "GET",
uri: url.replace("m10.music.126.net", `${await m10()}/m10.music.126.net`),
headers: {
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7",
Connection: "keep-alive",
"Cache-Control": "max-age=0",
DNT: 1,
"Upgrade-Insecure-Requests": 1,
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
Range: req.headers && req.headers.range ? req.headers.range : "",
Accept: req.headers && req.headers.accept ? req.headers.accept : ""
},
Expand Down Expand Up @@ -206,12 +204,12 @@ function migrate(org, t, offset = 10 ** -3) {
const strip = (x, precision = 12) => +parseFloat(x.toPrecision(precision)); // 數字精確化

const tagToTime = tag =>
isDigit(tag[0])
? tag
.split(":")
.reverse()
.reduce((acc, cur, index) => plus(acc, Number(cur) * 60 ** index), 0)
: tag;
isDigit(tag[0]) ?
tag
.split(":")
.reverse()
.reduce((acc, cur, index) => plus(acc, Number(cur) * 60 ** index), 0) :
tag;
const parse = (x, isTranslated = false) => {
let pLyricLines = x
.split("\n")
Expand Down Expand Up @@ -282,7 +280,9 @@ function migrate(org, t, offset = 10 ** -3) {
i += 1;
}
} else {
parsedLyricPairs.push([parsedLyrics[i][0], [parsedLyrics[i][1], parsedLyrics[i][1]]]);
parsedLyricPairs.push([parsedLyrics[i][0],
[parsedLyrics[i][1], parsedLyrics[i][1]]
]);
i += 1;
}
}
Expand Down Expand Up @@ -335,7 +335,7 @@ async function login(config) {
async function onLoaded() {
if (!config.enabled) return false;
console.log("[DataModules][Netease2] 正在登入...");
return await fs.ensureFile(pin).then(async () => {
return await fs.ensureFile(pin).then(async() => {
if (
config &&
config.login &&
Expand Down Expand Up @@ -388,7 +388,7 @@ function genReq(link) {

async function parseSongs(songs, br = 999000) {
return await Promise.all(
(await songs).map(async (song, index) => {
(await songs).map(async(song, index) => {
song = await song;
return {
name: song.name,
Expand All @@ -410,7 +410,10 @@ async function getSong(req, songRes, id) {
let isArray = Array.isArray(id);
id = isArray ? id : [id];
let result = await Promise.all(
(await getSongsUrl(id, br)).map(async x => request(await normalOptions(x.url, req)))
(await getSongsUrl(id, br)).map(async x => {
let url = x.url ? x.url : `http://music.163.com/song/media/outer/url?id=${x.id}.mp3`
return request(await normalOptions(url, req))
})
);
return isArray ? result : result[0];
}
Expand All @@ -437,7 +440,7 @@ async function getAlbum(id) {
async function getSongsUrl(songs, br = 999000) {
let isArray = Array.isArray(songs);
songs = isArray ? songs : [songs];
let result = await rp(options(`${server}music/url?br=${br}&id=${songs.join()}`));
let result = await rp(options(`${server}song/url?br=${br}&id=${songs.join()}`));
return isArray ? result.data : result.data[0];
}

Expand Down Expand Up @@ -503,13 +506,13 @@ async function search(keywords, limit = 30) {
album: 10,
artist: 100,
playlist: 1000
// user: 1002,
// mv: 1004,
// lyric: 1006,
// radio: 1009
// user: 1002,
// mv: 1004,
// lyric: 1006,
// radio: 1009
};

let result = await Object.keys(typeNums).reduce(async (results, type) => {
let result = await Object.keys(typeNums).reduce(async(results, type) => {
let types = type + "s";
let typeNum = typeNums[type];
let result;
Expand Down Expand Up @@ -567,15 +570,14 @@ async function resolveTopPlaylistStack(topPlaylistStack) {
(await Promise.all(topPlaylistStack)).map(x => (x[0] ? x[0].playlists : x.playlists))
).map(
x =>
x
? {
name: x.name,
source: "Netease2",
id: x.id,
image: imageUrl(x.coverImgUrl || x.picUrl),
from: "topPlaylistStack"
}
: false
x ? {
name: x.name,
source: "Netease2",
id: x.id,
image: imageUrl(x.coverImgUrl || x.picUrl),
from: "topPlaylistStack"
} :
false
);
return [].concat(...playlists);
}
Expand All @@ -584,22 +586,19 @@ async function resolvePlaylistStack(playlistStack) {
if (playlistStack.length === 0) return playlistStack;
return (await Promise.all(playlistStack)).map(
x =>
Array.isArray(x)
? {
name: x[1].name || x[0].playlist.name,
source: "Netease2",
id: x[0].playlist.id,
image:
x[1].image || imageUrl(x[0].playlist.coverImgUrl || x[0].playlist.picUrl),
from: "playlistStack"
}
: {
name: x.playlist.name,
source: "Netease2",
id: x.playlist.id,
image: imageUrl(x.playlist.coverImgUrl || x.playlist.picUrl),
from: "playlistStack"
}
Array.isArray(x) ? {
name: x[1].name || x[0].playlist.name,
source: "Netease2",
id: x[0].playlist.id,
image: x[1].image || imageUrl(x[0].playlist.coverImgUrl || x[0].playlist.picUrl),
from: "playlistStack"
} : {
name: x.playlist.name,
source: "Netease2",
id: x.playlist.id,
image: imageUrl(x.playlist.coverImgUrl || x.playlist.picUrl),
from: "playlistStack"
}
);
}

Expand All @@ -613,21 +612,19 @@ async function resolvedailyRecommendStack(dailyRecommendStack) {
)
).map(
x =>
Array.isArray(x)
? {
name: x[1].name,
id: x[1].id,
image: x[0] || imageUrl(x.coverImgUrl || x.picUrl),
source: "Netease2",
from: "dailyRecommendStack"
}
: {
name: x.name,
id: x.id,
image: imageUrl(x.coverImgUrl || x.picUrl),
source: "Netease2",
from: "dailyRecommendStack"
}
Array.isArray(x) ? {
name: x[1].name,
id: x[1].id,
image: x[0] || imageUrl(x.coverImgUrl || x.picUrl),
source: "Netease2",
from: "dailyRecommendStack"
} : {
name: x.name,
id: x.id,
image: imageUrl(x.coverImgUrl || x.picUrl),
source: "Netease2",
from: "dailyRecommendStack"
}
)
);
}
Expand Down Expand Up @@ -832,7 +829,7 @@ async function getPlaylists(playlists) {

async function getPlaylistSongs(id, br = 999000) {
let name;
if (isIdName(id)) [id, name] = decomposeIdName(id);
if (isIdName(id))[id, name] = decomposeIdName(id);
if (id == "dailyRecommendSongs") {
let result = await rp(options(`${server}recommend/songs`));
if (result.code == 200) {
Expand All @@ -848,13 +845,11 @@ async function getPlaylistSongs(id, br = 999000) {
}));
return {
songs: r,
playlists: [
{
name: "每日推薦歌曲",
source: "Netease2",
id
}
]
playlists: [{
name: "每日推薦歌曲",
source: "Netease2",
id
}]
};
} else {
console.error(`[DataModules][Netease2] 無法獲取每日推薦歌單。(${result.code})`);
Expand All @@ -865,13 +860,11 @@ async function getPlaylistSongs(id, br = 999000) {
if (result.code == 200) {
return {
songs: await parseSongs(result.data.map(x => x.simpleSong)),
playlists: [
{
name: "網易雲音樂雲盤",
source: "Netease2",
id: "yunPan"
}
]
playlists: [{
name: "網易雲音樂雲盤",
source: "Netease2",
id: "yunPan"
}]
};
} else {
console.error(`[DataModules][Netease2] 無法獲取網易雲音樂雲盤。(${result.code})`);
Expand All @@ -882,14 +875,12 @@ async function getPlaylistSongs(id, br = 999000) {
if (result.code == 200) {
return {
songs: await parseSongs(result.playlist.tracks),
playlists: [
{
name: name ? name : result.playlist.name,
source: "Netease2",
id: id,
image: imageUrl(result.playlist.coverImgUrl || result.playlist.picUrl)
}
]
playlists: [{
name: name ? name : result.playlist.name,
source: "Netease2",
id: id,
image: imageUrl(result.playlist.coverImgUrl || result.playlist.picUrl)
}]
};
} else {
console.error(`[DataModules][Netease2] 無法獲取歌單 ${id}。(${result.code})`);
Expand Down Expand Up @@ -1015,12 +1006,12 @@ async function getHome() {
topPlaylistStack.push(
new Promise((resolve, reject) => {
rp(
options(
`${server}top/playlist?limit=${c.limit}&order=${
options(
`${server}top/playlist?limit=${c.limit}&order=${
c.order in ["hot", "new"] ? c.order : "hot"
}&cat=${c.category}`
)
)
)
.then(data =>
resolve([data, { image: config.topPlaylist.image || defaultImage }])
)
Expand Down Expand Up @@ -1121,24 +1112,22 @@ async function getHome() {
function playlistOperation(operation) {
switch (operation) {
case "add":
return async (songIds, playlistId) => {
return async(songIds, playlistId) => {
if (Array.isArray(songIds)) songIds = songIds.join(",");
let response = await rp(
options(
`${server}playlist/tracks?op=add&pid=${playlistId}&tracks=${songIds}`,
{},
`${server}playlist/tracks?op=add&pid=${playlistId}&tracks=${songIds}`, {},
true
)
);
return response.code < 300 && code >= 200 ? response.body || true : false;
};
case "del":
return async (songIds, playlistId) => {
return async(songIds, playlistId) => {
if (Array.isArray(songIds)) songIds = songIds.join(",");
let response = await rp(
options(
`${server}playlist/tracks?op=del&pid=${playlistId}&tracks=${songIds}`,
{},
`${server}playlist/tracks?op=del&pid=${playlistId}&tracks=${songIds}`, {},
true
)
);
Expand Down Expand Up @@ -1202,4 +1191,4 @@ module.exports = {
req,
getUserPlaylists,
playlistOperation
};
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pokaplayer",
"version": "0.12.7",
"version": "0.13.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 6ec8a7b

Please sign in to comment.