2022-02-24 21:57:26 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-02-24 00:27:34 +00:00
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
using Selector.Model;
|
|
|
|
|
using Selector.Operations;
|
|
|
|
|
using SpotifyAPI.Web;
|
|
|
|
|
using System;
|
2022-03-03 22:32:29 +00:00
|
|
|
|
using System.Collections.Generic;
|
2022-02-24 00:27:34 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Selector
|
|
|
|
|
{
|
|
|
|
|
public class ScrobbleMapperConfig
|
|
|
|
|
{
|
|
|
|
|
public TimeSpan InterRequestDelay { get; set; }
|
|
|
|
|
public TimeSpan Timeout { get; set; } = new TimeSpan(0, 20, 0);
|
|
|
|
|
public int SimultaneousConnections { get; set; } = 3;
|
|
|
|
|
public int? Limit { get; set; } = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ScrobbleMapper
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<ScrobbleMapper> logger;
|
|
|
|
|
private readonly ILoggerFactory loggerFactory;
|
|
|
|
|
private readonly ISearchClient searchClient;
|
|
|
|
|
|
|
|
|
|
private readonly ScrobbleMapperConfig config;
|
|
|
|
|
|
|
|
|
|
private readonly IScrobbleRepository scrobbleRepo;
|
|
|
|
|
private readonly IScrobbleMappingRepository mappingRepo;
|
|
|
|
|
|
|
|
|
|
public ScrobbleMapper(ISearchClient _searchClient, ScrobbleMapperConfig _config, IScrobbleRepository _scrobbleRepository, IScrobbleMappingRepository _scrobbleMappingRepository, ILogger<ScrobbleMapper> _logger, ILoggerFactory _loggerFactory = null)
|
|
|
|
|
{
|
|
|
|
|
searchClient = _searchClient;
|
|
|
|
|
config = _config;
|
|
|
|
|
scrobbleRepo = _scrobbleRepository;
|
|
|
|
|
mappingRepo = _scrobbleMappingRepository;
|
|
|
|
|
logger = _logger;
|
|
|
|
|
loggerFactory = _loggerFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Execute(CancellationToken token)
|
|
|
|
|
{
|
2022-03-03 22:32:29 +00:00
|
|
|
|
await MapTracks(token);
|
2022-02-24 00:27:34 +00:00
|
|
|
|
|
2022-02-24 21:57:26 +00:00
|
|
|
|
await mappingRepo.Save();
|
|
|
|
|
}
|
2022-02-24 00:27:34 +00:00
|
|
|
|
|
2022-02-24 21:57:26 +00:00
|
|
|
|
private async Task MapTracks(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("Mapping scrobble tracks");
|
|
|
|
|
|
|
|
|
|
var currentTracks = mappingRepo.GetTracks();
|
|
|
|
|
var scrobbleTracks = scrobbleRepo.GetAll()
|
|
|
|
|
.GroupBy(x => (x.ArtistName, x.TrackName))
|
|
|
|
|
.Select(x => (x.Key, x.Count()))
|
|
|
|
|
.OrderByDescending(x => x.Item2)
|
|
|
|
|
.Select(x => x.Key);
|
|
|
|
|
|
|
|
|
|
if (config.Limit is not null)
|
|
|
|
|
{
|
|
|
|
|
scrobbleTracks = scrobbleTracks.Take(config.Limit.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tracksToPull = scrobbleTracks
|
|
|
|
|
.ExceptBy(currentTracks.Select(a => (a.LastfmArtistName, a.LastfmTrackName)), a => a);
|
|
|
|
|
|
|
|
|
|
var requests = tracksToPull.Select(a => new ScrobbleTrackMapping(
|
|
|
|
|
searchClient,
|
|
|
|
|
loggerFactory.CreateLogger<ScrobbleTrackMapping>() ?? NullLogger<ScrobbleTrackMapping>.Instance,
|
|
|
|
|
a.TrackName, a.ArtistName)
|
|
|
|
|
).ToArray();
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Found {} tracks to map, starting", requests.Length);
|
|
|
|
|
|
2022-03-03 22:32:29 +00:00
|
|
|
|
var batchRequest = GetOperation(requests);
|
2022-02-24 21:57:26 +00:00
|
|
|
|
|
|
|
|
|
await batchRequest.TriggerRequests(token);
|
|
|
|
|
|
|
|
|
|
logger.LogInformation("Finished mapping tracks");
|
|
|
|
|
|
2022-03-03 22:32:29 +00:00
|
|
|
|
var newTracks = batchRequest.DoneRequests
|
2022-02-24 21:57:26 +00:00
|
|
|
|
.Select(a => a.Result)
|
|
|
|
|
.Cast<FullTrack>()
|
|
|
|
|
.Where(a => a is not null);
|
|
|
|
|
|
2022-03-03 22:32:29 +00:00
|
|
|
|
var existingTrackUris = currentTracks.Select(a => a.SpotifyUri).ToArray();
|
|
|
|
|
var existingAlbumUris = mappingRepo.GetAlbums().Select(a => a.SpotifyUri).ToArray();
|
|
|
|
|
var existingArtistUris = mappingRepo.GetArtists().Select(a => a.SpotifyUri).ToArray();
|
2022-02-24 22:35:39 +00:00
|
|
|
|
|
2022-03-03 22:32:29 +00:00
|
|
|
|
foreach (var track in newTracks)
|
2022-02-24 22:35:39 +00:00
|
|
|
|
{
|
2022-03-03 22:32:29 +00:00
|
|
|
|
if (existingTrackUris.Contains(track.Uri))
|
2022-02-24 22:35:39 +00:00
|
|
|
|
{
|
2022-03-03 22:32:29 +00:00
|
|
|
|
var artistName = track.Artists.FirstOrDefault()?.Name;
|
|
|
|
|
var duplicates = currentTracks.Where(a => a.LastfmArtistName.Equals(artistName, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
&& a.LastfmTrackName.Equals(track.Name, StringComparison.OrdinalIgnoreCase));
|
2022-02-24 22:35:39 +00:00
|
|
|
|
logger.LogWarning("Found duplicate Spotify uri ({}), [{}, {}] {}",
|
2022-03-03 22:32:29 +00:00
|
|
|
|
track.Uri,
|
|
|
|
|
track.Name,
|
|
|
|
|
artistName,
|
2022-02-24 22:35:39 +00:00
|
|
|
|
string.Join(", ", duplicates.Select(d => $"{d.LastfmTrackName} {d.LastfmArtistName}"))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-03 22:32:29 +00:00
|
|
|
|
mappingRepo.Add(new TrackLastfmSpotifyMapping()
|
|
|
|
|
{
|
|
|
|
|
LastfmTrackName = track.Name,
|
|
|
|
|
LastfmArtistName = track.Artists.FirstOrDefault()?.Name,
|
|
|
|
|
SpotifyUri = track.Uri
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!existingAlbumUris.Contains(track.Album.Uri))
|
|
|
|
|
{
|
|
|
|
|
mappingRepo.Add(new AlbumLastfmSpotifyMapping()
|
|
|
|
|
{
|
|
|
|
|
LastfmAlbumName = track.Album.Name,
|
|
|
|
|
LastfmArtistName = track.Album.Artists.FirstOrDefault()?.Name,
|
|
|
|
|
SpotifyUri = track.Album.Uri
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(var artist in track.Artists.UnionBy(track.Album.Artists, a => a.Name))
|
|
|
|
|
{
|
|
|
|
|
if (!existingArtistUris.Contains(artist.Uri))
|
|
|
|
|
{
|
|
|
|
|
mappingRepo.Add(new ArtistLastfmSpotifyMapping()
|
|
|
|
|
{
|
|
|
|
|
LastfmArtistName = artist.Name,
|
|
|
|
|
SpotifyUri = artist.Uri
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-02-24 22:35:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-24 00:27:34 +00:00
|
|
|
|
}
|
2022-03-03 22:32:29 +00:00
|
|
|
|
|
|
|
|
|
private BatchingOperation<T> GetOperation<T>(IEnumerable<T> requests) where T: IOperation
|
|
|
|
|
=> new (config.InterRequestDelay, config.Timeout, config.SimultaneousConnections, requests);
|
2022-02-24 00:27:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|