Skip to content

Commit

Permalink
optimized batch save for download url logic #168
Browse files Browse the repository at this point in the history
  • Loading branch information
jitwxs committed Mar 26, 2023
1 parent 7eab27b commit 41d67e0
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 24 deletions.
64 changes: 52 additions & 12 deletions MusicLyricApp/Bean/CsvBean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ namespace MusicLyricApp.Bean
{
public class CsvBean
{
private readonly List<string> title;
public readonly List<string> Title;

private readonly List<List<string>> lines;
public readonly List<List<string>> Lines;

private int curLine;
private int _curLine;

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

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

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

lines[curLine].Add(data);
Lines[_curLine].Add(data);
}

public void NextLine()
{
curLine++;
_curLine++;
}

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

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

foreach (var line in lines)
foreach (var line in Lines)
{
sb
.Append(string.Join(",", line))
Expand All @@ -55,5 +55,45 @@ public override string ToString()

return sb.ToString();
}

public static CsvBean Deserialization(string str)
{
var csvBean = new CsvBean();

try
{
var lines = str.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

foreach (var name in lines[0].Split(','))
{
csvBean.AddColumn(name);
}

for (var i = 1; i < lines.Length; i++)
{
var line = lines[i];
if (string.IsNullOrWhiteSpace(line))
{
continue;
}

foreach (var data in line.Split(','))
{
csvBean.AddData(data);
}

csvBean.NextLine();
}

// delete last line
csvBean._curLine--;
}
catch (System.Exception)
{
// ignored
}

return csvBean;
}
}
}
80 changes: 68 additions & 12 deletions MusicLyricApp/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,24 +612,27 @@ private async void BatchSave()
{
var localFilePath = saveDialog.FileName;
// 获取文件后缀
var fileSuffix = localFilePath.Substring(localFilePath.LastIndexOf(".", StringComparison.Ordinal));
var fileSuffix = GlobalUtils.GetSuffix(localFilePath);
//获取文件路径,不带文件名
var filePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\", StringComparison.Ordinal));
foreach (var item in _globalSaveVoMap)

if (!await BatchSaveForUrl(filePath, success))
{
var saveVo = item.Value;
var lyricVo = saveVo.LyricVo;
if (lyricVo.IsEmpty() || (lyricVo.IsPureMusic() && _globalSearchInfo.SettingBean.Config.IgnorePureMusicInSave))
foreach (var item in _globalSaveVoMap)
{
skipCount++;
continue;
}
var saveVo = item.Value;
var lyricVo = saveVo.LyricVo;
if (lyricVo.IsEmpty() || (lyricVo.IsPureMusic() && _globalSearchInfo.SettingBean.Config.IgnorePureMusicInSave))
{
skipCount++;
continue;
}

var path = filePath + '/' + GlobalUtils.GetOutputName(saveVo, _globalSearchInfo.SettingBean.Config.OutputFileNameFormat) + fileSuffix;
var path = filePath + '/' + GlobalUtils.GetOutputName(saveVo, _globalSearchInfo.SettingBean.Config.OutputFileNameFormat) + fileSuffix;

await WriteToFile(path, lyricVo);
success.Add(item.Key);
await WriteToFile(path, lyricVo);
success.Add(item.Key);
}
}

MessageBox.Show(string.Format(ErrorMsg.SAVE_COMPLETE, success.Count, skipCount), "提示");
Expand All @@ -650,6 +653,59 @@ private async void BatchSave()
}
UpdateLrcTextBox(log.ToString());
}

private Task<bool> BatchSaveForUrl(string filePath, ISet<string> success)
{
var csvBean = CsvBean.Deserialization(Console_TextBox.Text);

int idIndex = -1, urlIndex = -1;

for (var i = 0; i < csvBean.Title.Count; i++)
{
if (csvBean.Title[i].Contains("Link"))
{
urlIndex = i;
}
else if (csvBean.Title[i].Equals("id"))
{
idIndex = i;
}
}

if (idIndex == -1 || urlIndex == -1)
{
return Task.FromResult(false);
}

foreach (var line in csvBean.Lines)
{
var url = line[urlIndex];
if (string.IsNullOrWhiteSpace(url))
{
continue;
}

var id = line[idIndex];
if (!_globalSaveVoMap.TryGetValue(id, out var saveVo))
{
continue;
}

var path = filePath + '/' + GlobalUtils.GetOutputName(saveVo, _globalSearchInfo.SettingBean.Config.OutputFileNameFormat) + GlobalUtils.GetSuffix(url);

try
{
HttpUtils.DownloadFile(url, path);
success.Add(id);
}
catch (System.Exception)
{
// ignored
}
}

return Task.FromResult(true);
}

private async Task WriteToFile(string path, LyricVo lyricVo)
{
Expand Down
5 changes: 5 additions & 0 deletions MusicLyricApp/Utils/GlobalUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ public static Encoding GetEncoding(OutputEncodingEnum encodingEnum)
}
}

public static string GetSuffix(string str)
{
return str.Substring(str.LastIndexOf(".", StringComparison.Ordinal));
}

public static int toInt(string str, int defaultValue)
{
var result = defaultValue;
Expand Down
9 changes: 9 additions & 0 deletions MusicLyricApp/Utils/HttpUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -170,5 +171,13 @@ public static async Task<T> HttpGetAsync<T>(string url, string contentType = "ap
var res = await HttpGetAsync(url, contentType, headers);
return res.ToEntity<T>();
}

public static async void DownloadFile(string url, string path)
{
var client = new HttpClient();
var s = await client.GetStreamAsync(url);
var fs = new FileStream(path, FileMode.OpenOrCreate);
await s.CopyToAsync(fs);
}
}
}

0 comments on commit 41d67e0

Please sign in to comment.