Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
weespin committed Nov 7, 2020
1 parent a31399a commit f21243a
Show file tree
Hide file tree
Showing 10 changed files with 471 additions and 502 deletions.
4 changes: 2 additions & 2 deletions AcapellaDownloader.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2036
# Visual Studio Version 16
VisualStudioVersion = 16.0.30503.244
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AcapellaDownloader", "AcapellaDownloader\AcapellaDownloader.csproj", "{B3CC2F18-3283-450C-B82B-6AE19B3E9A25}"
EndProject
Expand Down
1 change: 1 addition & 0 deletions AcapellaDownloader/AcapellaDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CLI.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
136 changes: 136 additions & 0 deletions AcapellaDownloader/CLI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;

namespace AcapellaDownloader
{


class CommandLineUtils
{
public CommandLineUtils(string dataIn, string[] keysIn)
{
//Store keys
foreach (var key in keysIn)
{
_ConsoleKeyValues.Add(key, "");
}

foreach (var key in _ConsoleKeyValues.Keys.ToList())
{
int index = dataIn.IndexOf(key, StringComparison.Ordinal);
if (index == -1)
{
continue;
}
int end = dataIn.Length;
//Find next key
foreach (var anotherkey in _ConsoleKeyValues.Keys.Where(n => n != key).ToList())
{
int anotherindex = dataIn.IndexOf(anotherkey);
if (anotherindex > index)
{
end = Math.Min(end, anotherindex);
}
}

string value = dataIn.Substring(index + key.Length, end - index - key.Length);
value = value.Trim();
_ConsoleKeyValues[key] = value;

}
}

public string GetValue(string key)
{
if (_ConsoleKeyValues.ContainsKey(key))
{
return _ConsoleKeyValues[key];
}
return "";
}

Dictionary<string, string> _ConsoleKeyValues = new Dictionary<string, string>();
}

static class CLI
{
//WinAPI stuff
[DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
[DllImport("kernel32.dll", EntryPoint = "AttachConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int AttachConsole(int dwProcessId);


public static void PassCLI(string[] commandLineArgs)
{
//Switch to CLI Mode
if (AttachConsole(-1) == 0)
{
AllocConsole();
}
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);

var arguments = commandLineArgs.ToList();
arguments.RemoveAt(0);

string argString = string.Join(" ", arguments.ToArray());
CommandLineUtils utils = new CommandLineUtils(argString, new string[] { "-v ", "-o ", "-t " });

if (argString.Contains("--voice-list"))
{
foreach (var curVoice in Voices.VoiceList)
{
Console.WriteLine($"Name: {curVoice.Name} Lang: {curVoice.Lang} Gender {curVoice.Gender} VoiceId: {curVoice.VoiceId}");
}
return;
}
string text = utils.GetValue("-t ");
string path = utils.GetValue("-o ");
string voice = utils.GetValue("-v ");

if (path.Length == 0)
{
Console.WriteLine("No path specified, using output.mp3 instead.");
path = "output.mp3";
}
if (text.Length == 0)
{
Console.WriteLine("No text specified. Args -t (text) -o (output file) -v (voice code) --voice-list (get all voice codes)");
return;
}

if (voice.Length == 0)
{
Console.WriteLine("No VoiceId specified. Try to launch with --voice-list to get all voices");
return;
}
else
{
if (Voices.VoiceList.FirstOrDefault(n => n.VoiceId == voice) == null)
{
Console.WriteLine("Voice code is not valid. Try to launch with --voice-list to get all voices");
}
}
string dlLink = Utils.GetSoundLink(text, voice);
if (dlLink == "")
{
Console.WriteLine(Form1.downloadError);
return;
}
using (var web = new WebClient())
{
web.DownloadFile(dlLink, path);
Console.WriteLine(Form1.downloaded);
}

return;
}

}
}
156 changes: 78 additions & 78 deletions AcapellaDownloader/Form1.Designer.cs

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

Loading

0 comments on commit f21243a

Please sign in to comment.