Last active
August 8, 2017 14:48
-
-
Save HakurouKen/695ce259646a32ca8b1b to your computer and use it in GitHub Desktop.
A netease-music daily sign in program.
This file contains 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
[{ | |
"user_name": "YOUR NAME", | |
"music_u": "YOUR MUSIC_U KEY IN COOKIE" | |
}] |
This file contains 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
# -*- coding: utf-8 -*- | |
import urllib,urllib2 | |
import json | |
import logging | |
class NeteaseMusicSigner(object): | |
''' | |
netease music signer | |
''' | |
PC = 1 | |
MOBILE = 0 | |
URL = 'http://music.163.com/api/point/dailyTask?csrf_token=&type=%s' | |
def __init__(self, music_u, user_name= 'noname',logging=True): | |
super(NeteaseMusicSigner, self).__init__() | |
self.music_u = music_u | |
self.opener = self._build_opener() | |
self.user_name = user_name | |
self.logging = logging | |
if logging: | |
self._init_log('NeteaseMusicSigner.log') | |
def _init_log(self,filename): | |
logging.basicConfig( | |
level=logging.INFO, | |
format= '%(asctime)s [%(levelname)s] %(message)s', | |
datefmt='[%Y-%m-%d %H:%M:%S]', | |
filename=filename | |
) | |
def _build_opener(self): | |
cookies = {'MUSIC_U': self.music_u} | |
opener = urllib2.build_opener() | |
opener.addheaders = [ | |
('Cookie',';'.join( ['%s=%s' % (k,cookies[k]) for k in cookies]) ), | |
('Referer','http://music.163.com/') | |
] | |
return opener | |
def sign(self,dv=PC): | |
try: | |
resp = self.opener.open( self.URL%dv, urllib.urlencode({})) | |
content = resp.read() | |
result = json.loads(content) | |
if dv == self.PC: | |
result['plat'] = 'pc' | |
elif dv == self.MOBILE: | |
result['plat'] = 'mobile' | |
else: | |
result['plat'] = 'unknown' | |
self.log(result) | |
return result | |
except Exception,e: | |
self.log(e,'error') | |
return None | |
pass | |
def signall(self): | |
result = {} | |
result['pc'] = self.sign(self.PC) | |
result['mobile'] = self.sign(self.MOBILE) | |
return result | |
def log(self,result,log_type='info'): | |
if log_type == 'info': | |
if result.has_key('code'): | |
code = result['code'] | |
if code == -2: | |
logging.info('%s at platform %s: Error: Already signed.' % (self.user_name, result['plat'])) | |
elif code == 200: | |
logging.info('%s at platform %s: Success, Experience +%s.' % (self.user_name, result['plat'], result['point'])) | |
else: | |
logging.info('%s at platform %s: Error: %s' % (self.user_name, result['plat'], result)) | |
else: | |
logging.error('Sign Failed, result given:%s' % result) | |
elif log_type == 'error': | |
logging.error('Sign Failed: %s' % result) | |
if __name__ == '__main__': | |
try: | |
with open('./config.json') as f: | |
data = f.read() | |
config = json.loads(data) | |
for cfg in config: | |
music_u = cfg['music_u'] | |
user_name = cfg['user_name'] if cfg.has_key('user_name') else 'UID: %s' % music_u | |
nms = NeteaseMusicSigner(music_u,user_name) | |
nms.signall() | |
except Exception,e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment