This commit is contained in:
Demetria Lovato 2021-06-23 19:55:41 -07:00
parent 547d92aa2c
commit f7397e817f
10 changed files with 50 additions and 124 deletions

Binary file not shown.

View File

@ -1,5 +1,6 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Reflection;
@ -13,104 +14,56 @@ namespace youtube_downloader
class Program
{
static void SendStringArray(System.IO.Stream s, string[] array)
public static string[] ArgsFromString(string s)
{
s.Write(BitConverter.GetBytes(array.Length), 0, 4);
foreach (var item in array)
List<string> s2 = new List<string>();
StringBuilder b = new StringBuilder();
bool inQuote=false;
foreach(var c in s)
{
byte[] lenOfitem = BitConverter.GetBytes(item.Length);
byte[] argtext = System.Text.Encoding.UTF8.GetBytes(item);
s.Write(lenOfitem, 0, lenOfitem.Length);
s.Write(argtext, 0, argtext.Length);
if(c == '\"')
{
inQuote = !inQuote;
continue;
}
if(c == ' ')
{
if (!inQuote)
{
if (b.Length > 0)
{
s2.Add(b.ToString());
b.Clear();
}
continue;
}
}
b.Append(c);
}
}
static string[] GetStringArray(System.IO.Stream s)
{
byte[] items = new byte[4];
s.Read(items, 0, items.Length);
int items2 = BitConverter.ToInt32(items, 0);
string[] arraydata = new string[items2];
for (int i = 0; i < items2; i++)
if (b.Length > 0)
{
s.Read(items, 0, 4);
int item3 = BitConverter.ToInt32(items, 0);
byte[] v = new byte[item3];
s.Read(v, 0, item3);
arraydata[i] = System.Text.Encoding.UTF8.GetString(v);
s2.Add(b.ToString());
b.Clear();
}
return arraydata;
return s2.ToArray();
}
static string _AppName =
Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().GetName().Name);
static void Main(string[] args)
{
// we need to get our app name so that
// we can create unique names for our mutex and our pipe
var notAlreadyRunning = true;
// wrap the meat of the application code with a named mutex so it runs only once
using (var mutex = new Mutex(true, _AppName + "Singleton", out notAlreadyRunning))
do
{
if (notAlreadyRunning)
{
// do additional work here, startup stuff
// Console.WriteLine("Running. Press any key to exit...");
// ...
// now process our initial main command line
_ProcessCommandLine(args);
// start the IPC sink.
var srv = new NamedPipeServerStream(_AppName + "IPC",
PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
// it's easier to use the AsyncCallback than it is to use Tasks here:
// this can't block, so some form of async is a must
srv.BeginWaitForConnection(new AsyncCallback(_ConnectionHandler), srv);
// block here until exit
Console.ReadKey();
// if this was a windows forms app you would put your
// "Applicantion.Run(new MyForm());" here
// finally, run any teardown code and exit
srv.Close();
}
else // another instance is running
{
// connect to the main app
var cli = new NamedPipeClientStream(".", _AppName + "IPC", PipeDirection.InOut);
cli.Connect();
SendStringArray(cli, args);
byte[] leng = new byte[4];
cli.Read(leng, 0, 4);
int sz = BitConverter.ToInt32(leng,0);
byte[] sdata = new byte[sz];
cli.Read(sdata, 0, sz);
string strdata = System.Text.Encoding.UTF8.GetString(sdata);
Console.Write(strdata);
// serialize and send the command line
cli.Close();
// and exit
}
}
}
static void _ConnectionHandler(IAsyncResult result)
{
var srv = result.AsyncState as NamedPipeServerStream;
srv.EndWaitForConnection(result);
// we're connected, now deserialize the incoming command line
Console.Write("> ");
var a2 = ArgsFromString(Console.ReadLine());
_ProcessCommandLine(a2);
} while (true);
var inargs = GetStringArray(srv);
r = new RESPONSE(srv);
// process incoming command line
_ProcessCommandLine(inargs);
r.SendResponse();
srv = new NamedPipeServerStream(_AppName + "IPC", PipeDirection.InOut,
1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
srv.BeginWaitForConnection(new AsyncCallback(_ConnectionHandler), srv);
}
[Verb("exit", HelpText = "Download Video")]
@ -179,7 +132,7 @@ namespace youtube_downloader
public string Page { get; set; }
}
static RESPONSE r=null;
static void _ProcessCommandLine(string[] args)
{
CommandLine.Parser.Default.ParseArguments<DownloadVideo, DownloadPlaylist, DownloadChannel, DownloadUser, MoveQueue, Info, ExitApp>(args)
@ -228,15 +181,16 @@ namespace youtube_downloader
}
}
private static int RunInfo(Info opts)
{ if (r != null)
{
{
//if ( != null)
switch (opts.Page)
{
case "progress":
if (opts.ForMachine)
{
string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(Server.Functions.Downloader.GetProgress());
r.SendResponse(jsonData);
Console.WriteLine(jsonData);
}
else
{
@ -248,15 +202,15 @@ namespace youtube_downloader
sb.AppendLine();
sb.AppendLine("=======Video Info=======");
WriteVideoInfo(sb, s.Saved, false);
r.SendResponse(sb.ToString());
Console.WriteLine(sb.ToString());
}
break;
case "queue":
if (opts.ForMachine)
{
string jsonData = Server.Functions.Downloader.GetQueue();
r.SendResponse(jsonData);
Console.WriteLine(jsonData);
}
else
{
@ -267,17 +221,17 @@ namespace youtube_downloader
WriteVideoInfo(sb, item.Video, false);
sb.AppendLine();
}
r.SendResponse(sb.ToString());
Console.WriteLine(sb.ToString());
}
break;
case "location":
r.SendResponse(Server.Functions.Downloader.DL.StorageLocation);
Console.WriteLine(Server.Functions.Downloader.DL.StorageLocation);
break;
}
}
return 0;
}

View File

@ -1,27 +0,0 @@
using System;
namespace youtube_downloader
{
internal class RESPONSE
{
public RESPONSE(System.IO.Stream strm)
{
this.strm = strm;
}
System.IO.Stream strm;
public void SendResponse(string response)
{
if(strm != null)
{
strm.Write(BitConverter.GetBytes(response.Length), 0, 4);
strm.Write(System.Text.Encoding.UTF8.GetBytes(response), 0, response.Length);
strm.Dispose();
strm = null;
}
}
public void SendResponse()
{
SendResponse("");
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
6acf12007b654605f4a149e8bf10292d9c49014f
2a5e8789af6d18f40163043d238b63564245c869

Binary file not shown.

Binary file not shown.

View File

@ -88,7 +88,6 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RESPONSE.cs" />
<Compile Include="Server\Functions\Downloader.cs" />
<Compile Include="Server\Functions\ffmpeg.cs" />
<Compile Include="Server\Models\SavedChannel.cs" />