Cleanup
This commit is contained in:
parent
babf15072d
commit
88e5379e25
@ -1,8 +1,9 @@
|
|||||||
// See https://aka.ms/new-console-template for more information
|
using Tranga;
|
||||||
using Tranga;
|
|
||||||
using Tranga.Connectors;
|
using Tranga.Connectors;
|
||||||
|
|
||||||
public class Program
|
namespace Tranga_CLI;
|
||||||
|
|
||||||
|
public static class Tranga_Cli
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
@ -15,7 +15,7 @@ public abstract class Connector
|
|||||||
public abstract Publication[] GetPublications(string publicationTitle = "");
|
public abstract Publication[] GetPublications(string publicationTitle = "");
|
||||||
public abstract Chapter[] GetChapters(Publication publication, string language = "");
|
public abstract Chapter[] GetChapters(Publication publication, string language = "");
|
||||||
public abstract void DownloadChapter(Publication publication, Chapter chapter); //where to?
|
public abstract void DownloadChapter(Publication publication, Chapter chapter); //where to?
|
||||||
internal abstract void DownloadImage(string url, string path);
|
protected abstract void DownloadImage(string url, string path);
|
||||||
|
|
||||||
internal void DownloadChapter(string[] imageUrls, string outputFilePath)
|
internal void DownloadChapter(string[] imageUrls, string outputFilePath)
|
||||||
{
|
{
|
||||||
@ -45,7 +45,7 @@ public abstract class Connector
|
|||||||
{
|
{
|
||||||
private readonly TimeSpan _requestSpeed;
|
private readonly TimeSpan _requestSpeed;
|
||||||
private DateTime _lastRequest;
|
private DateTime _lastRequest;
|
||||||
static readonly HttpClient client = new HttpClient();
|
private static readonly HttpClient Client = new();
|
||||||
|
|
||||||
public DownloadClient(uint delay)
|
public DownloadClient(uint delay)
|
||||||
{
|
{
|
||||||
@ -60,7 +60,7 @@ public abstract class Connector
|
|||||||
_lastRequest = DateTime.Now;
|
_lastRequest = DateTime.Now;
|
||||||
|
|
||||||
HttpRequestMessage requestMessage = new(HttpMethod.Get, url);
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, url);
|
||||||
HttpResponseMessage response = client.Send(requestMessage);
|
HttpResponseMessage response = Client.Send(requestMessage);
|
||||||
Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null;
|
Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null;
|
||||||
return new RequestResult(response.StatusCode, resultString);
|
return new RequestResult(response.StatusCode, resultString);
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ namespace Tranga.Connectors;
|
|||||||
public class MangaDex : Connector
|
public class MangaDex : Connector
|
||||||
{
|
{
|
||||||
public override string name { get; }
|
public override string name { get; }
|
||||||
private DownloadClient _downloadClient = new (750);
|
private readonly DownloadClient _downloadClient = new (750);
|
||||||
|
|
||||||
public MangaDex(string downloadLocation) : base(downloadLocation)
|
public MangaDex(string downloadLocation) : base(downloadLocation)
|
||||||
{
|
{
|
||||||
@ -31,9 +31,10 @@ public class MangaDex : Connector
|
|||||||
|
|
||||||
total = result["total"]!.GetValue<int>();
|
total = result["total"]!.GetValue<int>();
|
||||||
JsonArray mangaInResult = result["data"]!.AsArray();
|
JsonArray mangaInResult = result["data"]!.AsArray();
|
||||||
foreach (JsonObject manga in mangaInResult)
|
foreach (JsonNode? mangeNode in mangaInResult)
|
||||||
{
|
{
|
||||||
JsonObject attributes = manga["attributes"].AsObject();
|
JsonObject manga = (JsonObject)mangeNode!;
|
||||||
|
JsonObject attributes = manga["attributes"]!.AsObject();
|
||||||
|
|
||||||
string title = attributes["title"]!.AsObject().ContainsKey("en") && attributes["title"]!["en"] is not null
|
string title = attributes["title"]!.AsObject().ContainsKey("en") && attributes["title"]!["en"] is not null
|
||||||
? attributes["title"]!["en"]!.GetValue<string>()
|
? attributes["title"]!["en"]!.GetValue<string>()
|
||||||
@ -46,19 +47,21 @@ public class MangaDex : Connector
|
|||||||
JsonArray altTitlesObject = attributes["altTitles"]!.AsArray();
|
JsonArray altTitlesObject = attributes["altTitles"]!.AsArray();
|
||||||
string[,] altTitles = new string[altTitlesObject.Count, 2];
|
string[,] altTitles = new string[altTitlesObject.Count, 2];
|
||||||
int titleIndex = 0;
|
int titleIndex = 0;
|
||||||
foreach (JsonObject altTitleObject in altTitlesObject)
|
foreach (JsonNode? altTitleNode in altTitlesObject)
|
||||||
{
|
{
|
||||||
string key = ((IDictionary<string, JsonNode?>)altTitleObject!).Keys.ToArray()[0];
|
JsonObject altTitleObject = (JsonObject)altTitleNode!;
|
||||||
|
string key = ((IDictionary<string, JsonNode?>)altTitleObject).Keys.ToArray()[0];
|
||||||
altTitles[titleIndex, 0] = key;
|
altTitles[titleIndex, 0] = key;
|
||||||
altTitles[titleIndex++, 1] = altTitleObject[key]!.GetValue<string>();
|
altTitles[titleIndex++, 1] = altTitleObject[key]!.GetValue<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray tagsObject = attributes["tags"]!.AsArray();
|
JsonArray tagsObject = attributes["tags"]!.AsArray();
|
||||||
HashSet<string> tags = new();
|
HashSet<string> tags = new();
|
||||||
foreach (JsonObject tagObject in tagsObject)
|
foreach (JsonNode? tagNode in tagsObject)
|
||||||
{
|
{
|
||||||
if(tagObject!["attributes"]!["name"]!.AsObject().ContainsKey("en"))
|
JsonObject tagObject = (JsonObject)tagNode!;
|
||||||
tags.Add(tagObject!["attributes"]!["name"]!["en"]!.GetValue<string>());
|
if(tagObject["attributes"]!["name"]!.AsObject().ContainsKey("en"))
|
||||||
|
tags.Add(tagObject["attributes"]!["name"]!["en"]!.GetValue<string>());
|
||||||
}
|
}
|
||||||
|
|
||||||
string? poster = null;
|
string? poster = null;
|
||||||
@ -130,9 +133,10 @@ public class MangaDex : Connector
|
|||||||
|
|
||||||
total = result["total"]!.GetValue<int>();
|
total = result["total"]!.GetValue<int>();
|
||||||
JsonArray chaptersInResult = result["data"]!.AsArray();
|
JsonArray chaptersInResult = result["data"]!.AsArray();
|
||||||
foreach (JsonObject chapter in chaptersInResult)
|
foreach (JsonNode? jsonNode in chaptersInResult)
|
||||||
{
|
{
|
||||||
JsonObject attributes = chapter!["attributes"]!.AsObject();
|
JsonObject chapter = (JsonObject)jsonNode!;
|
||||||
|
JsonObject attributes = chapter["attributes"]!.AsObject();
|
||||||
string chapterId = chapter["id"]!.GetValue<string>();
|
string chapterId = chapter["id"]!.GetValue<string>();
|
||||||
|
|
||||||
string? title = attributes.ContainsKey("title") && attributes["title"] is not null
|
string? title = attributes.ContainsKey("title") && attributes["title"] is not null
|
||||||
@ -154,7 +158,7 @@ public class MangaDex : Connector
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NumberFormatInfo chapterNumberFormatInfo = new NumberFormatInfo()
|
NumberFormatInfo chapterNumberFormatInfo = new()
|
||||||
{
|
{
|
||||||
NumberDecimalSeparator = "."
|
NumberDecimalSeparator = "."
|
||||||
};
|
};
|
||||||
@ -173,7 +177,7 @@ public class MangaDex : Connector
|
|||||||
string hash = result["chapter"]!["hash"]!.GetValue<string>();
|
string hash = result["chapter"]!["hash"]!.GetValue<string>();
|
||||||
JsonArray imageFileNames = result["chapter"]!["data"]!.AsArray();
|
JsonArray imageFileNames = result["chapter"]!["data"]!.AsArray();
|
||||||
HashSet<string> imageUrls = new();
|
HashSet<string> imageUrls = new();
|
||||||
foreach (JsonNode image in imageFileNames)
|
foreach (JsonNode? image in imageFileNames)
|
||||||
imageUrls.Add($"{baseUrl}/data/{hash}/{image!.GetValue<string>()}");
|
imageUrls.Add($"{baseUrl}/data/{hash}/{image!.GetValue<string>()}");
|
||||||
|
|
||||||
string seriesFolder = string.Concat(publication.sortName.Split(Path.GetInvalidPathChars()));
|
string seriesFolder = string.Concat(publication.sortName.Split(Path.GetInvalidPathChars()));
|
||||||
@ -181,7 +185,7 @@ public class MangaDex : Connector
|
|||||||
DownloadChapter(imageUrls.ToArray(), Path.Join(downloadLocation, seriesFolder, chapter.relativeFilePath));
|
DownloadChapter(imageUrls.ToArray(), Path.Join(downloadLocation, seriesFolder, chapter.relativeFilePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void DownloadImage(string url, string path)
|
protected override void DownloadImage(string url, string path)
|
||||||
{
|
{
|
||||||
DownloadClient.RequestResult requestResult = _downloadClient.MakeRequest(url);
|
DownloadClient.RequestResult requestResult = _downloadClient.MakeRequest(url);
|
||||||
byte[] buffer = new byte[requestResult.result.Length];
|
byte[] buffer = new byte[requestResult.result.Length];
|
||||||
|
@ -6,7 +6,7 @@ public struct Publication
|
|||||||
public string[,] altTitles { get; }
|
public string[,] altTitles { get; }
|
||||||
public string? description { get; }
|
public string? description { get; }
|
||||||
public string[] tags { get; }
|
public string[] tags { get; }
|
||||||
public string? posterUrl { get; } //maybe there is a better way?
|
public string? posterUrl { get; }
|
||||||
public string[,]? links { get; }
|
public string[,]? links { get; }
|
||||||
public int? year { get; }
|
public int? year { get; }
|
||||||
public string? originalLanguage { get; }
|
public string? originalLanguage { get; }
|
||||||
|
Loading…
Reference in New Issue
Block a user