using CommandLine; using System; using System.Collections.Generic; using System.IO; using System.IO.Pipes; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading; using TessesYoutubeDownloader.Server.Models; namespace youtube_downloader { class Program { public static string[] ArgsFromString(string s) { List s2 = new List(); StringBuilder b = new StringBuilder(); bool inQuote=false; foreach(var c in s) { if(c == '\"') { inQuote = !inQuote; continue; } if(c == ' ') { if (!inQuote) { if (b.Length > 0) { s2.Add(b.ToString()); b.Clear(); } continue; } } b.Append(c); } if (b.Length > 0) { s2.Add(b.ToString()); b.Clear(); } return s2.ToArray(); } static void Main(string[] args) { Thread t = new Thread(new ThreadStart(() => { Server.Functions.Downloader.DL.DownloadThread().GetAwaiter().GetResult(); })); t.Start(); // we need to get our app name so that // we can create unique names for our mutex and our pipe do { Console.Write("> "); var a2 = ArgsFromString(Console.ReadLine()); _ProcessCommandLine(a2); } while (true); } [Verb("exit", HelpText = "Download Video")] public class ExitApp { } [Verb("video", true, HelpText = "Download Video")] public class DownloadVideo { [Value(0, Required = true, HelpText = "The id or url of video")] public string Id { get; set; } [Option('f', "format", Default = Resolution.NoConvert, HelpText = "possible values are (NoConvert,Convert,Audio)")] public Resolution Format { get; set; } } [Verb("playlist", HelpText = "Download entire Playlist")] public class DownloadPlaylist { [Value(0, Required = true, HelpText = "The id or url of playlist")] public string Id { get; set; } [Option('f', "format", Default = Resolution.NoConvert, HelpText = "possible values are (NoConvert,Convert,Audio)")] public Resolution Format { get; set; } } [Verb("channel", HelpText = "Download entire Channel (using channel id)")] public class DownloadChannel { [Value(0, Required = true, HelpText = "The id or url of channel")] public string Id { get; set; } [Option('f', "format", Default = Resolution.NoConvert, HelpText = "possible values are (NoConvert,Convert,Audio)")] public Resolution Format { get; set; } } [Verb("user", HelpText = "Download entire Channel (using username)")] public class DownloadUser { [Value(0, Required = true, HelpText = "The name or url of the user")] public string Id { get; set; } [Option('f', "format", Default = Resolution.NoConvert, HelpText = "possible values are (NoConvert,Convert,Audio)")] public Resolution Format { get; set; } } [Verb("move", HelpText = "Move item in queue")] public class MoveQueue { [Option('d', "moveto", HelpText = "can be (up,down,top,bottom,remove)")] public string MoveTo { get; set; } [Option('s', "movefrom", HelpText = "Can be number (index) or \"last\"")] public string MoveFrom { get; set; } } [Verb("info", HelpText = "Get info")] public class Info { [Option('m', "machine", Default = false, HelpText = "In json")] public bool ForMachine { get; set; } [Option('p', "page", Required = true, HelpText = "can be (progress,queue,location)")] public string Page { get; set; } } static void _ProcessCommandLine(string[] args) { CommandLine.Parser.Default.ParseArguments(args) .MapResult( (DownloadVideo opts) => RunDownloadVideo(opts), (DownloadPlaylist opts) => RunDownloadPlaylist(opts), (DownloadChannel opts) => RunDownloadChannel(opts), (DownloadUser opts) => RunDownloadUser(opts), (MoveQueue opts) => RunMoveQueue(opts), (Info opts) => RunInfo(opts), (ExitApp opts) => RunExitApp(opts), errs => 1); } static void WriteVideoInfo(StringBuilder b,SavedVideo v,bool description) { if (v != null) { //v.AuthorChannelId //v.AuthorTitle //v.Dislikes //v.Duration //v.Id //v.Keywords //v.Likes //v.Title //v.UploadDate //v.Views //v.Description b.AppendLine($"Title: {v.Title}"); b.AppendLine($"AuthorName: {v.AuthorTitle}"); b.AppendLine($"AuthorID: {v.AuthorChannelId}"); b.AppendLine($"Duration: {TimeSpan.FromSeconds(v.Duration).ToString()}"); b.AppendLine($"Id: {v.Id}"); b.AppendLine($"UploadDate: {v.UploadDate}"); b.AppendLine($"Views: {v.Views}, Likes: {v.Likes}, Dislikes: {v.Dislikes}"); b.AppendLine("Keywords:"); foreach (var kw in v.Keywords) { b.AppendLine($"\t{kw}"); } if (description) { b.AppendLine("Description:"); b.AppendLine(v.Description); } } } private static int RunInfo(Info opts) { //if ( != null) switch (opts.Page) { case "progress": if (opts.ForMachine) { string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(Server.Functions.Downloader.GetProgress()); Console.WriteLine(jsonData); } else { var s=Server.Functions.Downloader.GetProgress(); if (s != null) { StringBuilder sb = new StringBuilder(); sb.AppendLine("=======Progress======="); sb.AppendLine($"Progress: {s.Progress}%"); sb.AppendLine($"Size: {Math.Round((double)s.Length / (double)(1000 * 1000), 2)} MB"); sb.AppendLine(); sb.AppendLine("=======Video Info======="); WriteVideoInfo(sb, s.Saved, false); Console.WriteLine(sb.ToString()); } } break; case "queue": if (opts.ForMachine) { string jsonData = Server.Functions.Downloader.GetQueue(); Console.WriteLine(jsonData); } else { try { var s = Server.Functions.Downloader.GetQueueItems(); StringBuilder sb = new StringBuilder(); foreach (var item in s) { WriteVideoInfo(sb, item.Video, false); sb.AppendLine(); } Console.WriteLine(sb.ToString()); } catch (Exception ex) { _ = ex; } } break; case "location": Console.WriteLine(Server.Functions.Downloader.DL.StorageLocation); break; } return 1; } private static int RunMoveQueue(MoveQueue opts) { Server.Functions.Downloader.ModQueue(opts.MoveTo, opts.MoveFrom); return 1; } private static int RunDownloadUser(DownloadUser opts) { Server.Functions.Downloader.DownloadUser(opts.Id, opts.Format); return 1; } private static int RunExitApp(ExitApp opts) { Environment.Exit(0); return 1; } private static int RunDownloadVideo(DownloadVideo opts) { Server.Functions.Downloader.DownloadVideo(opts.Id, opts.Format); return 1; } private static int RunDownloadPlaylist(DownloadPlaylist opts) { Server.Functions.Downloader.DownloadPlaylist(opts.Id, opts.Format); return 1; } private static int RunDownloadChannel(DownloadChannel opts) { Server.Functions.Downloader.DownloadChannel(opts.Id, opts.Format); return 1; } } }