Moved files

Added DownloadClient to Connector
This commit is contained in:
glax
2023-05-18 12:26:15 +02:00
parent 45713b868d
commit d6ec91c896
8 changed files with 36 additions and 10 deletions

22
Tranga/Chapter.cs Normal file
View File

@ -0,0 +1,22 @@
namespace Tranga;
public struct Chapter
{
public Publication publication { get; }
public string name { get; }
public uint volumeNumber { get; }
public uint chapterNumber { get; }
public string summary { get; }
public string posterUrl { get; }//Better way?
public Chapter(Publication publication, string name, uint volumeNumber, uint chapterNumber, string summary,
string posterUrl)
{
this.publication = publication;
this.name = name;
this.volumeNumber = volumeNumber;
this.chapterNumber = chapterNumber;
this.summary = summary;
this.posterUrl = posterUrl;
}
}

35
Tranga/Connector.cs Normal file
View File

@ -0,0 +1,35 @@
using System.Net;
namespace Tranga;
public abstract class Connector
{
public abstract string name { get; }
public abstract bool GetPublications(out Publication[] publications);
public abstract bool GetChapters(Publication publication, out Chapter[] chapters);
public abstract bool DownloadChapter(Chapter chapter); //where to?
internal class DownloadClient
{
static readonly HttpClient client = new HttpClient();
public RequestResult GetPage(string url)
{
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
HttpResponseMessage response = client.Send(requestMessage);
Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null;
return new RequestResult(response.StatusCode, resultString);
}
public struct RequestResult
{
public HttpStatusCode statusCode { get; }
public Stream result { get; }
public RequestResult(HttpStatusCode statusCode, Stream result)
{
this.statusCode = statusCode;
this.result = result;
}
}
}
}

20
Tranga/Publication.cs Normal file
View File

@ -0,0 +1,20 @@
namespace Tranga;
public struct Publication
{
public string sortName { get; }
public string[] titles { get; }
public string description { get; }
public string[] tags { get; }
public string posterUrl { get; } //maybe there is a better way?
public Publication(string sortName, string description, string[] titles, string[] tags, string posterUrl)
{
this.sortName = sortName;
this.description = description;
this.titles = titles;
this.tags = tags;
this.posterUrl = posterUrl;
}
}

52
Tranga/TaskManager.cs Normal file
View File

@ -0,0 +1,52 @@
namespace Tranga;
public class TaskManager
{
private readonly Dictionary<Publication, TrangaTask> _allTasks;
private bool _continueRunning = true;
public TaskManager()
{
_allTasks = new Dictionary<Publication, TrangaTask>();
Thread taskChecker = new(TaskCheckerThread);
taskChecker.Start();
}
private void TaskCheckerThread()
{
while (_continueRunning)
{
foreach (TrangaTask task in _allTasks.Values.Where(tt => (DateTime.Now - tt.lastExecuted) > tt.reoccurrence))
{
if (!task.lastExecutedSuccessfully)
{
task.Abort();
//Add logging that task has failed
}
task.Execute();
}
Thread.Sleep(1000);
}
}
public bool PublicationAlreadyAdded(Publication publication)
{
throw new NotImplementedException();
//TODO fuzzy check publications
}
public Publication[] GetAddedPublications()
{
return _allTasks.Keys.ToArray();
}
public TrangaTask[] GetTasks()
{
return _allTasks.Values.ToArray();
}
public void Shutdown()
{
_continueRunning = false;
}
}

9
Tranga/Tranga.csproj Normal file
View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

49
Tranga/TrangaTask.cs Normal file
View File

@ -0,0 +1,49 @@
namespace Tranga;
public class TrangaTask
{
private readonly Action _taskAction;
private Task? _task;
public bool lastExecutedSuccessfully => _task is not null && _task.IsCompleted;
public TimeSpan reoccurrence { get; }
public DateTime lastExecuted { get; private set; }
public TrangaTask(Action taskAction, TimeSpan reoccurrence)
{
this._taskAction = taskAction;
this.reoccurrence = reoccurrence;
}
public void Abort()
{
if(_task is not null && !_task.IsCompleted)
_task.Dispose();
}
public void Execute()
{
lastExecuted = DateTime.Now;
_task = new (_taskAction);
_task.Start();
}
public static TrangaTask CreateDownloadChapterTask(Connector connector, Chapter chapter, TimeSpan reoccurrence)
{
void TaskAction()
{
connector.DownloadChapter(chapter);
}
return new TrangaTask(TaskAction, reoccurrence);
}
public static TrangaTask CreateUpdateChaptersTask(Connector connector, Publication publication, TimeSpan reoccurrence)
{
throw new NotImplementedException();
}
public static TrangaTask CreateUpdatePublicationsTask(Connector connector, TimeSpan reoccurrence)
{
throw new NotImplementedException();
}
}