2023-05-18 16:42:00 +02:00
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Net;
|
2023-05-18 12:26:15 +02:00
|
|
|
|
|
|
|
|
|
namespace Tranga;
|
|
|
|
|
|
|
|
|
|
public abstract class Connector
|
|
|
|
|
{
|
2023-05-18 16:40:11 +02:00
|
|
|
|
internal abstract string downloadLocation { get; }
|
2023-05-18 12:26:15 +02:00
|
|
|
|
public abstract string name { get; }
|
2023-05-18 16:41:14 +02:00
|
|
|
|
public abstract Publication[] GetPublications(string publicationTitle = "");
|
2023-05-18 15:47:48 +02:00
|
|
|
|
public abstract Chapter[] GetChapters(Publication publication);
|
2023-05-18 16:21:02 +02:00
|
|
|
|
public abstract void DownloadChapter(Publication publication, Chapter chapter); //where to?
|
2023-05-18 16:42:00 +02:00
|
|
|
|
internal abstract void DownloadImage(string url, string path);
|
2023-05-18 16:21:02 +02:00
|
|
|
|
|
2023-05-18 17:21:06 +02:00
|
|
|
|
internal void DownloadChapter(string[] imageUrls, string outputFolderPath)
|
2023-05-18 16:21:02 +02:00
|
|
|
|
{
|
2023-05-18 16:42:00 +02:00
|
|
|
|
string tempFolder = Path.GetTempFileName();
|
|
|
|
|
File.Delete(tempFolder);
|
|
|
|
|
Directory.CreateDirectory(tempFolder);
|
2023-05-18 17:21:06 +02:00
|
|
|
|
|
|
|
|
|
int chapter = 0;
|
|
|
|
|
foreach(string imageUrl in imageUrls)
|
|
|
|
|
DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}"));
|
2023-05-18 16:21:02 +02:00
|
|
|
|
|
2023-05-18 17:21:06 +02:00
|
|
|
|
ZipFile.CreateFromDirectory(tempFolder, $"{outputFolderPath}.cbz");
|
2023-05-18 16:21:02 +02:00
|
|
|
|
}
|
2023-05-18 12:26:15 +02:00
|
|
|
|
|
|
|
|
|
internal class DownloadClient
|
|
|
|
|
{
|
2023-05-18 17:41:44 +02:00
|
|
|
|
private readonly TimeSpan _requestSpeed;
|
|
|
|
|
private DateTime _lastRequest;
|
2023-05-18 12:26:15 +02:00
|
|
|
|
static readonly HttpClient client = new HttpClient();
|
2023-05-18 17:18:41 +02:00
|
|
|
|
|
|
|
|
|
public DownloadClient(uint delay)
|
|
|
|
|
{
|
2023-05-18 17:41:44 +02:00
|
|
|
|
_requestSpeed = TimeSpan.FromMilliseconds(delay);
|
|
|
|
|
_lastRequest = DateTime.Now.Subtract(_requestSpeed);
|
2023-05-18 17:18:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RequestResult MakeRequest(string url)
|
2023-05-18 12:26:15 +02:00
|
|
|
|
{
|
2023-05-18 17:41:44 +02:00
|
|
|
|
while((DateTime.Now - _lastRequest) < _requestSpeed)
|
2023-05-18 17:18:41 +02:00
|
|
|
|
Thread.Sleep(10);
|
2023-05-18 17:41:44 +02:00
|
|
|
|
_lastRequest = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, url);
|
2023-05-18 12:26:15 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|