2019-06-08 22:32:15 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Roadie.Library.Caching;
|
|
|
|
|
using Roadie.Library.Configuration;
|
|
|
|
|
using Roadie.Library.Encoding;
|
|
|
|
|
using Roadie.Library.MetaData.LastFm;
|
|
|
|
|
using Roadie.Library.Models.Users;
|
2019-06-09 21:31:02 +00:00
|
|
|
|
using Roadie.Library.Utility;
|
2019-06-08 22:32:15 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using data = Roadie.Library.Data;
|
|
|
|
|
|
|
|
|
|
namespace Roadie.Library.Scrobble
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-07-03 16:21:29 +00:00
|
|
|
|
/// Handles NowPlaying and Scrobbling for all integrations
|
2019-06-08 22:32:15 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScrobbleHandler : IScrobbleHandler
|
|
|
|
|
{
|
|
|
|
|
private IRoadieSettings Configuration { get; }
|
2019-07-03 16:21:29 +00:00
|
|
|
|
|
2019-06-08 22:32:15 +00:00
|
|
|
|
private data.IRoadieDbContext DbContext { get; }
|
2019-07-03 16:21:29 +00:00
|
|
|
|
|
2019-06-09 21:31:02 +00:00
|
|
|
|
private IHttpContext HttpContext { get; }
|
2019-06-08 22:32:15 +00:00
|
|
|
|
|
2019-07-03 16:21:29 +00:00
|
|
|
|
private IHttpEncoder HttpEncoder { get; }
|
|
|
|
|
|
|
|
|
|
private ILogger Logger { get; }
|
|
|
|
|
|
2019-06-08 22:32:15 +00:00
|
|
|
|
private IEnumerable<IScrobblerIntegration> Scrobblers { get; }
|
|
|
|
|
|
2019-07-03 16:21:29 +00:00
|
|
|
|
public ScrobbleHandler(IRoadieSettings configuration, ILogger<ScrobbleHandler> logger,
|
|
|
|
|
data.IRoadieDbContext dbContext,
|
|
|
|
|
ICacheManager cacheManager, IHttpEncoder httpEncoder, IHttpContext httpContext)
|
2019-06-08 22:32:15 +00:00
|
|
|
|
{
|
|
|
|
|
Logger = logger;
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
DbContext = dbContext;
|
|
|
|
|
HttpEncoder = httpEncoder;
|
2019-06-09 21:31:02 +00:00
|
|
|
|
HttpContext = httpContext;
|
2019-06-08 22:32:15 +00:00
|
|
|
|
var scrobblers = new List<IScrobblerIntegration>
|
|
|
|
|
{
|
2019-06-09 21:31:02 +00:00
|
|
|
|
new RoadieScrobbler(configuration, logger, dbContext, cacheManager, httpContext)
|
2019-06-08 22:32:15 +00:00
|
|
|
|
};
|
|
|
|
|
if (configuration.Integrations.LastFmProviderEnabled)
|
|
|
|
|
scrobblers.Add(new LastFmHelper(configuration, cacheManager, logger, dbContext, httpEncoder));
|
|
|
|
|
Scrobblers = scrobblers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-07-03 16:21:29 +00:00
|
|
|
|
/// Send Now Playing Requests
|
2019-06-08 22:32:15 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<OperationResult<bool>> NowPlaying(User user, ScrobbleInfo scrobble)
|
|
|
|
|
{
|
|
|
|
|
var s = GetScrobbleInfoDetails(scrobble);
|
2019-07-03 16:21:29 +00:00
|
|
|
|
foreach (var scrobbler in Scrobblers) await Task.Run(async () => await scrobbler.NowPlaying(user, s));
|
2019-06-08 22:32:15 +00:00
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
Data = true,
|
|
|
|
|
IsSuccess = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-07-03 16:21:29 +00:00
|
|
|
|
/// Send any Scrobble Requests
|
2019-06-08 22:32:15 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<OperationResult<bool>> Scrobble(User user, ScrobbleInfo scrobble)
|
|
|
|
|
{
|
|
|
|
|
var s = GetScrobbleInfoDetails(scrobble);
|
2019-07-03 16:21:29 +00:00
|
|
|
|
foreach (var scrobbler in Scrobblers) await Task.Run(async () => await scrobbler.Scrobble(user, s));
|
2019-06-08 22:32:15 +00:00
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
Data = true,
|
|
|
|
|
IsSuccess = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ScrobbleInfo GetScrobbleInfoDetails(ScrobbleInfo scrobble)
|
|
|
|
|
{
|
2019-07-03 16:21:29 +00:00
|
|
|
|
var scrobbleInfo = (from t in DbContext.Tracks
|
|
|
|
|
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
join r in DbContext.Releases on rm.ReleaseId equals r.Id
|
|
|
|
|
join a in DbContext.Artists on r.ArtistId equals a.Id
|
2019-06-08 22:32:15 +00:00
|
|
|
|
where t.RoadieId == scrobble.TrackId
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ArtistName = a.Name,
|
|
|
|
|
ReleaseTitle = r.Title,
|
|
|
|
|
TrackTitle = t.Title,
|
|
|
|
|
t.TrackNumber,
|
|
|
|
|
t.Duration
|
|
|
|
|
}).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
scrobble.ArtistName = scrobbleInfo.ArtistName;
|
|
|
|
|
scrobble.ReleaseTitle = scrobbleInfo.ReleaseTitle;
|
|
|
|
|
scrobble.TrackTitle = scrobbleInfo.TrackTitle;
|
|
|
|
|
scrobble.TrackNumber = scrobbleInfo.TrackNumber.ToString();
|
2019-07-03 16:21:29 +00:00
|
|
|
|
scrobble.TrackDuration = TimeSpan.FromMilliseconds(scrobbleInfo.Duration ?? 0);
|
2019-06-08 22:32:15 +00:00
|
|
|
|
return scrobble;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|