TYTD w api

This commit is contained in:
Michael Nolan 2021-12-08 01:09:16 -06:00
parent d5cc6be2a5
commit dc99b89fc3
13 changed files with 87 additions and 11 deletions

View File

@ -98,6 +98,7 @@ namespace TYTD
ApiLoader.Init();
/* Other */
Route.Add("/", (HttpAction)Index);
Route.Add("/", (HttpAction)Extensions);
Route.Add("/{Path}", (HttpAction)RootPath);
Console.CancelKeyPress += (sender, e) => { ApiLoader.Dispose();var date = DateTime.Now.ToString("yyyyMMdd_HHmmss");File.WriteAllText(Path.Combine("config","queues-close",$"{date}.json"), Downloader.GetQueue()); return; };
@ -452,6 +453,10 @@ namespace TYTD
{
rp.AsFile(rq, Path.Combine(webSitePath, "index.html"));
}
public static void Extensions(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
rp.AsText(ApiLoader.Page);
}
public static void RootPath(HttpListenerRequest rq, HttpListenerResponse rp, Dictionary<string, string> args)
{
string path = Path.Combine(webSitePath, args["Path"].Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries)[0]);

View File

@ -5,15 +5,14 @@ using System.Threading;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Text;
using SimpleHttp;
namespace TYTD
{
public abstract class Api : IDisposable
{
public Api()
{
}
public virtual void OnStart()
{
Console.WriteLine("Extension Loaded");
@ -21,6 +20,7 @@ namespace TYTD
internal string storage;
protected string StorageLocation { get { return storage; } }
public virtual string Name { get { return Path.GetFileName(StorageLocation); } }
private System.Timers.Timer t = null;
protected void SetTimer(TimeSpan timespan)
{
@ -37,6 +37,10 @@ namespace TYTD
t.Start();
}
public bool TimerEnabled { get { if (t != null) { return t.Enabled; } else { return false; } } set { if (t != null) { t.Enabled = value; } } }
public virtual IEnumerable<KeyValuePair<string, string>> GetUrls()
{
yield return new KeyValuePair<string, string>("Home",$"api/Extensions/{Name}");
}
protected virtual void TimerElapsed()
{
Console.WriteLine("Event Fired");
@ -87,6 +91,7 @@ namespace TYTD
public static class ApiLoader
{
public static string Page { get; private set; }
internal static List<Api> apis = new List<Api>();
internal static void DownloadStarted(object sender,DownloadStartEventArgs evt)
@ -110,10 +115,35 @@ namespace TYTD
api.Dispose();
}
}
public static void GenerateTr(Api api,StringBuilder b)
{
string data = "";
string root = Path.Combine(Environment.CurrentDirectory, "config");
string fallbackIcon = Path.Combine(root, "default_icon.png");
string icon = Path.Combine(root, "apiicons", api.Name + ".png");
string icon2 = File.Exists(icon) ? icon : fallbackIcon;
if (File.Exists(icon2))
{
data = Convert.ToBase64String(File.ReadAllBytes(icon2));
}
b.Append($"<tr><td><h3>{api.Name}</h3><img src=\"data:image/png;base64,{data}\" alt=\"{api.Name}\"></td><td>");
foreach (var link in api.GetUrls())
{
b.Append($"<a href=\"{link.Value}\">{link.Key}</a><br>");
}
b.Append("</td></tr>");
Dictionary<string, string> templating = new Dictionary<string, string>();
templating.Add("Items", b.ToString());
Page= Templating.RenderFile(Path.Combine("WebSite", "extensions.html"),templating);
}
internal static void Load(string dll,string confpath)
{
var asm=Assembly.LoadFrom(dll);
StringBuilder b = new StringBuilder();
foreach (var item in asm.GetTypes())
{
if(typeof(Api).IsAssignableFrom(item))
@ -121,10 +151,11 @@ namespace TYTD
Api api = (Api)Activator.CreateInstance(item);
api.storage = confpath;
apis.Add(api);
Thread thrd = new Thread(new ThreadStart(api.OnStart));
thrd.Start();
api.OnStart();
GenerateTr(api, b);
}
}
}
public static void Init()
{

View File

@ -76,6 +76,34 @@
<Reference Include="CookiesTxtParser">
<HintPath>..\packages\CookiesTxtParser.1.0.1\lib\netstandard2.0\CookiesTxtParser.dll</HintPath>
</Reference>
<Reference Include="MimeTypesMap">
<HintPath>..\packages\MimeTypesMap.1.0.2\lib\net45\MimeTypesMap.dll</HintPath>
</Reference>
<Reference Include="System.IO">
<HintPath>..\packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
</Reference>
<Reference Include="System.Runtime">
<HintPath>..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Security.Cryptography.Encoding">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http">
<HintPath>..\packages\System.Net.Http.4.3.3\lib\net46\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="SimpleHTTP">
<HintPath>..\packages\Simple-HTTP.1.0.6\lib\net47\SimpleHTTP.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MyClass.cs" />

Binary file not shown.

View File

@ -1 +1 @@
3eb6dde99f6eaaac2f2eb82e830940474adc1a21
43d1539c7b8a6555a9e7a98302a38acdd2e39a06

View File

@ -126,3 +126,6 @@
/home/ddlovato/tytd/site/TYTD.Api/obj/Release/TYTD.Api.csproj.CopyComplete
/home/ddlovato/tytd/site/TYTD.Api/obj/Release/TYTD.Api.dll
/home/ddlovato/tytd/site/TYTD.Api/bin/Release/System.Text.Encoding.CodePages.xml
/home/ddlovato/tytd/site/TYTD.Api/bin/Release/MimeTypesMap.dll
/home/ddlovato/tytd/site/TYTD.Api/bin/Release/SimpleHTTP.dll
/home/ddlovato/tytd/site/TYTD.Api/bin/Release/SimpleHTTP.xml

Binary file not shown.

View File

@ -4,11 +4,20 @@
<package id="AsyncEnumerator" version="4.0.2" targetFramework="net47" />
<package id="CookiesTxtParser" version="1.0.1" targetFramework="net47" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net47" />
<package id="MimeTypesMap" version="1.0.2" targetFramework="net47" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net47" />
<package id="Simple-HTTP" version="1.0.6" targetFramework="net47" />
<package id="System.Buffers" version="4.5.1" targetFramework="net47" />
<package id="System.IO" version="4.3.0" targetFramework="net47" />
<package id="System.Memory" version="4.5.4" targetFramework="net47" />
<package id="System.Net.Http" version="4.3.3" targetFramework="net47" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net47" />
<package id="System.Runtime" version="4.3.0" targetFramework="net47" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net47" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net47" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net47" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net47" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net47" />
<package id="System.Text.Encoding.CodePages" version="6.0.0" targetFramework="net47" />
<package id="System.Text.Encodings.Web" version="6.0.0" targetFramework="net47" />
<package id="System.Text.Json" version="6.0.0" targetFramework="net47" />

Binary file not shown.

Binary file not shown.