refactor(starlight): simplify episode handling using EpisodeTpl class

- Remove manual JSON and HTML building logic in Controller.cs
- Eliminate unused image handling and selection logic
- Remove redundant JsonSerializerOptions and helper methods
- Update Episode model to remove Image property
- Simplify image selection in StarLightInvoke.cs to use single source
This commit is contained in:
baliasnyifeliks 2026-01-14 10:03:13 +02:00
parent b7cd641da6
commit 1525e163dd
3 changed files with 5 additions and 120 deletions

View File

@ -1,9 +1,6 @@
using System; using System;
using System.Collections.Generic; 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;
@ -84,7 +81,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 episodeItems = new List<(string name, object json, string image)>(); var episode_tpl = new EpisodeTpl();
int index = 1; int index = 1;
foreach (var ep in episodes) foreach (var ep in episodes)
{ {
@ -93,30 +90,11 @@ 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)}";
string image = string.IsNullOrEmpty(ep.Image) ? null : ep.Image; episode_tpl.Append(episodeName, title ?? original_title, (s + 1).ToString(), index.ToString("D2"), accsArgs(callUrl), "call");
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, image));
index++; index++;
} }
if (episodeItems.Count == 0) return rjson ? Content(episode_tpl.ToJson(), "application/json; charset=utf-8") : Content(episode_tpl.ToHtml(), "text/html; charset=utf-8");
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
{ {
@ -156,82 +134,5 @@ namespace StarLight.Controllers
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, string image)> 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("'>");
if (!string.IsNullOrEmpty(item.image))
{
html.Append("<div class=\"videos__item-imgbox videos__movie-imgbox\" style=\"background-image:url('");
HtmlEncode(item.image, html);
html.Append("')\"></div>");
}
else
{
html.Append("<div class=\"videos__item-imgbox videos__movie-imgbox\"></div>");
}
html.Append("<div class=\"videos__item-title\">");
HtmlEncode(item.name, html);
html.Append("</div></div>");
firstjson = false;
}
html.Append("</div>");
return html.ToString();
}
private static void HtmlEncode(string value, StringBuilder sb)
{
if (string.IsNullOrEmpty(value))
return;
foreach (var c in value)
{
switch (c)
{
case '<': sb.Append("&lt;"); break;
case '>': sb.Append("&gt;"); break;
case '&': sb.Append("&amp;"); break;
case '"': sb.Append("&quot;"); break;
case '\'': sb.Append("&#39;"); break;
default: sb.Append(c); break;
}
}
}
} }
} }

View File

@ -24,7 +24,6 @@ namespace StarLight.Models
public string VideoSlug { get; set; } public string VideoSlug { get; set; }
public string Date { get; set; } public string Date { get; set; }
public string SeasonSlug { get; set; } public string SeasonSlug { get; set; }
public string Image { get; set; }
} }
public class ProjectInfo public class ProjectInfo

View File

@ -119,7 +119,7 @@ namespace StarLight
{ {
Title = root.TryGetProperty("title", out var titleProp) ? titleProp.GetString() : null, Title = root.TryGetProperty("title", out var titleProp) ? titleProp.GetString() : null,
Description = root.TryGetProperty("description", out var descProp) ? descProp.GetString() : null, Description = root.TryGetProperty("description", out var descProp) ? descProp.GetString() : null,
Poster = NormalizeImage(SelectImage(root, "imageMob", "imageTab", "image", "logoImage")), Poster = NormalizeImage(root.TryGetProperty("image", out var imageProp) ? imageProp.GetString() : null),
Hash = root.TryGetProperty("hash", out var hashProp) ? hashProp.GetString() : null, Hash = root.TryGetProperty("hash", out var hashProp) ? hashProp.GetString() : null,
Type = root.TryGetProperty("typeSlug", out var typeProp) ? typeProp.GetString() : null, Type = root.TryGetProperty("typeSlug", out var typeProp) ? typeProp.GetString() : null,
Channel = root.TryGetProperty("channelTitle", out var channelProp) ? channelProp.GetString() : null Channel = root.TryGetProperty("channelTitle", out var channelProp) ? channelProp.GetString() : null
@ -143,8 +143,7 @@ namespace StarLight
Hash = item.TryGetProperty("hash", out var eHash) ? eHash.GetString() : null, Hash = item.TryGetProperty("hash", out var eHash) ? eHash.GetString() : null,
VideoSlug = item.TryGetProperty("videoSlug", out var eSlug) ? eSlug.GetString() : null, VideoSlug = item.TryGetProperty("videoSlug", out var eSlug) ? eSlug.GetString() : null,
Date = item.TryGetProperty("dateOfBroadcast", out var eDate) ? eDate.GetString() : (item.TryGetProperty("timeUploadVideo", out var eDate2) ? eDate2.GetString() : null), Date = item.TryGetProperty("dateOfBroadcast", out var eDate) ? eDate.GetString() : (item.TryGetProperty("timeUploadVideo", out var eDate2) ? eDate2.GetString() : null),
SeasonSlug = seasonSlug, SeasonSlug = seasonSlug
Image = NormalizeImage(SelectImage(item, "imageMob", "image"))
}); });
} }
} }
@ -241,20 +240,6 @@ namespace StarLight
return $"{_init.host}{path}"; return $"{_init.host}{path}";
} }
private static string SelectImage(JsonElement element, params string[] keys)
{
foreach (var key in keys)
{
if (element.TryGetProperty(key, out var prop))
{
var value = prop.GetString();
if (!string.IsNullOrEmpty(value))
return value;
}
}
return null;
}
public static TimeSpan cacheTime(int multiaccess, int home = 5, int mikrotik = 2, OnlinesSettings init = null, int rhub = -1) public static TimeSpan cacheTime(int multiaccess, int home = 5, int mikrotik = 2, OnlinesSettings init = null, int rhub = -1)
{ {