Created
April 22, 2014 04:37
-
-
Save kaiix/11165467 to your computer and use it in GitHub Desktop.
netease cloud music
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
| # coding: utf-8 | |
| import requests | |
| MAX_COUNT = 10 | |
| DOWNLOAD_DIR = "/Users/kaiix/Music/Songs/" | |
| common_headers = { | |
| "User-Agent": "网易云音乐 1.7.1 (iPhone; iPhone OS 7.1; en_US)", | |
| "Cookie": "os=iPhone OS; osver=7.1; appver=1.7.1;", | |
| } | |
| keyword = raw_input('Search: ') | |
| r = requests.post( | |
| "http://music.163.com/api/search/get", | |
| data={ | |
| "type": "1", | |
| "strategy": "1", | |
| "sub": False, | |
| "match": False, | |
| "limit": "20", | |
| "offset": "0", | |
| "s": keyword, | |
| }, | |
| headers=common_headers) | |
| search_result = r.json().get('result') | |
| assert search_result != None | |
| song_count = int(search_result['songCount']) | |
| if song_count > MAX_COUNT: song_count = MAX_COUNT | |
| songs = search_result['songs'] | |
| print "=" * 20 | |
| for i in xrange(song_count): | |
| song = songs[i] | |
| print '[%d] %s - %s - %s\n' % (i, song['album']['name'].encode('utf8'), song['artists'][0]['name'].encode('utf8'), song['name'].encode('utf8')) | |
| print "=" * 20 | |
| mid = raw_input('1-9: ') | |
| if not mid.isdigit(): | |
| exit() | |
| mid = int(mid) | |
| song_id = songs[mid]['id'] | |
| # GET http://music.163.com/api/song/detail?ids=[song_id] | |
| r = requests.get( | |
| 'http://music.163.com/api/song/detail', | |
| params={ | |
| 'ids': '[%d]' % song_id | |
| }, | |
| headers=common_headers) | |
| result = r.json() | |
| song = result['songs'][0] | |
| music_id = str(song['hMusic']['dfsId']) | |
| commit = raw_input('Download %s by %s?\n' % (song['name'].encode('utf8'), song['artists'][0]['name'].encode('utf8'))); | |
| if commit != 'yes': | |
| exit() | |
| import md5 | |
| def encrypted_id(id): | |
| ''' from https://github.com/yanunon/NeteaseCloudMusic ''' | |
| byte1 = bytearray('3go8&$8*3*3h0k(2)2') | |
| byte2 = bytearray(id) | |
| byte1_len = len(byte1) | |
| for i in xrange(len(byte2)): | |
| byte2[i] = byte2[i] ^ byte1[i % byte1_len] | |
| m = md5.new() | |
| m.update(byte2) | |
| result = m.digest().encode('base64')[:-1] | |
| result = result.replace('/', '_') | |
| result = result.replace('+', '-') | |
| return result | |
| import os | |
| os.system('cd %s; wget http://m1.music.126.net/%s/%s.mp3' % (DOWNLOAD_DIR, encrypted_id(music_id), music_id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment