From 5695b2287ca23c5193b9176978f8813d87747977 Mon Sep 17 00:00:00 2001 From: timerix Date: Thu, 12 Oct 2023 13:52:05 +0600 Subject: [PATCH] updated dependencies --- VkAudioDownloader.CLI/LaunchArgument.cs | 44 ------- VkAudioDownloader.CLI/LaunchArgumentParser.cs | 114 ------------------ VkAudioDownloader.CLI/Program.cs | 12 +- VkAudioDownloader/ExtensionMethods.cs | 2 +- VkAudioDownloader/Helpers/FFMPegHelper.cs | 39 +++--- VkAudioDownloader/Helpers/HttpHelper.cs | 2 +- VkAudioDownloader/VkAudioDownloader.csproj | 10 +- VkAudioDownloader/VkClient.cs | 22 ++-- 8 files changed, 43 insertions(+), 202 deletions(-) delete mode 100644 VkAudioDownloader.CLI/LaunchArgument.cs delete mode 100644 VkAudioDownloader.CLI/LaunchArgumentParser.cs diff --git a/VkAudioDownloader.CLI/LaunchArgument.cs b/VkAudioDownloader.CLI/LaunchArgument.cs deleted file mode 100644 index cdbba4f..0000000 --- a/VkAudioDownloader.CLI/LaunchArgument.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Text; -using DTLib.Extensions; - -namespace VkAudioDownloader.CLI; - -public class LaunchArgument -{ - public string[] Aliases; - public string Description; - public string? ParamName; - public Action? Handler; - public Action? HandlerWithArg; - - private LaunchArgument(string[] aliases, string description) - { - Aliases = aliases; - Description = description; - } - - public LaunchArgument(string[] aliases, string description, Action handler) - : this(aliases, description) => Handler = handler; - - public LaunchArgument(string[] aliases, string description, Action handler, string paramName) - : this(aliases, description) - { - HandlerWithArg = handler; - ParamName = paramName; - } - - public StringBuilder AppendHelpInfo(StringBuilder b) - { - b.Append(Aliases[0]); - for (int i = 1; i < Aliases.Length; i++) - b.Append(", ").Append(Aliases[i]); - if (!String.IsNullOrEmpty(ParamName)) - b.Append(" [").Append(ParamName).Append(']'); - b.Append(" - ").Append(Description); - return b; - } - - public override string ToString() => - $"{{{{{Aliases.MergeToString(", ")}}}, Handler: {Handler is null}, HandlerWithArg: {HandlerWithArg is null}}}"; -} \ No newline at end of file diff --git a/VkAudioDownloader.CLI/LaunchArgumentParser.cs b/VkAudioDownloader.CLI/LaunchArgumentParser.cs deleted file mode 100644 index a0607e3..0000000 --- a/VkAudioDownloader.CLI/LaunchArgumentParser.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace VkAudioDownloader.CLI; - -public class LaunchArgumentParser -{ - private Dictionary argDict = new(); - private List argList = new(); - public bool ExitIfNoArgs = true; - - public class ExitAfterHelpException : Exception - { - internal ExitAfterHelpException() : base("your program can use this exception to exit after displaying help message") - { } - } - - public string CreateHelpMessage() - { - StringBuilder b = new(); - foreach (var arg in argList) - arg.AppendHelpInfo(b).Append('\n'); - b.Remove(b.Length-1, 1); - return b.ToString(); - } - public string CreateHelpArgMessage(string argAlias) - { - StringBuilder b = new(); - var arg = Get(argAlias); - arg.AppendHelpInfo(b); - return b.ToString(); - } - private void HelpHandler() - { - Console.WriteLine(CreateHelpMessage()); - throw new ExitAfterHelpException(); - } - - private void HelpArgHandler(string argAlias) - { - Console.WriteLine(CreateHelpArgMessage(argAlias)); - throw new ExitAfterHelpException(); - } - - - public LaunchArgumentParser() - { - var help = new LaunchArgument(new[] { "h", "help" }, - "shows help message", HelpHandler); - Add(help); - var helpArg = new LaunchArgument( new[]{ "ha", "helparg" }, - "shows help message for particular argument", - HelpArgHandler, "argAlias"); - Add(helpArg); - } - - public LaunchArgumentParser(ICollection arguments) : this() - { - foreach (var arg in arguments) - Add(arg); - } - public LaunchArgumentParser(params LaunchArgument[] arguments) : this() - { - for (var i = 0; i < arguments.Length; i++) - Add(arguments[i]); - } - - public void Add(LaunchArgument arg) - { - argList.Add(arg); - for(int a=0; aprogram launch args - /// argument {args[i]} should have a parameter after it - /// argument hasn't got any handlers - /// happens after help message is displayed - public void ParseAndHandle(string[] args) - { - if (args.Length == 0 && ExitIfNoArgs) - { - HelpHandler(); - } - - for (int i = 0; i < args.Length; i++) - { - LaunchArgument arg = Get(args[i]); - - if (arg.HandlerWithArg is not null) - { - if (i+1 >= args.Length) - throw new Exception($"argument <{args[i]}> should have a parameter after it"); - arg.HandlerWithArg(args[++i]); - } - else if (arg.Handler is not null) - arg.Handler(); - else throw new NullReferenceException($"argument <{args[i]}> hasn't got any handlers"); - } - } -} \ No newline at end of file diff --git a/VkAudioDownloader.CLI/Program.cs b/VkAudioDownloader.CLI/Program.cs index 3942187..d8cd2be 100644 --- a/VkAudioDownloader.CLI/Program.cs +++ b/VkAudioDownloader.CLI/Program.cs @@ -1,11 +1,11 @@ using System; using System.Linq; using DTLib.Dtsod; -using System.IO; +using DTLib.Console; using DTLib.Extensions; +using DTLib.Filesystem; +using DTLib.Logging; using VkAudioDownloader; -using DTLib.Logging.New; -using VkAudioDownloader.CLI; Console.InputEncoding = StringConverter.UTF8; Console.OutputEncoding = StringConverter.UTF8; @@ -14,7 +14,7 @@ LaunchArgumentParser argParser = new LaunchArgumentParser(); if(!File.Exists("config.dtsod")) { - File.Copy("config.dtsod.default", "config.dtsod"); + File.Copy("config.dtsod.default", "config.dtsod", true); throw new Exception("No config detected, default created. Edit it!"); } @@ -24,7 +24,7 @@ var logger = new CompositeLogger(new DefaultLogFormat(true), new ConsoleLogger(), new FileLogger("logs", "VkAudioDownloaer"), new FileLogger("logs", "VkAudioDownloaer_debug") { DebugLogEnabled = true}); -var mainLoggerContext = new ContextLogger(logger, "Main"); +var mainLoggerContext = new ContextLogger("Main", logger); mainLoggerContext.LogDebug("DEBUG LOG ENABLED"); try @@ -54,7 +54,7 @@ try var audio = audios[ain]; Console.WriteLine($"selected {audio.AudioToString()}"); - string downloadedFile = client.DownloadAudioAsync(audio, "downloads").GetAwaiter().GetResult(); + IOPath downloadedFile = client.DownloadAudioAsync(audio, "downloads").GetAwaiter().GetResult(); mainLoggerContext.LogInfo($"audio {audio.AudioToString()} downloaded to {downloadedFile}"); } } diff --git a/VkAudioDownloader/ExtensionMethods.cs b/VkAudioDownloader/ExtensionMethods.cs index 6b20e79..a9e115b 100644 --- a/VkAudioDownloader/ExtensionMethods.cs +++ b/VkAudioDownloader/ExtensionMethods.cs @@ -1,5 +1,5 @@ using System.Runtime.CompilerServices; -using VkNet.Model.Attachments; +using VkNet.Model; namespace VkAudioDownloader; diff --git a/VkAudioDownloader/Helpers/FFMPegHelper.cs b/VkAudioDownloader/Helpers/FFMPegHelper.cs index 69073a1..2f11780 100644 --- a/VkAudioDownloader/Helpers/FFMPegHelper.cs +++ b/VkAudioDownloader/Helpers/FFMPegHelper.cs @@ -1,24 +1,25 @@ using CliWrap; +using DTLib.Logging; namespace VkAudioDownloader.Helpers; public class FFMPegHelper { private ContextLogger _logger; - private readonly string ffmpeg; - public FFMPegHelper(ILogger logger, string ffmpegDir) + private readonly IOPath ffmpeg_exe; + public FFMPegHelper(ILogger logger, IOPath ffmpegDir) { - _logger = new ContextLogger(logger, nameof(FFMPegHelper)); - ffmpeg=Path.Concat(ffmpegDir,"ffmpeg"); + _logger = new ContextLogger(nameof(FFMPegHelper), logger); + ffmpeg_exe=Path.Concat(ffmpegDir,"ffmpeg"); } /// creates fragments list for ffmppeg concat /// there file list.txt will be created /// audio files in fragmentsDir (with or without dir in path) - /// - public string CreateFragmentList(string fragmentsDir, string[] fragments) + /// path to list.txt file + public IOPath CreateFragmentList(IOPath fragmentsDir, IOPath[] fragments) { - string listFile = Path.Concat(fragmentsDir, "list.txt"); + IOPath listFile = Path.Concat(fragmentsDir, "list.txt"); using var playlistFile = File.OpenWrite(listFile); for (var i = 0; i < fragments.Length; i++) { @@ -33,7 +34,7 @@ public class FFMPegHelper /// converts ts files in directory to opus /// directory with ts fragment files /// paths to created opus files - public Task ToOpus(string localDir) => + public Task ToOpus(IOPath localDir) => ToOpus(Directory.GetFiles(localDir, "*.ts")); /// @@ -41,24 +42,24 @@ public class FFMPegHelper /// /// ts fragment files /// paths to created opus files - public async Task ToOpus(string[] fragments) + public async Task ToOpus(IOPath[] fragments) { - string[] output = new string[fragments.Length]; + IOPath[] output = new IOPath[fragments.Length]; var tasks = new Task[fragments.Length]; for (var i = 0; i < fragments.Length; i++) { - string tsFile = fragments[i]; - string opusFile = tsFile.Replace(".ts",".opus"); + IOPath tsFile = fragments[i]; + IOPath opusFile = tsFile.Replace(".ts",".opus"); _logger.LogDebug($"{tsFile} -> {opusFile}"); - var command = Cli.Wrap(ffmpeg).WithArguments(new[] + var command = Cli.Wrap(ffmpeg_exe.Str).WithArguments(new[] { - "-i", tsFile, // input + "-i", tsFile.Str, // input "-loglevel", "warning", "-hide_banner", "-nostats", // print only warnings and errors "-map", "0:0", // select first audio track (sometimes there are blank buggy second thack) "-filter:a", "asetpts=PTS-STARTPTS", // fixes pts "-c", "libopus", "-b:a", "96k", // encoding params - opusFile // output + opusFile.Str // output }) // ffmpeg prints all log to stderr, because in stdout it ptints encoded file .WithStandardErrorPipe(PipeTarget.ToDelegate(StdErrHandle)); @@ -78,17 +79,17 @@ public class FFMPegHelper else _logger.LogWarn(msg); } - public async Task Concat(string outfile, string fragmentListFile, string codec="libopus") + public async Task Concat(IOPath outfile, IOPath fragmentListFile, string codec="libopus") { _logger.LogDebug($"{fragmentListFile} -> {outfile}"); - var command = Cli.Wrap(ffmpeg).WithArguments(new[] + var command = Cli.Wrap(ffmpeg_exe.Str).WithArguments(new[] { "-f", "concat", // mode - "-i", fragmentListFile, // input list + "-i", fragmentListFile.Str, // input list "-loglevel", "warning", "-hide_banner", "-nostats", // print only warnings and errors "-filter:a", "asetpts=PTS-STARTPTS", // fixes pts "-c", codec, "-b:a", "96k", // encoding params - outfile, "-y" // output override + outfile.Str, "-y" // output override }) // ffmpeg prints all log to stderr, because in stdout it ptints encoded file .WithStandardErrorPipe(PipeTarget.ToDelegate( diff --git a/VkAudioDownloader/Helpers/HttpHelper.cs b/VkAudioDownloader/Helpers/HttpHelper.cs index ff5d15c..79d1467 100644 --- a/VkAudioDownloader/Helpers/HttpHelper.cs +++ b/VkAudioDownloader/Helpers/HttpHelper.cs @@ -29,7 +29,7 @@ public class HttpHelper : HttpClient } public async Task DownloadAsync(HLSFragment fragment, string localDir) => - await WriteStreamAsync(await GetStreamAsync(fragment), Path.Concat(localDir, fragment.Name)); + await WriteStreamAsync(await GetStreamAsync(fragment), Path.Concat(localDir, fragment.Name).ToString()!); public async Task DownloadAsync(HLSPlaylist playlist, string localDir) { diff --git a/VkAudioDownloader/VkAudioDownloader.csproj b/VkAudioDownloader/VkAudioDownloader.csproj index 677c81c..1cf2406 100644 --- a/VkAudioDownloader/VkAudioDownloader.csproj +++ b/VkAudioDownloader/VkAudioDownloader.csproj @@ -20,10 +20,10 @@ - - - - - + + + + + diff --git a/VkAudioDownloader/VkClient.cs b/VkAudioDownloader/VkClient.cs index f712090..2cf4717 100644 --- a/VkAudioDownloader/VkClient.cs +++ b/VkAudioDownloader/VkClient.cs @@ -3,7 +3,7 @@ global using System.Threading.Tasks; global using System.Collections.Generic; global using DTLib.Filesystem; global using DTLib.Extensions; -global using DTLib.Logging.New; +using DTLib.Logging; using DTLib.Logging.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -11,9 +11,7 @@ using VkAudioDownloader.Helpers; using VkNet; using VkNet.Enums.Filters; using VkNet.Model; -using VkNet.Model.RequestParams; using VkNet.Utils; -using VkNet.Model.Attachments; using VkNet.AudioBypassService.Extensions; using VkAudioDownloader.VkM3U8; @@ -23,16 +21,16 @@ public class VkClient : IDisposable { public VkApi Api; public VkClientConfig Config; - private DTLib.Logging.New.ContextLogger _logger; + private ContextLogger _logger; public HttpHelper Http; public FFMPegHelper Ffmpeg; - public VkClient(VkClientConfig conf, DTLib.Logging.New.ILogger logger) + public VkClient(VkClientConfig conf, DTLib.Logging.ILogger logger) { Config = conf; - _logger = new ContextLogger(logger, nameof(VkClient)); + _logger = new ContextLogger(nameof(VkClient), logger); Http = new HttpHelper(); - Ffmpeg = new FFMPegHelper(logger,conf.FfmpegDir); + Ffmpeg = new FFMPegHelper(logger, conf.FfmpegDir); var services = new ServiceCollection() .Add(new LoggerService(logger)) .AddAudioBypass(); @@ -80,16 +78,16 @@ public class VkClient : IDisposable }); ///file name - public Task DownloadAudioAsync(Audio audio, string localDir) => + public Task DownloadAudioAsync(Audio audio, string localDir) => DownloadAudioAsync(audio, localDir,TimeSpan.FromHours(1)); ///file name - public async Task DownloadAudioAsync(Audio audio, string localDir, TimeSpan durationLimit) + public async Task DownloadAudioAsync(Audio audio, string localDir, TimeSpan durationLimit) { if (!audio.Url.ToString().StartsWith("http")) throw new Exception($"incorrect audio url: {audio.Url}"); - string outFile = Path.Concat(localDir, DTLib.Filesystem.Path.CorrectString($"{audio.Artist}-{audio.Title}.opus")); + IOPath outFile = Path.Concat(localDir, DTLib.Filesystem.Path.ReplaceRestrictedChars($"{audio.Artist}-{audio.Title}.opus")); string fragmentDir = $"{outFile}_{DateTime.Now.Ticks}"; if(File.Exists(outFile)) _logger.LogWarn( $"file {outFile} already exists"); @@ -101,8 +99,8 @@ public class VkClient : IDisposable throw new Exception($"duration limit <{durationLimit}> exceeded by track <{audio}> - <{hls.Duration}>"); await Http.DownloadAsync(hls, fragmentDir); - string[] opusFragments = await Ffmpeg.ToOpus(fragmentDir); - string listFile = Ffmpeg.CreateFragmentList(fragmentDir, opusFragments); + var opusFragments = await Ffmpeg.ToOpus(fragmentDir); + IOPath listFile = Ffmpeg.CreateFragmentList(fragmentDir, opusFragments); await Ffmpeg.Concat(outFile, listFile); Directory.Delete(fragmentDir);