forked from landno/nmdown
-
Notifications
You must be signed in to change notification settings - Fork 3
/
nmdown.py
executable file
·110 lines (85 loc) · 2.93 KB
/
nmdown.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import re
from argparse import ArgumentParser
from cloudmusic import make_songs, make_albums, make_playlists, make_artists
from downloader.download import (
download_songs, download_albums, download_playlists, download_artists)
def print_songs(songs, indent=0):
for song in songs:
if indent:
print ' ' * indent,
print song.id, song.title, song.best_quality_mp3_url
print
def print_albums(albums):
for album in albums:
print album.id, album.title
print_songs(album.songs, 2)
def print_playlists(playlists):
for playlist in playlists:
print playlist.id, playlist.title
print_songs(playlist.songs, 2)
def test():
songs = make_songs(['442723', '442727'])
print_songs(songs)
albums = make_albums(['42967'])
print_albums(albums)
playlists = make_playlists(['190990'])
print_playlists(playlists)
def down(url, config):
reg = re.compile('http://music.163.com/#/(.+?)\?id=(.+)')
matches = reg.findall(url)
if not matches:
return
output_folder = config['output']
if not os.path.exists(output_folder):
os.makedirs(output_folder)
type, id = matches[0]
if type == 'song':
songs = make_songs([id])
download_songs(songs, config)
elif type == 'album':
albums = make_albums([id])
download_albums(albums, config)
elif type == 'playlist':
playlists = make_playlists([id])
download_playlists(playlists, config)
elif type == 'artist':
artists = make_artists([id])
download_artists(artists, config)
def read_urls_from_file(filename):
with open(filename) as file:
text = file.read()
lines = map(lambda l: l.strip(), text.split('\n'))
urls = filter(lambda l: l.startswith('http://'), lines)
return urls
def argparser():
parser = ArgumentParser(description='网易云音乐批量下载工具')
addarg = parser.add_argument
addarg('uri', metavar='uri', nargs='+', type=str,
help='歌曲/专辑/歌单/艺术家地址,包含地址的文件名')
addarg('-q', '--quality', default='normal',
choices=['normal', 'low', 'medium', 'high', 'best'],
help='优先下载音质,默认是 normal')
addarg('-l', '--lyric', action='store_true',
help='同时下载歌词(如果有)')
addarg('-c', '--cover', action='store_true',
help='替换为高分辨率封面')
addarg('-o', '--output', type=str, default='./',
help='保存目录')
return parser
def main():
args = argparser().parse_args().__dict__
uris = args.pop('uri')
urls = []
for uri in uris:
if uri.startswith('http://'):
urls.append(uri)
else:
urls.extend(read_urls_from_file(uri))
config = args
for url in urls:
down(url, config)
if __name__ == '__main__':
main()