feat(starlight): refactor episode handling to use structured JSON data

- Replace EpisodeTpl with a new structured approach using List<(string name, object json)>
- Add BuildEpisodeJson and BuildEpisodeHtml methods for consistent output generation
- Implement proper JSON serialization with null value handling
- Add language check in OnlineApi to only process Ukrainian content
- Improve episode display with better title formatting and image handling
This commit is contained in:
baliasnyifeliks 2026-01-14 09:39:18 +02:00
parent 58bb8d194c
commit 90fe957674
2 changed files with 78 additions and 3 deletions

View File

@ -1,5 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web; using System.Web;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -80,7 +84,7 @@ namespace StarLight.Controllers
if (episodes == null || episodes.Count == 0) if (episodes == null || episodes.Count == 0)
return OnError("starlight", proxyManager); return OnError("starlight", proxyManager);
var episode_tpl = new EpisodeTpl(); var episodeItems = new List<(string name, object json)>();
int index = 1; int index = 1;
foreach (var ep in episodes) foreach (var ep in episodes)
{ {
@ -89,11 +93,30 @@ namespace StarLight.Controllers
string episodeName = string.IsNullOrEmpty(ep.Title) ? $"Епізод {index}" : ep.Title; string episodeName = string.IsNullOrEmpty(ep.Title) ? $"Епізод {index}" : ep.Title;
string callUrl = $"{host}/starlight/play?hash={HttpUtility.UrlEncode(ep.Hash)}&title={HttpUtility.UrlEncode(title ?? original_title)}"; string callUrl = $"{host}/starlight/play?hash={HttpUtility.UrlEncode(ep.Hash)}&title={HttpUtility.UrlEncode(title ?? original_title)}";
episode_tpl.Append(episodeName, title ?? original_title, (s + 1).ToString(), index.ToString("D2"), accsArgs(callUrl), "call", img: ep.Image); string image = string.IsNullOrEmpty(ep.Image) ? null : ep.Image;
string displayTitle = $"{title ?? original_title} ({index} серия)";
var jsonItem = new
{
method = "call",
url = accsArgs(callUrl),
s = (short)(s + 1),
e = (short)index,
name = episodeName,
title = displayTitle,
img = image
};
episodeItems.Add((episodeName, jsonItem));
index++; index++;
} }
return rjson ? Content(episode_tpl.ToJson(), "application/json; charset=utf-8") : Content(episode_tpl.ToHtml(), "text/html; charset=utf-8"); if (episodeItems.Count == 0)
return OnError("starlight", proxyManager);
return rjson
? Content(BuildEpisodeJson(episodeItems.Select(i => i.json)), "application/json; charset=utf-8")
: Content(BuildEpisodeHtml(episodeItems), "text/html; charset=utf-8");
} }
else else
{ {
@ -132,5 +155,53 @@ namespace StarLight.Controllers
string jsonResult = $"{{\"method\":\"play\",\"url\":\"{streamUrl}\",\"title\":\"{title ?? result.Name ?? ""}\"}}"; string jsonResult = $"{{\"method\":\"play\",\"url\":\"{streamUrl}\",\"title\":\"{title ?? result.Name ?? ""}\"}}";
return Content(jsonResult, "application/json; charset=utf-8"); return Content(jsonResult, "application/json; charset=utf-8");
} }
private static readonly JsonSerializerOptions EpisodeJsonOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
private static string BuildEpisodeJson(IEnumerable<object> items)
{
var payload = new
{
type = "episode",
data = items
};
return JsonSerializer.Serialize(payload, EpisodeJsonOptions);
}
private static string BuildEpisodeHtml(List<(string name, object json)> items)
{
var html = new StringBuilder();
bool firstjson = true;
html.Append("<div class=\"videos__line\">");
foreach (var item in items)
{
html.Append("<div class=\"videos__item videos__movie selector ");
if (firstjson)
html.Append("focused");
html.Append("\" ");
html.Append("media=\"\" ");
html.Append("data-json='");
html.Append(JsonSerializer.Serialize(item.json, EpisodeJsonOptions));
html.Append("'>");
html.Append("<div class=\"videos__item-imgbox videos__movie-imgbox\"></div><div class=\"videos__item-title\">");
UtilsTpl.HtmlEncode(item.name, html);
html.Append("</div></div>");
firstjson = false;
}
html.Append("</div>");
return html.ToString();
}
} }
} }

View File

@ -1,4 +1,5 @@
using Shared.Models.Base; using Shared.Models.Base;
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace StarLight namespace StarLight
@ -9,6 +10,9 @@ namespace StarLight
{ {
var online = new List<(string name, string url, string plugin, int index)>(); var online = new List<(string name, string url, string plugin, int index)>();
if (!string.Equals(original_language, "uk", StringComparison.OrdinalIgnoreCase))
return online;
var init = ModInit.StarLight; var init = ModInit.StarLight;
if (init.enable && !init.rip) if (init.enable && !init.rip)
{ {