Esempio n. 1
0
        /// <summary>
        /// Pulls a collection of data for a particular user.
        /// </summary>
        /// <returns>True if the request is successful.</returns>
        public bool PullUserData(DataRequest dataRequest)
        {
            var pauseDelay = 3000;

            if (string.IsNullOrWhiteSpace(dataRequest.OutputDirectory))
            {
                throw new InvalidOperationException("You must provide a path to the directory to output data to.");
            }
            else if (string.IsNullOrWhiteSpace(dataRequest.FileNameFormat))
            {
                throw new InvalidOperationException("You must provide a file name format.");
            }
            else if (string.IsNullOrWhiteSpace(dataRequest.Gamertag) || !IsValidGamertag(dataRequest.Gamertag))
            {
                throw new InvalidOperationException("You must provide the valid Xbox Live gamertag of the user to get data for.");
            }

            var outputDirectory = string.Format(dataRequest.OutputDirectory, dataRequest.Gamertag);
            var filePath        = Path.Combine(outputDirectory, dataRequest.FileNameFormat + ".xml");

            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }

            var currentTime = DateTime.Now.ToString("yyyyMMddHHmmss");

            if (dataRequest.GetProfile)
            {
                var profilePath = string.Format(filePath, "profile");
                dataRequest.PerformBackup("profile", "__{0}-" + currentTime);
                XDocument.Parse(this.GetProfile(dataRequest.Gamertag)).Save(profilePath);
                Thread.Sleep(pauseDelay);
            }
            var gamesPath = string.Format(filePath, "games");

            if (dataRequest.GetGames)
            {
                dataRequest.PerformBackup("games", "__{0}-" + currentTime);
                XDocument.Parse(this.GetGames(dataRequest.Gamertag)).Save(gamesPath);
                Thread.Sleep(pauseDelay);
            }
            var friendsPath = string.Format(filePath, "friends");

            if (dataRequest.GetFriends)
            {
                dataRequest.PerformBackup("friends", "__{0}-" + currentTime);
                XDocument.Parse(this.GetFriends(dataRequest.Gamertag)).Save(friendsPath);
                Thread.Sleep(pauseDelay);
            }
            if (dataRequest.GetGameAchievementData)
            {
                if (!File.Exists(gamesPath))
                {
                    throw new NotImplementedException("Games data does not exist. Please GetGames first.");
                }
                if (dataRequest.GameIds != null && dataRequest.GameIds.Count > 0)
                {
                    // Requesting very specific games.
                    foreach (var gameId in dataRequest.GameIds)
                    {
                        var gamePath = string.Format(filePath, "achievements-" + gameId);
                        dataRequest.PerformBackup("achievements-" + gameId, "__{0}-" + currentTime);
                        XDocument.Parse(this.GetAchievements(dataRequest.Gamertag, gameId)).Save(gamePath);
                        Thread.Sleep(pauseDelay);
                    }
                }
                else
                {
                    // We need to get what games the user has played and then get data for them.
                    var playedGames = XDocument.Load(gamesPath).Root.Descendants("Games");
                    if (dataRequest.GetLastGames.HasValue && dataRequest.GetLastGames.Value > 0)
                    {
                        // Limit to only the last x games.
                        playedGames = playedGames.Take(dataRequest.GetLastGames.Value);
                    }
                    foreach (var playedGame in playedGames)
                    {
                        var gameId   = playedGame.Element("ID").Value;
                        var gamePath = string.Format(filePath, "achievements-" + gameId);
                        dataRequest.PerformBackup("achievements-" + gameId, "__{0}-" + currentTime);
                        XDocument.Parse(this.GetAchievements(dataRequest.Gamertag, gameId)).Save(gamePath);
                        Thread.Sleep(pauseDelay);
                    }
                }
            }
            return(true);
        }
        /// <summary>
        /// Pulls a collection of data for a particular user.
        /// </summary>
        /// <returns>True if the request is successful.</returns>
        public bool PullUserData(DataRequest dataRequest)
        {
            var pauseDelay = 3000;

            if (string.IsNullOrWhiteSpace(dataRequest.OutputDirectory))
            {
                throw new InvalidOperationException("You must provide a path to the directory to output data to.");
            }
            else if (string.IsNullOrWhiteSpace(dataRequest.FileNameFormat))
            {
                throw new InvalidOperationException("You must provide a file name format.");
            }
            else if (string.IsNullOrWhiteSpace(dataRequest.Gamertag) || !IsValidGamertag(dataRequest.Gamertag))
            {
                throw new InvalidOperationException("You must provide the valid Xbox Live gamertag of the user to get data for.");
            }

            var outputDirectory = string.Format(dataRequest.OutputDirectory, dataRequest.Gamertag);
            var filePath = Path.Combine(outputDirectory, dataRequest.FileNameFormat + ".xml");
            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }

            var currentTime = DateTime.Now.ToString("yyyyMMddHHmmss");

            if (dataRequest.GetProfile)
            {
                var profilePath = string.Format(filePath, "profile");
                dataRequest.PerformBackup("profile", "__{0}-" + currentTime);
                XDocument.Parse(this.GetProfile(dataRequest.Gamertag)).Save(profilePath);
                Thread.Sleep(pauseDelay);
            }
            var gamesPath = string.Format(filePath, "games");
            if (dataRequest.GetGames)
            {
                dataRequest.PerformBackup("games", "__{0}-" + currentTime);
                XDocument.Parse(this.GetGames(dataRequest.Gamertag)).Save(gamesPath);
                Thread.Sleep(pauseDelay);
            }
            var friendsPath = string.Format(filePath, "friends");
            if (dataRequest.GetFriends)
            {
                dataRequest.PerformBackup("friends", "__{0}-" + currentTime);
                XDocument.Parse(this.GetFriends(dataRequest.Gamertag)).Save(friendsPath);
                Thread.Sleep(pauseDelay);
            }
            if (dataRequest.GetGameAchievementData)
            {
                if (!File.Exists(gamesPath))
                {
                    throw new NotImplementedException("Games data does not exist. Please GetGames first.");
                }
                if (dataRequest.GameIds != null && dataRequest.GameIds.Count > 0)
                {
                    // Requesting very specific games.
                    foreach (var gameId in dataRequest.GameIds)
                    {
                        var gamePath = string.Format(filePath, "achievements-" + gameId);
                        dataRequest.PerformBackup("achievements-" + gameId, "__{0}-" + currentTime);
                        XDocument.Parse(this.GetAchievements(dataRequest.Gamertag, gameId)).Save(gamePath);
                        Thread.Sleep(pauseDelay);
                    }
                }
                else
                {
                    // We need to get what games the user has played and then get data for them.
                    var playedGames = XDocument.Load(gamesPath).Root.Descendants("game");
                    if (dataRequest.GetLastGames.HasValue && dataRequest.GetLastGames.Value > 0)
                    {
                        // Limit to only the last x games.
                        playedGames = playedGames.Take(dataRequest.GetLastGames.Value);
                    }
                    foreach (var playedGame in playedGames)
                    {
                        var gameId = playedGame.Element("id").Value;
                        var gamePath = string.Format(filePath, "achievements-" + gameId);
                        dataRequest.PerformBackup("achievements-" + gameId, "__{0}-" + currentTime);
                        XDocument.Parse(this.GetAchievements(dataRequest.Gamertag, gameId)).Save(gamePath);
                        Thread.Sleep(pauseDelay);
                    }
                }
            }
            return true;
        }