diff --git a/StarLight/Controller.cs b/StarLight/Controller.cs index 4c2d352..7e449b5 100644 --- a/StarLight/Controller.cs +++ b/StarLight/Controller.cs @@ -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 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("
"); - - foreach (var item in items) - { - html.Append("
"); - - if (!string.IsNullOrEmpty(item.image)) - { - html.Append("
"); - } - else - { - html.Append("
"); - } - - html.Append("
"); - HtmlEncode(item.name, html); - html.Append("
"); - - firstjson = false; - } - - html.Append("
"); - - 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; - } - } - } } } diff --git a/StarLight/Models/StarLightModels.cs b/StarLight/Models/StarLightModels.cs index 43d416b..90142aa 100644 --- a/StarLight/Models/StarLightModels.cs +++ b/StarLight/Models/StarLightModels.cs @@ -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 diff --git a/StarLight/StarLightInvoke.cs b/StarLight/StarLightInvoke.cs index 002c81d..11c8bb6 100644 --- a/StarLight/StarLightInvoke.cs +++ b/StarLight/StarLightInvoke.cs @@ -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) {