2022-02-18 00:08:42 +00:00
|
|
|
|
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 ScrobbleSaveCommand : Command
|
|
|
|
|
{
|
|
|
|
|
public ScrobbleSaveCommand(string name, string description = null) : base(name, description)
|
|
|
|
|
{
|
|
|
|
|
var fromOption = new Option<DateTime>("--from", getDefaultValue: () => DateTime.UtcNow.AddDays(-7), "Date from which to pull scrobbles");
|
|
|
|
|
AddOption(fromOption);
|
|
|
|
|
|
|
|
|
|
var toOption = new Option<DateTime>( "--to", getDefaultValue: () => DateTime.UtcNow.AddHours(1), "Last date for which to pull scrobbles");
|
|
|
|
|
AddOption(toOption);
|
|
|
|
|
|
|
|
|
|
var pageOption = new Option<int>("--page", getDefaultValue: () => 100, "number of scrobbles per page");
|
|
|
|
|
pageOption.AddAlias("-p");
|
|
|
|
|
AddOption(pageOption);
|
|
|
|
|
|
|
|
|
|
var delayOption = new Option<int>("--delay", getDefaultValue: () => 200, "milliseconds to delay");
|
|
|
|
|
delayOption.AddAlias("-d");
|
|
|
|
|
AddOption(delayOption);
|
|
|
|
|
|
2022-02-20 21:22:32 +00:00
|
|
|
|
var simulOption = new Option<int>("--simultaneous", getDefaultValue: () => 5, "simultaneous connections when pulling");
|
|
|
|
|
simulOption.AddAlias("-s");
|
|
|
|
|
AddOption(simulOption);
|
|
|
|
|
|
2022-02-18 00:08:42 +00:00
|
|
|
|
var username = new Option<string>("--username", "user to pulls scrobbles for");
|
|
|
|
|
username.AddAlias("-u");
|
|
|
|
|
AddOption(username);
|
|
|
|
|
|
|
|
|
|
var dontAdd = new Option("--no-add", "don't add any scrobbles to the database");
|
|
|
|
|
dontAdd.AddAlias("-na");
|
|
|
|
|
AddOption(dontAdd);
|
|
|
|
|
|
|
|
|
|
var dontRemove = new Option("--no-remove", "don't remove any scrobbles from the database");
|
|
|
|
|
dontRemove.AddAlias("-nr");
|
|
|
|
|
AddOption(dontRemove);
|
|
|
|
|
|
2022-02-24 21:57:26 +00:00
|
|
|
|
Handler = CommandHandler.Create(async (DateTime from, DateTime to, int page, int delay, int simultaneous, string username, bool noAdd, bool noRemove, CancellationToken token) => await Execute(from, to, page, delay, simultaneous, username, noAdd, noRemove, token));
|
2022-02-18 00:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 21:57:26 +00:00
|
|
|
|
public static async Task<int> Execute(DateTime from, DateTime to, int page, int delay, int simultaneous, string username, bool noAdd, bool noRemove, CancellationToken token)
|
2022-02-18 00:08:42 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var context = new CommandContext().WithLogger().WithDb().WithLastfmApi();
|
|
|
|
|
var logger = context.Logger.CreateLogger("Scrobble");
|
|
|
|
|
|
2022-02-18 22:13:16 +00:00
|
|
|
|
using var db = new ApplicationDbContext(context.DatabaseConfig.Options, context.Logger.CreateLogger<ApplicationDbContext>());
|
2022-02-24 00:27:34 +00:00
|
|
|
|
var repo = new ScrobbleRepository(db);
|
2022-02-18 00:08:42 +00:00
|
|
|
|
|
2022-02-24 00:27:34 +00:00
|
|
|
|
logger.LogInformation("Running from {} to {}", from, to);
|
2022-02-18 00:08:42 +00:00
|
|
|
|
|
2022-02-24 00:27:34 +00:00
|
|
|
|
logger.LogInformation("Searching for {}", username);
|
2022-02-18 00:08:42 +00:00
|
|
|
|
var user = db.Users.AsNoTracking().FirstOrDefault(u => u.UserName == username);
|
|
|
|
|
|
|
|
|
|
if (user is not null)
|
|
|
|
|
{
|
|
|
|
|
if (user.LastFmConnected())
|
|
|
|
|
{
|
2022-02-24 00:27:34 +00:00
|
|
|
|
logger.LogInformation("Last.fm username found ({}), starting...", user.LastFmUsername);
|
|
|
|
|
|
|
|
|
|
if(from.Kind != DateTimeKind.Utc)
|
|
|
|
|
{
|
|
|
|
|
from = from.ToUniversalTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (to.Kind != DateTimeKind.Utc)
|
|
|
|
|
{
|
|
|
|
|
to = to.ToUniversalTime();
|
|
|
|
|
}
|
2022-02-18 00:08:42 +00:00
|
|
|
|
|
|
|
|
|
await new ScrobbleSaver(
|
|
|
|
|
context.LastFmClient.User,
|
2022-02-24 00:27:34 +00:00
|
|
|
|
new ()
|
2022-02-18 00:08:42 +00:00
|
|
|
|
{
|
|
|
|
|
User = user,
|
|
|
|
|
InterRequestDelay = new TimeSpan(0, 0, 0, 0, delay),
|
|
|
|
|
From = from,
|
|
|
|
|
To = to,
|
|
|
|
|
PageSize = page,
|
|
|
|
|
DontAdd = noAdd,
|
2022-02-20 21:22:32 +00:00
|
|
|
|
DontRemove = noRemove,
|
2022-02-24 21:57:26 +00:00
|
|
|
|
SimultaneousConnections = simultaneous
|
2022-02-18 00:08:42 +00:00
|
|
|
|
},
|
2022-02-24 00:27:34 +00:00
|
|
|
|
repo,
|
2022-02-20 21:22:32 +00:00
|
|
|
|
context.Logger.CreateLogger<ScrobbleSaver>(),
|
|
|
|
|
context.Logger)
|
2022-02-18 00:08:42 +00:00
|
|
|
|
.Execute(token);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-02-24 00:27:34 +00:00
|
|
|
|
logger.LogError("{} doesn't have a Last.fm username", username);
|
2022-02-18 00:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-02-24 00:27:34 +00:00
|
|
|
|
logger.LogError("{} not found", username);
|
2022-02-18 00:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|