Skip to content

Commit

Permalink
feature: add get song pic feature #116
Browse files Browse the repository at this point in the history
  • Loading branch information
jitwxs committed Sep 4, 2022
1 parent df18e71 commit ba9d2d7
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 30 deletions.
1 change: 1 addition & 0 deletions MusicLyricApp/Api/NetEaseMusicApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected override IEnumerable<string> GetSongIdsFromAlbum0(string albumId)
result[songId] = new SongVo
{
Links = datum.Url,
Pics = song.Al.PicUrl,
Name = song.Name,
Singer = ContractSinger(song.Ar),
Album = song.Al.Name,
Expand Down
8 changes: 7 additions & 1 deletion MusicLyricApp/Api/QQMusicApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected override IEnumerable<string> GetSongIdsFromAlbum0(string albumId)
result[songId] = new SongVo
{
Links = links,
Pics = BuildPicUrl(song.Album),
Name = song.Name,
Singer = ContractSinger(song.Singer),
Album = song.Album.Name,
Expand Down Expand Up @@ -81,7 +82,7 @@ protected override LyricVo GetLyricVo0(string songId)
/// <summary>
/// 拼接歌手名
/// </summary>
public static string ContractSinger(QQMusicBean.Singer[] singers)
private static string ContractSinger(QQMusicBean.Singer[] singers)
{
if (singers == null || !singers.Any())
{
Expand All @@ -96,5 +97,10 @@ public static string ContractSinger(QQMusicBean.Singer[] singers)

return sb.Remove(sb.Length - 1, 1).ToString();
}

private static string BuildPicUrl(QQMusicBean.Album album)
{
return "https://y.qq.com/music/photo_new/T002R300x300M000" + album.Pmid + ".jpg";
}
}
}
59 changes: 59 additions & 0 deletions MusicLyricApp/Bean/CsvBean.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace MusicLyricApp.Bean
{
public class CsvBean
{
private readonly List<string> title;

private readonly List<List<string>> lines;

private int curLine;

public CsvBean()
{
title = new List<string>();
lines = new List<List<string>>();
}

public void AddColumn(string name)
{
title.Add(name);
}

public void AddData(string data)
{
while (lines.Count <= curLine)
{
lines.Add(new List<string>());
}

lines[curLine].Add(data);
}

public void NextLine()
{
curLine++;
}

public override string ToString()
{
var sb = new StringBuilder();

sb
.Append(string.Join(",", title))
.Append(Environment.NewLine);

foreach (var line in lines)
{
sb
.Append(string.Join(",", line))
.Append(Environment.NewLine);
}

return sb.ToString();
}
}
}
14 changes: 11 additions & 3 deletions MusicLyricApp/Bean/MusicLyricsVO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ public static class ErrorMsg
public const string SUCCESS = "成功";
public const string SEARCH_RESULT_STAGE = "查询成功,结果已暂存";
public const string MUST_SEARCH_BEFORE_SAVE = "您必须先搜索,才能保存内容";
public const string MUST_SEARCH_BEFORE_COPY_SONG_URL = "您必须先搜索,才能获取直链";
public const string MUST_SEARCH_BEFORE_GET_SONG_URL = "您必须先搜索,才能获取歌曲链接";
public const string MUST_SEARCH_BEFORE_GET_SONG_PIC = "您必须先搜索,才能获取歌曲封面";
public const string INPUT_ID_ILLEGAL = "您输入的ID不合法";
public const string ALBUM_NOT_EXIST = "专辑信息暂未被收录或查询失败";
public const string SONG_NOT_EXIST = "歌曲信息暂未被收录或查询失败";
public const string LRC_NOT_EXIST = "歌词信息暂未被收录或查询失败";
public const string FUNCTION_NOT_SUPPORT = "该功能暂不可用,请等待后续更新";
public const string SONG_URL_COPY_SUCCESS = "歌曲直链,已复制到剪切板";
public const string SONG_URL_GET_SUCCESS = "歌曲直链,已复制到剪切板";
public const string SONG_URL_GET_FAILED = "歌曲直链,获取失败";
public const string SONG_PIC_GET_SUCCESS = "歌曲封面,已复制到剪切板";
public const string SONG_PIC_GET_FAILED = "歌曲封面,获取失败";
public const string DEPENDENCY_LOSS = "缺少必须依赖,请前往项目主页下载 {0} 插件";
public const string SAVE_COMPLETE = "保存完毕,成功 {0} 跳过 {1}";

Expand Down Expand Up @@ -158,9 +161,14 @@ public class SongVo
public string Album { get; set; }

/// <summary>
/// 歌曲直链 Url
/// 歌曲链接 Url
/// </summary>
public string Links { get; set; }

/// <summary>
/// 歌曲封面 Url
/// </summary>
public string Pics { get; set; }

/// <summary>
/// 歌曲时长 ms
Expand Down
35 changes: 22 additions & 13 deletions MusicLyricApp/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 60 additions & 13 deletions MusicLyricApp/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,34 +402,33 @@ private async void Search_Btn_Click(object sender, EventArgs e)
}

/// <summary>
/// 获取直链按钮,点击事件
/// 获取歌曲链接按钮,点击事件
/// </summary>
private void SongLink_Btn_Click(object sender, EventArgs e)
{
if (_globalSaveVoMap == null || _globalSaveVoMap.Count == 0)
{
MessageBox.Show(ErrorMsg.MUST_SEARCH_BEFORE_COPY_SONG_URL, "提示");
MessageBox.Show(ErrorMsg.MUST_SEARCH_BEFORE_GET_SONG_URL, "提示");
return;
}

var log = new StringBuilder();
if (_globalSaveVoMap.Count > 1)
{
// 输出日志
var csv = new CsvBean();

csv.AddColumn("id");
csv.AddColumn("songLink");

foreach (var songId in _globalSearchInfo.SongIds)
{
_globalSaveVoMap.TryGetValue(songId, out var saveVo);

var link = "failure";
if (saveVo != null)
{
link = saveVo.SongVo.Links ?? "failure";
}

log.Append($"ID: {songId}, Links: {link}").Append(Environment.NewLine);
csv.AddData(songId);
csv.AddData(saveVo == null ? string.Empty : saveVo.SongVo.Links);
csv.NextLine();
}

UpdateLrcTextBox(log.ToString());
UpdateLrcTextBox(csv.ToString());
}
else
{
Expand All @@ -444,12 +443,60 @@ private void SongLink_Btn_Click(object sender, EventArgs e)
else
{
Clipboard.SetDataObject(link);
MessageBox.Show(ErrorMsg.SONG_URL_COPY_SUCCESS, "提示");
MessageBox.Show(ErrorMsg.SONG_URL_GET_SUCCESS, "提示");
}
}
}
}

/// <summary>
/// 获取歌曲封面按钮,点击事件
/// </summary>
private void SongPic_Btn_Click(object sender, EventArgs e)
{
if (_globalSaveVoMap == null || _globalSaveVoMap.Count == 0)
{
MessageBox.Show(ErrorMsg.MUST_SEARCH_BEFORE_GET_SONG_PIC, "提示");
return;
}

if (_globalSaveVoMap.Count > 1)
{
var csv = new CsvBean();

csv.AddColumn("id");
csv.AddColumn("picLink");

foreach (var songId in _globalSearchInfo.SongIds)
{
_globalSaveVoMap.TryGetValue(songId, out var saveVo);

csv.AddData(songId);
csv.AddData(saveVo == null ? string.Empty : saveVo.SongVo.Pics);
csv.NextLine();
}

UpdateLrcTextBox(csv.ToString());
}
else
{
// only loop one times
foreach (var item in _globalSaveVoMap)
{
var pic = item.Value.SongVo.Pics;
if (pic == null)
{
MessageBox.Show(ErrorMsg.SONG_PIC_GET_FAILED, "提示");
}
else
{
Clipboard.SetDataObject(pic);
MessageBox.Show(ErrorMsg.SONG_PIC_GET_SUCCESS, "提示");
}
}
}
}

/**
* 单个保存
*/
Expand Down
1 change: 1 addition & 0 deletions MusicLyricApp/MusicLyricApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<Compile Include="Api\QQMusicApiV2.cs" />
<Compile Include="Api\QQMusicNativeApi.cs" />
<Compile Include="Bean\Constants.cs" />
<Compile Include="Bean\CsvBean.cs" />
<Compile Include="Bean\GitHubBase.cs" />
<Compile Include="Bean\NetEaseMusicBean.cs" />
<Compile Include="Bean\MusicLyricsVO.cs" />
Expand Down
41 changes: 41 additions & 0 deletions MusicLyricAppTest/Bean/CsvBeanTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using MusicLyricApp.Bean;
using NUnit.Framework;

namespace MusicLyricAppTest.Bean
{
[TestFixture]
public class CsvUtils
{
[Test]
public void TestNewInstance()
{
var songVos = new List<SongVo>();

for (var i = 0; i < 10; i++)
{
var vo = new SongVo
{
Album = i.ToString(),
Name = (i * 100).ToString()
};

songVos.Add(vo);
}

var bean = new CsvBean();

bean.AddColumn("name");
bean.AddColumn("album");

foreach (var songVo in songVos)
{
bean.AddData(songVo.Name);
bean.AddData(songVo.Album);
bean.NextLine();
}
Console.WriteLine(bean);
}
}
}
Loading

0 comments on commit ba9d2d7

Please sign in to comment.