Update to use tarkov.dev api instead of passed in csv by default
This commit is contained in:
parent
dc75a68226
commit
bfcff97fc5
File diff suppressed because it is too large
Load Diff
@ -71,9 +71,8 @@ namespace MarketPriceLookup.Common.Helpers
|
||||
|
||||
private static void LogMessage(string message)
|
||||
{
|
||||
Console.Write(message);
|
||||
Console.WriteLine(message);
|
||||
ResetConsoleColours();
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,10 @@ using Microsoft.VisualBasic.FileIO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MarketPriceLookup.Common.Helpers
|
||||
{
|
||||
@ -15,18 +18,120 @@ namespace MarketPriceLookup.Common.Helpers
|
||||
{
|
||||
if (priceFile.Count == 0)
|
||||
{
|
||||
HydrateDictionary();
|
||||
//HydrateDictionaryCSV();
|
||||
HydrateDictionaryTarkovDev();
|
||||
}
|
||||
|
||||
return priceFile;
|
||||
}
|
||||
|
||||
private static void HydrateDictionaryTarkovDev()
|
||||
{
|
||||
var request = new Dictionary<string, string>()
|
||||
{
|
||||
{"query", "{ items(lang: en) { name avg24hPrice changeLast48hPercent id historicalPrices{ price timestamp } }}"}
|
||||
};
|
||||
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
// call tarkov dev api
|
||||
var httpResponse = httpClient.PostAsJsonAsync("https://api.tarkov.dev/graphql", request).GetAwaiter().GetResult();
|
||||
var responseContent = httpResponse.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
var parsedResponse = JsonSerializer.Deserialize<TarkovDevResponse>(responseContent, new JsonSerializerOptions());
|
||||
|
||||
// iterate over all items returned and filter out bad prices
|
||||
foreach (var item in parsedResponse.data.items)
|
||||
{
|
||||
if (item.historicalPrices.Length == 0)
|
||||
{
|
||||
LoggingHelpers.LogError($"unable to add item {item.id} {item.name} with no historical prices, ignoring");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.changeLast48hPercent > 100)
|
||||
{
|
||||
LoggingHelpers.LogWarning($"Item {item.id} {item.name} Has had recent {item.changeLast48hPercent}% increase in price. {item.historicalPrices.Length} price values");
|
||||
}
|
||||
|
||||
var averagedItemPrice = GetAveragedPrice(item);
|
||||
if (averagedItemPrice == 0)
|
||||
{
|
||||
LoggingHelpers.LogError($"unable to add item {item.id} {item.name} with average price of 0, ignoring");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.name.Contains(" (0/"))
|
||||
{
|
||||
LoggingHelpers.LogWarning($"Skipping 0 durability item: {item.id} {item.name}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (priceFile.ContainsKey(item.id))
|
||||
{
|
||||
// Item already exists in the csv
|
||||
var existingItem = priceFile[item.id];
|
||||
LoggingHelpers.LogError($"Unable to add item: {item.id} {item.name} (price: {item.avg24hPrice}). existing item: {existingItem.TemplateId} {existingItem.Name} (price: {existingItem.Average24hPrice})");
|
||||
if (existingItem.Average24hPrice < averagedItemPrice)
|
||||
{
|
||||
LoggingHelpers.LogError($"Price diff found: existing item price: {existingItem.Average7DaysPrice} new item price: {averagedItemPrice}, using larger price");
|
||||
priceFile.Remove(item.id);
|
||||
}
|
||||
}
|
||||
|
||||
// Item not already in dictionary, add it
|
||||
if (!priceFile.ContainsKey(item.id))
|
||||
{
|
||||
priceFile.Add(item.id, new Prices
|
||||
{
|
||||
Name = item.name,
|
||||
//Price = price,
|
||||
Average24hPrice = item.avg24hPrice,
|
||||
Average7DaysPrice = averagedItemPrice,
|
||||
//Trader = trader,
|
||||
//BuyPackPrice = buyBackPrice,
|
||||
//Currency = currency,
|
||||
TemplateId = item.id,
|
||||
//TraderPrice = traderPrice,
|
||||
});
|
||||
|
||||
LoggingHelpers.LogSuccess($"Adding item: {item.id} {item.name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get items average flea price from all readings taken over the past 7 days
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
private static int GetAveragedPrice(Item item)
|
||||
{
|
||||
var sevenDaysAgoTimestamp = DateTimeOffset.UtcNow.AddDays(-7).ToUnixTimeSeconds();
|
||||
var pricesWithinLast7days = new List<int>();
|
||||
foreach (var historicalPrice in item.historicalPrices)
|
||||
{
|
||||
if (long.Parse(historicalPrice.timestamp) > sevenDaysAgoTimestamp)
|
||||
{
|
||||
pricesWithinLast7days.Add(historicalPrice.price);
|
||||
}
|
||||
}
|
||||
|
||||
if (pricesWithinLast7days.Count == 0)
|
||||
{
|
||||
LoggingHelpers.LogError($"No prices found for item {item.name} in last 7 days, using any availible");
|
||||
pricesWithinLast7days.AddRange(item.historicalPrices.Select(x => x.price));
|
||||
}
|
||||
|
||||
return (int)Math.Round(pricesWithinLast7days.Average());
|
||||
}
|
||||
|
||||
public static Prices GetItemPrice(string key)
|
||||
{
|
||||
// parse csv if dictionary is empty
|
||||
if (priceFile.Count == 0)
|
||||
{
|
||||
HydrateDictionary();
|
||||
//HydrateDictionaryCSV();
|
||||
HydrateDictionaryTarkovDev();
|
||||
}
|
||||
|
||||
if (!priceFile.ContainsKey(key))
|
||||
@ -37,7 +142,7 @@ namespace MarketPriceLookup.Common.Helpers
|
||||
return priceFile[key];
|
||||
}
|
||||
|
||||
private static void HydrateDictionary()
|
||||
private static void HydrateDictionaryCSV()
|
||||
{
|
||||
var workingPath = Directory.GetCurrentDirectory();
|
||||
var inputPath = $"{workingPath}//input";
|
||||
|
30
MarketPriceLookup.Common/Helpers/TarkovDevResponse.cs
Normal file
30
MarketPriceLookup.Common/Helpers/TarkovDevResponse.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MarketPriceLookup.Common.Helpers
|
||||
{
|
||||
|
||||
public class TarkovDevResponse
|
||||
{
|
||||
public Data data { get; set; }
|
||||
}
|
||||
|
||||
public class Data
|
||||
{
|
||||
public List<Item> items { get; set; }
|
||||
}
|
||||
|
||||
public class Item
|
||||
{
|
||||
public string name { get; set; }
|
||||
public int avg24hPrice { get; set; }
|
||||
public double changeLast48hPercent { get; set; }
|
||||
public string id { get; set; }
|
||||
public HistoricalPrice[] historicalPrices { get; set; }
|
||||
}
|
||||
|
||||
public class HistoricalPrice
|
||||
{
|
||||
public int price { get; set; }
|
||||
public string timestamp { get; set; }
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@ namespace MarketPriceLookup
|
||||
foreach (var item in ItemTemplateHelper.Items)
|
||||
{
|
||||
var priceData = MarketPricesHelper.GetItemPrice(item.Key);
|
||||
if (priceData != null && priceData.Price != 0)
|
||||
if (priceData != null && priceData.Average7DaysPrice != 0)
|
||||
{
|
||||
priceList.Add(item.Key, priceData.Average7DaysPrice);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user