forked from tianhan0/Export-NeteaseMusic-Playlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-netease-music-playlist.py
86 lines (70 loc) · 2.4 KB
/
export-netease-music-playlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sqlite3
import json
import codecs
import os
database_path = "/Library/Containers/com.netease.163music/Data/Documents/storage/sqlite_storage.sqlite3"
cx = sqlite3.connect(os.path.expanduser('~') + database_path)
cx.row_factory = sqlite3.Row
def getPlaylistNameFromJson(jsonStr):
playlistDetail = json.loads(jsonStr)
# return playlistDetail["name"].encode("GBK", 'ignore');
return playlistDetail["name"].encode("utf-8", 'ignore');
def getMusicNameFromJson(jsonStr):
musicDetail = json.loads(jsonStr)
return musicDetail["name"];
def getArtistNameFromJson(jsonStr):
load = json.loads(jsonStr)
ret = []
for artist in load["artists"]:
ret.append(artist["name"].encode("utf-8", 'ignore'))
return ret
def getPlaylist():
cu = cx.cursor()
cu.execute("select * from web_playlist")
playlists = []
for item in cu.fetchall():
playlist = (item["pid"], getPlaylistNameFromJson(item["playlist"]))
playlists.append(playlist)
return playlists
def getPlayListMusic(pid):
cu = cx.cursor()
cu.execute("select * from web_playlist_track where pid=?",[pid])
musics=[]
for item in cu.fetchall():
musics.append(item["tid"]);
return musics
def getMusicDetail(tid):
cu = cx.cursor()
cu.execute("select * from web_track where tid=?",[tid])
music = cu.fetchone()
if music is None:
return None
# detail = (getMusicNameFromJson(music["detail"]), music["relative_path"])
musicName = getMusicNameFromJson(music["track"])
musicArtists = getArtistNameFromJson(music["track"])
return (musicName, musicArtists)
def main():
playlists = getPlaylist()
for item in playlists:
playlistID = item[0]
playlistName = item[1]
output = open(os.getcwd() + "/" + playlistName + ".csv", 'w')
musicIds = getPlayListMusic(playlistID)
for tid in musicIds:
if tid is not None:
musicInfo = getMusicDetail(tid)
if musicInfo is not None:
musicName, musicArtists = musicInfo
if musicName is not None:
output.write(musicName.encode("utf-8", 'ignore'))
for artist in musicArtists:
output.write(", " + artist)
output.write("\n")
cx.close()
if __name__ == '__main__':
main()
# References:
# 1. https://github.com/vileer/NeteaseCloudMusicPlaylistCreator
# 2. Online sqlite3 viewer: http://inloop.github.io/sqlite-viewer/
# 3. Online JSON viewer: http://json.parser.online.fr/
# 4. http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20