almost forgot server

This commit is contained in:
Mike Nolan 2022-11-01 02:22:10 -05:00
parent 6755cb3b3d
commit 6d322cd098
2 changed files with 106 additions and 65 deletions

View File

@ -12,6 +12,7 @@ using YoutubeExplode.Playlists;
using YoutubeExplode.Channels;
using Tesses.Extensions;
using YoutubeExplode.Videos.Streams;
using Tesses.WebServer.Swagme;
namespace Tesses.Extensions
{
@ -32,16 +33,7 @@ namespace Tesses.YouTubeDownloader.Server
{
using Tesses.YouTubeDownloader;
public class Progress
{
public long Length {get;set;}
public double Percent {get;set;}
public SavedVideo Video {get;set;}
public bool StartEvent {get;set;}
public bool StopEvent {get;set;}
}
internal static class B64
{
public static string Base64UrlEncodes(string arg)
@ -855,44 +847,60 @@ internal static class B64
}
}
}
internal class ApiV2Server : RouteServer
internal class ApiV2Server
{
public SwagmeServer Swagme {get;private set;}=new SwagmeServer();
IDownloader Downloader;
public ApiV2Server(IDownloader downloader)
{
this.Downloader=downloader;
AddBoth("/event",Event);
AddBoth("/CancelDownload",Cancel);
AddBoth("/Search",Search);
AddBoth("/AddItem",AddItem);
AddBoth("/AddChannel",AddChannel);
AddBoth("/AddUser",AddUser);
AddBoth("/AddPlaylist",AddPlaylist);
AddBoth("/AddVideo",AddVideo);
AddBoth("/AddFile",AddFile);
AddBoth("/Progress",ProgressFunc);
AddBoth("/QueueList",QueueList);
AddBoth("/subscribe",Subscribe);
AddBoth("/resubscribe",Resubscribe);
AddBoth("/unsubscribe",Unsubscribe);
AddBoth("/subscriptions",Subscriptions);
AddBoth("/Subscribe",Subscribe);
AddBoth("/Resubscribe",Resubscribe);
AddBoth("/Unsubscribe",Unsubscribe);
AddBoth("/Subscriptions",Subscriptions);
AddBoth("/AddToList",AddToList);
AddBoth("/DeleteFromList",DeleteFromList);
Add("/ReplaceList",ReplaceList,"POST");
AddBoth("/DeleteList",DeleteList);
AddBoth("/SetResolutionInList",SetResolutionInList);
Swagme.AbsoluteUrl=true;
Add("/export/everything.json",Everything_Export,"GET");
Add("/export/videos.json",VideosExport,"GET");
Add("/export/playlists.json",PlaylistsExport,"GET");
Add("/export/channels.json",ChannelsExport,"GET");
Add("/export/filedownloads.json",FilesExport,"GET");
Add("/export/subscriptions.json",SubscriptionsExport,"GET");
Add("/export/personal_lists.json",PersonalListsExport,"GET");
this.Downloader=downloader;
/*Adding items*/
AddBoth("/AddVideo",AddVideo,new SwagmeDocumentation("/AddVideo?v=jNQXAC9IVRw&res=PreMuxed","Add youtube video","v: Video Id Or encodeUriComponent Url<br>res: NoDownload=Do not download clip but just info, Mux=Requires conversion, PreMuxed=Does not require conversion, AudioOnly=Best audio only, VideoOnly=Best video only"),"Adding items");
AddBoth("/AddPlaylist",AddPlaylist,new SwagmeDocumentation("/AddPlaylist?id=PLgXAgLm6Kre7M3c8G2OlQTG-PETLHs4Vd&res=PreMuxed","Add youtube playlist","id: Playlist Id Or encodeUriComponent Url<br>res: NoDownload=Do not download clip but just info, Mux=Requires conversion, PreMuxed=Does not require conversion, AudioOnly=Best audio only, VideoOnly=Best video only"),"Adding items");
AddBoth("/AddChannel",AddChannel,new SwagmeDocumentation("/AddChannel?id=UC4QobU6STFB0P71PMvOGN5A&res=PreMuxed","Add youtube channel","id: YouTube Channel Id Or encodeUriComponent Url<br>res: NoDownload=Do not download clip but just info, Mux=Requires conversion, PreMuxed=Does not require conversion, AudioOnly=Best audio only, VideoOnly=Best video only"),"Adding items");
AddBoth("/AddUser",AddUser,new SwagmeDocumentation("/AddUser?id=jawed&res=PreMuxed","Add youtube user","id: YouTube Channel Name Or encodeUriComponent Url<br>res: NoDownload=Do not download clip but just info, Mux=Requires conversion, PreMuxed=Does not require conversion, AudioOnly=Best audio only, VideoOnly=Best video only"),"Adding items");
AddBoth("/AddItem",AddItem,new SwagmeDocumentation("/AddItem?v=jNQXAC9IVRw&res=PreMuxed","Add any type of item","v: Media Id Or encodeUriComponent Url<br>res: NoDownload=Do not download clip but just info, Mux=Requires conversion, PreMuxed=Does not require conversion, AudioOnly=Best audio only, VideoOnly=Best video only"),"Adding items");
AddBoth("/AddFile",AddFile,new SwagmeDocumentation("/AddFile?url=https%3A%2F%2Ftesses.cf%2Fimages%2Frvl.jpg&download=true","Add normal file download","url: Url to file<br>download: whether to download file"),"Adding items");
/*Getting status*/
AddBoth("/event",Event,new SwagmeDocumentation("Server sent events","Returns events with json"),"Getting status");
AddBoth("/Progress",ProgressFunc,new SwagmeDocumentation("Get video progress","<a href=\"https://tesses.net/apps/tytd/2022/progress.php\">More Info</a>"),"Getting status");
AddBoth("/QueueList",QueueList,new SwagmeDocumentation("Get items in Queue","<a href=\"https://tesses.net/apps/tytd/2022/queuels.php\">More Info</a>"),"Getting status");
/*Subscriptions*/
AddBoth("/Subscribe",Subscribe,new SwagmeDocumentation("/Subscribe?id=UC4QobU6STFB0P71PMvOGN5A&conf=NotifyAndDownload","Subscribe to youtuber","id: Channel Id<br>conf: DoNothing=Disable, GetInfo=Just Get info, Notify=Bell event, Download=Download video no bell event, NotifyAndDownload=Download video with bell event"),"Subscriptions");
AddBoth("/subscribe",Subscribe,new SwagmeDocumentation("/subscribe?id=UC4QobU6STFB0P71PMvOGN5A&conf=NotifyAndDownload","Subscribe to youtuber","id: Channel Id<br>conf: DoNothing=Disable, GetInfo=Just Get info, Notify=Bell event, Download=Download video no bell event, NotifyAndDownload=Download video with bell event"),"Subscriptions");
AddBoth("/Resubscribe",Resubscribe,new SwagmeDocumentation("/Resubscribe?id=UC4QobU6STFB0P71PMvOGN5A&conf=Download","Change subscription settings","id: Channel Id<br>conf: DoNothing=Disable, GetInfo=Just Get info, Notify=Bell event, Download=Download video no bell event, NotifyAndDownload=Download video with bell event"),"Subscriptions");
AddBoth("/resubscribe",Resubscribe,new SwagmeDocumentation("/resubscribe?id=UC4QobU6STFB0P71PMvOGN5A&conf=Download","Change subscription settings","id: Channel Id<br>conf: DoNothing=Disable, GetInfo=Just Get info, Notify=Bell event, Download=Download video no bell event, NotifyAndDownload=Download video with bell event"),"Subscriptions");
AddBoth("/Unsubscribe",Unsubscribe,new SwagmeDocumentation("/Unsubscribe?id=UC4QobU6STFB0P71PMvOGN5A","Unsubscribe from youtuber","id: Channel Id"),"Subscriptions");
AddBoth("/unsubscribe",Unsubscribe,new SwagmeDocumentation("/unsubscribe?id=UC4QobU6STFB0P71PMvOGN5A","Unsubscribe from youtuber","id: Channel Id"),"Subscriptions");
AddBoth("/Subscriptions",Subscriptions,new SwagmeDocumentation("Get subscriptions","Returned Json array<br>&emsp;Id: Channel Id<br>&emsp;BellInfo: 0=DoNothing, 1=GetInfo, 3=Notify, 5=Download, 7=NotifyAndDownload"),"Subscriptions");
AddBoth("/subscriptions",Subscriptions,new SwagmeDocumentation("Get subscriptions","Returned Json array<br>&emsp;Id: Channel Id<br>&emsp;BellInfo: 0=DoNothing, 1=GetInfo, 3=Notify, 5=Download, 7=NotifyAndDownload"),"Subscriptions");
/*Personal Lists*/
AddBoth("/AddToList",AddToList,new SwagmeDocumentation("/AddToList?name=SomeList&v=jNQXAC9IVRw&res=PreMuxed","Add item to personal list","NOTE, this will create list if not created<br>name: Name of personal list<br><b>Works on GET and POST</b><hr>v: VideoId<br>res: NoDownload=Do not download clip but just info, Mux=Requires conversion, PreMuxed=Does not require conversion, AudioOnly=Best audio only, VideoOnly=Best video only<br><b>Works only on POST</b><hr>data: Json Array with objects with entries:<br>&emsp;Id: VideoId<br>&emsp;Resolution: -1=NoDownload, 0=Mux, 1=PreMuxed, 2=AudioOnly, 3=VideoOnly"),"Personal Lists");
Swagme.Add("/ReplaceList",ReplaceList,new SwagmeDocumentation("/ReplaceList","Replace personal list's contents","name: Name of personal playlist<br>data: Json Array with objects with entries:<br>&emsp;Id: VideoId<br>&emsp;Resolution: -1=NoDownload, 0=Mux, 1=PreMuxed, 2=AudioOnly, 3=VideoOnly"),"POST","Personal Lists");
AddBoth("/SetResolutionInList",SetResolutionInList,new SwagmeDocumentation("/SetResolutionInList?name=SomeList&v=jNQXAC9IVRw&res=Mux","Set resolution for item in personal list","name: Name of personal playlist<br>name: Name of personal list<br>v: VideoId<br>res: NoDownload=Do not download clip but just info, Mux=Requires conversion, PreMuxed=Does not require conversion, AudioOnly=Best audio only, VideoOnly=Best video only"),"Personal Lists");
AddBoth("/DeleteFromList",DeleteFromList,new SwagmeDocumentation("/DeleteFromList?name=SomeList&v=jNQXAC9IVRw","Remove item from personal list","name: Name of personal list<br>id: Video Id"),"Personal Lists");
AddBoth("/DeleteList",DeleteList,new SwagmeDocumentation("/DeleteList?name=SomeList","Remove personal list","name: Name of personal playlist"),"Personal Lists");
/*Searching*/
AddBoth("/Search",Search,new SwagmeDocumentation("/Search?q=Demi%20Lovato","Search youtube","q: search query<br>getinfo: whether to fetch info as well"),"Searching");
/*Exporting*/
Swagme.Add("/export/everything.json",Everything_Export,new SwagmeDocumentation("Everything","<a href=\"https://tesses.net/apps/tytd/2022/export/everything.php\">More Info</a>"),"GET","Exporting");
Swagme.Add("/export/videos.json",VideosExport,new SwagmeDocumentation("Videos","<a href=\"https://tesses.net/apps/tytd/2022/export/videos.php\">More Info</a>"),"GET","Exporting");
Swagme.Add("/export/playlists.json",PlaylistsExport,new SwagmeDocumentation("Playlists","<a href=\"https://tesses.net/apps/tytd/2022/export/playlists.php\">More Info</a>"),"GET","Exporting");
Swagme.Add("/export/channels.json",ChannelsExport,new SwagmeDocumentation("Channels","<a href=\"https://tesses.net/apps/tytd/2022/export/channels.php\">More Info</a>"),"GET","Exporting");
Swagme.Add("/export/filedownloads.json",FilesExport,new SwagmeDocumentation("File Downloads","<a href=\"https://tesses.net/apps/tytd/2022/export/filedownloads.php\">More Info</a>"),"GET","Exporting");
Swagme.Add("/export/subscriptions.json",SubscriptionsExport,new SwagmeDocumentation("Subscriptions","<a href=\"https://tesses.net/apps/tytd/2022/export/subscriptions.php\">More Info</a>"),"GET","Exporting");
Swagme.Add("/export/personal_lists.json",PersonalListsExport,new SwagmeDocumentation("Personal Lists","<a href=\"https://tesses.net/apps/tytd/2022/export/personallists.php\">More Info</a>"),"GET","Exporting");
/*Other*/
AddBoth("/extra_data.json",ExtraData,new SwagmeDocumentation("Get extra info about downloader","Get extra data such as TYTD Tag, item count in queue and tytd version"));
AddBoth("/CancelDownload",Cancel,new SwagmeDocumentation("/CancelDownload?restart=true","Cancel or Restart download","restart:<br>&emsp;true: Restart download<br>&emsp;false: Cancel Download"));
/*
public async Task AddToPersonalPlaylistAsync(string name, IEnumerable<(VideoId Id, Resolution Resolution)> items)
@ -915,6 +923,16 @@ internal static class B64
throw new NotImplementedException();
}*/
}
public async Task ExtraData(ServerContext ctx)
{
var twebserverVersion = ctx.GetType().Assembly.GetName().Version.ToString();
var tytdserverversion= this.GetType().Assembly.GetName().Version.ToString();
ExtraData data=Downloader.GetExtraData();
data.TessesWebServerVersion=twebserverVersion;
data.TYTDServerVersion=tytdserverversion;
await ctx.SendJsonAsync(data);
}
public async Task Event(ServerContext ctx)
{
IStorage storage=Downloader as IStorage;
@ -928,9 +946,11 @@ internal static class B64
storage.VideoStarted += (sender,e)=>
{
len=e.EstimatedLength;
Progress p=new Progress();
ProgressItem p=new ProgressItem();
p.StartEvent=true;
p.StopEvent=false;
p.BellEvent=false;
p.ErrorEvent=false;
p.Length=e.EstimatedLength;
p.Percent=0;
@ -938,11 +958,13 @@ internal static class B64
evts.SendEvent(p);
};
storage.VideoProgress += (sender,e)=>{
Progress p=new Progress();
ProgressItem p=new ProgressItem();
p.StartEvent=false;
p.StopEvent=false;
p.Length=len;
p.Percent=e.Progress;
p.BellEvent=false;
p.ErrorEvent=false;
if(first)
p.Video=e.VideoInfo;
@ -950,14 +972,41 @@ internal static class B64
evts.SendEvent(p);
};
storage.VideoFinished +=(sender,e)=>{
Progress p=new Progress();
ProgressItem p=new ProgressItem();
p.StartEvent=false;
p.StopEvent=true;
p.BellEvent=false;
p.ErrorEvent=false;
p.Length=len;
p.Percent=1;
p.Video=e.VideoInfo;
evts.SendEvent(p);
};
storage.Error += (sender,e)=>{
ProgressItem p=new ProgressItem();
p.StartEvent=false;
p.StopEvent=false;
p.BellEvent=false;
p.ErrorEvent=true;
p.Length=0;
p.Percent=0;
p.Error=e.Exception.ToString();
p.Message = e.Exception.Message;
p.Id = e.Id;
p.ExceptionName = e.Exception.GetType().FullName;
evts.SendEvent(p);
};
storage.Bell += (sender,e)=>{
ProgressItem p=new ProgressItem();
p.StartEvent=false;
p.StopEvent=false;
p.BellEvent=true;
p.ErrorEvent=false;
p.Length=0;
p.Percent=0;
p.Id=e.Id.Value;
evts.SendEvent(p);
};
ctx.ServerSentEvents(evts);
}else{
await ctx.SendTextAsync("Error no IStorage");
@ -1004,13 +1053,13 @@ internal static class B64
}
}
private void AddBoth(string url,HttpActionAsync action)
private void AddBoth(string url,HttpActionAsync action,SwagmeDocumentation documentation,string group="Other")
{
Add(url,action);
Add(url,async(evt)=>{
Swagme.Add(url,action,documentation,method:"GET",group:group);
Swagme.Add(url,async(evt)=>{
evt.ParseBody();
await action(evt);
},"POST");
},documentation,method:"POST",group:group);
}
public async Task DeleteList(ServerContext ctx)
{
@ -1326,15 +1375,6 @@ internal static class B64
if(ctx.QueryParams.TryGetFirst("id",out id))
{
string getinfostr;
bool getinfo=true;
if(ctx.QueryParams.TryGetFirst("getinfo",out getinfostr))
{
if(getinfostr == "false")
{
getinfo=false;
}
}
string confstr;
ChannelBellInfo conf=ChannelBellInfo.NotifyAndDownload;
if(ctx.QueryParams.TryGetFirst("conf",out confstr))
@ -1350,7 +1390,7 @@ internal static class B64
if(cid.HasValue)
{
await storage.SubscribeAsync(cid.Value,getinfo,conf);
await storage.SubscribeAsync(cid.Value,conf);
}else{
UserName? uname=UserName.TryParse(id);
await storage.SubscribeAsync(uname.Value,conf);
@ -1520,7 +1560,7 @@ internal static class B64
if(downloader != null)
{
mountableServer.Mount("/api/",new ApiV1Server(downloader));
mountableServer.Mount("/api/v2/",new ApiV2Server(downloader));
mountableServer.Mount("/api/v2/",new ApiV2Server(downloader).Swagme);
}
mountableServer.Mount("/api/v2/Extensions/",ExtensionsServer);
mountableServer.Mount("/api/Storage/",new ApiStorage(baseCtl));

View File

@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Tesses.WebServer" Version="1.0.3.8" />
<PackageReference Include="Tesses.WebServer.Swagme" Version="1.0.1" />
</ItemGroup>
<PropertyGroup>
@ -15,9 +16,9 @@
<PackageId>Tesses.YouTubeDownloader.Server</PackageId>
<Author>Mike Nolan</Author>
<Company>Tesses</Company>
<Version>1.1.7</Version>
<AssemblyVersion>1.1.7</AssemblyVersion>
<FileVersion>1.1.7</FileVersion>
<Version>1.1.8</Version>
<AssemblyVersion>1.1.8</AssemblyVersion>
<FileVersion>1.1.8</FileVersion>
<Description>Adds WebServer to TYTD</Description>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>