tytd-server/Program.cs

275 lines
9.3 KiB
C#
Raw Normal View History

2021-06-24 01:10:20 +00:00
using CommandLine;
using System;
2021-06-24 02:55:41 +00:00
using System.Collections.Generic;
2021-06-24 01:10:20 +00:00
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
{
2021-06-24 02:55:41 +00:00
public static string[] ArgsFromString(string s)
2021-06-24 01:10:20 +00:00
{
2021-06-24 02:55:41 +00:00
List<string> s2 = new List<string>();
StringBuilder b = new StringBuilder();
bool inQuote=false;
foreach(var c in s)
2021-06-24 01:10:20 +00:00
{
2021-06-24 02:55:41 +00:00
if(c == '\"')
{
inQuote = !inQuote;
continue;
}
if(c == ' ')
{
if (!inQuote)
{
if (b.Length > 0)
{
s2.Add(b.ToString());
b.Clear();
}
continue;
}
}
b.Append(c);
2021-06-24 01:10:20 +00:00
}
2021-06-24 02:55:41 +00:00
if (b.Length > 0)
2021-06-24 01:10:20 +00:00
{
2021-06-24 02:55:41 +00:00
s2.Add(b.ToString());
b.Clear();
2021-06-24 01:10:20 +00:00
}
2021-06-24 02:55:41 +00:00
return s2.ToArray();
2021-06-24 01:10:20 +00:00
}
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
2021-06-24 02:55:41 +00:00
do
2021-06-24 01:10:20 +00:00
{
2021-06-24 02:55:41 +00:00
Console.Write("> ");
var a2 = ArgsFromString(Console.ReadLine());
_ProcessCommandLine(a2);
} while (true);
2021-06-24 01:10:20 +00:00
}
2021-06-24 02:55:41 +00:00
2021-06-24 01:10:20 +00:00
[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; }
}
2021-06-24 02:55:41 +00:00
2021-06-24 01:10:20 +00:00
static void _ProcessCommandLine(string[] args)
{
CommandLine.Parser.Default.ParseArguments<DownloadVideo, DownloadPlaylist, DownloadChannel, DownloadUser, MoveQueue, Info, ExitApp>(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)
{
//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)
2021-06-24 02:55:41 +00:00
{
//if ( != null)
2021-06-24 01:10:20 +00:00
switch (opts.Page)
{
case "progress":
if (opts.ForMachine)
{
string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(Server.Functions.Downloader.GetProgress());
2021-06-24 02:55:41 +00:00
Console.WriteLine(jsonData);
2021-06-24 01:10:20 +00:00
}
else
{
var s=Server.Functions.Downloader.GetProgress();
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);
2021-06-24 02:55:41 +00:00
Console.WriteLine(sb.ToString());
2021-06-24 01:10:20 +00:00
}
break;
case "queue":
if (opts.ForMachine)
{
string jsonData = Server.Functions.Downloader.GetQueue();
2021-06-24 02:55:41 +00:00
Console.WriteLine(jsonData);
2021-06-24 01:10:20 +00:00
}
else
{
var s = Server.Functions.Downloader.GetQueueItems();
StringBuilder sb = new StringBuilder();
foreach(var item in s)
{
WriteVideoInfo(sb, item.Video, false);
sb.AppendLine();
}
2021-06-24 02:55:41 +00:00
Console.WriteLine(sb.ToString());
2021-06-24 01:10:20 +00:00
}
break;
case "location":
2021-06-24 02:55:41 +00:00
Console.WriteLine(Server.Functions.Downloader.DL.StorageLocation);
2021-06-24 01:10:20 +00:00
break;
}
2021-06-24 02:55:41 +00:00
2021-06-24 03:00:49 +00:00
return 1;
2021-06-24 01:10:20 +00:00
}
private static int RunMoveQueue(MoveQueue opts)
{
Server.Functions.Downloader.ModQueue(opts.MoveTo, opts.MoveFrom);
2021-06-24 03:00:49 +00:00
return 1;
2021-06-24 01:10:20 +00:00
}
private static int RunDownloadUser(DownloadUser opts)
{
Server.Functions.Downloader.DownloadUser(opts.Id, opts.Format);
2021-06-24 03:00:49 +00:00
return 1;
2021-06-24 01:10:20 +00:00
}
private static int RunExitApp(ExitApp opts)
{
Environment.Exit(0);
2021-06-24 03:00:49 +00:00
return 1;
2021-06-24 01:10:20 +00:00
}
private static int RunDownloadVideo(DownloadVideo opts)
{
Server.Functions.Downloader.DownloadVideo(opts.Id, opts.Format);
2021-06-24 03:00:49 +00:00
return 1;
2021-06-24 01:10:20 +00:00
}
private static int RunDownloadPlaylist(DownloadPlaylist opts)
{
Server.Functions.Downloader.DownloadPlaylist(opts.Id, opts.Format);
2021-06-24 03:00:49 +00:00
return 1;
2021-06-24 01:10:20 +00:00
}
private static int RunDownloadChannel(DownloadChannel opts)
{
Server.Functions.Downloader.DownloadChannel(opts.Id, opts.Format);
2021-06-24 03:00:49 +00:00
return 1;
2021-06-24 01:10:20 +00:00
}
}
}