refactoring
This commit is contained in:
parent
696fd07e86
commit
50d32abd49
20 changed files with 334 additions and 225 deletions
15
src/Poe2Trade.Trade/ITradeMonitor.cs
Normal file
15
src/Poe2Trade.Trade/ITradeMonitor.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using Microsoft.Playwright;
|
||||
using Poe2Trade.Core;
|
||||
|
||||
namespace Poe2Trade.Trade;
|
||||
|
||||
public interface ITradeMonitor : IAsyncDisposable
|
||||
{
|
||||
event Action<string, List<string>, IPage>? NewListings;
|
||||
Task Start(string? dashboardUrl = null);
|
||||
Task AddSearch(string tradeUrl);
|
||||
Task PauseSearch(string searchId);
|
||||
Task<bool> ClickTravelToHideout(IPage page, string? itemId = null);
|
||||
Task<(IPage Page, List<TradeItem> Items)> OpenScrapPage(string tradeUrl);
|
||||
string ExtractSearchId(string url);
|
||||
}
|
||||
|
|
@ -5,12 +5,12 @@ using Serilog;
|
|||
|
||||
namespace Poe2Trade.Trade;
|
||||
|
||||
public class TradeMonitor : IAsyncDisposable
|
||||
public class TradeMonitor : ITradeMonitor
|
||||
{
|
||||
private IBrowserContext? _context;
|
||||
private readonly Dictionary<string, IPage> _pages = new();
|
||||
private readonly HashSet<string> _pausedSearches = new();
|
||||
private readonly AppConfig _config;
|
||||
private readonly SavedSettings _config;
|
||||
|
||||
private const string StealthScript = """
|
||||
Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
|
||||
|
|
@ -38,7 +38,7 @@ public class TradeMonitor : IAsyncDisposable
|
|||
|
||||
public event Action<string, List<string>, IPage>? NewListings;
|
||||
|
||||
public TradeMonitor(AppConfig config)
|
||||
public TradeMonitor(SavedSettings config)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
|
@ -96,7 +96,7 @@ public class TradeMonitor : IAsyncDisposable
|
|||
_pages[searchId] = page;
|
||||
|
||||
await page.GotoAsync(tradeUrl, new PageGotoOptions { WaitUntil = WaitUntilState.NetworkIdle });
|
||||
await Helpers.Sleep(2000);
|
||||
await Helpers.Sleep(Delays.PageLoad);
|
||||
|
||||
page.WebSocket += (_, ws) => HandleWebSocket(ws, searchId, page);
|
||||
|
||||
|
|
@ -177,11 +177,11 @@ public class TradeMonitor : IAsyncDisposable
|
|||
items.Add(ParseTradeItem(r));
|
||||
}
|
||||
}
|
||||
catch { /* Response may not be JSON */ }
|
||||
catch (Exception ex) { Log.Debug(ex, "Non-JSON trade response"); }
|
||||
};
|
||||
|
||||
await page.GotoAsync(tradeUrl, new PageGotoOptions { WaitUntil = WaitUntilState.NetworkIdle });
|
||||
await Helpers.Sleep(2000);
|
||||
await Helpers.Sleep(Delays.PageLoad);
|
||||
Log.Information("Scrap page opened: {Url} ({Count} items)", tradeUrl, items.Count);
|
||||
return (page, items);
|
||||
}
|
||||
|
|
@ -259,7 +259,7 @@ public class TradeMonitor : IAsyncDisposable
|
|||
}
|
||||
}
|
||||
}
|
||||
catch { /* Not all frames are JSON */ }
|
||||
catch (Exception ex) { Log.Debug(ex, "Non-JSON WebSocket frame"); }
|
||||
};
|
||||
|
||||
ws.Close += (_, _) => Log.Warning("WebSocket closed: {SearchId}", searchId);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue