added clear command, apply network changes to db

This commit is contained in:
andy 2022-02-18 19:47:11 +00:00
parent cd176cea2e
commit a9f33d38f4
6 changed files with 153 additions and 13 deletions

View File

@ -0,0 +1,16 @@
using System.CommandLine;
namespace Selector.CLI
{
public class ScrobbleCommand : Command
{
public ScrobbleCommand(string name, string description = null) : base(name, description)
{
var saveCommand = new ScrobbleSaveCommand("save", "save scrobbles to database");
AddCommand(saveCommand);
var clearCommand = new ScrobbleClearCommand("clear", "clear user scrobbles");
AddCommand(clearCommand);
}
}
}

View File

@ -0,0 +1,70 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Selector.CLI.Extensions;
using Selector.Model;
using Selector.Model.Extensions;
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Selector.CLI
{
public class ScrobbleClearCommand : Command
{
public ScrobbleClearCommand(string name, string description = null) : base(name, description)
{
var fromOption = new Option<DateTime>("--from", "Date from which to pull scrobbles");
AddOption(fromOption);
var toOption = new Option<DateTime>( "--to", "Last date for which to pull scrobbles");
AddOption(toOption);
var username = new Option<string>("--username", "user to pulls scrobbles for");
username.AddAlias("-u");
AddOption(username);
Handler = CommandHandler.Create(async (DateTime? from, DateTime? to, string username, CancellationToken token) => await Execute(from, to, username, token));
}
public static async Task<int> Execute(DateTime? from, DateTime? to, string username, CancellationToken token)
{
try
{
var context = new CommandContext().WithLogger().WithDb();
var logger = context.Logger.CreateLogger("Scrobble");
var db = new ApplicationDbContext(context.DatabaseConfig.Options, context.Logger.CreateLogger<ApplicationDbContext>());
logger.LogInformation("Searching for {0}", username);
var user = db.Users
.Include(u => u.Scrobbles)
.FirstOrDefault(u => u.UserName == username);
if (user is not null)
{
user.Scrobbles = user.Scrobbles
.Where(s => s.Timestamp < (from ?? DateTime.MinValue)
&& s.Timestamp > (to ?? DateTime.MaxValue))
.ToList();
await db.SaveChangesAsync();
}
else
{
logger.LogError("{0} not found", username);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 1;
}
return 0;
}
}
}

View File

@ -12,16 +12,6 @@ using System.Threading.Tasks;
namespace Selector.CLI namespace Selector.CLI
{ {
public class ScrobbleCommand : Command
{
public ScrobbleCommand(string name, string description = null) : base(name, description)
{
var saveCommand = new ScrobbleSaveCommand("save", "save scrobbles to");
AddCommand(saveCommand);
}
}
public class ScrobbleSaveCommand : Command public class ScrobbleSaveCommand : Command
{ {
public ScrobbleSaveCommand(string name, string description = null) : base(name, description) public ScrobbleSaveCommand(string name, string description = null) : base(name, description)

View File

@ -9,7 +9,7 @@
}, },
"Selector.CLI.Scrobble": { "Selector.CLI.Scrobble": {
"commandName": "Project", "commandName": "Project",
"commandLineArgs": "scrobble save -u sarsoo --from \"2022/01/01\"", "commandLineArgs": "scrobble save -u sarsoo --from \"2022/01/01\" -nr -na",
"environmentVariables": { "environmentVariables": {
"DOTNET_ENVIRONMENT": "Development" "DOTNET_ENVIRONMENT": "Development"
}, },
@ -17,7 +17,15 @@
}, },
"Selector.CLI.Scrobble.All": { "Selector.CLI.Scrobble.All": {
"commandName": "Project", "commandName": "Project",
"commandLineArgs": "scrobble save -u sarsoo --from \"2017/01/01\" -p 200 -d 75 --no-remove", "commandLineArgs": "scrobble save -u sarsoo --from \"2017/01/01\" -p 200 -d 75 -na -nr",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
},
"nativeDebugging": true
},
"Selector.CLI.Scrobble.Clear": {
"commandName": "Project",
"commandLineArgs": "scrobble clear -u sarsoo --from \"2022/01/01\"",
"environmentVariables": { "environmentVariables": {
"DOTNET_ENVIRONMENT": "Development" "DOTNET_ENVIRONMENT": "Development"
}, },

View File

@ -8,6 +8,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -69,6 +70,8 @@ namespace Selector
} }
} }
IdentifyDuplicates(scrobbles);
logger.LogDebug("Ordering and filtering pulled scrobbles"); logger.LogDebug("Ordering and filtering pulled scrobbles");
var nativeScrobbles = scrobbles var nativeScrobbles = scrobbles
@ -154,5 +157,34 @@ namespace Selector
return tasks; return tasks;
} }
private void IdentifyDuplicates(IEnumerable<LastTrack> tracks)
{
logger.LogDebug("Identifying duplicates");
var duplicates = tracks
.GroupBy(t => t.TimePlayed?.UtcDateTime)
.Where(g => g.Count() > 1);
foreach(var dupe in duplicates)
{
var dupeString = new StringBuilder();
foreach(var scrobble in dupe)
{
dupeString.Append("(");
dupeString.Append(scrobble.Name);
dupeString.Append(", ");
dupeString.Append(scrobble.AlbumName);
dupeString.Append(", ");
dupeString.Append(scrobble.ArtistName);
dupeString.Append(")");
dupeString.Append(" ");
}
logger.LogInformation("Duplicate at {0}: {1}", dupe.Key, dupeString.ToString());
}
}
} }
} }

View File

@ -1,4 +1,5 @@
using IF.Lastfm.Core.Objects; using IF.Lastfm.Core.Objects;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
@ -13,7 +14,7 @@ namespace Selector
public static bool MatchTime(Scrobble nativeScrobble, Scrobble serviceScrobble) public static bool MatchTime(Scrobble nativeScrobble, Scrobble serviceScrobble)
=> serviceScrobble.Timestamp.Equals(nativeScrobble.Timestamp); => serviceScrobble.Timestamp.Equals(nativeScrobble.Timestamp);
public static (IEnumerable<Scrobble>, IEnumerable<Scrobble>) IdentifyDiffs(IEnumerable<Scrobble> existing, IEnumerable<Scrobble> toApply) public static (IEnumerable<Scrobble>, IEnumerable<Scrobble>) IdentifyDiffs(IEnumerable<Scrobble> existing, IEnumerable<Scrobble> toApply, bool matchContents = true)
{ {
existing = existing.OrderBy(s => s.Timestamp); existing = existing.OrderBy(s => s.Timestamp);
toApply = toApply.OrderBy(s => s.Timestamp); toApply = toApply.OrderBy(s => s.Timestamp);
@ -37,6 +38,11 @@ namespace Selector
if (MatchTime(currentExisting, toApplyIter.Current)) if (MatchTime(currentExisting, toApplyIter.Current))
{ {
if (matchContents)
{
MatchData(currentExisting, toApplyIter.Current);
}
toApplyIter.MoveNext(); toApplyIter.MoveNext();
} }
else else
@ -54,6 +60,24 @@ namespace Selector
return (toAdd, toRemove); return (toAdd, toRemove);
} }
public static void MatchData(Scrobble currentExisting, Scrobble toApply)
{
if (!currentExisting.TrackName.Equals(toApply.TrackName, StringComparison.InvariantCultureIgnoreCase))
{
currentExisting.TrackName = toApply.TrackName;
}
if (!currentExisting.AlbumName.Equals(toApply.AlbumName, StringComparison.InvariantCultureIgnoreCase))
{
currentExisting.AlbumName = toApply.AlbumName;
}
if (!currentExisting.ArtistName.Equals(toApply.ArtistName, StringComparison.InvariantCultureIgnoreCase))
{
currentExisting.ArtistName = toApply.ArtistName;
}
}
public static (IEnumerable<Scrobble>, IEnumerable<Scrobble>) IdentifyDiffsContains(IEnumerable<Scrobble> existing, IEnumerable<Scrobble> toApply) public static (IEnumerable<Scrobble>, IEnumerable<Scrobble>) IdentifyDiffsContains(IEnumerable<Scrobble> existing, IEnumerable<Scrobble> toApply)
{ {
var toAdd = toApply.Where(s => !existing.Contains(s, new ScrobbleComp())); var toAdd = toApply.Where(s => !existing.Contains(s, new ScrobbleComp()));