blob: c7c415dfec0d28d27e55d660f8da0389b94ef503 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
using System;
using System.Collections.Concurrent;
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;
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);
}
private ConcurrentDictionary<string, SemaphoreSlim> cacheLock = new();
private ConcurrentDictionary<string, JsonNode> cache = new();
public async Task<JsonNode> GetMediuxMetadata(string url)
{
var semaphore = cacheLock.GetOrAdd(url, ignored => new SemaphoreSlim(1));
try
{
await semaphore.WaitAsync().ConfigureAwait(false);
if (cache.TryGetValue(url, out var data))
{
Plugin.Logger.LogInformation("Loading cached data from {Url}", url);
return data;
}
Plugin.Logger.LogInformation("Loading data from {Url}", url);
var text = await GetString(url).ConfigureAwait(false);
var node = ExtractJsonNodes(text).First();
cache[url] = node;
return node;
}
finally
{
semaphore.Release();
}
}
public async Task<HttpResponseMessage> DownloadFile(string url)
{
return await httpClientFactory.GetAsync(url).ConfigureAwait(false);
}
}
|