First running version with all fields, sync, m3u generation and without old controller
This commit is contained in:
10
Tv7Playlist.Core/IPlaylistBuilder.cs
Normal file
10
Tv7Playlist.Core/IPlaylistBuilder.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
public interface IPlaylistBuilder
|
||||||
|
{
|
||||||
|
Task<Stream> GeneratePlaylistAsync();
|
||||||
|
}
|
||||||
|
}
|
9
Tv7Playlist.Core/IPlaylistSynchronizer.cs
Normal file
9
Tv7Playlist.Core/IPlaylistSynchronizer.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
public interface IPlaylistSynchronizer
|
||||||
|
{
|
||||||
|
Task SynchronizeAsync();
|
||||||
|
}
|
||||||
|
}
|
@@ -4,8 +4,8 @@ namespace Tv7Playlist.Core
|
|||||||
{
|
{
|
||||||
public const int Startup = 1000;
|
public const int Startup = 1000;
|
||||||
public const int Playlist = 1001;
|
public const int Playlist = 1001;
|
||||||
public const int ParsingM3uPlayList = 1002;
|
public const int ParsingM3UPlayList = 1002;
|
||||||
|
public const int Synchronize = 1003;
|
||||||
public const int PlaylistNotFound = 4000;
|
public const int PlaylistNotFound = 4000;
|
||||||
}
|
}
|
||||||
}
|
}
|
8
Tv7Playlist.Core/M3UConstants.cs
Normal file
8
Tv7Playlist.Core/M3UConstants.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
internal static class M3UConstants
|
||||||
|
{
|
||||||
|
public const string ExtInfStartTag = "#EXTINF:";
|
||||||
|
public const string ExtFileStartTag = "#EXTM3U";
|
||||||
|
}
|
||||||
|
}
|
@@ -8,8 +8,6 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
{
|
{
|
||||||
public class M3UParser : IPlaylistParser
|
public class M3UParser : IPlaylistParser
|
||||||
{
|
{
|
||||||
private const string ExtInfStartTag = "#EXTINF:";
|
|
||||||
private const string ExtFileStartTag = "#EXTM3U";
|
|
||||||
private const int IdStartNumber = 1000;
|
private const int IdStartNumber = 1000;
|
||||||
private const int IdIncrementNumber = 5;
|
private const int IdIncrementNumber = 5;
|
||||||
|
|
||||||
@@ -24,7 +22,7 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
{
|
{
|
||||||
if (stream == null) throw new ArgumentNullException(nameof(stream));
|
if (stream == null) throw new ArgumentNullException(nameof(stream));
|
||||||
|
|
||||||
_logger.LogInformation(LoggingEvents.ParsingM3uPlayList, "Parsing m3u file content");
|
_logger.LogInformation(LoggingEvents.ParsingM3UPlayList, "Parsing m3u file content");
|
||||||
|
|
||||||
EnsureStreamIsAtBeginning(stream);
|
EnsureStreamIsAtBeginning(stream);
|
||||||
|
|
||||||
@@ -32,7 +30,7 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
{
|
{
|
||||||
var tracks = await ParseTracksFromStreamAsync(reader);
|
var tracks = await ParseTracksFromStreamAsync(reader);
|
||||||
|
|
||||||
_logger.LogInformation(LoggingEvents.ParsingM3uPlayList, "Parsing m3u file finished");
|
_logger.LogInformation(LoggingEvents.ParsingM3UPlayList, "Parsing m3u file finished");
|
||||||
|
|
||||||
return tracks;
|
return tracks;
|
||||||
}
|
}
|
||||||
@@ -44,7 +42,7 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
|
|
||||||
if (!await StreamHasValidStartTagAsync(reader))
|
if (!await StreamHasValidStartTagAsync(reader))
|
||||||
{
|
{
|
||||||
_logger.LogError(LoggingEvents.ParsingM3uPlayList, $"Could not parse stream as it did not start with {ExtFileStartTag}");
|
_logger.LogError(LoggingEvents.ParsingM3UPlayList, $"Could not parse stream as it did not start with {M3UConstants.ExtFileStartTag}");
|
||||||
return new List<ParsedTrack>(300);
|
return new List<ParsedTrack>(300);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +61,7 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
private static async Task<bool> StreamHasValidStartTagAsync(StreamReader reader)
|
private static async Task<bool> StreamHasValidStartTagAsync(StreamReader reader)
|
||||||
{
|
{
|
||||||
var startLine = await ReadLineSafeAsync(reader);
|
var startLine = await ReadLineSafeAsync(reader);
|
||||||
var stramHasValidStartTag = !startLine.Trim().ToUpper().Equals(ExtInfStartTag);
|
var stramHasValidStartTag = !startLine.Trim().ToUpper().Equals(M3UConstants.ExtInfStartTag);
|
||||||
return stramHasValidStartTag;
|
return stramHasValidStartTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +69,7 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
{
|
{
|
||||||
if (stream.Position == 0) return;
|
if (stream.Position == 0) return;
|
||||||
|
|
||||||
_logger.LogWarning(LoggingEvents.ParsingM3uPlayList, "Stream not positioned at the beginning. Repositioning!");
|
_logger.LogWarning(LoggingEvents.ParsingM3UPlayList, "Stream not positioned at the beginning. Repositioning!");
|
||||||
stream.Position = 0;
|
stream.Position = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,9 +80,9 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
if (currentId <= 0) throw new ArgumentOutOfRangeException(nameof(currentId));
|
if (currentId <= 0) throw new ArgumentOutOfRangeException(nameof(currentId));
|
||||||
|
|
||||||
var metaLine = await ReadLineSafeAsync(reader);
|
var metaLine = await ReadLineSafeAsync(reader);
|
||||||
if (!metaLine.StartsWith(ExtInfStartTag))
|
if (!metaLine.StartsWith(M3UConstants.ExtInfStartTag))
|
||||||
{
|
{
|
||||||
_logger.LogDebug(LoggingEvents.ParsingM3uPlayList,
|
_logger.LogDebug(LoggingEvents.ParsingM3UPlayList,
|
||||||
"Line {lineNumber} {metaLine} is not a valid start channel start line",
|
"Line {lineNumber} {metaLine} is not a valid start channel start line",
|
||||||
currentId.ToString(), metaLine);
|
currentId.ToString(), metaLine);
|
||||||
return;
|
return;
|
||||||
@@ -95,11 +93,11 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
if (track != null)
|
if (track != null)
|
||||||
{
|
{
|
||||||
tracks.Add(track);
|
tracks.Add(track);
|
||||||
_logger.LogDebug(LoggingEvents.ParsingM3uPlayList, "Parsed track {track}", track);
|
_logger.LogDebug(LoggingEvents.ParsingM3UPlayList, "Parsed track {track}", track);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.LogWarning(LoggingEvents.ParsingM3uPlayList,
|
_logger.LogWarning(LoggingEvents.ParsingM3UPlayList,
|
||||||
"Could not parse lines {metaLine} with url {url}", metaLine, url);
|
"Could not parse lines {metaLine} with url {url}", metaLine, url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,7 +121,7 @@ namespace Tv7Playlist.Core.Parsers.M3u
|
|||||||
|
|
||||||
//TODO: Check line parsing of https://github.com/tellytv/telly/blob/dev/internal/m3uplus/main.go
|
//TODO: Check line parsing of https://github.com/tellytv/telly/blob/dev/internal/m3uplus/main.go
|
||||||
//format is base for telly to export.
|
//format is base for telly to export.
|
||||||
var fields = metaLine.Replace(ExtInfStartTag, string.Empty).Split(',');
|
var fields = metaLine.Replace(M3UConstants.ExtInfStartTag, string.Empty).Split(',');
|
||||||
var name = fields.Length >= 2 ? fields[1] : $"{currentId}-unknown";
|
var name = fields.Length >= 2 ? fields[1] : $"{currentId}-unknown";
|
||||||
|
|
||||||
return new ParsedTrack(currentId, name, url);
|
return new ParsedTrack(currentId, name, url);
|
||||||
|
57
Tv7Playlist.Core/PlaylistBuilder.cs
Normal file
57
Tv7Playlist.Core/PlaylistBuilder.cs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Tv7Playlist.Data;
|
||||||
|
|
||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
public class PlaylistBuilder : IPlaylistBuilder
|
||||||
|
{
|
||||||
|
private readonly ILogger<PlaylistBuilder> _logger;
|
||||||
|
private readonly PlaylistContext _playlistContext;
|
||||||
|
|
||||||
|
public PlaylistBuilder(ILogger<PlaylistBuilder> logger, PlaylistContext playlistContext)
|
||||||
|
{
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
|
_playlistContext = playlistContext ?? throw new ArgumentNullException(nameof(playlistContext));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Stream> GeneratePlaylistAsync()
|
||||||
|
{
|
||||||
|
_logger.LogInformation(LoggingEvents.Playlist, "Building m3u file content");
|
||||||
|
|
||||||
|
var playlistEntries = await _playlistContext.PlaylistEntries.Where(e => e.IsAvailable).Where(e => e.IsEnabled)
|
||||||
|
.OrderBy(e => e.Position).AsNoTracking().ToListAsync();
|
||||||
|
|
||||||
|
|
||||||
|
return await CreatePlaylistStreamAsync(playlistEntries);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<MemoryStream> CreatePlaylistStreamAsync(IEnumerable<PlaylistEntry> playlistEntries)
|
||||||
|
{
|
||||||
|
var outStream = new MemoryStream();
|
||||||
|
var outWriter = new StreamWriter(outStream);
|
||||||
|
|
||||||
|
await outWriter.WriteLineAsync(M3UConstants.ExtFileStartTag);
|
||||||
|
|
||||||
|
foreach (var entry in playlistEntries) await WriteEntryAsync(outWriter, entry);
|
||||||
|
|
||||||
|
outStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
return outStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task WriteEntryAsync(TextWriter outWriter, PlaylistEntry entry)
|
||||||
|
{
|
||||||
|
const int duration = -1;
|
||||||
|
var number = entry.TrackNumberOverride != 0 ? entry.TrackNumberOverride : entry.TrackNumber;
|
||||||
|
var extInfo = $"{M3UConstants.ExtInfStartTag}{duration},{number},{entry.Name}";
|
||||||
|
await outWriter.WriteLineAsync(extInfo);
|
||||||
|
await outWriter.WriteLineAsync(entry.Url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
126
Tv7Playlist.Core/PlaylistSynchronizer.cs
Normal file
126
Tv7Playlist.Core/PlaylistSynchronizer.cs
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Tv7Playlist.Core.Parsers;
|
||||||
|
using Tv7Playlist.Data;
|
||||||
|
|
||||||
|
namespace Tv7Playlist.Core
|
||||||
|
{
|
||||||
|
public class PlaylistSynchronizer : IPlaylistSynchronizer
|
||||||
|
{
|
||||||
|
private static readonly Regex MultiCastRegex = new Regex(@"(udp\:\/\/@)([0-9.:]+)",
|
||||||
|
RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
private readonly IAppConfig _appConfig;
|
||||||
|
private readonly ILogger<PlaylistSynchronizer> _logger;
|
||||||
|
private readonly PlaylistContext _playlistContext;
|
||||||
|
private readonly IPlaylistLoader _playlistLoader;
|
||||||
|
private readonly string _proxyUrl;
|
||||||
|
|
||||||
|
public PlaylistSynchronizer(ILogger<PlaylistSynchronizer> logger, IAppConfig appConfig, IPlaylistLoader playlistLoader,
|
||||||
|
PlaylistContext playlistContext)
|
||||||
|
{
|
||||||
|
_playlistContext = playlistContext ?? throw new ArgumentNullException(nameof(playlistContext));
|
||||||
|
_appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
|
_playlistLoader = playlistLoader ?? throw new ArgumentNullException(nameof(playlistLoader));
|
||||||
|
_proxyUrl = GetProxyUrl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SynchronizeAsync()
|
||||||
|
{
|
||||||
|
_logger.LogDebug(LoggingEvents.Synchronize, "Synchronizing playlist from server...");
|
||||||
|
|
||||||
|
var tv7Url = GetTv7SourceUrl();
|
||||||
|
var existingEntries = await _playlistContext.PlaylistEntries.ToDictionaryAsync(e => e.TrackNumber);
|
||||||
|
var tracks = await _playlistLoader.LoadPlaylistFromUrl(tv7Url);
|
||||||
|
|
||||||
|
MarkNotAvailableEntries(existingEntries, tracks);
|
||||||
|
AddOrUpdateEntries(existingEntries, tracks);
|
||||||
|
|
||||||
|
_logger.LogDebug("Synchronizing playlist completed saving changes...");
|
||||||
|
|
||||||
|
await _playlistContext.SaveChangesAsync();
|
||||||
|
_logger.LogDebug("Playlist changes saved successfully...");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddOrUpdateEntries(IReadOnlyDictionary<int, PlaylistEntry> existingEntries, IEnumerable<ParsedTrack> tracks)
|
||||||
|
{
|
||||||
|
foreach (var track in tracks)
|
||||||
|
{
|
||||||
|
if (!existingEntries.TryGetValue(track.Id, out var entry))
|
||||||
|
{
|
||||||
|
_logger.LogInformation(LoggingEvents.Synchronize, $"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(LoggingEvents.Synchronize, $"Updating playlist entry {track.Id} - {track.Name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
entry.IsAvailable = true;
|
||||||
|
entry.Name = track.Name;
|
||||||
|
entry.UrlOriginal = track.Url;
|
||||||
|
entry.Url = BuildUrl(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string BuildUrl(PlaylistEntry entry)
|
||||||
|
{
|
||||||
|
return MultiCastRegex.Replace(entry.UrlOriginal, $"{_proxyUrl}/$2/");
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetProxyUrl()
|
||||||
|
{
|
||||||
|
var proxyUrl = _appConfig.UdpxyUrl;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(proxyUrl))
|
||||||
|
{
|
||||||
|
_logger.LogInformation(LoggingEvents.Synchronize, "No proxy URL set in the application config. Using original URLs");
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Uri.IsWellFormedUriString(proxyUrl, UriKind.Absolute))
|
||||||
|
{
|
||||||
|
var message = $"The proxyUrl is set to {proxyUrl}. This is not a well formed uri!";
|
||||||
|
_logger.LogError(LoggingEvents.Synchronize, message);
|
||||||
|
throw new ApplicationException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return proxyUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetTv7SourceUrl()
|
||||||
|
{
|
||||||
|
var tv7Url = _appConfig.TV7Url;
|
||||||
|
|
||||||
|
if (!Uri.IsWellFormedUriString(tv7Url, UriKind.Absolute))
|
||||||
|
{
|
||||||
|
var message = $"The TV7Url is set to {tv7Url}. This is not a well formed uri!";
|
||||||
|
_logger.LogError(LoggingEvents.Synchronize, message);
|
||||||
|
throw new ApplicationException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tv7Url;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MarkNotAvailableEntries(Dictionary<int, PlaylistEntry> existingEntries,
|
||||||
|
IReadOnlyCollection<ParsedTrack> tracks)
|
||||||
|
{
|
||||||
|
var unavailableEntries = existingEntries.Where(e => tracks.All(t => t.Id != e.Key)).Select(e => e.Value);
|
||||||
|
foreach (var entry in unavailableEntries)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(LoggingEvents.Synchronize, $"Channel {entry.TrackNumber} - {entry.Name} is no longer available.");
|
||||||
|
entry.IsAvailable = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,11 +1,16 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Tv7Playlist.Data\Tv7Playlist.Data.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@@ -9,14 +9,14 @@ using Tv7Playlist.Data;
|
|||||||
namespace Tv7Playlist.Data.Migrations
|
namespace Tv7Playlist.Data.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(PlaylistContext))]
|
[DbContext(typeof(PlaylistContext))]
|
||||||
[Migration("20190124231927_InitialCreate")]
|
[Migration("20190125212615_InitialCreate")]
|
||||||
partial class InitialCreate
|
partial class InitialCreate
|
||||||
{
|
{
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
#pragma warning disable 612, 618
|
#pragma warning disable 612, 618
|
||||||
modelBuilder
|
modelBuilder
|
||||||
.HasAnnotation("ProductVersion", "2.2.0-rtm-35687");
|
.HasAnnotation("ProductVersion", "2.2.1-servicing-10028");
|
||||||
|
|
||||||
modelBuilder.Entity("Tv7Playlist.Data.PlaylistEntry", b =>
|
modelBuilder.Entity("Tv7Playlist.Data.PlaylistEntry", b =>
|
||||||
{
|
{
|
||||||
@@ -35,8 +35,12 @@ namespace Tv7Playlist.Data.Migrations
|
|||||||
|
|
||||||
b.Property<int>("TrackNumber");
|
b.Property<int>("TrackNumber");
|
||||||
|
|
||||||
|
b.Property<int>("TrackNumberOverride");
|
||||||
|
|
||||||
b.Property<string>("Url");
|
b.Property<string>("Url");
|
||||||
|
|
||||||
|
b.Property<string>("UrlOriginal");
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.HasIndex("Name");
|
b.HasIndex("Name");
|
@@ -14,9 +14,11 @@ namespace Tv7Playlist.Data.Migrations
|
|||||||
Id = table.Column<Guid>(nullable: false),
|
Id = table.Column<Guid>(nullable: false),
|
||||||
Position = table.Column<int>(nullable: false),
|
Position = table.Column<int>(nullable: false),
|
||||||
TrackNumber = table.Column<int>(nullable: false),
|
TrackNumber = table.Column<int>(nullable: false),
|
||||||
|
TrackNumberOverride = table.Column<int>(nullable: false),
|
||||||
Name = table.Column<string>(nullable: true),
|
Name = table.Column<string>(nullable: true),
|
||||||
NameOverride = table.Column<string>(nullable: true),
|
NameOverride = table.Column<string>(nullable: true),
|
||||||
Url = table.Column<string>(nullable: true),
|
Url = table.Column<string>(nullable: true),
|
||||||
|
UrlOriginal = table.Column<string>(nullable: true),
|
||||||
IsAvailable = table.Column<bool>(nullable: false),
|
IsAvailable = table.Column<bool>(nullable: false),
|
||||||
IsEnabled = table.Column<bool>(nullable: false)
|
IsEnabled = table.Column<bool>(nullable: false)
|
||||||
},
|
},
|
@@ -14,7 +14,7 @@ namespace Tv7Playlist.Data.Migrations
|
|||||||
{
|
{
|
||||||
#pragma warning disable 612, 618
|
#pragma warning disable 612, 618
|
||||||
modelBuilder
|
modelBuilder
|
||||||
.HasAnnotation("ProductVersion", "2.2.0-rtm-35687");
|
.HasAnnotation("ProductVersion", "2.2.1-servicing-10028");
|
||||||
|
|
||||||
modelBuilder.Entity("Tv7Playlist.Data.PlaylistEntry", b =>
|
modelBuilder.Entity("Tv7Playlist.Data.PlaylistEntry", b =>
|
||||||
{
|
{
|
||||||
@@ -33,8 +33,12 @@ namespace Tv7Playlist.Data.Migrations
|
|||||||
|
|
||||||
b.Property<int>("TrackNumber");
|
b.Property<int>("TrackNumber");
|
||||||
|
|
||||||
|
b.Property<int>("TrackNumberOverride");
|
||||||
|
|
||||||
b.Property<string>("Url");
|
b.Property<string>("Url");
|
||||||
|
|
||||||
|
b.Property<string>("UrlOriginal");
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.HasIndex("Name");
|
b.HasIndex("Name");
|
||||||
|
@@ -7,14 +7,19 @@ namespace Tv7Playlist.Data
|
|||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
public int Position { get; set; }
|
public int Position { get; set; }
|
||||||
|
|
||||||
public int TrackNumber { get; set; }
|
public int TrackNumber { get; set; }
|
||||||
|
|
||||||
|
public int TrackNumberOverride { get; set; }
|
||||||
|
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
public string NameOverride { get; set; }
|
public string NameOverride { get; set; }
|
||||||
|
|
||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
|
|
||||||
|
public string UrlOriginal { get; set; }
|
||||||
|
|
||||||
public bool IsAvailable { get; set; }
|
public bool IsAvailable { get; set; }
|
||||||
|
|
||||||
public bool IsEnabled { get; set; }
|
public bool IsEnabled { get; set; }
|
||||||
|
@@ -6,9 +6,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.1" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.1" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.1" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@@ -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;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Tv7Playlist.Core;
|
using Tv7Playlist.Core;
|
||||||
using Tv7Playlist.Core.Parsers;
|
|
||||||
using Tv7Playlist.Data;
|
|
||||||
|
|
||||||
namespace Tv7Playlist.Controllers
|
namespace Tv7Playlist.Controllers
|
||||||
{
|
{
|
||||||
@@ -15,74 +10,54 @@ namespace Tv7Playlist.Controllers
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
public class PlaylistApiController : Controller
|
public class PlaylistApiController : Controller
|
||||||
{
|
{
|
||||||
|
private const string PlayListContentType = "audio/mpegurl";
|
||||||
|
|
||||||
private readonly IAppConfig _appConfig;
|
private readonly IAppConfig _appConfig;
|
||||||
private readonly ILogger<HomeController> _logger;
|
private readonly ILogger<HomeController> _logger;
|
||||||
private readonly PlaylistContext _playlistContext;
|
|
||||||
private readonly IPlaylistLoader _playlistLoader;
|
|
||||||
|
|
||||||
public PlaylistApiController(ILogger<HomeController> logger, PlaylistContext playlistContext, IPlaylistLoader playlistLoader,
|
private readonly IPlaylistBuilder _playlistBuilder;
|
||||||
IAppConfig appConfig)
|
private readonly IPlaylistSynchronizer _playlistSynchronizer;
|
||||||
|
|
||||||
|
public PlaylistApiController(ILogger<HomeController> logger, IPlaylistSynchronizer playlistSynchronizer,
|
||||||
|
IPlaylistBuilder playlistBuilder, IAppConfig appConfig)
|
||||||
{
|
{
|
||||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
_playlistContext = playlistContext ?? throw new ArgumentNullException(nameof(playlistContext));
|
_playlistSynchronizer = playlistSynchronizer ?? throw new ArgumentNullException(nameof(playlistSynchronizer));
|
||||||
_playlistLoader = playlistLoader ?? throw new ArgumentNullException(nameof(playlistLoader));
|
_playlistBuilder = playlistBuilder ?? throw new ArgumentNullException(nameof(playlistBuilder));
|
||||||
_appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig));
|
_appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[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()
|
public async Task<IActionResult> Synchronize()
|
||||||
{
|
{
|
||||||
//TODO: Refactor to post method
|
await _playlistSynchronizer.SynchronizeAsync();
|
||||||
_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...");
|
|
||||||
|
|
||||||
return Ok();
|
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;
|
private string GetDownloadFileName()
|
||||||
entry.Name = track.Name;
|
{
|
||||||
entry.Url = track.Url;
|
var downloadFileName = _appConfig.DownloadFileName;
|
||||||
}
|
if (string.IsNullOrEmpty(downloadFileName)) downloadFileName = "playlist.m3u";
|
||||||
}
|
|
||||||
|
|
||||||
private void MarkNotAvailableEntries(Dictionary<int, PlaylistEntry> existingEntries,
|
return downloadFileName;
|
||||||
IReadOnlyCollection<ParsedTrack> tracks)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -86,6 +86,8 @@ namespace Tv7Playlist
|
|||||||
private void ConfigureParser(IServiceCollection services, IAppConfig appConfig)
|
private void ConfigureParser(IServiceCollection services, IAppConfig appConfig)
|
||||||
{
|
{
|
||||||
services.AddTransient<IPlaylistLoader, PlaylistLoader>();
|
services.AddTransient<IPlaylistLoader, PlaylistLoader>();
|
||||||
|
services.AddTransient<IPlaylistBuilder, PlaylistBuilder>();
|
||||||
|
services.AddTransient<IPlaylistSynchronizer, PlaylistSynchronizer>();
|
||||||
|
|
||||||
var type = appConfig.SourceType;
|
var type = appConfig.SourceType;
|
||||||
switch (type)
|
switch (type)
|
||||||
|
@@ -9,7 +9,8 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
<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>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
@@ -2,16 +2,19 @@
|
|||||||
@{
|
@{
|
||||||
ViewData["Title"] = "TV7 Playlist";
|
ViewData["Title"] = "TV7 Playlist";
|
||||||
}
|
}
|
||||||
|
<div class="row">
|
||||||
|
<div class="col col-12">
|
||||||
<table class="table table-hover table-striped">
|
<table class="table table-hover table-striped">
|
||||||
<tr>
|
<tr>
|
||||||
<th>Position</th>
|
<th>Position</th>
|
||||||
<th>Number</th>
|
<th>Number</th>
|
||||||
|
<th>Number override</th>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>Name override</th>
|
<th>Name override</th>
|
||||||
<th>Enabled</th>
|
<th>Enabled</th>
|
||||||
<th>Available</th>
|
<th>Available</th>
|
||||||
<th>URL</th>
|
<th>URL</th>
|
||||||
|
<th>original URL</th>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
@{
|
@{
|
||||||
@@ -20,12 +23,16 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td>@track.Position</td>
|
<td>@track.Position</td>
|
||||||
<td>@track.TrackNumber</td>
|
<td>@track.TrackNumber</td>
|
||||||
|
<td>@track.TrackNumberOverride</td>
|
||||||
<td>@track.Name</td>
|
<td>@track.Name</td>
|
||||||
<td>@track.NameOverride</td>
|
<td>@track.NameOverride</td>
|
||||||
<td>@track.IsEnabled</td>
|
<td>@track.IsEnabled</td>
|
||||||
<td>@track.IsAvailable</td>
|
<td>@track.IsAvailable</td>
|
||||||
<td>@track.Url</td>
|
<td>@track.Url</td>
|
||||||
|
<td>@track.UrlOriginal</td>
|
||||||
</tr>
|
</tr>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</table>
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
@@ -34,7 +34,7 @@
|
|||||||
|
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container-fluid">
|
||||||
<main role="main" class="pb-3">
|
<main role="main" class="pb-3">
|
||||||
@RenderBody()
|
@RenderBody()
|
||||||
</main>
|
</main>
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
|
|
||||||
<footer class="border-top footer text-muted">
|
<footer class="border-top footer text-muted">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
© 2018 - Tv7Playlist
|
© 2018 - Philipp Häfelfinger - Tv7Playlist
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user