Skip to content

Commit

Permalink
Merge pull request #7 from yuygfgg/develop
Browse files Browse the repository at this point in the history
fix several bugs
  • Loading branch information
yuygfgg authored Aug 12, 2024
2 parents 5d27906 + 44397b8 commit d4600bb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 57 deletions.
33 changes: 33 additions & 0 deletions music_library/extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import importlib
import os

from .tag_extracter import TagExtractor


class MusicLibraryTagExtractor:
def load_extractors(self):
extractors = {}
for file in os.listdir("music_library/tag_extracter"):
if file.endswith("_extractor.py"):
module_name = file[:-3]
module = importlib.import_module(
f"music_library.tag_extracter.{module_name}"
)
for attr in dir(module):
cls = getattr(module, attr)
if (
isinstance(cls, type)
and issubclass(cls, TagExtractor)
and cls is not TagExtractor
):
# Extract the extension from the module name
extension = module_name.split("_")[0]
extractors[extension] = cls()
return extractors

def extract_tags(self, file_path):
extension = os.path.splitext(file_path)[1][1:].lower()
extractor = self.extractors.get(extension)
if extractor:
return extractor.extract(file_path)
return None
44 changes: 4 additions & 40 deletions music_library/library.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import importlib
import os

from functools import lru_cache

from music_library import utils
from . import utils
from .extractor import MusicLibraryTagExtractor


from .tag_extracter import TagExtractor
from .display import MusicLibraryDisplay
from .search import MusicLibrarySearch
from .graph import MusicLibraryGraph
Expand All @@ -14,7 +13,7 @@


class MusicLibrary(
MusicLibrarySearch, MusicLibraryGraph, MusicLibraryMergeArtist, MusicLibraryScan
MusicLibrarySearch, MusicLibraryGraph, MusicLibraryMergeArtist, MusicLibraryScan, MusicLibraryTagExtractor
):
def __init__(self):
self.events = {}
Expand All @@ -26,40 +25,12 @@ def __init__(self):
self.extractors = self.load_extractors()

def __getstate__(self):
# 获取对象的状态,并移除不可序列化的部分
state = self.__dict__.copy()
return state

def __setstate__(self, state):
self.__dict__.update(state)

def load_extractors(self):
extractors = {}
for file in os.listdir("music_library/tag_extracter"):
if file.endswith("_extractor.py"):
module_name = file[:-3]
module = importlib.import_module(
f"music_library.tag_extracter.{module_name}"
)
for attr in dir(module):
cls = getattr(module, attr)
if (
isinstance(cls, type)
and issubclass(cls, TagExtractor)
and cls is not TagExtractor
):
# Extract the extension from the module name
extension = module_name.split("_")[0]
extractors[extension] = cls()
return extractors

def extract_tags(self, file_path):
extension = os.path.splitext(file_path)[1][1:].lower()
extractor = self.extractors.get(extension)
if extractor:
return extractor.extract(file_path)
return None

def add_song(self, song):
self.songs[song.uuid] = song

Expand Down Expand Up @@ -88,13 +59,6 @@ def find_artist_by_name(self, name):
return artist
return None

def find_album_by_name(self, name):
name = name.strip()
for album in self.albums.values():
if album.name == name:
return album
return None

def find_song_by_name(self, name):
name = name.strip()
for song in self.songs.values():
Expand Down
4 changes: 3 additions & 1 deletion music_library/merge_artists.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def auto_merge(self):
)

for event in self.events.values():
if event.name == "Unknown Event":
continue
for album in event.albums:
if album.year is None:
print(
Expand Down Expand Up @@ -142,4 +144,4 @@ def merge_artist_by_name(self, name1, name2):
uuid2 = artist2.uuid

self.merge_artist_by_uuid(uuid1, uuid2)
print(f"Artist {name2} merged into {name1}.")
print(f"Artist {name2} merged into {name1}.")
36 changes: 20 additions & 16 deletions webui/artist.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,19 @@ <h2>Songs</h2>
} else {
document.getElementById("artist-art").src = "default_artist.png";
}

document.getElementById("artist-name").textContent = artist.name;

const artistLike = document.getElementById("artist-like");
artistLike.className = artist.is_liked
? "artist-like text-danger"
: "artist-like";
artistLike.innerHTML = artist.is_liked ? "&#9829;" : "&#9825;";
artistLike.onclick = toggleArtistLike;

const albumList = document.getElementById("album-list");
albumList.innerHTML = "";

// 收集专辑数据和图片URL
const albumsWithArt = await Promise.all(
artist.albums.map(async (album) => {
Expand All @@ -199,63 +199,67 @@ <h2>Songs</h2>
return { ...album, albumArtUrl };
})
);

// 专辑排序逻辑
const sortedAlbums = albumsWithArt.sort((a, b) => {
const parseDate = (dateStr) => {
if (!dateStr) {
return new Date(0);
}
if (/^\d{4}$/.test(dateStr)) {
return new Date(dateStr, 0, 1);
}

const parsedDate = new Date(dateStr.replace(/\./g, '-'));
return isNaN(parsedDate) ? new Date(0) : parsedDate;
};

const dateA = parseDate(a.date);
const dateB = parseDate(b.date);

return dateB - dateA;
});

// 渲染专辑
sortedAlbums.forEach((album) => {
const albumItem = document.createElement("div");
albumItem.className = "col-md-3 col-sm-4 col-6 album-item";

const card = document.createElement("div");
card.className = "card bg-dark text-white";
card.style.cursor = "pointer";

card.onclick = () => {
window.parent.postMessage(
{ action: "navigateTo", url: `album.html?uuid=${album.uuid}` },
"*"
); // 跳转到专辑详情页
};

const albumArt = document.createElement("img");
albumArt.className = "album-art card-img-top";
albumArt.src = album.albumArtUrl;
albumArt.alt = album.name;

const cardBody = document.createElement("div");
cardBody.className = "card-body";

const albumName = document.createElement("h5");
albumName.className = "card-title";
albumName.textContent = album.name;

const albumDate = document.createElement("p");
albumDate.className = "card-text";
albumDate.textContent = album.date;

cardBody.appendChild(albumName);
cardBody.appendChild(albumDate);
card.appendChild(albumArt);
card.appendChild(cardBody);
albumItem.appendChild(card);
albumList.appendChild(albumItem);
});

const songList = document.getElementById("song-list");
songList.innerHTML = "";
songs.sort((a, b) => {
Expand Down
10 changes: 10 additions & 0 deletions webui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ <h3>Lyrics</h3>
let isRepeating = false;
let currentSongUUID = null;
let lrc = null;
let lrcLoaded = false;

if (!apiBaseUrl.endsWith('/')) {
apiBaseUrl += '/';
Expand Down Expand Up @@ -717,6 +718,12 @@ <h3>Lyrics</h3>
}
document.getElementById("song-popup").style.display = "flex";
isManuallyOpen = true;
if(lrcLoaded){
lrc.play();
if(audioPlayer.paused){
lrc.pause();
}
}
}
function openLyricsSelectionPopup() {
document.getElementById("lyrics-selection-popup").style.display =
Expand Down Expand Up @@ -825,6 +832,7 @@ <h3>Lyrics</h3>
}

function selectLyrics(lyrics) {
lrcLoaded = true;
parseLrc(lyrics);
startLrcSync();
if (currentSongUUID) {
Expand Down Expand Up @@ -932,6 +940,8 @@ <h3>Lyrics</h3>

async function playSong(song) {
try {

lrcLoaded = false;
console.log("Playing song:", song);

// 暂停并重置当前音频
Expand Down

0 comments on commit d4600bb

Please sign in to comment.