Skip to content

Commit

Permalink
feat: support saving Cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
LynMoe committed Feb 26, 2021
1 parent dfdc7c4 commit e52e937
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
MUSIC/
dist/
/config.js
/account.json
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ module.exports = {
phone: 13912345678,
// 网易云密码
password: '1234567',
// 是否保存 Cookie
saveCookie: true,

// 附加的歌单
extraPlaylist: [
Expand Down Expand Up @@ -110,6 +112,10 @@ module.exports = {

为本地目录创建播放列表文件 -> 登录&获取歌单列表&处理播放器端播放列表变动 -> 下载音乐及歌词至播放器&异步写入播放列表文件

#### Save Cookie

开启后运行时会在根目录下生成 `account.json` 用于存放账号 Cookie

#### Generate Playlist File

为本地 `/MUSIC` 目录下已有的目录中的文件生成播放列表文件,与文件夹同名
Expand Down
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ if (!fs.existsSync(path.resolve(__root, '.azusa/'))) {

// Login to Cloudmusic
logger.info('Logging in to Cloudmusic')
await api.login(config('phone'), config('password'))
await api.login(config('phone'), config('password'), config('saveCookie', true) ? path.resolve(__dirname, 'account.json') : '')

// Generate infomation for downloading
const playlistList = []
Expand Down Expand Up @@ -106,7 +106,7 @@ if (!fs.existsSync(path.resolve(__root, '.azusa/'))) {
const oldTrackIds = JSON.parse(fs.readFileSync(filePath).toString())

const playlistData = fs.readFileSync(playlistPath).toString()
const modifiedTrackIds = playlistData.match(/[\/\\](\d+)\./g).map(v => parseInt(v.match(/(\d+)/)[0], 10))
const modifiedTrackIds = playlistData.match(/[/\\](\d+)\./g).map(v => parseInt(v.match(/(\d+)/)[0], 10))

const idDiff = fad.diff(oldTrackIds, modifiedTrackIds)
await Promise.all([...(new Set(idDiff.added))].map(id => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azusa",
"version": "1.0.1",
"version": "1.0.3",
"description": "For and by music lover",
"main": "app.js",
"scripts": {
Expand Down
26 changes: 24 additions & 2 deletions source/api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs')
const config = require('./config')
const logger = require('./logger')

Expand All @@ -6,17 +7,38 @@ const NeteaseCloudMusicApi = require('NeteaseCloudMusicApi')
module.exports = {
_uid: -1,
_cookie: '',
async login (username, password) {
async login (username, password, cachePath) {
if (cachePath && fs.existsSync(cachePath)) {
const data = JSON.parse(fs.readFileSync(cachePath).toString())
this._uid = data.uid
this._cookie = data.cookie

const result = await NeteaseCloudMusicApi.login_refresh({
uid: this._uid,
cookie: this._cookie
})
logger.debug(result.body)
if (result.body.code === 200) {
logger.info('Login succeed with cached cookie')
return
}
}

let result = await NeteaseCloudMusicApi.login_cellphone({
phone: username,
password
})
result = result.body
logger.debug(result)

if (result.code === 200 && result.profile) {
logger.info('Login success')
logger.info('Login succeed')
this._uid = result.profile.userId
this._cookie = result.cookie
fs.writeFileSync(cachePath, JSON.stringify({
uid: this._uid,
cookie: this._cookie
}))
} else {
logger.error('Login failed')
throw new Error(result.msg)
Expand Down

0 comments on commit e52e937

Please sign in to comment.