forked from 0xHJK/music-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (82 loc) · 2.68 KB
/
main.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
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author: HJK
@file: main.py
@time: 2019-01-08
"""
import sys
import importlib
import threading
import traceback
import glovar
from core import extractors
from core.common import music_list_merge
from core.exceptions import *
from utils import echo
from utils import cli
from utils.customlog import CustomLog
logger = CustomLog(__name__).getLogger()
def music_search(source, music_list, errors):
''' 音乐搜索,music_list是搜索结果 '''
try:
addon = importlib.import_module('.' + source, 'core.extractors')
music_list += addon.search(glovar.get_option('keyword'))
except (RequestError, ResponseError, DataError) as e:
errors.append((source, e))
except Exception as e:
err = traceback.format_exc() if glovar.get_option('verbose') else str(e)
errors.append((source, err))
finally:
# 放在搜索后输出是为了营造出搜索很快的假象
echo.brand(source=source)
def music_download(idx, music_list):
''' 音乐下载,music_list是搜索结果 '''
music = music_list[int(idx)]
try:
addon = importlib.import_module('.' + music['source'], 'core.extractors')
addon.download(music)
except Exception as e:
logger.error('下载音乐失败')
err = traceback.format_exc() if glovar.get_option('verbose') else str(e)
logger.error(err)
def main():
music_list = []
thread_pool = []
errors = []
if not glovar.get_option('keyword'):
# 如果未设置关键词
cli.set_music_keyword('请输入要搜索的歌曲,名称和歌手一起输入可以提高匹配(如 空帆船 朴树):\n > ')
echo.notice(glovar.get_option('keyword'))
# 多线程搜索
for source in glovar.get_option('source').split():
t = threading.Thread(target=music_search, args=(source, music_list, errors))
thread_pool.append(t)
t.start()
for t in thread_pool:
t.join()
# 分割线
echo.line()
# 输出错误信息
for err in errors:
logger.error('Get %s music list failed.' % err[0].upper())
logger.error(err[1])
if glovar.get_option('merge'):
# 对搜索结果排序和去重
music_list = music_list_merge(music_list)
echo.menu(music_list)
selected = cli.get_music_select()
for idx in selected:
music_download(idx, music_list)
# 下载完后继续搜索
cli.set_music_keyword()
main()
if __name__ == '__main__':
# 初始化全局变量
glovar.init_option()
if len(sys.argv) > 1:
cli.set_opts(sys.argv[1:])
try:
main()
except KeyboardInterrupt:
sys.exit(0)