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 18:51:19 +02:00
|
|
|
|
public Connector(string downloadLocation)
|
|
|
|
|
{
|
|
|
|
|
this.downloadLocation = downloadLocation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal 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 18:19:04 +02:00
|
|
|
|
public abstract Chapter[] GetChapters(Publication publication, string language = "");
|
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 18:43:22 +02:00
|
|
|
|
internal void DownloadChapter(string[] imageUrls, string outputFilePath)
|
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;
|
2023-05-18 17:42:02 +02:00
|
|
|
|
foreach (string imageUrl in imageUrls)
|
|
|
|
|
{
|
|
|
|
|
string[] split = imageUrl.Split('.');
|
|
|
|
|
string extension = split[split.Length - 1];
|
|
|
|
|
DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"));
|
|
|
|
|
}
|
2023-05-18 18:43:22 +02:00
|
|
|
|
|
|
|
|
|
string[] splitPath = outputFilePath.Split(Path.DirectorySeparatorChar);
|
|
|
|
|
string directoryPath = Path.Combine(splitPath.Take(splitPath.Length - 1).ToArray());
|
|
|
|
|
if (!Directory.Exists(directoryPath))
|
|
|
|
|
Directory.CreateDirectory(directoryPath);
|
|
|
|
|
|
|
|
|
|
string fullPath = $"{outputFilePath}.cbz";
|
|
|
|
|
File.Delete(fullPath);
|
|
|
|
|
ZipFile.CreateFromDirectory(tempFolder, fullPath);
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|