Ejemplo n.º 1
0
        public async Task <List <ExpandedGameLegacy> > GetSearchResults(string searchString, bool alternativeSearch)
        {
            if (searchString.IsNullOrEmpty())
            {
                return(new List <ExpandedGameLegacy>());
            }

            List <ulong> searchResult         = null;
            var          modifiedSearchString = ModelsUtils.GetIgdbSearchString(searchString);
            var          filter = Builders <IgdbSearchResult> .Filter.Eq(a => a.Id, modifiedSearchString);

            var col = alternativeSearch ? Database.Instance.IgdbAltSearches : Database.Instance.IgdbStdSearches;

            var cached = col.Find(filter).FirstOrDefault();

            if (cached != null)
            {
                searchResult = cached.Games;
            }

            if (searchResult == null)
            {
                var matchString = HttpUtility.UrlDecode(modifiedSearchString);
                if (matchString.ContainsAny(bracketsMatchList))
                {
                    return(new List <ExpandedGameLegacy>());
                }

                var whereQuery         = $"where (name ~ *\"{matchString}\"*) | (alternative_names.name ~ *\"{matchString}\"*); fields id; limit 50;";
                var searchQuery        = $"search \"{matchString}\"; fields id; limit 50;";
                var query              = alternativeSearch ? whereQuery : searchQuery;
                var searchStringResult = await igdbApi.SendStringRequest("games", query);

                var tempRes = JsonConvert.DeserializeObject <List <Game> >(searchStringResult);
                searchResult = tempRes.Select(a => a.id).ToList();
                col.InsertOne(new IgdbSearchResult
                {
                    Id    = modifiedSearchString,
                    Games = searchResult
                });
            }

            if (!searchResult.HasItems())
            {
                return(new List <ExpandedGameLegacy>());
            }

            var finalResult = new List <ExpandedGameLegacy>();

            foreach (var game in await igdbApi.Games.Get(searchResult))
            {
                if (game.id == 0)
                {
                    continue;
                }

                var xpanded = new ExpandedGameLegacy()
                {
                    id   = game.id,
                    name = game.name,
                    first_release_date = game.first_release_date * 1000
                };

                if (game.alternative_names?.Any() == true)
                {
                    xpanded.alternative_names = await alternativeNames.Get(game.alternative_names);
                }

                finalResult.Add(xpanded);
            }

            return(finalResult);
        }
Ejemplo n.º 2
0
        public static async Task <List <ExpandedGame> > GetSearchResults(string searchString)
        {
            List <Game> searchResult = null;

            searchString = ModelsUtils.GetIgdbSearchString(searchString);
            var cachePath = Path.Combine(IGDB.CacheDirectory, cacheDir, Playnite.Common.Paths.GetSafeFilename(searchString) + ".json");

            lock (CacheLock)
            {
                if (System.IO.File.Exists(cachePath))
                {
                    var fileInfo = new FileInfo(cachePath);
                    fileInfo.Refresh();
                    if ((DateTime.Now - fileInfo.LastWriteTime).TotalHours <= IGDB.SearchCacheTimeout)
                    {
                        searchResult = JsonConvert.DeserializeObject <List <Game> >(System.IO.File.ReadAllText(cachePath));
                    }
                }
            }

            if (searchResult == null)
            {
                var libraryStringResult = await IGDB.SendStringRequest("games", $"search \"{HttpUtility.UrlDecode(searchString)}\"; fields id; limit 40;");

                searchResult = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);
                lock (CacheLock)
                {
                    Playnite.Common.FileSystem.PrepareSaveFile(cachePath);
                    System.IO.File.WriteAllText(cachePath, libraryStringResult);
                }
            }

            var finalResult = new List <ExpandedGame>();

            for (int i = 0; i < searchResult.Count; i++)
            {
                Game result = null;
                try
                {
                    result = (await GameController.GetItem(searchResult[i].id)).Data;
                }
                catch (Exception e)
                {
                    logger.Error(e, $"Failed to get game {searchResult[i].id}");
                    continue;
                }

                if (result.id == 0)
                {
                    continue;
                }

                var xpanded = new ExpandedGame()
                {
                    id   = result.id,
                    name = result.name,
                    first_release_date = result.first_release_date * 1000
                };

                if (result.alternative_names?.Any() == true)
                {
                    xpanded.alternative_names = new List <AlternativeName>();
                    foreach (var nameId in result.alternative_names)
                    {
                        xpanded.alternative_names.Add((await AlternativeNameController.GetItem(nameId)).Data);
                    }
                }

                finalResult.Add(xpanded);
            }

            return(finalResult);
        }
Ejemplo n.º 3
0
        public async Task <List <ExpandedGameLegacy> > GetSearchResults(string searchString, bool alternativeSearch)
        {
            if (searchString.IsNullOrEmpty())
            {
                return(new List <ExpandedGameLegacy>());
            }

            List <Game> searchResult         = null;
            var         modifiedSearchString = ModelsUtils.GetIgdbSearchString(searchString);
            var         cachePath            = Path.Combine(
                igdbApi.CacheRoot,
                cacheDirName,
                (alternativeSearch ? "alt_" : "srch_") + Playnite.Common.Paths.GetSafePathName(modifiedSearchString) + ".json");

            lock (CacheLock)
            {
                if (System.IO.File.Exists(cachePath))
                {
                    var fileInfo = new FileInfo(cachePath);
                    fileInfo.Refresh();
                    if ((DateTime.Now - fileInfo.LastWriteTime).TotalHours <= settings.Settings.IGDB.SearchCacheTimeout)
                    {
                        using (var fs = new FileStream(cachePath, FileMode.Open, FileAccess.Read))
                            using (var sr = new StreamReader(fs))
                                using (var reader = new JsonTextReader(sr))
                                {
                                    searchResult = jsonSerializer.Deserialize <List <Game> >(reader);
                                }
                    }
                }
            }

            if (searchResult == null)
            {
                var matchString = HttpUtility.UrlDecode(modifiedSearchString);
                if (matchString.ContainsAny(bracketsMatchList))
                {
                    return(new List <ExpandedGameLegacy>());
                }

                var whereQuery         = $"where (name ~ *\"{matchString}\"*) | (alternative_names.name ~ *\"{matchString}\"*); fields id; limit 50;";
                var searchQuery        = $"search \"{matchString}\"; fields id; limit 50;";
                var query              = alternativeSearch ? whereQuery : searchQuery;
                var searchStringResult = await igdbApi.SendStringRequest("games", query);

                searchResult = JsonConvert.DeserializeObject <List <Game> >(searchStringResult);

                lock (CacheLock)
                {
                    Playnite.Common.FileSystem.PrepareSaveFile(cachePath);
                    System.IO.File.WriteAllText(cachePath, searchStringResult);
                }
            }

            var finalResult = new List <ExpandedGameLegacy>();

            for (int i = 0; i < searchResult.Count; i++)
            {
                Game result = null;
                try
                {
                    result = await games.Get(searchResult[i].id);
                }
                catch (Exception e)
                {
                    logger.Error(e, $"Failed to get game {searchResult[i].id}");
                    continue;
                }

                if (result.id == 0)
                {
                    continue;
                }

                var xpanded = new ExpandedGameLegacy()
                {
                    id   = result.id,
                    name = result.name,
                    first_release_date = result.first_release_date * 1000
                };

                if (result.alternative_names?.Any() == true)
                {
                    xpanded.alternative_names = new List <AlternativeName>();
                    foreach (var nameId in result.alternative_names)
                    {
                        xpanded.alternative_names.Add(await alternativeNames.Get(nameId));
                    }
                }

                finalResult.Add(xpanded);
            }

            return(finalResult);
        }