|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.Windows; |
| 6 | +using System.Net; |
| 7 | +using System.Linq; |
| 8 | +using System.Reflection; |
| 9 | +using System.Text.Json; |
| 10 | +using ZemotoCommon; |
| 11 | +using EnhancePoE.Model; |
| 12 | +using EnhancePoE.Api.Data; |
| 13 | + |
| 14 | +namespace EnhancePoE.Api; |
| 15 | + |
| 16 | +internal sealed class StashTabGetter |
| 17 | +{ |
| 18 | + private bool _isFetching; |
| 19 | + |
| 20 | + public async Task<List<StashTab>> FetchStashTabsAsync() |
| 21 | + { |
| 22 | + string accName = Properties.Settings.Default.accName.Trim(); |
| 23 | + string league = Properties.Settings.Default.League.Trim(); |
| 24 | + |
| 25 | + var stashTabPropsList = await GetStashPropsAsync(accName, league); |
| 26 | + if (stashTabPropsList is not null) |
| 27 | + { |
| 28 | + var stashTabs = new List<StashTab>(); |
| 29 | + for (int i = 0; i < stashTabPropsList.tabs.Count; i++) |
| 30 | + { |
| 31 | + var stashTabProps = stashTabPropsList.tabs[i]; |
| 32 | + var uri = new Uri($"https://www.pathofexile.com/character-window/get-stash-items?accountName={accName}&tabIndex={i}&league={league}"); |
| 33 | + stashTabs.Add(new StashTab(stashTabProps.n, stashTabProps.i, uri)); |
| 34 | + } |
| 35 | + |
| 36 | + return stashTabs; |
| 37 | + } |
| 38 | + |
| 39 | + _isFetching = false; |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + private async Task<StashTabPropsList> GetStashPropsAsync(string accName, string league) |
| 44 | + { |
| 45 | + if (_isFetching || RateLimit.CheckForBan()) |
| 46 | + { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + // -1 for 1 request + 3 times if ratelimit high exceeded |
| 51 | + if (RateLimit.RateLimitState[0] >= RateLimit.MaximumRequests - 4) |
| 52 | + { |
| 53 | + RateLimit.RateLimitExceeded = true; |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + _isFetching = true; |
| 58 | + using var __ = new ScopeGuard(() => _isFetching = false); |
| 59 | + |
| 60 | + var propsUri = new Uri($"https://www.pathofexile.com/character-window/get-stash-items?accountName={accName}&tabs=1&league={league}&tabIndex="); |
| 61 | + using var res = await DoAuthenticatedGetRequestAsync(propsUri); |
| 62 | + if (!res.IsSuccessStatusCode) |
| 63 | + { |
| 64 | + _ = MessageBox.Show(res.StatusCode == HttpStatusCode.Forbidden ? "Connection forbidden. Please check your Account Name and Session ID." : res.ReasonPhrase, |
| 65 | + "Error fetching data", MessageBoxButton.OK, MessageBoxImage.Error); |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + using var content = res.Content; |
| 70 | + string resContent = await content.ReadAsStringAsync(); |
| 71 | + return JsonSerializer.Deserialize<StashTabPropsList>(resContent); |
| 72 | + } |
| 73 | + |
| 74 | + public async Task<bool> GetItemsAsync(StashTab stashTab) |
| 75 | + { |
| 76 | + if (_isFetching || RateLimit.CheckForBan()) |
| 77 | + { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + if (RateLimit.RateLimitState[0] >= RateLimit.MaximumRequests - 4) |
| 82 | + { |
| 83 | + RateLimit.RateLimitExceeded = true; |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + _isFetching = true; |
| 88 | + using var __ = new ScopeGuard(() => _isFetching = false); |
| 89 | + |
| 90 | + using var res = await DoAuthenticatedGetRequestAsync(stashTab.StashTabUri); |
| 91 | + if (!res.IsSuccessStatusCode) |
| 92 | + { |
| 93 | + _ = MessageBox.Show(res.ReasonPhrase, "Error fetching data", MessageBoxButton.OK, MessageBoxImage.Error); |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + using var content = res.Content; |
| 98 | + string resContent = await content.ReadAsStringAsync(); |
| 99 | + var deserializedContent = JsonSerializer.Deserialize<ItemList>(resContent); |
| 100 | + |
| 101 | + stashTab.Quad = deserializedContent.quadLayout; |
| 102 | + stashTab.FilterItemsForChaosRecipe(deserializedContent.items); |
| 103 | + |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + private async Task<HttpResponseMessage> DoAuthenticatedGetRequestAsync(Uri uri) |
| 108 | + { |
| 109 | + var cookieContainer = new CookieContainer(); |
| 110 | + cookieContainer.Add(uri, new Cookie("POESESSID", Properties.Settings.Default.SessionId)); |
| 111 | + |
| 112 | + using var handler = new HttpClientHandler() { CookieContainer = cookieContainer }; |
| 113 | + using var client = new HttpClient(handler); |
| 114 | + |
| 115 | + // add user agent |
| 116 | + client.DefaultRequestHeaders.Add("User-Agent", $"EnhancePoEApp/v{Assembly.GetExecutingAssembly().GetName().Version}"); |
| 117 | + |
| 118 | + var res = await client.GetAsync(uri); |
| 119 | + |
| 120 | + // get new rate limit values |
| 121 | + string rateLimit = res.Headers.GetValues("X-Rate-Limit-Account").FirstOrDefault(); |
| 122 | + string rateLimitState = res.Headers.GetValues("X-Rate-Limit-Account-State").FirstOrDefault(); |
| 123 | + string responseTime = res.Headers.GetValues("Date").FirstOrDefault(); |
| 124 | + RateLimit.DeserializeRateLimits(rateLimit, rateLimitState); |
| 125 | + RateLimit.DeserializeResponseSeconds(responseTime); |
| 126 | + |
| 127 | + return res; // Needs to be disposed |
| 128 | + } |
| 129 | +} |
0 commit comments