public static async Task <ulong> GetIgdbMatch(ulong gameId)
        {
            var cache = Database.SteamIgdbMatches.FindById(gameId);

            if (cache != null)
            {
                return(cache.igdbId);
            }

            var libraryStringResult = await IGDB.SendStringRequest("games",
                                                                   $"fields id; where external_games.uid = \"{gameId}\" & external_games.category = 1; limit 1;");

            var games = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

            if (games.Any())
            {
                Database.SteamIgdbMatches.Upsert(new SteamIdGame()
                {
                    steamId = gameId,
                    igdbId  = games.First().id
                });

                return(games.First().id);
            }
            else
            {
                return(0);
            }
        }
Beispiel #2
0
        public async Task <ServicesResponse <List <Game> > > Get(string gameName)
        {
            gameName = gameName.ToLower();
            var cachePath = Path.Combine(IGDB.CacheDirectory, cacheDir, Playnite.Common.System.Paths.GetSafeFilename(gameName) + ".json");

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

            var url = string.Format(@"games/?fields=*&limit=40&offset=0&search={0}", gameName);
            var libraryStringResult = await IGDB.SendStringRequest(url);

            var games = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

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

            return(new ServicesResponse <List <Game> >(games));
        }
        public async Task <ServicesResponse <ulong> > Get(ulong gameId)
        {
            var cache = cacheCollection.FindById(gameId);

            if (cache != null)
            {
                return(new ServicesResponse <ulong>(cache.igdbId));
            }

            var url = string.Format(@"/games/?fields=name,id&filter[external.steam][eq]={0}&limit=1", gameId);
            var libraryStringResult = await IGDB.SendStringRequest(url);

            var games = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

            if (games.Any())
            {
                cacheCollection.Upsert(new SteamIdGame()
                {
                    steamId       = gameId,
                    igdbId        = games.First().id,
                    creation_time = DateTime.Now
                });

                return(new ServicesResponse <ulong>(games.First().id));
            }
            else
            {
                return(new ServicesResponse <ulong>(0));
            }
        }
Beispiel #4
0
        public async Task <TItem> GetItem <TItem>(ulong itemId, string endpointPath, object cacheLock)
        {
            var cachePath = Path.Combine(IGDB.CacheDirectory, endpointPath, itemId + ".json");

            lock (cacheLock)
            {
                if (System.IO.File.Exists(cachePath))
                {
                    var cacheItem = JsonConvert.DeserializeObject <TItem>(System.IO.File.ReadAllText(cachePath));
                    if (cacheItem != null)
                    {
                        return(cacheItem);
                    }
                }
            }

            var url          = string.Format(endpointPath + @"/{0}?fields=*", itemId);
            var stringResult = await IGDB.SendStringRequest(url);

            var item = Serialization.FromJson <List <TItem> >(stringResult)[0];

            lock (cacheLock)
            {
                FileSystem.PrepareSaveFile(cachePath);
                System.IO.File.WriteAllText(cachePath, Serialization.ToJson(item));
            }

            return(item);
        }
        public async Task <ServicesResponse <ulong> > Get(ulong gameId)
        {
            var cache = cacheCollection.FindById(gameId);

            if (cache != null)
            {
                return(new ServicesResponse <ulong>(cache.igdbId));
            }

            var libraryStringResult = await IGDB.SendStringRequest("games",
                                                                   $"fields id; where external_games.uid = \"{gameId}\" & external_games.category = 1; limit 1;");

            var games = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

            if (games.Any())
            {
                cacheCollection.Upsert(new SteamIdGame()
                {
                    steamId       = gameId,
                    igdbId        = games.First().id,
                    creation_time = DateTime.Now
                });

                return(new ServicesResponse <ulong>(games.First().id));
            }
            else
            {
                return(new ServicesResponse <ulong>(0));
            }
        }
        public async Task <ServicesResponse <ulong> > Get(string gameName, [FromQuery] string apiKey)
        {
            //var cacheCollection = Program.DatabaseCache.GetCollection<SteamIdGame>("IGBDSteamIdCache");
            //var cache = cacheCollection.FindById(ulongId);
            //if (cache != null)
            //{
            //    var dateDiff = DateTime.Now - cache.creation_time;
            //    if (dateDiff.TotalHours <= (IGDB.CacheTimeout + 100))
            //    {
            //        return new ServicesResponse<ulong>(cache.igdbId, string.Empty);
            //    }
            //}

            var url = string.Format(@"https://namematcher.igdb.com/entityrecognition?subject={0}&limit=5", gameName);
            var libraryStringResult = await IGDB.SendDirectRequest(url);

            var games = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

            if (games.Any())
            {
                //cacheCollection.Upsert(new SteamIdGame()
                //{
                //    steamId = ulongId,
                //    igdbId = games.First().id,
                //    creation_time = DateTime.Now
                //});

                return(new ServicesResponse <ulong>(games.First().id));
            }
            else
            {
                return(new ServicesResponse <ulong>(0));
            }
        }
Beispiel #7
0
        public async Task <ServicesResponse <List <Game> > > Get(string gameName, [FromQuery] string apiKey)
        {
            gameName = gameName.ToLower();
            var cacheCollection = Program.DatabaseCache.GetCollection <GamesSearch>("IGBDSearchCache");
            var cache           = cacheCollection.FindById(gameName);

            if (cache != null)
            {
                var dateDiff = DateTime.Now - cache.creation_time;
                if (dateDiff.TotalHours <= IGDB.CacheTimeout)
                {
                    return(new ServicesResponse <List <Game> >(cache.results, string.Empty));
                }
            }

            var url = string.Format(@"games/?fields=name,first_release_date&limit=40&offset=0&search={0}", gameName);
            var libraryStringResult = await IGDB.SendStringRequest(url, apiKey);

            var games = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

            cacheCollection.Upsert(new GamesSearch()
            {
                keyword       = gameName,
                results       = games,
                creation_time = DateTime.Now
            });

            return(new ServicesResponse <List <Game> >(games, string.Empty));
        }
        public async Task <ServicesResponse <Game> > Get(ulong gameId, [FromQuery] string apiKey)
        {
            var url = string.Format(@"games/{0}?fields=*&limit=40&offset=0&search={0}", gameId);
            var libraryStringResult = await IGDB.SendStringRequest(url, apiKey);

            var game = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

            return(new ServicesResponse <Game>(game[0], string.Empty));
        }
Beispiel #9
0
        public async Task <ServicesResponse <Game> > Get(ulong gameId, [FromQuery] string apiKey)
        {
            var url = string.Format(@"games/{0}?fields=name%2Csummary%2Cdevelopers%2Cpublishers%2Cgenres%2Cfirst_release_date%2Ccover%2Cthemes%2Cgame_modes%2Cwebsites&limit=40&offset=0&search={0}", gameId);
            var libraryStringResult = await IGDB.SendStringRequest(url, apiKey);

            var game = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

            return(new ServicesResponse <Game>(game[0], string.Empty));
        }
Beispiel #10
0
        public static async Task <TItem> GetItem <TItem>(ulong itemId, string endpointPath, object cacheLock)
        {
            var cachePath = Path.Combine(IGDB.CacheDirectory, endpointPath, itemId + ".json");

            lock (cacheLock)
            {
                if (System.IO.File.Exists(cachePath))
                {
                    using (var fs = new FileStream(cachePath, FileMode.Open, FileAccess.Read))
                        using (var sr = new StreamReader(fs))
                            using (var reader = new JsonTextReader(sr))
                            {
                                var cacheItem = jsonSerializer.Deserialize <TItem>(reader);
                                if (cacheItem != null)
                                {
                                    return(cacheItem);
                                }
                            }
                }
            }

            var stringResult = await IGDB.SendStringRequest(endpointPath, $"fields *; where id = {itemId};");

            var items = Serialization.FromJson <List <TItem> >(stringResult);

            TItem item;

            // IGDB resturns empty results of the id is a duplicate of another game
            if (items.Count > 0)
            {
                item = items[0];
            }
            else
            {
                item = typeof(TItem).CrateInstance <TItem>();
            }

            lock (cacheLock)
            {
                FileSystem.PrepareSaveFile(cachePath);

                if (items.Count > 0)
                {
                    System.IO.File.WriteAllText(cachePath, stringResult.Trim(arrayTrim), Encoding.UTF8);
                }
                else
                {
                    System.IO.File.WriteAllText(cachePath, Serialization.ToJson(item), Encoding.UTF8);
                }
            }

            return(item);
        }
Beispiel #11
0
        public async Task <ServicesResponse <Platform> > Get(UInt64 platformId, [FromQuery] string apiKey)
        {
            var cacheCollection = Program.DatabaseCache.GetCollection <Platform>("IGBDPlatformsCache");
            var cache           = cacheCollection.FindById(platformId);

            if (cache != null)
            {
                return(new ServicesResponse <Platform>(cache, string.Empty));
            }

            var url          = string.Format(@"platforms/{0}?fields=name,url", platformId);
            var stringResult = await IGDB.SendStringRequest(url, apiKey);

            var theme = JsonConvert.DeserializeObject <List <Platform> >(stringResult)[0];

            cacheCollection.Insert(theme);
            return(new ServicesResponse <Platform>(theme, string.Empty));
        }
Beispiel #12
0
        public async Task <ServicesResponse <Genre> > Get(UInt64 genreId, [FromQuery] string apiKey)
        {
            var cacheCollection = Program.DatabaseCache.GetCollection <Genre>("IGBDGenresCache");
            var cache           = cacheCollection.FindById(genreId);

            if (cache != null)
            {
                return(new ServicesResponse <Genre>(cache, string.Empty));
            }

            var url          = string.Format(@"genres/{0}?fields=name", genreId);
            var stringResult = await IGDB.SendStringRequest(url, apiKey);

            var genre = JsonConvert.DeserializeObject <List <Genre> >(stringResult)[0];

            cacheCollection.Insert(genre);
            return(new ServicesResponse <Genre>(genre, string.Empty));
        }
        public async Task <ServicesResponse <List <Game> > > Get(string gameName)
        {
            List <Game> searchResult = null;

            gameName = gameName.ToLower();
            var cachePath = Path.Combine(IGDB.CacheDirectory, cacheDir, Playnite.Common.System.Paths.GetSafeFilename(gameName) + ".json");

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

            if (searchResult == null)
            {
                var url = string.Format(@"games/?fields=id&limit=40&offset=0&search={0}", gameName);
                var libraryStringResult = await IGDB.SendStringRequest(url);

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

            using (var gameController = new GameController())
            {
                for (int i = 0; i < searchResult.Count; i++)
                {
                    searchResult[i] = (await gameController.Get(searchResult[i].id)).Data;
                }
            }

            return(new ServicesResponse <List <Game> >(searchResult));
        }
Beispiel #14
0
        public static async Task <TItem> GetItem <TItem>(ulong itemId, string endpointPath, object cacheLock)
        {
            var cachePath = Path.Combine(IGDB.CacheDirectory, endpointPath, itemId + ".json");

            lock (cacheLock)
            {
                if (System.IO.File.Exists(cachePath))
                {
                    var cacheItem = JsonConvert.DeserializeObject <TItem>(System.IO.File.ReadAllText(cachePath));
                    if (cacheItem != null)
                    {
                        return(cacheItem);
                    }
                }
            }

            var stringResult = await IGDB.SendStringRequest(endpointPath, $"fields *; where id = {itemId};");

            var items = Serialization.FromJson <List <TItem> >(stringResult);

            TItem item;

            // IGDB resturns empty results of the id is a duplicate of another game
            if (items.Count > 0)
            {
                item = items[0];
            }
            else
            {
                item = typeof(TItem).CrateInstance <TItem>();
            }

            lock (cacheLock)
            {
                FileSystem.PrepareSaveFile(cachePath);
                System.IO.File.WriteAllText(cachePath, Serialization.ToJson(item));
            }

            return(item);
        }
Beispiel #15
0
        public async Task <ServicesResponse <ulong> > Get(string gameId, [FromQuery] string apiKey)
        {
            var ulongId         = ulong.Parse(gameId);
            var cacheCollection = Program.DatabaseCache.GetCollection <SteamIdGame>("IGBDSteamIdCache");
            var cache           = cacheCollection.FindById(ulongId);

            if (cache != null)
            {
                var dateDiff = DateTime.Now - cache.creation_time;
                if (dateDiff.TotalHours <= (IGDB.CacheTimeout + 100))
                {
                    return(new ServicesResponse <ulong>(cache.igdbId, string.Empty));
                }
            }

            var url = string.Format(@"/games/?fields=name,id&filter[external.steam][eq]={0}&limit=1", gameId);
            var libraryStringResult = await IGDB.SendStringRequest(url, apiKey);

            var games = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);

            if (games.Any())
            {
                cacheCollection.Upsert(new SteamIdGame()
                {
                    steamId       = ulongId,
                    igdbId        = games.First().id,
                    creation_time = DateTime.Now
                });

                return(new ServicesResponse <ulong>(games.First().id, string.Empty));
            }
            else
            {
                return(new ServicesResponse <ulong>(0, string.Empty));
            }
        }
Beispiel #16
0
        public async Task <ServicesResponse <List <ExpandedGame> > > Get(string gameName)
        {
            List <Game> searchResult = null;

            gameName = gameName.ToLower().Trim();
            gameName = Regex.Replace(gameName, @"\s+", " ");
            var cachePath = Path.Combine(IGDB.CacheDirectory, cacheDir, Playnite.Common.Paths.GetSafeFilename(gameName) + ".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(gameName)}\"; 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>();

            using (var gameController = new GameController(appSettings))
            {
                for (int i = 0; i < searchResult.Count; i++)
                {
                    Game result = null;
                    try
                    {
                        result = (await gameController.Get(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(new ServicesResponse <List <ExpandedGame> >(finalResult));
        }
Beispiel #17
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)
                    {
                        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 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);
        }
        public async Task <ServicesResponse <ParsedGame> > Get(ulong gameId, [FromQuery] string apiKey)
        {
            var cacheCollection = Program.DatabaseCache.GetCollection <ParsedGame>("IGBDParsedGameCache");
            var cache           = cacheCollection.FindById(gameId);

            if (cache != null)
            {
                var dateDiff = DateTime.Now - cache.creation_time;
                if (dateDiff.TotalHours <= IGDB.CacheTimeout)
                {
                    return(new ServicesResponse <ParsedGame>(cache, string.Empty));
                }
            }

            var url = string.Format(@"games/{0}?fields=*&limit=40&offset=0&search={0}", gameId);
            var libraryStringResult = await IGDB.SendStringRequest(url, apiKey);

            var game       = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult)[0];
            var parsedGame = new ParsedGame()
            {
                id   = game.id,
                name = game.name,
                first_release_date = game.first_release_date,
                cover             = game.cover?.url,
                websites          = game.websites,
                summary           = game.summary,
                creation_time     = DateTime.Now,
                rating            = game.rating,
                aggregated_rating = game.aggregated_rating,
                total_rating      = game.total_rating,
                alternative_names = game.alternative_names,
                external          = game.external,
                screenshots       = game.screenshots,
                videos            = game.videos
            };

            if (game.developers?.Any() == true)
            {
                parsedGame.developers = new List <string>();
                foreach (var dev in game.developers)
                {
                    var dbDev = (await(new CompanyController()).Get(dev, apiKey)).Data;
                    parsedGame.developers.Add(dbDev.name);
                }
            }

            if (game.game_modes?.Any() == true)
            {
                parsedGame.game_modes = new List <string>();
                foreach (var mode in game.game_modes)
                {
                    var dbMode = (await(new GameModeController()).Get(mode, apiKey)).Data;
                    parsedGame.game_modes.Add(dbMode.name);
                }
            }

            if (game.genres?.Any() == true)
            {
                parsedGame.genres = new List <string>();
                foreach (var genre in game.genres)
                {
                    var dbGenre = (await(new GenreController()).Get(genre, apiKey)).Data;
                    parsedGame.genres.Add(dbGenre.name);
                }
            }

            if (game.publishers?.Any() == true)
            {
                parsedGame.publishers = new List <string>();
                foreach (var pub in game.publishers)
                {
                    var dbDev = (await(new CompanyController()).Get(pub, apiKey)).Data;
                    parsedGame.publishers.Add(dbDev.name);
                }
            }

            if (game.themes?.Any() == true)
            {
                parsedGame.themes = new List <string>();
                foreach (var genre in game.themes)
                {
                    var dbTheme = (await(new ThemeController()).Get(genre, apiKey)).Data;
                    parsedGame.themes.Add(dbTheme.name);
                }
            }

            cacheCollection.Upsert(parsedGame);
            return(new ServicesResponse <ParsedGame>(parsedGame, string.Empty));
        }