Repository navigation
Expand file tree
/
Copy pathmusic.py
More file actions
252 lines (226 loc) · 7.83 KB
/
Copy pathmusic.py
File metadata and controls
252 lines (226 loc) · 7.83 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: HJK
@file: music.py
@time: 2019-01-27
music object
"""
import os
import re
import datetime
import logging
import click
import requests
from . import config
from .utils import colorize
class Music:
"""
定义music对象,
包括基本属性(如title,singer,url等)
以及一些方法(如download,info等)
"""
def __init__(self):
self.idx = 0
self.id = ""
self.title = ""
self.ext = "mp3"
self.singer = ""
self.album = ""
self.size = ""
self.rate = ""
self.source = ""
self.lyrics = ""
self.cover = ""
self._duration = ""
self._url = ""
self.outdir = config.get("outdir")
self.verbose = config.get("verbose")
self.logger = logging.getLogger(__name__)
def __str__(self):
""" 在打印详情时调用 """
idx = colorize("[ %s ] " % self.idx, "cyan")
source = colorize("%s" % self.source.upper(), self.source)
return "\n ------------ \n" + _(
" -> 来源: {idx}{source} #{id}\n"
" -> 歌曲: {title}\n"
" -> 歌手: {singer}\n"
" -> 专辑: {album}\n"
" -> 时长: {duration}\n"
" -> 大小: {size}MB\n"
" -> 比特率: {rate}\n"
" -> URL: {url} \n"
" -> 歌词: {lyrics} \n"
" -> 封面: {cover} \n"
).format(
idx=idx,
source=source,
id=self.id,
title=self.title,
singer=self.singer,
album=self.album,
duration=self.duration,
size=self.size,
rate=self.rate,
url=self.url,
lyrics=self.lyrics,
cover=self.cover,
)
@property
def avaiable(self):
""" 是否有效,如果URL为None或大小为0则无效 """
return self.url and self.size
@property
def name(self):
""" 歌曲文件名 """
return "%s - %s.%s" % (self.singer, self.title, self.ext)
@property
def duration(self):
""" 持续时间 H:M:S """
return self._duration
@duration.setter
def duration(self, seconds):
self._duration = str(datetime.timedelta(seconds=int(seconds)))
@property
def info(self):
""" 歌曲摘要信息,列出搜索歌曲时使用 """
idx = colorize(" [ %2s ] " % self.idx, "cyan")
source = colorize("%7s" % self.source.upper(), self.source)
size = colorize("%5sMB" % self.size, "yellow")
title = colorize(self.title, "yellow")
v = colorize(" | ", self.source)
h = colorize(" - ", self.source)
return (
idx
+ source
+ v
+ self.duration
+ h
+ size
+ h
+ self.singer
+ h
+ title
+ h
+ self.album
)
@property
def row(self):
""" 歌曲摘要信息,列出搜索歌曲时使用PrettyTable """
keywords = re.split(";|,|\s|\*", config.get("keyword"))
def highlight(s, k):
return s.replace(k, colorize(k, "xiami")).replace(
k.title(), colorize(k.title(), "xiami")
)
ht_singer = self.singer if len(self.singer) < 30 else self.singer[:30] + "..."
ht_title = self.title if len(self.title) < 30 else self.title[:30] + "..."
ht_album = self.album if len(self.album) < 20 else self.album[:20] + "..."
for k in keywords:
if not k:
continue
ht_singer = highlight(ht_singer, k)
ht_title = highlight(ht_title, k)
ht_album = highlight(ht_album, k)
size = "%sMB" % self.size
ht_size = size if int(self.size) < 8 else colorize(size, "flac")
return [
colorize(self.idx, "baidu"),
ht_title,
ht_singer,
ht_size,
self.duration,
ht_album,
self.source.upper(),
]
@property
def url(self):
return self._url
@url.setter
def url(self, url):
""" 设置URL的时候同时更新size大小 """
try:
r = requests.get(
url,
stream=True,
headers=config.get("wget_headers"),
proxies=config.get("proxies"),
)
self._url = url
size = int(r.headers.get("Content-Length", 0))
# 转换成MB并保留两位小数
self.size = round(size / 1048576, 2)
except Exception as e:
self.logger.info(_("请求失败: {url}").format(url=url))
self.logger.info(e)
@property
def fullname(self):
""" 唯一有效的完整路径,如果冲突则在名称加数字,如music(1).mp3 """
outfile = os.path.abspath(os.path.join(self.outdir, self.name))
if os.path.exists(outfile):
name, ext = self.name.rsplit(".", 1)
names = [x for x in os.listdir(self.outdir) if x.startswith(name)]
names = [x.rsplit(".", 1)[0] for x in names]
suffixes = [x.replace(name, "") for x in names]
# filter suffixes that match ' (x)' pattern
suffixes = [
x[2:-1] for x in suffixes if x.startswith(" (") and x.endswith(")")
]
indexes = [int(x) for x in suffixes if set(x) <= set("0123456789")]
idx = 1
if indexes:
idx += sorted(indexes)[-1]
outfile = os.path.abspath(
os.path.join(self.outdir, "%s (%d).%s" % (name, idx, ext))
)
return outfile
def download(self):
""" 下载音乐 """
if config.get("verbose"):
click.echo(str(self))
else:
click.echo(self.info)
music_file = self.fullname.replace("?", "")
self._download_file(self.url, music_file, stream=True)
if config.get("lyrics") and self.lyrics:
lyrics_file = (
music_file.rpartition(".")[0] + "." + self.lyrics.rpartition(".")[-1]
)
self._download_file(self.lyrics, lyrics_file)
if config.get("picture") and self.cover:
cover_file = music_file.rpartition(".")[0] + ".jpg"
self._download_file(self.cover, cover_file)
click.echo("-------------\n")
def _download_file(self, url, outfile, stream=False):
"""
下载文件用的辅助函数
:param url: 下载地址
:param outfile: 含完整路径的文件名
:param stream: 是否需要进度条
:return:
"""
try:
r = requests.get(
url,
stream=stream,
headers=config.get("wget_headers"),
proxies=config.get("proxies"),
)
if stream:
total_size = int(r.headers["content-length"])
with click.progressbar(length=total_size, label=_("下载中...")) as bar:
with open(outfile, "wb") as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
bar.update(len(chunk))
else:
with open(outfile, "wb") as f:
f.write(r.content)
click.echo(_("已保存到: {outfile}").format(outfile=outfile))
except Exception as e:
click.echo("")
self.logger.error(_("下载失败: ") + "\n")
self.logger.error(_("URL: {url}").format(url=self.url) + "\n")
self.logger.error(_("位置: {outfile}").format(outfile=outfile) + "\n")
if self.verbose:
self.logger.error(e)