Created
February 29, 2016 09:26
-
-
Save jxinging/b990267b7903a20decea to your computer and use it in GitHub Desktop.
网易云音乐命令行版本登录工具
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# coding: utf8 | |
""" 网易云音乐命令行版本<https://github.com/darknessomi/musicbox>登录工具 | |
因为使用内置的登录系统一直出现 501 错误码,所以写了这个小工具 | |
原理: | |
提取网页版登录的 response 供命令行版本使用, 从而得到 Cookie 和用户 ID 信息 | |
使用方法: | |
1. 使用 Chrome 打开网易云音乐网页版 | |
2. 使用 F12 打开开发者工具, 并切换到 Network 标签 | |
3. 点击登录,输入账号名密码, 并登陆 | |
4. 在开发者工具中找到 URL 为 http://music.163.com/weapi/login/?csrf_token= 的请求 | |
5. 在该请求上右击,按顺序 Copy response headers、Copy response 将响应头和响应内容拷贝到本地保存 | |
(拷贝到 vim 可能会格式错乱,请使用 gedit), 如下图: | |
http://i.imgur.com/GtuKP6g.png | |
6. 以上一步保存的文件路径作为参数执行该脚本, 完成设置 | |
""" | |
import sys | |
from StringIO import StringIO | |
from requests.packages.urllib3.connection import HTTPConnection as HTTPConnection3 | |
from requests.packages.urllib3 import connectionpool | |
class FileSocket(file): | |
def makefile(self, *_): | |
buf = StringIO() | |
for line in self: | |
if line.lower().startswith('transfer-encoding') \ | |
or line.lower().startswith('content-encoding'): | |
continue | |
buf.write(line) | |
return StringIO(buf.getvalue()) | |
def settimeout(self, *_): | |
return | |
def close(self): | |
return | |
class FileStreamHTTPConnection(HTTPConnection3): | |
filepath = None | |
def connect(self): | |
self.sock = FileSocket(self.filepath, 'rb') | |
def send(self, data): | |
if self.sock is None: | |
self.connect() | |
print "FileStreamHTTPConnection.send(): %r" % data | |
FileStreamHTTPConnection.filepath = sys.argv[1] | |
sys.argv.pop(-1) | |
connectionpool.HTTPConnectionPool.ConnectionCls = FileStreamHTTPConnection | |
# NEMbox.__init__ 会解析 sys.argv 参数导致异常,所以这里在 sys.argv 参数处理完后再 import NEMbox | |
from NEMbox.api import NetEase | |
ne = NetEase() | |
user_info = ne.httpRequest('Login_POST', 'http://music.163.com/weapi/login/', '') | |
ne.storage.database['user']['user_id'] = user_info['account']['id'] | |
ne.storage.database['user']['nickname'] = user_info['profile']['nickname'] | |
ne.storage.save() | |
print ne.cookies | |
print ne.storage.database['user'] | |
print "========================" | |
print u"设置成功" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment