added playlist loader
registered appconfig as singleton added config interfaces
This commit is contained in:
13
Tv7Playlist.Core/IAppConfig.cs
Normal file
13
Tv7Playlist.Core/IAppConfig.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
public interface IAppConfig
|
||||||
|
{
|
||||||
|
string TV7Url { get; set; }
|
||||||
|
|
||||||
|
SourceTypeEnum SourceType { get; set; }
|
||||||
|
|
||||||
|
string UdpxyUrl { get; set; }
|
||||||
|
|
||||||
|
string DownloadFileName { get; set; }
|
||||||
|
}
|
||||||
|
}
|
11
Tv7Playlist.Core/IPlaylistLoader.cs
Normal file
11
Tv7Playlist.Core/IPlaylistLoader.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Tv7Playlist.Core.Parsers;
|
||||||
|
|
||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
public interface IPlaylistLoader
|
||||||
|
{
|
||||||
|
Task<IReadOnlyCollection<ParsedTrack>> LoadPlaylistFromUrl();
|
||||||
|
}
|
||||||
|
}
|
@@ -6,6 +6,6 @@ namespace Tv7Playlist.Core.Parsers
|
|||||||
{
|
{
|
||||||
public interface IPlaylistParser
|
public interface IPlaylistParser
|
||||||
{
|
{
|
||||||
Task<IReadOnlyCollection<ParsedTrack>> ParseFromStream(Stream stream);
|
Task<IReadOnlyCollection<ParsedTrack>> ParseFromStreamAsync(Stream stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -20,7 +20,7 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyCollection<ParsedTrack>> ParseFromStream(Stream stream)
|
public async Task<IReadOnlyCollection<ParsedTrack>> ParseFromStreamAsync(Stream stream)
|
||||||
{
|
{
|
||||||
if (stream == null) throw new ArgumentNullException(nameof(stream));
|
if (stream == null) throw new ArgumentNullException(nameof(stream));
|
||||||
|
|
||||||
|
@@ -10,7 +10,7 @@ namespace Tv7Playlist.Core.Parsers.Xspf
|
|||||||
{
|
{
|
||||||
public class XspfParser : IPlaylistParser
|
public class XspfParser : IPlaylistParser
|
||||||
{
|
{
|
||||||
public Task<IReadOnlyCollection<ParsedTrack>> ParseFromStream(Stream stream)
|
public Task<IReadOnlyCollection<ParsedTrack>> ParseFromStreamAsync(Stream stream)
|
||||||
{
|
{
|
||||||
var deserializedList = DeserializePlaylist(stream);
|
var deserializedList = DeserializePlaylist(stream);
|
||||||
|
|
||||||
|
52
Tv7Playlist.Core/PlaylistLoader.cs
Normal file
52
Tv7Playlist.Core/PlaylistLoader.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Tv7Playlist.Core.Parsers;
|
||||||
|
|
||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
public class PlaylistLoader : IPlaylistLoader
|
||||||
|
{
|
||||||
|
private readonly ILogger<PlaylistLoader> _logger;
|
||||||
|
private readonly IPlaylistParser _playlistParser;
|
||||||
|
private readonly string _tv7Url;
|
||||||
|
|
||||||
|
public PlaylistLoader(ILogger<PlaylistLoader> logger, IAppConfig appConfig, IPlaylistParser playlistParser)
|
||||||
|
{
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
|
_playlistParser = playlistParser ?? throw new ArgumentNullException(nameof(playlistParser));
|
||||||
|
|
||||||
|
_tv7Url = appConfig.TV7Url;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IReadOnlyCollection<ParsedTrack>> LoadPlaylistFromUrl()
|
||||||
|
{
|
||||||
|
using (var httpClient = new HttpClient())
|
||||||
|
{
|
||||||
|
_logger.LogInformation(LoggingEvents.Playlist, "Downloading playlist from {tv7url}", _tv7Url);
|
||||||
|
|
||||||
|
var tv7Response = await httpClient.GetAsync(_tv7Url);
|
||||||
|
if (!tv7Response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(LoggingEvents.PlaylistNotFound, "Could not download playlist from {tv7url}. The StatusCode was: {StatusCode}", _tv7Url, tv7Response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ParseTracksFromResponseAsync(tv7Response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<IReadOnlyCollection<ParsedTrack>> ParseTracksFromResponseAsync(HttpResponseMessage tv7Response)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(LoggingEvents.Playlist, "Parse");
|
||||||
|
|
||||||
|
using (var tv7ReadStream = await tv7Response.Content.ReadAsStreamAsync())
|
||||||
|
{
|
||||||
|
return await _playlistParser.ParseFromStreamAsync(tv7ReadStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
Tv7Playlist.Core/SourceTypeEnum.cs
Normal file
8
Tv7Playlist.Core/SourceTypeEnum.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
public enum SourceTypeEnum
|
||||||
|
{
|
||||||
|
M3U,
|
||||||
|
Xspf
|
||||||
|
}
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
@@ -1,15 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using Tv7Playlist.Core;
|
||||||
|
|
||||||
namespace Tv7Playlist
|
namespace Tv7Playlist
|
||||||
{
|
{
|
||||||
public class AppConfig
|
public class AppConfig : IAppConfig
|
||||||
{
|
{
|
||||||
public enum SourceTypeEnum
|
|
||||||
{
|
|
||||||
M3U,
|
|
||||||
Xspf
|
|
||||||
}
|
|
||||||
|
|
||||||
private string _tv7Url;
|
private string _tv7Url;
|
||||||
private string _udpxyUrl;
|
private string _udpxyUrl;
|
||||||
|
|
||||||
|
@@ -4,12 +4,25 @@ using System.Diagnostics;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Tv7Playlist.Models;
|
using Tv7Playlist.Models;
|
||||||
|
|
||||||
namespace Tv7Playlist.Controllers
|
namespace Tv7Playlist.Controllers
|
||||||
{
|
{
|
||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
{
|
{
|
||||||
|
private readonly ILogger<HomeController> _logger;
|
||||||
|
|
||||||
|
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
|
|
||||||
|
var appConfig = configuration.Get<AppConfig>();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public IActionResult Index()
|
public IActionResult Index()
|
||||||
{
|
{
|
||||||
return View();
|
return View();
|
||||||
|
@@ -37,8 +37,11 @@ namespace Tv7Playlist
|
|||||||
options.MinimumSameSitePolicy = SameSiteMode.None;
|
options.MinimumSameSitePolicy = SameSiteMode.None;
|
||||||
});
|
});
|
||||||
|
|
||||||
ConfigureParser(services);
|
var appConfig = ConfigureAppConfig(services);
|
||||||
ConfigureDatabase(services);
|
|
||||||
|
LogConfiguration(appConfig);
|
||||||
|
ConfigureParser(services, appConfig);
|
||||||
|
ConfigureDatabase(services, appConfig);
|
||||||
|
|
||||||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
||||||
}
|
}
|
||||||
@@ -46,8 +49,6 @@ namespace Tv7Playlist
|
|||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
||||||
{
|
{
|
||||||
LogConfiguration();
|
|
||||||
|
|
||||||
if (env.IsDevelopment())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
@@ -66,41 +67,49 @@ namespace Tv7Playlist
|
|||||||
app.UseMvc(routes =>
|
app.UseMvc(routes =>
|
||||||
{
|
{
|
||||||
routes.MapRoute(
|
routes.MapRoute(
|
||||||
name: "default",
|
"default",
|
||||||
template: "{controller=Home}/{action=Index}/{id?}");
|
"{controller=Home}/{action=Index}/{id?}");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ConfigureParser(IServiceCollection services)
|
private void ConfigureParser(IServiceCollection services, IAppConfig appConfig)
|
||||||
{
|
{
|
||||||
var type = Configuration.Get<AppConfig>().SourceType;
|
services.AddTransient<IPlaylistLoader, PlaylistLoader>();
|
||||||
|
|
||||||
|
var type = appConfig.SourceType;
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case AppConfig.SourceTypeEnum.M3U:
|
case SourceTypeEnum.M3U:
|
||||||
services.AddTransient<IPlaylistParser, M3UParser>();
|
services.AddTransient<IPlaylistParser, M3UParser>();
|
||||||
break;
|
break;
|
||||||
case AppConfig.SourceTypeEnum.Xspf:
|
case SourceTypeEnum.Xspf:
|
||||||
services.AddTransient<IPlaylistParser, XspfParser>();
|
services.AddTransient<IPlaylistParser, XspfParser>();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ConfigureDatabase(IServiceCollection services)
|
private IAppConfig ConfigureAppConfig(IServiceCollection services)
|
||||||
|
{
|
||||||
|
var appConfig = Configuration.Get<AppConfig>();
|
||||||
|
services.AddSingleton<IAppConfig>(appConfig);
|
||||||
|
return appConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ConfigureDatabase(IServiceCollection services, IAppConfig appConfig)
|
||||||
{
|
{
|
||||||
//TODO: Move to settings to make it configurable within docker.
|
//TODO: Move to settings to make it configurable within docker.
|
||||||
var connection = "Data Source=playlist.db";
|
var connection = "Data Source=playlist.db";
|
||||||
services.AddDbContext<PlaylistContext>(options => options.UseSqlite(connection));
|
services.AddDbContext<PlaylistContext>(options => options.UseSqlite(connection));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LogConfiguration()
|
private void LogConfiguration(IAppConfig appConfig)
|
||||||
{
|
{
|
||||||
var appConfig = Configuration.Get<AppConfig>();
|
|
||||||
_logger.LogInformation(LoggingEvents.Startup, "Using TV7 URL: {TV7Url}", appConfig.TV7Url);
|
_logger.LogInformation(LoggingEvents.Startup, "Using TV7 URL: {TV7Url}", appConfig.TV7Url);
|
||||||
_logger.LogInformation(LoggingEvents.Startup, "Using Udpxy URL: {UdpxyUrl}", appConfig.UdpxyUrl);
|
_logger.LogInformation(LoggingEvents.Startup, "Using Udpxy URL: {UdpxyUrl}", appConfig.UdpxyUrl);
|
||||||
_logger.LogInformation(LoggingEvents.Startup, "Using DownloadFileName: {DownloadFileName}",
|
_logger.LogInformation(LoggingEvents.Startup, "Using DownloadFileName: {DownloadFileName}",
|
||||||
appConfig.DownloadFileName);
|
appConfig.DownloadFileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user