-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
更新数据库格式 匹配失败时支持用户自己输入ID 稳定性更好
- Loading branch information
Showing
21 changed files
with
584 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using TagLib; | ||
|
||
namespace NLyric.Audio { | ||
/// <summary> | ||
/// 专辑 | ||
/// </summary> | ||
public class Album : ITrackOrAlbum { | ||
private readonly string _name; | ||
private readonly string[] _artists; | ||
private readonly int? _trackCount; | ||
private readonly int? _year; | ||
|
||
/// <summary> | ||
/// 名称 | ||
/// </summary> | ||
public string Name => _name; | ||
|
||
public string[] Artists => _artists; | ||
|
||
public int? TrackCount => _trackCount; | ||
|
||
public int? Year => _year; | ||
/// <summary> | ||
/// 艺术家 | ||
/// </summary> | ||
public IReadOnlyList<string> Artists => _artists; | ||
|
||
public Album(string name, string[] artists, int? trackCount, int? year) { | ||
public Album(string name, IEnumerable<string> artists) { | ||
if (name is null) | ||
throw new ArgumentNullException(nameof(name)); | ||
if (artists is null) | ||
throw new ArgumentNullException(nameof(artists)); | ||
|
||
_name = name; | ||
_artists = artists.Select(t => t.Trim()).ToArray(); | ||
_trackCount = trackCount; | ||
_year = year; | ||
Array.Sort(_artists, StringHelper.OrdinalComparer); | ||
} | ||
|
||
/// <summary> | ||
/// 构造器 | ||
/// </summary> | ||
/// <param name="track"></param> | ||
/// <param name="tag"></param> | ||
/// <param name="getArtistsFromTrack">当 <see cref="Track.AlbumArtist"/> 为空时,是否从 <see cref="Track.Artist"/> 获取艺术家</param> | ||
public Album(ATL.Track track, bool getArtistsFromTrack) { | ||
if (track is null) | ||
throw new ArgumentNullException(nameof(track)); | ||
if (!HasAlbumInfo(track)) | ||
throw new ArgumentException(nameof(track) + " 中不存在专辑信息"); | ||
public Album(Tag tag, bool getArtistsFromTrack) { | ||
if (tag is null) | ||
throw new ArgumentNullException(nameof(tag)); | ||
if (!HasAlbumInfo(tag)) | ||
throw new ArgumentException(nameof(tag) + " 中不存在专辑信息"); | ||
|
||
string artists; | ||
string[] artists; | ||
|
||
_name = track.Album.GetSafeString(); | ||
artists = track.AlbumArtist.GetSafeString(); | ||
_name = tag.Album.GetSafeString(); | ||
artists = tag.AlbumArtists.SelectMany(t => t.GetSafeString().SplitEx()).ToArray(); | ||
if (getArtistsFromTrack && artists.Length == 0) | ||
artists = track.Artist.GetSafeString(); | ||
_artists = artists.Length == 0 ? Array.Empty<string>() : artists.SplitEx(); | ||
if (track.TrackTotal != 0) | ||
_trackCount = track.TrackTotal; | ||
if (track.Year != 0) | ||
_year = track.Year; | ||
artists = tag.Performers.SelectMany(t => t.GetSafeString().SplitEx()).ToArray(); | ||
Array.Sort(artists, StringHelper.OrdinalComparer); | ||
_artists = artists; | ||
} | ||
|
||
public static bool HasAlbumInfo(ATL.Track track) { | ||
if (track is null) | ||
throw new ArgumentNullException(nameof(track)); | ||
/// <summary> | ||
/// 是否存在专辑信息 | ||
/// </summary> | ||
/// <param name="tag"></param> | ||
/// <returns></returns> | ||
public static bool HasAlbumInfo(Tag tag) { | ||
if (tag is null) | ||
throw new ArgumentNullException(nameof(tag)); | ||
|
||
return !string.IsNullOrWhiteSpace(track.Album); | ||
return !string.IsNullOrWhiteSpace(tag.Album); | ||
} | ||
|
||
public override string ToString() { | ||
return "Name:" + _name + " | Artists:" + string.Join(",", _artists) + " | TrackCount:" + _trackCount.ToString() + " | Year:" + _year.ToString(); | ||
return "Name:" + _name + " | Artists:" + string.Join(",", _artists); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace NLyric.Audio { | ||
public interface ITrackOrAlbum { | ||
string Name { get; } | ||
|
||
string[] Artists { get; } | ||
IReadOnlyList<string> Artists { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using NLyric.Audio; | ||
|
||
namespace NLyric.Database { | ||
/// <summary> | ||
/// 专辑信息 | ||
/// </summary> | ||
public sealed class AlbumInfo { | ||
/// <summary> | ||
/// 名称 | ||
/// </summary> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// 网易云音乐ID | ||
/// </summary> | ||
public int Id { get; set; } | ||
|
||
[JsonConstructor] | ||
[Obsolete("Deserialization only", true)] | ||
public AlbumInfo() { | ||
} | ||
|
||
public AlbumInfo(Album album, int id) : this(album.Name, id) { | ||
} | ||
|
||
public AlbumInfo(string name, int id) { | ||
if (name is null) | ||
throw new ArgumentNullException(nameof(name)); | ||
|
||
Name = name; | ||
Id = id; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NLyric.Audio; | ||
|
||
namespace NLyric.Database { | ||
public static class Extensions { | ||
public static AlbumInfo Match(this IEnumerable<AlbumInfo> caches, Album album) { | ||
if (album is null) | ||
throw new ArgumentNullException(nameof(album)); | ||
|
||
return caches.FirstOrDefault(t => IsMatched(t, album)); | ||
} | ||
|
||
public static TrackInfo Match(this IEnumerable<TrackInfo> caches, Track track, Album album) { | ||
if (track is null) | ||
throw new ArgumentNullException(nameof(track)); | ||
|
||
return caches.FirstOrDefault(t => IsMatched(t, track, album)); | ||
} | ||
|
||
public static bool IsMatched(this AlbumInfo cache, Album album) { | ||
if (album is null) | ||
throw new ArgumentNullException(nameof(album)); | ||
|
||
return cache.Name == album.Name; | ||
} | ||
|
||
public static bool IsMatched(this TrackInfo cache, Track track, Album album) { | ||
if (track is null) | ||
throw new ArgumentNullException(nameof(track)); | ||
|
||
return cache.Name == track.Name && (album is null ? cache.AlbumName is null : cache.AlbumName == album.Name) && cache.Artists.SequenceEqual(track.Artists); | ||
// 如果album为空,要求cache中AlbumName也为空,如果album不为空,要求cache中AlbumName匹配 | ||
} | ||
} | ||
} |
Oops, something went wrong.