Пример #1
0
        private SteamApp GetSteamApp(string steamAppId, string[] steamAppTypes = null)
        {
            // rate limiter, 200 requests/5 min
            System.Threading.Thread.Sleep(5000);

            var steamDbUrl = $"http://store.steampowered.com/api/appdetails?appids={ steamAppId }";

            var response = WebUtils.SilentWebRequest(steamDbUrl);

            dynamic deserializedResponse = JsonConvert.DeserializeObject(response);

            if (deserializedResponse == null || deserializedResponse[steamAppId].success == false)
            {
                return(null);
            }

            string appType = deserializedResponse[steamAppId].data.type.ToString();

            if (steamAppTypes != null && !steamAppTypes.Contains(appType))
            {
                return(null);
            }

            return(SteamApp.Create(
                       steamAppId,
                       appType,
                       deserializedResponse[steamAppId].data.name.ToString()
                       ));
        }
Пример #2
0
        private void Load(string file)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            Entries = new SortedDictionary <int, SteamApp>();

            var index      = 0;
            var cacheLines = File.ReadAllLines(file);

            foreach (var line in cacheLines)
            {
                try
                {
                    var steamApp = new SteamApp(line);
                    if (ValidTypes == null || ValidTypes.Contains(steamApp.Type.ToLower()))
                    {
                        Entries.Add(Int32.Parse(steamApp.Id), steamApp);
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteLine($"{ CurrentClassName }.{ Utils.GetCurrentMethodName() }({ file }): index={ index }, line='{ line }', ex={ ex }");
                }
                index++;
            }

            stopwatch.Stop();
            var duration = stopwatch.ElapsedMilliseconds;

            Log.WriteLine($"{ CurrentClassName }.{ Utils.GetCurrentMethodName() }({ file }): [{ Entries.Count } entries] in { duration }ms");

            CurrentCacheFile = file;
        }
Пример #3
0
        private void AddOrUpdateSteamApp(SteamApp newSteamApp, int?missingIndex = null)
        {
            lock (entriesLock)
            {
                // check if id and name is already in cache
                if (Entries.Any(e => e.Key == Int32.Parse(newSteamApp.Id) && e.Value.NormalizedTitle == newSteamApp.NormalizedTitle))
                {
                    return;
                }

                Log.WriteLine($"{ CurrentClassName }.{ Utils.GetCurrentMethodName() }({ newSteamApp.ToString() })");

                if (Entries.Any(e => e.Key == Int32.Parse(newSteamApp.Id)))
                {
                    Entries.Remove(Int32.Parse(newSteamApp.Id));
                }

                Entries.Add(Int32.Parse(newSteamApp.Id), newSteamApp);
                Save();

                if (missingIndex.HasValue)
                {
                    Missing.RemoveAt(missingIndex.Value);
                    SaveMissing();
                }
            }
        }
Пример #4
0
        /// <summary>
        /// query https://steamdb.info/search/?q=... for new SteamAppId-s
        /// also adds result to cache
        /// </summary>
        /// <param name="gameTitle"></param>
        /// <returns></returns>
        private SteamApp[] QuerySteamDb(string gameTitle)
        {
            var result = new List <SteamApp>();

            var url      = $"https://steamdb.info/search/?q={ WebUtility.UrlEncode(gameTitle) }";
            var response = WebUtils.WebRequest(url);

            if (response == null)
            {
                return(result.ToArray());
            }

            var tableElementStart = response.IndexOf("<tbody hidden>");

            if (tableElementStart < 0)
            {
                return(result.ToArray());
            }

            var tableBodyElement = response.Substring(tableElementStart, response.IndexOf("</tbody>", tableElementStart) - tableElementStart + "</tbody>".Length);

            tableBodyElement = tableBodyElement.Replace("<tbody hidden>", "<tbody>");

            var decodedAndCleaned = WebUtility.HtmlDecode(tableBodyElement).Replace("&", string.Empty);

            var xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(decodedAndCleaned);

            foreach (XmlNode steamAppNode in xmlDocument.DocumentElement.ChildNodes)
            {
                var type = steamAppNode.ChildNodes[1].InnerText.ToLower();

                if (!ValidTypes.Contains(type))
                {
                    continue;
                }

                var steamAppId = steamAppNode.Attributes["data-appid"].Value;

                var title = steamAppNode.ChildNodes[2].InnerText;

                // remove muted part of title
                title = Strings.RemoveFromTo(title, "<i class=\"muted\">", "</i>").Trim();
                title = title.Trim('\n');

                result.Add(SteamApp.Create(steamAppId, type, title));
            }

            // add new steam apps to cache
            foreach (var newSteamApp in result)
            {
                AddOrUpdateSteamApp(newSteamApp);
            }

            return(result.ToArray());
        }