Skip to content

Commit

Permalink
fix: convert song and lyric ID data types to long
Browse files Browse the repository at this point in the history
https://github.com/YoutubeClipPlaylist/Lyrics/actions/runs/9629636583/job/26559296260

- Change data type of songId and lyricId from `int` to `long` in DownloadLyricAndWriteFileAsync and GetSongIdAsync function declarations and their respective usages in LyricsDownloader.cs.
- Update the relationship mapping in LyricsDownloader.cs and GetLyricAsync method.
- In LyricConverter.cs, the queue.dequeue for lyricId data type has been updated from int to long.
- In Lyric.cs, the data type for LyricId has been modified in the properties of both ILyric interface and Lyric class from `int` to `long`.
- Change definition and usage of songId and lyricId from `int` to `long` in all functions of SongProcessor.cs, updating hashes, helper methods, and task methods accordingly.

Signed-off-by: 陳鈞 <[email protected]>
  • Loading branch information
jim60105 committed Jun 23, 2024
1 parent 42595dc commit ed97b13
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions Downloader/LyricsDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial class LyricsDownloader(CloudMusicApi cloudMusicApi)
{
private readonly CloudMusicApi _cloudMusicApi = cloudMusicApi;

public async Task<bool> DownloadLyricAndWriteFileAsync(int songId)
public async Task<bool> DownloadLyricAndWriteFileAsync(long songId)
{
if (songId < 0)
{
Expand Down Expand Up @@ -51,7 +51,7 @@ public async Task<bool> DownloadLyricAndWriteFileAsync(int songId)
return true;
}

public async Task<(int songId, string songName)> GetSongIdAsync(ISong song, int offset = 0)
public async Task<(long songId, string songName)> GetSongIdAsync(ISong song, int offset = 0)
{
if (string.IsNullOrEmpty(song.Title))
{
Expand All @@ -74,12 +74,12 @@ public async Task<bool> DownloadLyricAndWriteFileAsync(int songId)
json = (JObject)json["result"];
return null == json
|| json["songs"] is not IEnumerable<JToken> result
? (0, string.Empty)
: result.Select(t => ((int)t["id"], (string)t["name"]))
.FirstOrDefault();
? (0, string.Empty)
: result.Select(t => ((long)t["id"], (string)t["name"]))
.FirstOrDefault();
}

public async Task<string?> GetLyricAsync(int songId)
public async Task<string?> GetLyricAsync(long songId)
{
(bool isOk, JObject json) = await _cloudMusicApi.RequestAsync(CloudMusicApiProviders.Lyric,
new Dictionary<string, object> {
Expand Down
2 changes: 1 addition & 1 deletion Json/LyricConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override Lyric Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe
{
lyric.VideoId = queue.Dequeue().GetString() ?? "";
lyric.StartTime = queue.Dequeue().GetInt32();
lyric.LyricId = queue.Dequeue().GetInt32();
lyric.LyricId = queue.Dequeue().GetInt64();
lyric.Title = queue.Dequeue().GetString() ?? "";
lyric.Offset = queue.Dequeue().GetSingle();
}
Expand Down
4 changes: 2 additions & 2 deletions Models/Lyric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Lyrics.Models;
[JsonConverter(typeof(LyricConverter))]
public interface ILyric
{
int LyricId { get; set; }
long LyricId { get; set; }
int StartTime { get; set; }
string VideoId { get; set; }
string Title { get; set; }
Expand All @@ -17,7 +17,7 @@ public class Lyric : ILyric
{
public string VideoId { get; set; } = "";
public int StartTime { get; set; }
public int LyricId { get; set; }
public long LyricId { get; set; }
public string Title { get; set; } = "";
public float Offset { get; set; }
}
18 changes: 9 additions & 9 deletions Processor/SongProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public SongProcessor(LyricsDownloader lyricsDownloader, ref List<ILyric> lyrics)
internal async Task ProcessNewSongs(List<ISong> diffList, List<ILyric> removed)
{
Random random = new();
HashSet<int> failedIds = _lyrics.Where(p => p.LyricId < 0)
.Select(p => p.LyricId)
.ToHashSet();
HashSet<long> failedIds = _lyrics.Where(p => p.LyricId < 0)
.Select(p => p.LyricId)
.ToHashSet();

for (int i = 0; i < diffList.Count; i++)
{
Expand All @@ -31,7 +31,7 @@ internal async Task ProcessNewSongs(List<ISong> diffList, List<ILyric> removed)

try
{
if (TryFindLyricAtLocal(removed, song, out int songId, out string songName))
if (TryFindLyricAtLocal(removed, song, out long songId, out string songName))
{
AddToLyrics(i, song, songId, songName);
continue;
Expand Down Expand Up @@ -73,7 +73,7 @@ internal async Task ProcessNewSongs(List<ISong> diffList, List<ILyric> removed)
}
}

void AddToLyrics(int i, ISong song, int songId, string songName)
void AddToLyrics(int i, ISong song, long songId, string songName)
{
_lyrics.Add(new Lyric()
{
Expand All @@ -95,7 +95,7 @@ void AddToLyrics(int i, ISong song, int songId, string songName)
/// <param name="song"></param>
/// <param name="result">result</param>
/// <returns>是否成功在本地找到</returns>
private bool TryFindLyricAtLocal(List<ILyric> removed, ISong song, out int lyricId, out string title)
private bool TryFindLyricAtLocal(List<ILyric> removed, ISong song, out long lyricId, out string title)
{
ILyric? existLyric = removed.Find(p => p.Title.Equals(song.Title, StringComparison.InvariantCultureIgnoreCase))
?? _lyrics.Find(p => p.Title.Equals(song.Title, StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -114,10 +114,10 @@ private bool TryFindLyricAtLocal(List<ILyric> removed, ISong song, out int lyric
/// <param name="song"></param>
/// <param name="offset"></param>
/// <returns>如果成功,songId會是正整數;如果失敗,songId會是0</returns>
private async Task<(int songId, string songName)> FindLyricAtNeteaseCloudAsync(ISong song, int offset = 0)
private async Task<(long songId, string songName)> FindLyricAtNeteaseCloudAsync(ISong song, int offset = 0)
{
// Find lyric id at Netease Cloud Music.
(int songId, string songName) = await _lyricsDownloader.GetSongIdAsync(song, offset);
(long songId, string songName) = await _lyricsDownloader.GetSongIdAsync(song, offset);

// Can't find song from internet.
if (songId == 0)
Expand All @@ -133,7 +133,7 @@ private bool TryFindLyricAtLocal(List<ILyric> removed, ISong song, out int lyric
/// <param name="song"></param>
/// <param name="offset"></param>
/// <returns>如果成功,songId會是正整數;如果找不到歌曲,songId會是0;如果找到歌曲但是下載失敗,songId會是負數(-songId)</returns>
private async Task<(int songId, string songName)> FindAndDownloadFromNeteaseCloud(ISong song, int offset = 0, HashSet<int>? failedIds = null)
private async Task<(long songId, string songName)> FindAndDownloadFromNeteaseCloud(ISong song, int offset = 0, HashSet<long>? failedIds = null)
{
if (null == failedIds) failedIds = [];

Expand Down

0 comments on commit ed97b13

Please sign in to comment.