Externi titulky
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-11-11 12:42:36 +01:00
parent d4b4b0f2d4
commit 470fa96b17
3 changed files with 69 additions and 2 deletions

View File

@@ -6,4 +6,6 @@ public class StreamSubtitle
{
public string language { get; set; } // two letter ISO code
public bool forced { get; set; }
}
public string? _id { get; set; }
public string? src { get; set; } // url to external subtitles
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Web;
using Microsoft.Extensions.Primitives;
using StreamCinema.Webshare;
using StreamCinemaLib.API;
using StreamCinemaWeb.Layouts;
@@ -38,5 +39,42 @@ public class LinkPage : BasicLayout
}
w.WriteLine("<div><a href=\"" + link.ToString() + "\">" + HttpUtility.HtmlEncode(link.ToString()) + "</a></div>");
// External subtitles
StringValues subs = req.Query["sub"];
if (subs.Count != 0) {
w.WriteLine("<h2>Externí titulky</h2>");
foreach (string i in subs) {
(string lang, Uri uri) = await ProcessSubtitleLinkAsync(i, req.HttpContext.RequestAborted);
w.WriteLine("<div><a href=\"" + uri.ToString() + "\">" + HttpUtility.HtmlEncode(lang) + "</a></div>");
}
}
}
private static async Task<(string, Uri)> ProcessSubtitleLinkAsync(string link, CancellationToken cancel) {
if (link == null)
throw new ArgumentNullException();
// Format: <lang>:<provider>:<id>
int a = link.IndexOf(':');
int b = link.IndexOf(':', a + 1);
if (a < 0 || b < 0)
throw new FormatException();
string lang = link.Substring(0, a);
string provider = link.Substring(a + 1, b - a - 1);
string rawId = link.Substring(b + 1);
Uri result;
switch (provider) {
case "webshare":
result = await LinkGenerator.GenerateDownloadLinkAsync(rawId, "", cancel);
break;
case "http":
result = new Uri(rawId);
break;
default:
throw new NotSupportedException();
}
return (lang, result);
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Text;
using System.Web;
using StreamCinemaLib.API;
using StreamCinemaWeb.Layouts;
@@ -40,7 +41,16 @@ public class StreamsPage : BasicLayout
w.WriteLine("<div class=\"media-streams\">");
foreach (StreamCinemaLib.API.Stream i in streams)
{
w.WriteLine("<div class=\"media-stream\"><a href=\"/link/" + i.provider + "/" + HttpUtility.UrlEncode(i.ident) + "/" + HttpUtility.UrlEncode(i.name) + "/" + HttpUtility.UrlEncode(title) + "\">");
// Gather external subtitles
StringBuilder extSubtitles = new StringBuilder();
if (i.subtitles != null && i.subtitles.Count != 0)
foreach (StreamSubtitle j in i.subtitles)
if (j.src != null)
extSubtitles.Append("&sub=" + HttpUtility.UrlEncode(GetCompactExternalSubtitle(j.language, new Uri(j.src))));
if (extSubtitles.Length > 0)
extSubtitles[0] = '?';
w.WriteLine("<div class=\"media-stream\"><a href=\"/link/" + i.provider + "/" + HttpUtility.UrlEncode(i.ident) + "/" + HttpUtility.UrlEncode(i.name) + "/" + HttpUtility.UrlEncode(title) + extSubtitles.ToString() + "\">");
w.WriteLine($"<div class=\"stream-basic\">{Math.Round((double)(i.size ?? 0) / (1024 * 1024 * 1024), 2)} GB ({i.date_added})</div>");
@@ -75,4 +85,21 @@ public class StreamsPage : BasicLayout
}
w.WriteLine("</div>");
}
private static string GetCompactExternalSubtitle(string lang, Uri uri) {
string provider = "http";
string id = uri.ToString();
switch (uri.Host) {
case "webshare.cz":
const string WebsharePrefix = "#/file/";
if (uri.AbsolutePath == "/" && uri.Fragment.StartsWith(WebsharePrefix)) {
provider = "webshare";
id = uri.Fragment.Substring(WebsharePrefix.Length);
}
break;
}
// Format: <lang>:<provider>:<id>
return lang + ":" + provider + ":" + id;
}
}