Skip to content

Commit 932576d

Browse files
committed
Version 1.7.0
cli: Add option to login with `MUSIC_U` cookie (#68) Session: Implement `is_anonymous` property README: Update credits
1 parent 7567b64 commit 932576d

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ docs
1818
*.ncm
1919
private*
2020
.test
21+
.pyncm

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666

6767
登陆:
6868
--phone 手机 网易账户手机号
69+
--cookie Cookie (MUSIC_U)
70+
网易云音乐 MUSIC_U Cookie (形如 '00B2471D143...')
6971
--pwd 密码, --password 密码
7072
网易账户密码
7173
--save [保存到] 写本次登录信息于文件
7274
--load [保存的登陆信息文件] 从文件读取登录信息供本次登陆使用
7375
--http 优先使用 HTTP,不保证不被升级
7476
--deviceId 设备ID 指定设备 ID;匿名登陆时,设备 ID 既指定对应账户
75-
【注意】默认 ID 与当前设备无关,乃从内嵌 256 可用 ID 中随机选取;指定自定义 ID 不一定能登录,相关性暂时 未知
77+
【注意】默认 ID 与当前设备无关,乃从内嵌 256 可用 ID 中随机选取;指定自定义 ID 不一定能登录,相关性暂时未知
7678
--log-level LOG_LEVEL
7779
日志等级
7880

@@ -149,4 +151,4 @@ await GetTrackComments(..., session=session)
149151
# 感谢
150152
[Android逆向——网易云音乐排行榜api(上)](https://juejin.im/post/6844903586879520775)
151153

152-
[Binaryify/NeteaseCloudMusicApi](https://github.com/Binaryify/NeteaseCloudMusicApi)
154+
[Binaryify/NeteaseCloudMusicApi](https://gitlab.com/Binaryify/neteasecloudmusicapi)

pyncm/__init__.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
- (PR#11) 海外用户可能经历 460 "Cheating" 问题,可通过添加以下 Header 解决: `X-Real-IP = 118.88.88.88`
3434
"""
3535
__VERSION_MAJOR__ = 1
36-
__VERSION_MINOR__ = 6
37-
__VERSION_PATCH__ = 18
36+
__VERSION_MINOR__ = 7
37+
__VERSION_PATCH__ = 0
3838

3939
__version__ = "%s.%s.%s" % (__VERSION_MAJOR__, __VERSION_MINOR__, __VERSION_PATCH__)
4040

@@ -163,13 +163,22 @@ def lastIP(self):
163163
@property
164164
def vipType(self):
165165
"""账号 VIP 等级"""
166-
return self.login_info["content"]["profile"]["vipType"] if self.logged_in else 0
166+
return (
167+
self.login_info["content"]["profile"]["vipType"]
168+
if self.logged_in and not self.is_anonymous
169+
else 0
170+
)
167171

168172
@property
169173
def logged_in(self):
170174
"""是否已经登陆"""
171175
return self.login_info["success"]
172176

177+
@property
178+
def is_anonymous(self):
179+
"""是否匿名登陆"""
180+
return self.logged_in and not self.nickname
181+
173182
# endregion
174183
def request(
175184
self, method: str, url: Union[str, bytes, Text], *a, **k

pyncm/__main__.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,12 @@ def parse_args(quit_on_empty_args=True):
655655
)
656656
group = parser.add_argument_group("登陆")
657657
group.add_argument("--phone", metavar="手机", default="", help="网易账户手机号")
658+
group.add_argument(
659+
"--cookie",
660+
metavar="Cookie (MUSIC_U)",
661+
default="",
662+
help="网易云音乐 MUSIC_U Cookie (形如 '00B2471D143...')",
663+
)
658664
group.add_argument(
659665
"--pwd", "--password", metavar="密码", default="", help="网易账户密码"
660666
)
@@ -789,6 +795,16 @@ def write(__s):
789795
logger.warning("优先使用 HTTP")
790796
if args.phone and args.pwd:
791797
login.LoginViaCellphone(args.phone, args.pwd)
798+
if args.cookie:
799+
login.LoginViaCookie(args.cookie)
800+
if not GetCurrentSession().logged_in:
801+
login.LoginViaAnonymousAccount()
802+
logger.info(
803+
"以匿名身份登陆成功,deviceId=%s, UID: %s"
804+
% (GetCurrentSession().deviceId, GetCurrentSession().uid)
805+
)
806+
executor = TaskPoolExecutorThread(max_workers=args.max_workers)
807+
if not GetCurrentSession().is_anonymous:
792808
logger.info(
793809
"账号 :%s (VIP %s)"
794810
% (GetCurrentSession().nickname, GetCurrentSession().vipType)
@@ -797,13 +813,6 @@ def write(__s):
797813
logger.info("保存登陆信息于 : %s" % args.save)
798814
open(args.save, "w").write(DumpSessionAsString(GetCurrentSession()))
799815
return 0
800-
if not GetCurrentSession().logged_in:
801-
login.LoginViaAnonymousAccount()
802-
logger.info(
803-
"以匿名身份登陆成功,deviceId=%s, UID: %s"
804-
% (GetCurrentSession().deviceId, GetCurrentSession().uid)
805-
)
806-
executor = TaskPoolExecutorThread(max_workers=args.max_workers)
807816
executor.daemon = True
808817
executor.start()
809818

pyncm/apis/login.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ def LoginLogout():
4040
return "/weapi/logout", {}
4141

4242

43-
@WeapiCryptoRequest
43+
@EapiCryptoRequest
4444
def LoginRefreshToken():
4545
"""网页端 - 刷新登录令牌
4646
4747
Returns:
4848
dict
4949
"""
50-
return "/weapi/w/login/cellphone", {}
50+
return "/eapi/login/token/refresh", {}
5151

5252

5353
@WeapiCryptoRequest
@@ -102,6 +102,22 @@ def GetCurrentLoginStatus():
102102
return "/weapi/w/nuser/account/get", {}
103103

104104

105+
def LoginViaCookie(MUSIC_U="", **kwargs):
106+
"""通过 Cookie 登陆
107+
108+
Args:
109+
MUSIC_U (str, optional): Cookie 中的 MUSIC_U. Defaults to ''.
110+
111+
Returns:
112+
dict
113+
"""
114+
session = GetCurrentSession()
115+
session.cookies.update({"MUSIC_U": MUSIC_U, **kwargs})
116+
resp = GetCurrentLoginStatus()
117+
WriteLoginInfo(resp, session)
118+
return {"code": 200, "result": session.login_info}
119+
120+
105121
def LoginViaCellphone(
106122
phone="",
107123
password="",
@@ -298,7 +314,7 @@ def LoginViaAnonymousAccount(deviceId=None, session=None):
298314
WriteLoginInfo(
299315
{
300316
**login_status,
301-
"profile": {"nickname": "Anonymous", **login_status},
317+
"profile": {"nickname": "", **login_status},
302318
"account": {"id": login_status["userId"], **login_status},
303319
},
304320
session,

0 commit comments

Comments
 (0)