public static async Task <long?> GetPrimePlatSellOrders(string primeName)
        {
            if (string.IsNullOrEmpty(primeName))
            {
                return(null);
            }

            if (_marketCache.TryGetValue(primeName, out CacheEntry <long?> cacheItem))
            {
                if (!cacheItem.IsExpired(_expirationTimespan))
                {
                    return(cacheItem.Value);
                }
            }

            string partName = PrimePartQueryFix.FixQueryString(primeName);

            if (partName.Equals("forma_blueprint"))
            {
                _marketCache[primeName] = new CacheEntry <long?>(0);
                return(0);
            }

            using (var client = new WebClient())
            {
#if DEBUG
                Console.WriteLine("Hitting API for " + primeName + " platinum value");
#endif
                var uri = new Uri(string.Format(_baseUrl, Uri.EscapeDataString(partName)));

                try
                {
                    string jsonData = await client.DownloadStringTaskAsync(uri);

                    dynamic result = JsonConvert.DeserializeObject(jsonData);

                    IEnumerable <dynamic> orders = result.payload.orders;
                    long?smallestPrice           = orders
                                                   .Where(order => order.user.status == "online" || order.user.status == "ingame")
                                                   .Where(order => order.order_type == "sell")
                                                   .Min(order => order.platinum);

                    _marketCache[primeName] = new CacheEntry <long?>(smallestPrice);
                    return(smallestPrice);
                }
                catch
                {
                    Console.Error.WriteLine("Error getting platinum price for " + primeName);
                    return(null);
                }
            }
        }
Example #2
0
        public static async Task <int?> GetPrimePartDucats(string primeName)
        {
            if (string.IsNullOrEmpty(primeName))
            {
                return(null);
            }

            if (_marketCache.TryGetValue(primeName, out CacheEntry <int?> cacheItem))
            {
                return(cacheItem.Value);
            }

            string partName = PrimePartQueryFix.FixQueryString(primeName);

            if (partName.Equals("forma_blueprint"))
            {
                _marketCache[primeName] = new CacheEntry <int?>(0);
                return(0);
            }

            using (var client = new WebClient())
            {
#if DEBUG
                Console.WriteLine("Hitting API for " + primeName + " ducat value");
#endif
                var uri = new Uri(string.Format(_baseUrl, Uri.EscapeDataString(partName)));

                try
                {
                    string jsonData = await client.DownloadStringTaskAsync(uri);

                    dynamic result = JsonConvert.DeserializeObject(jsonData);

                    IEnumerable <dynamic> results = result.payload.item.items_in_set;
                    int?ducatValue = results
                                     .Where(order => order.url_name == partName)
                                     .Min(order => order.ducats);

                    _marketCache[primeName] = new CacheEntry <int?>(ducatValue);
                    return(ducatValue);
                }
                catch
                {
                    Console.Error.WriteLine("Error getting ducats for " + primeName);
                    return(null);
                }
            }
        }