mirror of
https://github.com/lampame/lampac-ukraine.git
synced 2026-04-16 17:32:20 +00:00
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:
parent
b7cd641da6
commit
1525e163dd
@ -1,9 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -84,7 +81,7 @@ namespace StarLight.Controllers
|
||||
if (episodes == null || episodes.Count == 0)
|
||||
return OnError("starlight", proxyManager);
|
||||
|
||||
var episodeItems = new List<(string name, object json, string image)>();
|
||||
var episode_tpl = new EpisodeTpl();
|
||||
int index = 1;
|
||||
foreach (var ep in episodes)
|
||||
{
|
||||
@ -93,30 +90,11 @@ namespace StarLight.Controllers
|
||||
|
||||
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 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, image));
|
||||
episode_tpl.Append(episodeName, title ?? original_title, (s + 1).ToString(), index.ToString("D2"), accsArgs(callUrl), "call");
|
||||
index++;
|
||||
}
|
||||
|
||||
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");
|
||||
return rjson ? Content(episode_tpl.ToJson(), "application/json; charset=utf-8") : Content(episode_tpl.ToHtml(), "text/html; charset=utf-8");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -156,82 +134,5 @@ namespace StarLight.Controllers
|
||||
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("<"); break;
|
||||
case '>': sb.Append(">"); break;
|
||||
case '&': sb.Append("&"); break;
|
||||
case '"': sb.Append("""); break;
|
||||
case '\'': sb.Append("'"); break;
|
||||
default: sb.Append(c); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,7 +24,6 @@ namespace StarLight.Models
|
||||
public string VideoSlug { get; set; }
|
||||
public string Date { get; set; }
|
||||
public string SeasonSlug { get; set; }
|
||||
public string Image { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectInfo
|
||||
|
||||
@ -119,7 +119,7 @@ namespace StarLight
|
||||
{
|
||||
Title = root.TryGetProperty("title", out var titleProp) ? titleProp.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,
|
||||
Type = root.TryGetProperty("typeSlug", out var typeProp) ? typeProp.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,
|
||||
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),
|
||||
SeasonSlug = seasonSlug,
|
||||
Image = NormalizeImage(SelectImage(item, "imageMob", "image"))
|
||||
SeasonSlug = seasonSlug
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -241,20 +240,6 @@ namespace StarLight
|
||||
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)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user