Add cs.money worker stack with per-worker IPRoyal residential proxy
Brings up the pull-model scraper: the .NET C2 hands skin+wear jobs to Python nodriver workers that scrape cs.money and post results back, plus the supporting Core/EFCore data model, migrations, and docker-compose orchestration. IPRoyal proxying lets workers scale horizontally with a distinct residential exit IP each: every worker process mints its own sticky session at startup, and an in-process forwarding proxy injects the gateway auth so Chromium talks only to an auth-free localhost endpoint (zero CDP). On a Cloudflare challenge a worker rotates to a fresh session/IP and re-warms. Verified end-to-end against live IPRoyal: distinct US residential exits per worker and IP rotation on demand. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,406 +1,89 @@
|
||||
using BlueLaminate.Cli;
|
||||
using BlueLaminate.Cli.Commands;
|
||||
using BlueLaminate.Cli.Logging;
|
||||
using BlueLaminate.Core.DependencyInjection;
|
||||
using BlueLaminate.EFCore.Data;
|
||||
using BlueLaminate.Scraper.CsFloat;
|
||||
using BlueLaminate.Scraper.Skins;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenTelemetry;
|
||||
using OpenTelemetry.Resources;
|
||||
using System.CommandLine;
|
||||
|
||||
// OpenTelemetry logging through a compact console sink that prints one
|
||||
// "{utc timestamp} {message}" line per record. Swapping in an OTLP exporter
|
||||
// later is a change here. Disposed at process exit so buffered records flush.
|
||||
using var loggerFactory = LoggerFactory.Create(logging =>
|
||||
// Generic Host = composition root. The exact same wiring a web frontend would use:
|
||||
// configuration → AddBlueLaminateCore → resolve services per command scope. Args are
|
||||
// deliberately NOT handed to the host (System.CommandLine owns parsing; the host's
|
||||
// command-line config provider would reject bare verbs like "sync-skins"). The
|
||||
// content root is the binary directory so appsettings.json is found regardless of CWD.
|
||||
var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings
|
||||
{
|
||||
logging.AddOpenTelemetry(otel =>
|
||||
{
|
||||
otel.SetResourceBuilder(
|
||||
ResourceBuilder.CreateDefault().AddService("BlueLaminate.Cli"));
|
||||
otel.IncludeFormattedMessage = true;
|
||||
otel.AddProcessor(new SimpleLogRecordExportProcessor(new CompactConsoleLogExporter()));
|
||||
});
|
||||
ContentRootPath = AppContext.BaseDirectory,
|
||||
});
|
||||
|
||||
// Entry point: System.CommandLine builds the command tree, parsing, and help.
|
||||
// New features are added as additional commands here as they're implemented.
|
||||
var forceOption = new Option<bool>("--force")
|
||||
{
|
||||
Description = "Ignore the once-a-month throttle and sync now."
|
||||
};
|
||||
var dryRunOption = new Option<bool>("--dry-run")
|
||||
{
|
||||
Description = "Load and print the skins without writing to the database."
|
||||
};
|
||||
// Reuse the connection string stored in the EFCore project's user secrets (dev).
|
||||
builder.Configuration.AddUserSecrets<SkinTrackerDbContextFactory>(optional: true);
|
||||
|
||||
var syncSkins = new Command(
|
||||
"sync-skins",
|
||||
"Load the CS2 skin catalogue from the CSGO-API dataset and upsert it (throttled to once a month).")
|
||||
// OpenTelemetry logging through a compact console sink that prints one
|
||||
// "{utc timestamp} {message}" line per record. Swapping in an OTLP exporter later
|
||||
// is a change here. ClearProviders drops the default console logger so we don't
|
||||
// double-print.
|
||||
builder.Logging.ClearProviders();
|
||||
// IHttpClientFactory logs each request at Information under these categories; mute
|
||||
// to Warning so the compact console stays one line per app message.
|
||||
builder.Logging.AddFilter("System.Net.Http.HttpClient", LogLevel.Warning);
|
||||
// EF Core logs every SQL command at Information; we only care about failures, so
|
||||
// raise its floor to Warning (failed commands still log at Error).
|
||||
builder.Logging.AddFilter("Microsoft.EntityFrameworkCore", LogLevel.Warning);
|
||||
builder.Logging.AddOpenTelemetry(otel =>
|
||||
{
|
||||
forceOption,
|
||||
dryRunOption,
|
||||
};
|
||||
syncSkins.SetAction((parseResult, ct) =>
|
||||
SyncSkinsAsync(
|
||||
parseResult.GetValue(forceOption),
|
||||
parseResult.GetValue(dryRunOption),
|
||||
loggerFactory,
|
||||
ct));
|
||||
otel.SetResourceBuilder(
|
||||
ResourceBuilder.CreateDefault().AddService("BlueLaminate.Cli"));
|
||||
otel.IncludeFormattedMessage = true;
|
||||
otel.AddProcessor(new SimpleLogRecordExportProcessor(new CompactConsoleLogExporter()));
|
||||
});
|
||||
|
||||
var defIndexOption = new Option<int?>("--def-index")
|
||||
{
|
||||
Description = "CSFloat weapon def_index (e.g. AK-47=7, M4A4=16)."
|
||||
};
|
||||
var paintIndexOption = new Option<int?>("--paint-index")
|
||||
{
|
||||
Description = "CSFloat paint_index for a specific skin (e.g. M4A4 | Cyber Security=985)."
|
||||
};
|
||||
builder.Services.AddBlueLaminateCore(builder.Configuration);
|
||||
|
||||
var sortByOption = new Option<string>("--sort-by")
|
||||
{
|
||||
Description = "Listing sort order: lowest_price, highest_price, most_recent, "
|
||||
+ "lowest_float, highest_float, best_deal, etc.",
|
||||
DefaultValueFactory = _ => "lowest_price",
|
||||
};
|
||||
var maxOption = new Option<int>("--max")
|
||||
{
|
||||
Description = "Maximum number of listings to fetch (paged 50 at a time).",
|
||||
DefaultValueFactory = _ => 50,
|
||||
};
|
||||
var dumpOption = new Option<string?>("--dump")
|
||||
{
|
||||
Description = "Optional file path to write the fetched listings as JSON."
|
||||
};
|
||||
using var host = builder.Build();
|
||||
|
||||
var fetchListings = new Command(
|
||||
"fetch-listings",
|
||||
"Fetch active CSFloat listings for one skin via the official API and print them. "
|
||||
+ "Reads CSFLOAT_API_KEY. Fetch-and-print only — nothing is written to the database.")
|
||||
// This CLI builds the host but doesn't run it, so ValidateOnStart won't fire on its
|
||||
// own — trigger it explicitly. Invalid configuration (e.g. CsFloat:MaxLimit out of
|
||||
// range) fails fast here with a clear message instead of being silently clamped.
|
||||
try
|
||||
{
|
||||
defIndexOption,
|
||||
paintIndexOption,
|
||||
sortByOption,
|
||||
maxOption,
|
||||
dumpOption,
|
||||
};
|
||||
fetchListings.SetAction((parseResult, ct) =>
|
||||
FetchListingsAsync(
|
||||
parseResult.GetValue(defIndexOption),
|
||||
parseResult.GetValue(paintIndexOption),
|
||||
parseResult.GetValue(sortByOption)!,
|
||||
parseResult.GetValue(maxOption),
|
||||
parseResult.GetValue(dumpOption),
|
||||
loggerFactory,
|
||||
ct));
|
||||
|
||||
var maxRequestsOption = new Option<int>("--max-requests")
|
||||
{
|
||||
Description = "Hard cap on API pages this run (rate-limit budget; 200/window).",
|
||||
DefaultValueFactory = _ => 4,
|
||||
};
|
||||
var maxIngestOption = new Option<int>("--max-listings")
|
||||
{
|
||||
Description = "Hard cap on listings ingested this run.",
|
||||
DefaultValueFactory = _ => 200,
|
||||
};
|
||||
var fullOption = new Option<bool>("--full")
|
||||
{
|
||||
Description = "Cold full pass: keep paging past already-seen listings (default is "
|
||||
+ "incremental — stop once caught up)."
|
||||
};
|
||||
|
||||
var sweepListings = new Command(
|
||||
"sweep-listings",
|
||||
"Global incremental sweep of active CSFloat listings into the database. Pages most_recent, "
|
||||
+ "upserts by listing id, paces off rate-limit headers. Reads CSFLOAT_API_KEY.")
|
||||
{
|
||||
maxRequestsOption,
|
||||
maxIngestOption,
|
||||
fullOption,
|
||||
};
|
||||
sweepListings.SetAction((parseResult, ct) =>
|
||||
SweepListingsAsync(
|
||||
parseResult.GetValue(maxRequestsOption),
|
||||
parseResult.GetValue(maxIngestOption),
|
||||
parseResult.GetValue(fullOption),
|
||||
loggerFactory,
|
||||
ct));
|
||||
|
||||
var catalogMaxRequestsOption = new Option<int>("--max-requests")
|
||||
{
|
||||
Description = "Hard cap on API pages across the whole run (rate-limit budget; 200/window).",
|
||||
DefaultValueFactory = _ => 50,
|
||||
};
|
||||
var perSkinCapOption = new Option<int>("--max-per-skin")
|
||||
{
|
||||
Description = "Safety cap on listings fetched per skin before moving on.",
|
||||
DefaultValueFactory = _ => 500,
|
||||
};
|
||||
|
||||
var sweepCatalog = new Command(
|
||||
"sweep-catalog",
|
||||
"Catalogue-driven sweep: query each catalogue skin's listings by def_index+paint_index so "
|
||||
+ "only weapons are fetched (no stickers/cases/agents). Per-skin Removed-tracking. "
|
||||
+ "Reads CSFLOAT_API_KEY.")
|
||||
{
|
||||
catalogMaxRequestsOption,
|
||||
perSkinCapOption,
|
||||
};
|
||||
sweepCatalog.SetAction((parseResult, ct) =>
|
||||
SweepCatalogAsync(
|
||||
parseResult.GetValue(catalogMaxRequestsOption),
|
||||
parseResult.GetValue(perSkinCapOption),
|
||||
loggerFactory,
|
||||
ct));
|
||||
host.Services.GetRequiredService<IStartupValidator>().Validate();
|
||||
}
|
||||
catch (OptionsValidationException ex)
|
||||
{
|
||||
Console.Error.WriteLine("Invalid configuration:");
|
||||
foreach (var failure in ex.Failures)
|
||||
{
|
||||
Console.Error.WriteLine($" - {failure}");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// System.CommandLine builds the command tree, parsing, and help. Each command lives
|
||||
// in its own file under Commands/ and resolves its service from a DI scope.
|
||||
var root = new RootCommand("BlueLaminate CLI — Counter-Strike skin tracker tools.")
|
||||
{
|
||||
syncSkins,
|
||||
fetchListings,
|
||||
sweepListings,
|
||||
sweepCatalog,
|
||||
SyncSkinsCommand.Build(host),
|
||||
FetchListingsCommand.Build(host),
|
||||
SweepListingsCommand.Build(host),
|
||||
SweepCatalogCommand.Build(host),
|
||||
ProbeProxyCommand.Build(host),
|
||||
CaptureCsMoneyCommand.Build(host),
|
||||
};
|
||||
|
||||
return await root.Parse(args).InvokeAsync();
|
||||
|
||||
// Fetch active listings for one skin via CSFloat's official API and print them.
|
||||
// Fetch-and-print only — no DB — so we can verify the real field shapes against a
|
||||
// live key before designing the Listing schema. Defaults to the M4A4 | Cyber
|
||||
// Security sample so it runs with no args.
|
||||
static async Task<int> FetchListingsAsync(
|
||||
int? defIndex, int? paintIndex, string sortBy, int max, string? dumpPath,
|
||||
ILoggerFactory loggerFactory, CancellationToken ct)
|
||||
// Ctrl+C → cancel the action's token so long-running commands (e.g. sweep-catalog,
|
||||
// which loops until stopped) unwind gracefully instead of hard-killing the process
|
||||
// mid-write.
|
||||
using var cts = new CancellationTokenSource();
|
||||
Console.CancelKeyPress += (_, e) =>
|
||||
{
|
||||
var apiKey = Environment.GetEnvironmentVariable("CSFLOAT_API_KEY");
|
||||
if (string.IsNullOrWhiteSpace(apiKey))
|
||||
{
|
||||
Console.Error.WriteLine("Set the CSFLOAT_API_KEY environment variable first.");
|
||||
return 1;
|
||||
}
|
||||
e.Cancel = true; // prevent immediate termination; let the token cancel cleanly
|
||||
cts.Cancel();
|
||||
};
|
||||
|
||||
var def = defIndex ?? 16;
|
||||
var paint = paintIndex ?? 985;
|
||||
|
||||
using var http = CreateHttpClient();
|
||||
var client = new CsFloatListingsClient(
|
||||
http, apiKey, loggerFactory.CreateLogger<CsFloatListingsClient>());
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Fetching up to {max} active listings for def_index={def}, paint_index={paint} "
|
||||
+ $"(sort: {sortBy})…");
|
||||
var listings = await client.GetListingsAsync(def, paint, sortBy, max, ct: ct);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(client.LastRateLimit.ToString());
|
||||
Console.WriteLine();
|
||||
if (listings.Count == 0)
|
||||
{
|
||||
Console.WriteLine("No active listings found.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Console.WriteLine($"{listings.Count} listing(s):");
|
||||
Console.WriteLine($" {"Price",10} {"Float",-10} {"Seed",-6} {"Wear",-16} {"Name"}");
|
||||
foreach (var l in listings)
|
||||
{
|
||||
var st = (l.IsStatTrak ? " ST" : "") + (l.IsSouvenir ? " SV" : "")
|
||||
+ (l.StickerCount > 0 ? $" +{l.StickerCount}stk" : "");
|
||||
Console.WriteLine(
|
||||
$" {l.Price,10:C} {l.FloatValue,-10:0.000000} {l.PaintSeed,-6} "
|
||||
+ $"{l.WearName,-16} {l.MarketHashName}{st}");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(dumpPath))
|
||||
{
|
||||
var json = System.Text.Json.JsonSerializer.Serialize(
|
||||
listings, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
|
||||
await File.WriteAllTextAsync(dumpPath, json, ct);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Wrote {listings.Count} listing(s) to {Path.GetFullPath(dumpPath)}");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch (CsFloatApiException ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
Console.Error.WriteLine(client.LastRateLimit.ToString());
|
||||
return 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Fetch failed: {ex.Message}");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Global incremental sweep of active CSFloat listings into the database. Paces
|
||||
// off rate-limit headers and only marks listings Removed on a complete pass.
|
||||
static async Task<int> SweepListingsAsync(
|
||||
int maxRequests, int maxListings, bool full, ILoggerFactory loggerFactory, CancellationToken ct)
|
||||
{
|
||||
var apiKey = Environment.GetEnvironmentVariable("CSFLOAT_API_KEY");
|
||||
if (string.IsNullOrWhiteSpace(apiKey))
|
||||
{
|
||||
Console.Error.WriteLine("Set the CSFLOAT_API_KEY environment variable first.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
using var http = CreateHttpClient();
|
||||
var client = new CsFloatListingsClient(
|
||||
http, apiKey, loggerFactory.CreateLogger<CsFloatListingsClient>());
|
||||
|
||||
using var db = new SkinTrackerDbContextFactory().CreateDbContext([]);
|
||||
var service = new ListingSweepService(
|
||||
db, client, loggerFactory.CreateLogger<ListingSweepService>());
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Sweeping listings ({(full ? "full cold pass" : "incremental")}; "
|
||||
+ $"max {maxRequests} requests, {maxListings} listings)…");
|
||||
|
||||
var r = await service.SweepAsync(
|
||||
maxRequests: maxRequests,
|
||||
maxListings: maxListings,
|
||||
incremental: !full,
|
||||
ct: ct);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Sweep complete ({r.StoppedReason}):");
|
||||
Console.WriteLine($" Pages fetched : {r.Pages}");
|
||||
Console.WriteLine($" Listings seen : {r.Seen}");
|
||||
Console.WriteLine($" Inserted : {r.Inserted}");
|
||||
Console.WriteLine($" Updated : {r.Updated}");
|
||||
Console.WriteLine($" Removed : {r.Removed}");
|
||||
Console.WriteLine($" Catalog-linked: {r.Linked}");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(client.LastRateLimit.ToString());
|
||||
return 0;
|
||||
}
|
||||
catch (CsFloatApiException ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
Console.Error.WriteLine(client.LastRateLimit.ToString());
|
||||
return 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Sweep failed: {ex.Message}");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Catalogue-driven sweep: query each catalogue skin's listings by def/paint so
|
||||
// only weapons are fetched (no stickers/cases/agents) and Removed-tracking is
|
||||
// accurate per skin. Writes to the database.
|
||||
static async Task<int> SweepCatalogAsync(
|
||||
int maxRequests, int maxPerSkin, ILoggerFactory loggerFactory, CancellationToken ct)
|
||||
{
|
||||
var apiKey = Environment.GetEnvironmentVariable("CSFLOAT_API_KEY");
|
||||
if (string.IsNullOrWhiteSpace(apiKey))
|
||||
{
|
||||
Console.Error.WriteLine("Set the CSFLOAT_API_KEY environment variable first.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
using var http = CreateHttpClient();
|
||||
var client = new CsFloatListingsClient(
|
||||
http, apiKey, loggerFactory.CreateLogger<CsFloatListingsClient>());
|
||||
|
||||
using var db = new SkinTrackerDbContextFactory().CreateDbContext([]);
|
||||
var service = new ListingSweepService(
|
||||
db, client, loggerFactory.CreateLogger<ListingSweepService>());
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Catalogue sweep (weapons only; max {maxRequests} requests, {maxPerSkin}/skin)…");
|
||||
|
||||
var r = await service.SweepCatalogAsync(
|
||||
maxRequests: maxRequests, maxListingsPerSkin: maxPerSkin, ct: ct);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Catalogue sweep complete ({r.StoppedReason}):");
|
||||
Console.WriteLine($" Skins covered : {r.SkinsCovered}");
|
||||
Console.WriteLine($" Skins skipped : {r.SkinsSkipped}");
|
||||
Console.WriteLine($" Pages fetched : {r.Pages}");
|
||||
Console.WriteLine($" Listings seen : {r.Seen}");
|
||||
Console.WriteLine($" Inserted : {r.Inserted}");
|
||||
Console.WriteLine($" Updated : {r.Updated}");
|
||||
Console.WriteLine($" Removed : {r.Removed}");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(client.LastRateLimit.ToString());
|
||||
return 0;
|
||||
}
|
||||
catch (CsFloatApiException ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
Console.Error.WriteLine(client.LastRateLimit.ToString());
|
||||
return 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Catalogue sweep failed: {ex.Message}");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Load the CS2 skin catalogue from the CSGO-API dataset and upsert it. Weapons
|
||||
// and collections are derived from the skins themselves. Throttled to once a
|
||||
// month unless --force; --dry-run loads and prints without a DB.
|
||||
static async Task<int> SyncSkinsAsync(
|
||||
bool force, bool dryRun, ILoggerFactory loggerFactory, CancellationToken ct)
|
||||
{
|
||||
var logger = loggerFactory.CreateLogger("BlueLaminate.Cli.SyncSkins");
|
||||
var client = new SkinCatalogClient(CreateHttpClient());
|
||||
|
||||
if (dryRun)
|
||||
{
|
||||
logger.LogInformation("Loading skin catalogue (dry run — nothing will be written).");
|
||||
var skins = await client.FetchAsync(ct);
|
||||
logger.LogInformation("Loaded {Count} skins.", skins.Count);
|
||||
Console.WriteLine($"Loaded {skins.Count} skins (dry run, nothing written):");
|
||||
foreach (var s in skins)
|
||||
{
|
||||
var tags = (s.StatTrakAvailable ? " ST" : "") + (s.SouvenirAvailable ? " SV" : "");
|
||||
var range = s.FloatMin is not null ? $"{s.FloatMin:0.00}-{s.FloatMax:0.00}" : "—";
|
||||
var sources = s.Sources.Count > 0 ? string.Join(", ", s.Sources.Select(x => x.Name)) : "—";
|
||||
var idx = $"{s.DefIndex?.ToString() ?? "—"}/{s.PaintIndex?.ToString() ?? "—"}";
|
||||
Console.WriteLine(
|
||||
$" {idx,-10} {s.WeaponName,-16} {s.Name,-24} {s.Rarity,-14} {range,-10} {sources}{tags}");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
using var db = new SkinTrackerDbContextFactory().CreateDbContext([]);
|
||||
var service = new SkinSyncService(db, client, loggerFactory.CreateLogger<SkinSyncService>());
|
||||
var result = await service.SyncAsync(force, ct);
|
||||
|
||||
if (result.Skipped)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Skipped: skins were last synced {result.LastRanAt:u}. "
|
||||
+ "Next run allowed one month later — pass --force to override.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Synced {result.Loaded} skins: {result.Inserted} inserted, "
|
||||
+ $"{result.Updated} updated, "
|
||||
+ $"{result.Loaded - result.Inserted - result.Updated} unchanged "
|
||||
+ $"({result.WeaponsCreated} weapons, {result.CollectionsCreated} collections created).");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static HttpClient CreateHttpClient()
|
||||
{
|
||||
var http = new HttpClient();
|
||||
http.Timeout = TimeSpan.FromMinutes(2);
|
||||
http.DefaultRequestHeaders.UserAgent.ParseAdd("BlueLaminate.Cli");
|
||||
return http;
|
||||
}
|
||||
return await root.Parse(args).InvokeAsync(cancellationToken: cts.Token);
|
||||
|
||||
Reference in New Issue
Block a user