From dc21cb5b0b8d87aaa6074063e31aa9073b35448b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Mon, 29 Oct 2018 22:57:34 +0100 Subject: [PATCH] added application config --- AppConfig.cs | 9 ++++ Controllers/PlayListController.cs | 70 +++++++++++++++++++++++++++---- Program.cs | 21 ++++------ README.md | 7 ++++ appsettings.json | 5 ++- 5 files changed, 91 insertions(+), 21 deletions(-) create mode 100644 AppConfig.cs diff --git a/AppConfig.cs b/AppConfig.cs new file mode 100644 index 0000000..45b88ff --- /dev/null +++ b/AppConfig.cs @@ -0,0 +1,9 @@ +namespace tv7playlist +{ + public class AppConfig + { + public string TV7Url { get; set; } + public string UdpxyUrl { get; set; } + public string DownloadFileName { get; set; } + } +} \ No newline at end of file diff --git a/Controllers/PlayListController.cs b/Controllers/PlayListController.cs index 285b1f2..3a663c0 100644 --- a/Controllers/PlayListController.cs +++ b/Controllers/PlayListController.cs @@ -1,8 +1,10 @@ -using System.IO; +using System; +using System.IO; using System.Net.Http; using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; namespace tv7playlist.Controllers { @@ -10,19 +12,45 @@ namespace tv7playlist.Controllers [ApiController] public class PlayListController : ControllerBase { - private const string Tv7OriginUrl = "https://api.init7.net/tvchannels.m3u"; - private const string UdpxyRootUrl = "http://192.168.15.2:4022/udp"; + private const string PlayListContentType = "audio/mpegurl"; - private static readonly Regex MulticastRegex = new Regex(@"(udp\:\/\/@)([0-9.:]+)", + private static readonly Regex MultiCastRegex = new Regex(@"(udp\:\/\/@)([0-9.:]+)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase); + private readonly string _downloadFileName; + private readonly string _proxyUrl; + + private readonly string _tv7Url; + + public PlayListController(IConfiguration configuration) + { + if (configuration == null) throw new ArgumentNullException(nameof(configuration)); + + var appConfig = configuration.Get(); + + _tv7Url = GetTv7Url(appConfig); + _proxyUrl = GetProxyUrl(appConfig); + _downloadFileName = GetDownloadFileName(appConfig); + } + [HttpGet] public async Task Get() { - var httpClient = new HttpClient(); - var tv7Response = await httpClient.GetAsync(Tv7OriginUrl); + using (var httpClient = new HttpClient()) + { + var tv7Response = await httpClient.GetAsync(_tv7Url); + var modifiedPlaylist = await BuildProxyPlaylist(tv7Response); + return new FileStreamResult(modifiedPlaylist, PlayListContentType) + { + FileDownloadName = _downloadFileName + }; + } + } + + private async Task BuildProxyPlaylist(HttpResponseMessage tv7Response) + { var outStream = new MemoryStream(); var outWriter = new StreamWriter(outStream); @@ -32,7 +60,7 @@ namespace tv7playlist.Controllers while (!reader.EndOfStream) { var line = await reader.ReadLineAsync(); - line = MulticastRegex.Replace(line, $"{UdpxyRootUrl}/$2/"); + line = MultiCastRegex.Replace(line, $"{_proxyUrl}/$2/"); outWriter.WriteLine(line); } @@ -40,7 +68,33 @@ namespace tv7playlist.Controllers } outStream.Seek(0, SeekOrigin.Begin); - return new FileStreamResult(outStream, "audio/mpegurl") {FileDownloadName = "PlaylistTV7udpxy.m3u"}; + + return outStream; + } + + private string GetTv7Url(AppConfig configuration) + { + var tv7Url = configuration.TV7Url; + if (!Uri.IsWellFormedUriString(tv7Url, UriKind.Absolute)) + throw new ApplicationException($"The TV7Url is set to {tv7Url}. This is not a well formed uri!"); + return tv7Url; + } + + private string GetProxyUrl(AppConfig configuration) + { + var proxyUrl = configuration.UdpxyUrl; + if (!Uri.IsWellFormedUriString(proxyUrl, UriKind.Absolute)) + throw new ApplicationException($"The proxyUrl is set to {proxyUrl}. This is not a well formed uri!"); + + return proxyUrl; + } + + private string GetDownloadFileName(AppConfig configuration) + { + var downloadFileName = configuration.DownloadFileName; + if (string.IsNullOrEmpty(downloadFileName)) downloadFileName = "playlist.m3u"; + + return downloadFileName; } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 8a26866..c023da7 100644 --- a/Program.cs +++ b/Program.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; namespace tv7playlist { @@ -14,11 +7,15 @@ namespace tv7playlist { public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + CreateWebHostBuilder(args) + .Build() + .Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) + public static IWebHostBuilder CreateWebHostBuilder(string[] args) + { + return WebHost.CreateDefaultBuilder(args) .UseStartup(); + } } -} +} \ No newline at end of file diff --git a/README.md b/README.md index 09cd50f..2441cfc 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # tv7playlist +This little application is used to rewrite the TV7 multicast channel list by fiber7 m3u. +The updated list will proxy the multicast stream through udpxy and builds a stream that plex can handle. + +Others have changed the code in telly. As I did not want to change any external source, I just +wrote this little program. + +This is licenced under GPLv2. See License file. \ No newline at end of file diff --git a/appsettings.json b/appsettings.json index 7376aad..b0a37b7 100644 --- a/appsettings.json +++ b/appsettings.json @@ -4,5 +4,8 @@ "Default": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "TV7Url":"https://api.init7.net/tvchannels.m3u", + "UdpxyUrl":"http://192.168.15.2:4022/udp", + "DownloadFileName":"PlaylistTV7udpxy.m3u" }