added playlist loader

registered appconfig as singleton
added config interfaces
This commit is contained in:
2018-12-29 00:38:20 +01:00
parent f37d8d49ea
commit 590c73f834
11 changed files with 128 additions and 26 deletions

View 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);
}
}
}
}