Skip to content

Commit

Permalink
修复大量bug
Browse files Browse the repository at this point in the history
更新数据库格式
匹配失败时支持用户自己输入ID
稳定性更好
  • Loading branch information
wwh1004 committed Nov 24, 2019
1 parent df985bb commit ad2de13
Show file tree
Hide file tree
Showing 21 changed files with 584 additions and 515 deletions.
66 changes: 36 additions & 30 deletions NLyric/Audio/Album.cs
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);
}
}
}
4 changes: 3 additions & 1 deletion NLyric/Audio/ITrackOrAlbum.cs
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; }
}
}
26 changes: 19 additions & 7 deletions NLyric/Audio/Track.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TagLib;

namespace NLyric.Audio {
/// <summary>
/// 单曲
/// </summary>
public class Track : ITrackOrAlbum {
private readonly string _name;
private readonly string[] _artists;

/// <summary>
/// 名称
/// </summary>
public string Name => _name;

public string[] Artists => _artists;
/// <summary>
/// 艺术家
/// </summary>
public IReadOnlyList<string> Artists => _artists;

public Track(string name, string[] artists) {
public Track(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();
Array.Sort(_artists, StringHelper.OrdinalComparer);
}

public Track(ATL.Track track) {
if (track is null)
throw new ArgumentNullException(nameof(track));
public Track(Tag tag) {
if (tag is null)
throw new ArgumentNullException(nameof(tag));

_name = track.Title.GetSafeString();
_artists = track.Artist.GetSafeString().SplitEx();
_name = tag.Title.GetSafeString();
_artists = tag.Performers.SelectMany(s => s.GetSafeString().SplitEx()).ToArray();
Array.Sort(_artists, StringHelper.OrdinalComparer);
}

Expand Down
27 changes: 0 additions & 27 deletions NLyric/Caches/AlbumCache.cs

This file was deleted.

11 changes: 0 additions & 11 deletions NLyric/Caches/AllCaches.cs

This file was deleted.

66 changes: 0 additions & 66 deletions NLyric/Caches/Extensions.cs

This file was deleted.

43 changes: 0 additions & 43 deletions NLyric/Caches/TrackCache.cs

This file was deleted.

36 changes: 36 additions & 0 deletions NLyric/Database/AlbumInfo.cs
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;
}
}
}
37 changes: 37 additions & 0 deletions NLyric/Database/Extensions.cs
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匹配
}
}
}
Loading

0 comments on commit ad2de13

Please sign in to comment.