First running version with all fields, sync, m3u generation and without old controller
This commit is contained in:
@@ -1,123 +0,0 @@
|
||||
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;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Tv7Playlist.Core;
|
||||
|
||||
namespace Tv7Playlist.Controllers
|
||||
{
|
||||
[Route("api/playlist-old")]
|
||||
[ApiController]
|
||||
public class PlayListController : ControllerBase
|
||||
{
|
||||
private const string PlayListContentType = "audio/mpegurl";
|
||||
|
||||
/// <summary>
|
||||
/// This is the regex used to build up the proxy url.
|
||||
/// The first part (udp://@) is ignored while generating the final url
|
||||
/// The multicast address is expected to be a ip-address with a port and is reused
|
||||
/// </summary>
|
||||
private static readonly Regex MultiCastRegex = new Regex(@"(udp\:\/\/@)([0-9.:]+)",
|
||||
RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
|
||||
|
||||
private readonly string _downloadFileName;
|
||||
private readonly ILogger<PlayListController> _logger;
|
||||
private readonly string _proxyUrl;
|
||||
|
||||
private readonly string _tv7Url;
|
||||
|
||||
public PlayListController(ILogger<PlayListController> logger, IConfiguration configuration)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
|
||||
|
||||
var appConfig = configuration.Get<AppConfig>();
|
||||
|
||||
_tv7Url = GetTv7Url(appConfig);
|
||||
_proxyUrl = GetProxyUrl(appConfig);
|
||||
_downloadFileName = GetDownloadFileName(appConfig);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> Get()
|
||||
{
|
||||
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 StatusCode((int) tv7Response.StatusCode);
|
||||
}
|
||||
|
||||
var modifiedPlaylist = await BuildProxyPlaylist(tv7Response);
|
||||
|
||||
_logger.LogInformation(LoggingEvents.Playlist, "Sending updated playlist {filename}",
|
||||
_downloadFileName);
|
||||
|
||||
return new FileStreamResult(modifiedPlaylist, PlayListContentType)
|
||||
{
|
||||
FileDownloadName = _downloadFileName
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<MemoryStream> BuildProxyPlaylist(HttpResponseMessage tv7Response)
|
||||
{
|
||||
var outStream = new MemoryStream();
|
||||
var outWriter = new StreamWriter(outStream);
|
||||
_logger.LogInformation(LoggingEvents.Playlist, "Building m3u file content");
|
||||
|
||||
using (var tv7ReadStream = await tv7Response.Content.ReadAsStreamAsync())
|
||||
using (var reader = new StreamReader(tv7ReadStream))
|
||||
{
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = await reader.ReadLineAsync();
|
||||
var proxyLine = MultiCastRegex.Replace(line, $"{_proxyUrl}/$2/");
|
||||
outWriter.WriteLine(proxyLine);
|
||||
_logger.LogDebug(LoggingEvents.Playlist, "Transformed {src} to {dst}", line, proxyLine);
|
||||
}
|
||||
|
||||
await outWriter.FlushAsync();
|
||||
}
|
||||
|
||||
outStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,13 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Tv7Playlist.Core;
|
||||
using Tv7Playlist.Core.Parsers;
|
||||
using Tv7Playlist.Data;
|
||||
|
||||
namespace Tv7Playlist.Controllers
|
||||
{
|
||||
@@ -15,74 +10,54 @@ namespace Tv7Playlist.Controllers
|
||||
[ApiController]
|
||||
public class PlaylistApiController : Controller
|
||||
{
|
||||
private const string PlayListContentType = "audio/mpegurl";
|
||||
|
||||
private readonly IAppConfig _appConfig;
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
private readonly PlaylistContext _playlistContext;
|
||||
private readonly IPlaylistLoader _playlistLoader;
|
||||
|
||||
public PlaylistApiController(ILogger<HomeController> logger, PlaylistContext playlistContext, IPlaylistLoader playlistLoader,
|
||||
IAppConfig appConfig)
|
||||
private readonly IPlaylistBuilder _playlistBuilder;
|
||||
private readonly IPlaylistSynchronizer _playlistSynchronizer;
|
||||
|
||||
public PlaylistApiController(ILogger<HomeController> logger, IPlaylistSynchronizer playlistSynchronizer,
|
||||
IPlaylistBuilder playlistBuilder, IAppConfig appConfig)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_playlistContext = playlistContext ?? throw new ArgumentNullException(nameof(playlistContext));
|
||||
_playlistLoader = playlistLoader ?? throw new ArgumentNullException(nameof(playlistLoader));
|
||||
_playlistSynchronizer = playlistSynchronizer ?? throw new ArgumentNullException(nameof(playlistSynchronizer));
|
||||
_playlistBuilder = playlistBuilder ?? throw new ArgumentNullException(nameof(playlistBuilder));
|
||||
_appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("synchronize")]
|
||||
[Route("")]
|
||||
public async Task<IActionResult> GetPlaylist()
|
||||
{
|
||||
var playlistStream = await _playlistBuilder.GeneratePlaylistAsync();
|
||||
var downloadFileName = GetDownloadFileName();
|
||||
|
||||
_logger.LogInformation(LoggingEvents.Playlist, "Sending updated playlist {filename}",
|
||||
downloadFileName);
|
||||
|
||||
return new FileStreamResult(playlistStream, PlayListContentType)
|
||||
{
|
||||
FileDownloadName = downloadFileName
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("")]
|
||||
public async Task<IActionResult> Synchronize()
|
||||
{
|
||||
//TODO: Refactor to post method
|
||||
_logger.LogDebug("Synchronizing playlist from server...");
|
||||
|
||||
var existingEntries = await _playlistContext.PlaylistEntries.ToDictionaryAsync(e => e.TrackNumber);
|
||||
var tracks = await _playlistLoader.LoadPlaylistFromUrl(_appConfig.TV7Url);
|
||||
|
||||
MarkNotAvailableEntries(existingEntries, tracks);
|
||||
AddOrUpdateEntries(existingEntries, tracks);
|
||||
|
||||
_logger.LogDebug("Synchronizing playlist completed saving changes...");
|
||||
|
||||
await _playlistContext.SaveChangesAsync();
|
||||
_logger.LogDebug("Playlist changes saved successfully...");
|
||||
|
||||
await _playlistSynchronizer.SynchronizeAsync();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
private void AddOrUpdateEntries(Dictionary<int, PlaylistEntry> existingEntries, IEnumerable<ParsedTrack> tracks)
|
||||
{
|
||||
foreach (var track in tracks)
|
||||
{
|
||||
if (!existingEntries.TryGetValue(track.Id, out var entry))
|
||||
{
|
||||
_logger.LogInformation($"Adding playlist entry {track.Id} - {track.Name}");
|
||||
entry = new PlaylistEntry
|
||||
{
|
||||
Id = Guid.NewGuid(), Position = track.Id, TrackNumber = track.Id, IsEnabled = true
|
||||
};
|
||||
_playlistContext.PlaylistEntries.Add(entry);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation($"Updating playlist entry {track.Id} - {track.Name}");
|
||||
}
|
||||
|
||||
entry.IsAvailable = true;
|
||||
entry.Name = track.Name;
|
||||
entry.Url = track.Url;
|
||||
}
|
||||
}
|
||||
|
||||
private void MarkNotAvailableEntries(Dictionary<int, PlaylistEntry> existingEntries,
|
||||
IReadOnlyCollection<ParsedTrack> tracks)
|
||||
private string GetDownloadFileName()
|
||||
{
|
||||
var unavailableEntries = existingEntries.Where(e => tracks.All(t => t.Id != e.Key)).Select(e => e.Value);
|
||||
foreach (var entry in unavailableEntries)
|
||||
{
|
||||
_logger.LogInformation($"Channel {entry.TrackNumber} - {entry.Name} is no longer available.");
|
||||
entry.IsAvailable = false;
|
||||
}
|
||||
var downloadFileName = _appConfig.DownloadFileName;
|
||||
if (string.IsNullOrEmpty(downloadFileName)) downloadFileName = "playlist.m3u";
|
||||
|
||||
return downloadFileName;
|
||||
}
|
||||
}
|
||||
}
|
@@ -86,6 +86,8 @@ namespace Tv7Playlist
|
||||
private void ConfigureParser(IServiceCollection services, IAppConfig appConfig)
|
||||
{
|
||||
services.AddTransient<IPlaylistLoader, PlaylistLoader>();
|
||||
services.AddTransient<IPlaylistBuilder, PlaylistBuilder>();
|
||||
services.AddTransient<IPlaylistSynchronizer, PlaylistSynchronizer>();
|
||||
|
||||
var type = appConfig.SourceType;
|
||||
switch (type)
|
||||
|
@@ -9,7 +9,8 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
@@ -2,30 +2,37 @@
|
||||
@{
|
||||
ViewData["Title"] = "TV7 Playlist";
|
||||
}
|
||||
<div class="row">
|
||||
<div class="col col-12">
|
||||
<table class="table table-hover table-striped">
|
||||
<tr>
|
||||
<th>Position</th>
|
||||
<th>Number</th>
|
||||
<th>Number override</th>
|
||||
<th>Name</th>
|
||||
<th>Name override</th>
|
||||
<th>Enabled</th>
|
||||
<th>Available</th>
|
||||
<th>URL</th>
|
||||
<th>original URL</th>
|
||||
</tr>
|
||||
|
||||
<table class="table table-hover table-striped">
|
||||
<tr>
|
||||
<th>Position</th>
|
||||
<th>Number</th>
|
||||
<th>Name</th>
|
||||
<th>Name override</th>
|
||||
<th>Enabled</th>
|
||||
<th>Available</th>
|
||||
<th>URL</th>
|
||||
</tr>
|
||||
|
||||
@{
|
||||
foreach (var track in Model.PlaylistEntries)
|
||||
{
|
||||
<tr>
|
||||
<td>@track.Position</td>
|
||||
<td>@track.TrackNumber</td>
|
||||
<td>@track.Name</td>
|
||||
<td>@track.NameOverride</td>
|
||||
<td>@track.IsEnabled</td>
|
||||
<td>@track.IsAvailable</td>
|
||||
<td>@track.Url</td>
|
||||
</tr>
|
||||
}
|
||||
}
|
||||
</table>
|
||||
@{
|
||||
foreach (var track in Model.PlaylistEntries)
|
||||
{
|
||||
<tr>
|
||||
<td>@track.Position</td>
|
||||
<td>@track.TrackNumber</td>
|
||||
<td>@track.TrackNumberOverride</td>
|
||||
<td>@track.Name</td>
|
||||
<td>@track.NameOverride</td>
|
||||
<td>@track.IsEnabled</td>
|
||||
<td>@track.IsAvailable</td>
|
||||
<td>@track.Url</td>
|
||||
<td>@track.UrlOriginal</td>
|
||||
</tr>
|
||||
}
|
||||
}
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
@@ -34,7 +34,7 @@
|
||||
|
||||
</header>
|
||||
|
||||
<div class="container">
|
||||
<div class="container-fluid">
|
||||
<main role="main" class="pb-3">
|
||||
@RenderBody()
|
||||
</main>
|
||||
@@ -42,7 +42,7 @@
|
||||
|
||||
<footer class="border-top footer text-muted">
|
||||
<div class="container">
|
||||
© 2018 - Tv7Playlist
|
||||
© 2018 - Philipp Häfelfinger - Tv7Playlist
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
|
Reference in New Issue
Block a user