Skip to content

Commit

Permalink
adjusted output filename, added custom file naming (config.json)
Browse files Browse the repository at this point in the history
  • Loading branch information
Venipa committed Oct 25, 2021
1 parent 1c49812 commit 6e45fa2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
30 changes: 17 additions & 13 deletions CoubDownload-Bridge/Commands/DownloadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Text;
using System.Collections.Generic;
using System.Windows.Threading;
using CoubDownload_Bridge.Utils;

namespace CoubDownload_Bridge.Commands
{
Expand Down Expand Up @@ -98,19 +99,14 @@ public string Execute(DownloadArgs args)
var videoInput = Path.Combine(tempPath, $"video_{data.Id}-({Guid.NewGuid().ToString()}).tmp");
var audioInput = Path.Combine(tempPath, $"audio_{data.Id}-({Guid.NewGuid().ToString()}).tmp");

var dataCommunity = $"[{(App.Config.addCommunityPrefix ? $"{data.Communities?.Where(x => x.Visible != false)?.FirstOrDefault()?.Title ?? ""}" : "")}]";
var dataCategory = $"[{(App.Config.addCategoryPrefix ? $"{data.Categories?.Where(x => x.Visible != false && (App.Config.addCommunityPrefix ? (x.Title != dataCommunity) : true))?.FirstOrDefault()?.Title ?? "General"}" : "")}]";
var dataCommunity = $"{(App.Config.addCommunityPrefix ? $"{data.Communities?.Where(x => x.Visible != false)?.FirstOrDefault()?.Title ?? ""}" : "")}";
var dataCategory = $"{(App.Config.addCategoryPrefix ? $"{data.Categories?.Where(x => x.Visible != false && (App.Config.addCommunityPrefix ? (x.Title != dataCommunity) : true))?.FirstOrDefault()?.Title ?? "General"}" : "")}";
dataCommunity = dataCommunity.EndsWith("[]") ? "" : dataCommunity;
dataCategory = dataCategory.EndsWith("[]") ? "" : dataCategory;
var resultOutputPrefix = new StringBuilder();
if (App.Config.addCommunityPrefix)
{
resultOutputPrefix.Append(dataCommunity);
}
if (App.Config.addCategoryPrefix && resultOutputPrefix.ToString() != dataCommunity)
{
resultOutputPrefix.Append(dataCategory);
}
var category = dataCategory;
var community = dataCommunity;
var fileFormatName = App.Config.customFilenameFormat ?? "[%category%]%id%";
if (!App.Config.nsfwFolderEnabled || data.Communities?.Count(x => x.Title?.ToLower().IndexOf("nsfw") != -1) == 0 ||
data.Categories?.Count(x => x.Title?.ToLower().IndexOf("nsfw") != -1) == 0)
{
Expand All @@ -131,9 +127,17 @@ public string Execute(DownloadArgs args)
Directory.CreateDirectory(outputPath);
}
}
var resultOutput = Path.Combine(outputPath, $"{resultOutputPrefix}{CoubId}{(args.full ? "-full" : "")}.mp4");
var resultOutputAudio = Path.Combine(outputPath, $"{resultOutputPrefix}{CoubId}.mp3");
var resultGif = Path.Combine(outputPath, $"{resultOutputPrefix}{CoubId}.gif");
var formatVariables = new Tuple<string, string>[]
{
Tuple.Create("id", CoubId),
Tuple.Create("category", category),
Tuple.Create("community", community),
Tuple.Create("name", data.Title.SanitizeFilename())
};
var outFileName = fileFormatName.ReplaceVariables(formatVariables);
var resultOutput = Path.Combine(outputPath, $"{outFileName}{(args.full ? "-full" : "")}.mp4");
var resultOutputAudio = Path.Combine(outputPath, $"{outFileName}.mp3");
var resultGif = Path.Combine(outputPath, $"{outFileName}.gif");

var withPercentage = new ProgressBar(PbStyle.SingleLine, 100, 20, '█');
var currentType = CoubDownloadType.Video;
Expand Down
2 changes: 2 additions & 0 deletions CoubDownload-Bridge/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public Config() { }
public bool addCategoryPrefix { get; private set; } = true;
[JsonProperty("addCommunityPrefix")]
public bool addCommunityPrefix { get; private set; } = false;
[JsonProperty("customFilenameFormat")]
public string customFilenameFormat { get; private set; } = "[%category%]%id%";
[JsonProperty("gifWidth")]
public int gifWidth { get; set; } = 320;
[JsonProperty("additional_gif_conversions")]
Expand Down
1 change: 1 addition & 0 deletions CoubDownload-Bridge/CoubDownload-Bridge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests\DownloadTest.cs" />
<Compile Include="Utils\StringExtension.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
25 changes: 25 additions & 0 deletions CoubDownload-Bridge/Utils/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoubDownload_Bridge.Utils
{
public static class StringExtension
{
public static string ReplaceVariables(this string replaceable, Tuple<string, string>[] vars)
{
vars.ToList().ForEach(x =>
{
replaceable = string.Join(x.Item2, replaceable.Split(new string[] { $"%{x.Item1}%" }, StringSplitOptions.None));
});
return replaceable;
}
public static string SanitizeFilename(this string filename)
{
return String.Concat(filename.Split(Path.GetInvalidFileNameChars()));
}
}
}

0 comments on commit 6e45fa2

Please sign in to comment.