Ejemplo n.º 1
0
        /// <summary>
        /// Parses the account and profile settings page of the user and creates a user out of it.
        /// </summary>
        /// <param name="accountSettingsPage">The account settings page, which contains the user name and the email address of the user.</param>
        /// <param name="profileSettingsPage">The profile settings page, which contains the full name and the avatar of the user.</param>
        /// <returns>Returns the created user with the parsed information.</returns>
        public static User FromHtml(IHtmlDocument accountSettingsPage, IHtmlDocument profileSettingsPage)
        {
            // Creates a new user
            User user = new User();

            // Tries to parse the account settings page for the user name and the email address, if it could not be parsed, then an exception is thrown
            try
            {
                IElement accountSettingsForm = accountSettingsPage.QuerySelector("#setting");
                user.UserName = accountSettingsForm.QuerySelectorAll("input").FirstOrDefault(input => input.GetAttribute("name") == "login_name").GetAttribute("value");
                user.EmailAddress = accountSettingsForm.QuerySelectorAll("input").FirstOrDefault(input => input.GetAttribute("name") == "email").GetAttribute("value");
            }
            catch (Exception exception)
            {
                throw new NineGagException("The user name and the email address could not be parsed. This could be an indicator, that the 9GAG website is down or its content has changed. If this problem keeps coming, then please report this problem to 9GAG or the maintainer of the library.", exception);
            }

            // Tries to parse the profile settings page for the full name and the avatar image of the user, if it could not be parsed, then an exception is thrown
            try
            {
                user.FullName = profileSettingsPage.QuerySelectorAll("input").FirstOrDefault(input => input.GetAttribute("name") == "fullName").GetAttribute("value");
                user.AvatarUri = new Uri(profileSettingsPage.QuerySelector("#jsid-profile-avatar").GetAttribute("src"), UriKind.Absolute);
            }
            catch (Exception exception)
            {
                throw new NineGagException("The full name and the avatar image could not be parsed. This could be an indicator, that the 9GAG website is down or its content has changed. If this problem keeps coming, then please report this problem to 9GAG or the maintainer of the library.", exception);
            }

            // Returns the created user
            return user;
        }
        /// <summary>
        /// Gets the information about the currently signed in user.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token, which can be used to cancel the retrieval of the user information.</param>
        /// <exception cref="NineGagException">If the information about the currently signed in user could not be retrieved, then a <see cref="NineGagException"/> exception is thrown.</exception>
        /// <returns>Returns the information about the current user. If the user is not signed in, then <c>null</c> is returned.</returns>
        public async Task<User> GetCurrentUserAsync(CancellationToken cancellationToken)
        {
            // Checks if the current user is signed in, if not then nothing needs to be done
            if (!this.IsUserSignedIn)
                return null;

            // Checks if the user information has already been retrieved, if not then it is fetched
            if (this.currentUser == null)
            {
                // Tries to get the account and profile settings pages for the currently signed in user, if they could not be retrieved, then an exception is thrown
                string accountSettingsPageContent = null, profileSettingsPageContent = null;
                try
                {
                    HttpResponseMessage responseMessage = await this.httpClient.GetAsync(NineGagClient.accountSettingsPath, cancellationToken);
                    responseMessage.EnsureSuccessStatusCode();
                    accountSettingsPageContent = await responseMessage.Content.ReadAsStringAsync();
                    responseMessage = await this.httpClient.GetAsync(NineGagClient.profileSettingsPath, cancellationToken);
                    responseMessage.EnsureSuccessStatusCode();
                    profileSettingsPageContent = await responseMessage.Content.ReadAsStringAsync();
                }
                catch (Exception exception)
                {
                    throw new NineGagException("The account settings page or the profile page of the currently signed in user could not be retrieved. Maybe there is no internet connection available.", exception);
                }

                // Tries to parse the HTML of the two settings pages, if the HTML could not be parsed, then an exception is thrown
                IHtmlDocument accountSettingsPageHtmlDocument = null, profileSettingsPageHtmlDocument = null;
                try
                {
                    accountSettingsPageHtmlDocument = await this.htmlParser.ParseAsync(accountSettingsPageContent);
                    profileSettingsPageHtmlDocument = await this.htmlParser.ParseAsync(profileSettingsPageContent);
                }
                catch (Exception exception)
                {
                    throw new NineGagException("The HTML of the account settings page or the profile settings page could not be parsed. This could be an indicator, that the 9GAG website is down or its content has changed. If this problem keeps coming, then please report this problem to 9GAG or the maintainer of the library.", exception);
                }

                // Tries to parse the user information, if the user information could not be parsed, then an exception is thrown
                try
                {
                    this.currentUser = User.FromHtml(accountSettingsPageHtmlDocument, profileSettingsPageHtmlDocument);
                }
                catch (Exception exception)
                {
                    throw new NineGagException("The information about the currently signed in user could not be parsed. This could be an indicator, that the 9GAG website is down or its content has changed. If this problem keeps coming, then please report this problem to 9GAG or the maintainer of the library.", exception);
                }
            }

            // Returns the information about the currently signed in user
            return this.currentUser;
        }
        /// <summary>
        /// Signs the user out of 9GAG.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token, which can be used to cancel the sign out action.</param>
        /// <returns>Returns <c>true</c> if the user was signed out successfully.</returns>
        public async Task<bool> SignOutAsync(CancellationToken cancellationToken)
        {
            // Checks if the user is signed in, if not, then nothing needs to be done
            if (!this.IsUserSignedIn)
                return true;

            // Tries to sign the user out, if anything went wrong, then an exception is thrown
            try
            {
                // Makes the request to sign the user out
                HttpResponseMessage responseMessage = await this.httpClient.GetAsync(NineGagClient.signOutPath, cancellationToken);

                // Validates that the user was successfully signed out, which is when the status code is 302 Found, if the user could not be signed out, then an HTTP 200 OK is returned
                this.IsUserSignedIn = responseMessage.StatusCode != HttpStatusCode.Found;
            }
            catch (Exception) { }

            // Checks if the user was signed out successfully, in that case the user information is removed
            if (!this.IsUserSignedIn)
                this.currentUser = null;

            // Returns true if the user was signed out successfully and false otherwise
            return !this.IsUserSignedIn;
        }