Skip to content

Commit

Permalink
main: fix recommend statistics and related album/playlists api
Browse files Browse the repository at this point in the history
  • Loading branch information
rocka committed Jul 7, 2023
1 parent 8b28475 commit 34a553c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
12 changes: 7 additions & 5 deletions src/main/api/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ import { encodeWeb, encodeLinux, encodeEApi, decodeEApi, getCacheKey } from './c

const d = debug('HTTP');

class HttpClient {
export default class HttpClient {

static DesktopUserAgent = 'Mozilla/5.0 (Linux) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36';
static MobileUserAgent = 'Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Mobile Safari/537.36';

constructor() {
this.clientHeaders = {
Accept: '*/*',
'Accept-Language': 'zh',
'Accept-Encoding': 'gzip',
Referer: 'https://music.163.com/',
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Mobile Safari/537.36'
'User-Agent': HttpClient.MobileUserAgent
};
this.initCookieJar();
}

initCookieJar() {
this.cookieJar = new CookieJar();
this.cookieJar.setCookies([
'appver=8.8.12',
'appver=8.10.20',
'mobilename=linux',
'os=android',
'osver=10.0.0',
Expand Down Expand Up @@ -234,5 +238,3 @@ class HttpClient {
return json;
}
}

export default HttpClient;
53 changes: 31 additions & 22 deletions src/main/api/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import url from 'url';
import path from 'path';
import crypto from 'crypto';
import qs from 'querystring';
Expand All @@ -9,15 +8,15 @@ import { decodeHTML } from 'entities';

import Cache from './cache';
import migrate from './migrate';
import Client from './httpClient';
import HttpClient from './httpClient';
import { encodePicUrl } from './codec';
import * as Settings from '../settings';
import MusicServer from './musicServer';
import { getDiskUsage, clearDirectory } from '../util/fs';
import Downloader from './downloader';

const BaseURL = 'https://music.163.com';
const client = new Client();
const client = new HttpClient();

const dataPath = app.getPath('userData');
const CachePath = {
Expand Down Expand Up @@ -288,14 +287,12 @@ export function getMusicUrlE(idOrIds, quality) {
* @returns {Promise<Types.MusicUrlLocalRes>}
*/
export async function getMusicUrlLocal(id, quality, ignoreCache = false) {
const search = new URLSearchParams({ id, quality });
if (ignoreCache) {
search.append(ignoreCache, 'true');
}
return {
url: url.format({
protocol: 'http:',
hostname: 'localhost',
port: musicServerPort,
pathname: '/music',
query: ignoreCache ? { id, quality, ignoreCache } : { id, quality }
})
url: `http://localhost:${musicServerPort}/music?${search.toString()}`
};
}

Expand Down Expand Up @@ -782,20 +779,17 @@ const RelatedPlaylists = {
* @param {string} u
*/
trimSrc(u) {
const o = url.parse(u);
return url.format({
protocol: 'https',
host: o.host,
pathname: o.pathname
});
const url = new URL(u);
url.search = '';
return url.href;
},
/**
* @param {string} u
*/
trimId(u) {
const o = url.parse(u);
const { id } = qs.parse(o.query);
return Array.isArray(id) ? id[0] : id;
const i = u.indexOf('?');
const s = new URLSearchParams(i > 0 ? u.slice(i) : u);
return s.get('id');
}
};

Expand All @@ -806,7 +800,12 @@ const RelatedPlaylists = {
*/
export async function getRelatedPlaylists(id) {
try {
const html = await client.get(`${BaseURL}/playlist?id=${id}`);
const html = await client.get({
url: `${BaseURL}/playlist?id=${id}`,
headers: {
'User-Agent': HttpClient.DesktopUserAgent
}
});
const data = [];
let match;
while ((match = RelatedPlaylists.regexp.exec(html)) !== null) {
Expand Down Expand Up @@ -835,7 +834,12 @@ const RecommendStatistics = {
*/
export async function getRecommendStatistics() {
try {
const html = await client.get(`${BaseURL}/discover/recommend/taste`);
const html = await client.get({
url: `${BaseURL}/discover/recommend/taste`,
headers: {
'User-Agent': HttpClient.DesktopUserAgent
}
});
const match = RecommendStatistics.regexp.exec(html);
return {
code: 200,
Expand All @@ -861,7 +865,12 @@ const RelatedAlbums = {
*/
export async function getRelatedAlbums(id) {
try {
const html = await client.get(`${BaseURL}/album?id=${id}`);
const html = await client.get({
url: `${BaseURL}/album?id=${id}`,
headers: {
'User-Agent': HttpClient.DesktopUserAgent
}
});
const data = [];
let match;
while ((match = RelatedAlbums.regexp.exec(html)) !== null) {
Expand Down

0 comments on commit 34a553c

Please sign in to comment.