summaryrefslogtreecommitdiff
path: root/MediuxDownloader.cs
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-08-11 21:47:46 +0200
committerLinnea Gräf <nea@nea.moe>2024-08-11 21:47:46 +0200
commitf60e3645e18a7f590dc913d0f555fcb82f072d07 (patch)
tree3e74108cecc54086c5c5241ad9751efa549fd298 /MediuxDownloader.cs
downloadJCoverXtremePro-f60e3645e18a7f590dc913d0f555fcb82f072d07.tar.gz
JCoverXtremePro-f60e3645e18a7f590dc913d0f555fcb82f072d07.tar.bz2
JCoverXtremePro-f60e3645e18a7f590dc913d0f555fcb82f072d07.zip
init
Diffstat (limited to 'MediuxDownloader.cs')
-rw-r--r--MediuxDownloader.cs85
1 files changed, 85 insertions, 0 deletions
diff --git a/MediuxDownloader.cs b/MediuxDownloader.cs
new file mode 100644
index 0000000..a027773
--- /dev/null
+++ b/MediuxDownloader.cs
@@ -0,0 +1,85 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.Http;
+using System.Text.Json;
+using System.Text.Json.Nodes;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+using Microsoft.Extensions.Logging;
+
+namespace Jellyfin.Plugin.JCoverXtremePro;
+
+public class MediuxDownloader
+{
+ public static MediuxDownloader instance;
+
+ private Regex contentRegex = new(@"<script[^>]*>self\.__next_f\.push(.*?)</script>");
+ private readonly HttpClient httpClientFactory;
+ private string sentinel = "date";
+
+ public MediuxDownloader(IHttpClientFactory httpClientFactory)
+ {
+ this.httpClientFactory = httpClientFactory.CreateClient("MediuxDownloader");
+ }
+
+ private List<JsonNode> ExtractJsonNodes(string httpText)
+ {
+ List<JsonNode> list = new();
+ foreach (Match match in contentRegex.Matches(httpText))
+ {
+ var pushArg = match.Groups[1].Value;
+ var strippedString = StripPushArg(pushArg);
+ if (!strippedString.Contains(sentinel))
+ {
+ Plugin.Logger.LogTrace("Ignoring chunk without sentinel {Sentinel}: {Chunk}", sentinel, strippedString);
+ continue;
+ }
+
+ list.Add(ParseStrippedJsonChunk(strippedString));
+ }
+
+ if (list.Count != 1)
+ {
+ Plugin.Logger.LogError("Found too many or too few chunks: {0}", list);
+ }
+
+ return list;
+ }
+
+ private JsonNode ParseStrippedJsonChunk(string text)
+ {
+ return JsonSerializer.Deserialize<JsonArray>(text.Substring(text.IndexOf(':') + 1))[3];
+ }
+
+ private string StripPushArg(string text)
+ {
+ var stringStart = text.IndexOf('"');
+ var stringEnd = text.LastIndexOf('"');
+ if (stringStart == stringEnd || stringStart == -1)
+ {
+ return "";
+ }
+
+ // TODO: 1 is regular data, 3 is base64 partial data
+ return JsonSerializer.Deserialize<string>(text.Substring(stringStart, stringEnd + 1 - stringStart)) ?? "";
+ }
+
+ private async Task<string> GetString(string url)
+ {
+ return await (await httpClientFactory.GetAsync(url).ConfigureAwait(false))
+ .Content.ReadAsStringAsync().ConfigureAwait(false);
+ }
+
+ public async Task<JsonNode> GetMediuxMetadata(string url)
+ {
+ Plugin.Logger.LogInformation("Loading data from {Url}", url);
+ var text = await GetString(url).ConfigureAwait(false);
+ return ExtractJsonNodes(text).First();
+ }
+
+ public async Task<HttpResponseMessage> DownloadFile(string url)
+ {
+ return await httpClientFactory.GetAsync(url).ConfigureAwait(false);
+ }
+} \ No newline at end of file