Skip to content

Commit

Permalink
改善模糊搜索逻辑,以及一些小的 Bug 修复和小功能的添加 (#862)
Browse files Browse the repository at this point in the history
* mac专用版本1.0

* mac专用版本1.0

* 1.0安装

* 修复首次进评论的bug,提高主循环效率

* 修复快速按下一首多个子进程bug

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 修复线程混乱问题增加数字跳转快捷键

* 增加显示颜色自定义,按键映射,使用定时器实现自动关闭功能;修复搜索按键功能映射错误;发现快速切换时会播放多首音乐

* 0.1

* 0.1

* 0.2

* 0.2

* 0.2

* Fix comments bug.

* Fix comments bug.

* Fix comments bug.

* Fix comments bug.

* Optimize align of comments.

* Optimize align of comments.

* Optimize align of comments.

* Optimize for someone who write ':' in their comments.

* Fix comcomments.

* Fix comcomments.

* Fix comcomments.

* Update README.md

* Remove DS_Store.

* Fix keymap.

* Restore default key bindings.

* Restore osdlyrics.py

* Remove fucking glyphs.

* Fix comments bug.

* Merge.

* Remove DS_Store.

* Merge kwph and wjy.

* Change is to ==.

* Debug.

* Fix bug: fast pressing [ ] will causing multiple play instance.

* Remove build_menu_progress.

* Optimize branches.

* Add a blank in song and fmsongs.

* Add mouse support.

* Change f to config_file to stop annoying pylint.

* Fix notification on Linux.

* Move update api from macmusicbox to original one.

* Switch to timer.

* Fix display of help page.

* Add fuzzy search.

* Clean code.

* Improve performance on urxvt.

* Optimize up down key logic.

* Format const.py

* Update README.md & reset to origin default keymap.

* Remove untested platform & add dependencies for fuzzy search.

* Don't play music when press num keys.

* make num jump usable anywhere.

* Fix bug when mouse support is not enabled

* Remove num jump when using mouse.

* Abitious plan.

* Configuration: curses timeout.

* Try to optimize responsiveness.

* Remove yield latency (?).

* Partical plan.

* Update README.md

* Modifieable menu length

* Probably fix of bug darknessomi #816

* Change plan. Mark fixed.

* Fix jump logical chaos in comment.

* Fix the flash of color of lyrics when press j and k.

* Remove some targets & Use more accurate regex match to get timestap from lyrics

* Add auto adjust of page length according to terminal size.

* Update README.md to keep consist of original one.

* Use black to format code.

* Use black to format code.

* Merge #653.

* Fix the bug that if the keymap are not defined in user's json, the
program will abort.

* Update config file when exit to keep the config file up to dated.

* 1. Add notification for the case when url NoneType
2. A possible fix to a possible bug: when fetch the version info
   sometimes one of latest and version will be 'int'
3. self.margin -> self.left_margin_ratio

* Fix double notification after 20 minutes (when urls are updated) & refrese -> refresh (typo).

* Margin related feature & bug fix.
1. Add right margin to menu and playinfo
2. adjust scrolling speed of the string

* Important bug fix
1. Fix the bug that behavior set by "page_length" will be override as auto adjusted.
2. make the comment section with right margin.

* Add process bar with scalable size.

* Refracte update_margin(), format code.

* Update dependencies.

* Update README.md
1. Set the classifier natural language in setup.py from English to
   Chinese (Simplified).
2. Use <kbd> label to wrap the shortcut key in README.md
3. Add description to num + key
4. Add bug report for fuzzy search

* Update README.md and setup.py
1. Set the classifier natural language in setup.py from English to
   Chinese (Simplified).
2. Use <kbd> label to warp the shortcut key in README.md.
3. Add description to num + key.
4. Add bug report for fuzzy search.

* Fix lyrics logic bug & black format.

* [Bug Fix] Now press 'p' will go to the entry in search target, not
search result. Corresponding to bug 3 in #10.

* [Bug Fix] Fuzzy search bug 1 in #10
Now the wrapped fuzzy search is disabled.

* [Bug Fix] #10

* Fix mistake in setup.py

* Update README.md
1. Remove problem & solution 858.
2. Make number key instructions more clear.
3. The path of log file should now obey XDG standard.

* [Bug fix] No lyrics in Chinese songs.

* [Bug fix] Remove unneccessary code to reduce increasing latency when call space_key_event()

* [Bug fix] Fix the bug that when no song is being played, call tune_volume() will abort the application.

* [Bug Fix] Fix the bug that sometimes (when refresh urls) the play/pause
status in playinfo will be wrong.

* Use black to format code.

Co-authored-by: wangjianyuan10 <wangjianyuan10@163.com>
Co-authored-by: kenhuang <hwp0415229@sina.cn>
  • Loading branch information
3 people committed Aug 26, 2020
1 parent 026e65c commit d0f6ddf
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 226 deletions.
23 changes: 23 additions & 0 deletions NEMbox/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ def __init__(self):
"default": False,
"describe": "Set true to make curses transparency.",
},
"left_margin_ratio": {
"value": 5,
"default": 5,
"describe": (
"Controls the ratio between width and left margin."
"Set to 0 to minimize the margin."
),
},
"right_margin_ratio": {
"value": 5,
"default": 5,
"describe": (
"Controls the ratio between width and right margin."
"Set to 0 to minimize the margin."
),
},
"mouse_movement": {
"value": False,
"default": False,
Expand Down Expand Up @@ -213,6 +229,13 @@ def save_config_file(self):
utf8_data_to_file(config_file, json.dumps(self.config, indent=2))

def get(self, name):
if name == "keymap":
for key in self.default_config[name]["value"].keys():
if key not in self.config[name]["value"].keys():
self.config[name]["value"][key] = self.default_config[name][
"value"
][key]
if name not in self.config.keys():
self.config[name] = self.default_config[name]
return self.default_config[name]["value"]
return self.config[name]["value"]
162 changes: 92 additions & 70 deletions NEMbox/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
# 1.增加按键映射功能;
# 2.修复搜索按键功能映射错误;
# 3.使用定时器实现自动关闭功能;
# bug:
# 1.快速切换时会播放多首音乐
"""
网易云音乐 Menu
"""
Expand Down Expand Up @@ -115,17 +113,17 @@ def __init__(self):
self.datatype = "main"
self.title = "网易云音乐"
self.datalist = [
"排行榜",
"艺术家",
"新碟上架",
"精选歌单",
"我的歌单",
"主播电台",
"每日推荐歌曲",
"每日推荐歌单",
"私人FM",
"搜索",
"帮助",
{"entry_name": "排行榜"},
{"entry_name": "艺术家"},
{"entry_name": "新碟上架"},
{"entry_name": "精选歌单"},
{"entry_name": "我的歌单"},
{"entry_name": "主播电台"},
{"entry_name": "每日推荐歌曲"},
{"entry_name": "每日推荐歌单"},
{"entry_name": "私人FM"},
{"entry_name": "搜索"},
{"entry_name": "帮助"},
]
self.offset = 0
self.index = 0
Expand Down Expand Up @@ -156,6 +154,7 @@ def __init__(self):
self.key_list = []
self.pre_keylist = []
self.parser = None
self.at_search_result = False

@property
def user(self):
Expand Down Expand Up @@ -201,10 +200,21 @@ def in_place_search(self):
prompt = "模糊搜索:"
keyword = self.ui.get_param(prompt)
if not keyword:
return []
search_result = process.extract(keyword, self.datalist, limit=20)
return [], ""
if self.datalist == []:
return [], keyword
origin_index = 0
for item in self.datalist:
item["origin_index"] = origin_index
origin_index += 1
try:
search_result = process.extract(
keyword, self.datalist, limit=max(10, 2 * self.step)
)
except Exception as e:
log.warn(e)
if not search_result:
return search_result
return search_result, keyword
search_result.sort(key=lambda ele: ele[1], reverse=True)
return (list(map(lambda ele: ele[0], search_result)), keyword)

Expand Down Expand Up @@ -243,7 +253,7 @@ def send_kill(self, signum, fram):

def update_alert(self, version):
latest = Menu().check_version()
if latest > version and latest != 0:
if str(latest) > str(version) and latest != 0:
notify("MusicBox Update == available", 1)
time.sleep(0.5)
notify(
Expand Down Expand Up @@ -327,6 +337,31 @@ def down_key_event(self):
)
self.menu_starts = time.time()

def space_key_event_in_search_result(self):
origin_index = self.datalist[self.index]["origin_index"]
(datatype, title, datalist, offset, index,) = self.stack[-1]
if datatype == "songs":
self.player.new_player_list("songs", title, datalist, -1)
self.player.end_callback = None
self.player.play_or_pause(origin_index, self.at_playing_list)
self.at_playing_list = False
elif datatype == "djchannels":
self.player.new_player_list("djchannels", title, datalist, -1)
self.player.end_callback = None
self.player.play_or_pause(origin_index, self.at_playing_list)
self.at_playing_list = False
elif datatype == "fmsongs":
self.player.change_mode(0)
self.player.new_player_list("fmsongs", title, datalist, -1)
self.player.end_callback = self.fm_callback
self.player.play_or_pause(origin_index, self.at_playing_list)
self.at_playing_list = False
else:
# 所在列表类型不是歌曲
is_not_songs = True
self.player.play_or_pause(self.player.info["idx"], is_not_songs)
self.build_menu_processbar()

def space_key_event(self):
idx = self.index
datatype = self.datatype
Expand All @@ -336,22 +371,18 @@ def space_key_event(self):
self.player.info["idx"] = 0

# If change to a new playing list. Add playing list and play.
if datatype == "songs":
self.player.new_player_list("songs", self.title, self.datalist, -1)
self.player.end_callback = None
self.player.play_or_pause(idx, self.at_playing_list)
self.at_playing_list = True
elif datatype == "djchannels":
self.player.new_player_list("djchannels", self.title, self.datalist, -1)
self.player.end_callback = None
self.player.play_or_pause(idx, self.at_playing_list)
self.at_playing_list = True
elif datatype == "fmsongs":
self.player.change_mode(0)
self.player.new_player_list("fmsongs", self.title, self.datalist, -1)
self.player.end_callback = self.fm_callback
datatype_callback = {
"songs": None,
"djchannels": None,
"fmsongs": self.fm_callback,
}

if datatype in ["songs", "djchannels", "fmsongs"]:
self.player.new_player_list(datatype, self.title, self.datalist, -1)
self.player.end_callback = datatype_callback[datatype]
self.player.play_or_pause(idx, self.at_playing_list)
self.at_playing_list = True

else:
# 所在列表类型不是歌曲
is_not_songs = True
Expand All @@ -378,6 +409,7 @@ def back_page_event(self):
self.index,
) = self.stack.pop()
self.at_playing_list = False
self.at_search_result = False

def enter_page_event(self):
idx = self.index
Expand Down Expand Up @@ -458,8 +490,6 @@ def digit_key_song_event(self):
self.build_menu_processbar()
self.ui.screen.refresh()

# self.space_key_event()

def time_key_event(self):
self.countdown_start = time.time()
countdown = self.ui.build_timing()
Expand Down Expand Up @@ -525,6 +555,7 @@ def build_menu_processbar(self):
)

def quit_event(self):
self.config.save_config_file()
sys.exit(0)

def stop(self):
Expand Down Expand Up @@ -655,23 +686,6 @@ def start(self):
elif self.config.get("mouse_movement") and key == keyMap["mouseDown"]:
self.down_key_event()

# 数字快捷键
# elif ord('0') <= key <= ord('9') and self.datatype not in ('songs', 'fmsongs'):
# idx = key - ord('0')
# self.ui.build_menu(
# self.datatype,
# self.title,
# self.datalist,
# self.offset,
# idx,
# self.step,
# self.menu_starts,
# )
# self.ui.build_loading()
# self.dispatch_enter(idx)
# self.index = 0
# self.offset = 0

# 向上翻页
elif C.keyname(key).decode("utf-8") == keyMap["prevPage"]:
self.up_page_event()
Expand All @@ -690,23 +704,22 @@ def start(self):

# 模糊搜索
elif C.keyname(key).decode("utf-8") == keyMap["search"]:
# 9 == the 'search' menu
# self.dispatch_enter(9)
if self.at_search_result == True:
self.back_page_event()
self.stack.append(
[self.datatype, self.title, self.datalist, self.offset, self.index]
)
self.datalist, keyword = self.in_place_search()
self.datatype = "songs"
self.title += " > " + keyword + " 的搜索结果"
self.offset = 0
self.index = 0
self.at_search_result = True

# 播放下一曲
elif C.keyname(key).decode("utf-8") == keyMap[
"nextSong"
] and pre_key not in range(ord("0"), ord("9")):
self.next_key_event()
# self.down_key_event()

# 播放上一曲
elif C.keyname(key).decode("utf-8") == keyMap[
Expand Down Expand Up @@ -764,7 +777,10 @@ def start(self):

# 播放、暂停
elif C.keyname(key).decode("utf-8") == keyMap["playPause"]:
self.space_key_event()
if self.at_search_result:
self.space_key_event_in_search_result()
else:
self.space_key_event()

# 加载当前播放列表
elif C.keyname(key).decode("utf-8") == keyMap["presentHistory"]:
Expand Down Expand Up @@ -942,10 +958,9 @@ def start(self):
self.ui.screen.refresh()
self.ui.update_size()
current_step = max(int(self.ui.y * 4 / 5) - 10, 1)
if self.step != current_step:
if self.step != current_step and self.config.get("page_length") == 0:
self.step = current_step
self.index = 0
log.warning("self.step = " + str(self.step))
self.build_menu_processbar()
self.stop()

Expand Down Expand Up @@ -1049,22 +1064,26 @@ def dispatch_enter(self, idx):
self.datalist = []
for one_comment in hotcomments:
self.datalist.append(
"(热评 %s❤️ ️) %s: %s"
% (
one_comment["likedCount"],
one_comment["user"]["nickname"],
one_comment["content"],
)
{
"comment_content": "(热评 %s❤️ ️) %s: %s"
% (
one_comment["likedCount"],
one_comment["user"]["nickname"],
one_comment["content"],
)
}
)
for one_comment in comcomments:
# self.datalist.append(one_comment["content"])
self.datalist.append(
"(%s❤️ ️) %s: %s"
% (
one_comment["likedCount"],
one_comment["user"]["nickname"],
one_comment["content"],
)
{
"comment_content": "(%s❤️ ️) %s: %s"
% (
one_comment["likedCount"],
one_comment["user"]["nickname"],
one_comment["content"],
)
}
)
self.datatype = "comments"
self.title = "网易云音乐 > 评论: %s" % datalist[idx]["song_name"]
Expand Down Expand Up @@ -1100,12 +1119,15 @@ def show_playing_song(self):
if self.player.is_empty:
return

if not self.at_playing_list:
if (not self.at_playing_list) and (not self.at_search_result):
self.stack.append(
[self.datatype, self.title, self.datalist, self.offset, self.index]
)
self.at_playing_list = True

if self.at_search_result == True:
self.back_page_event()

self.datatype = self.player.info["player_list_type"]
self.title = self.player.info["player_list_title"]
self.datalist = [self.player.songs[i] for i in self.player.info["player_list"]]
Expand Down
Loading

0 comments on commit d0f6ddf

Please sign in to comment.