Пример #1
0
        public async Task <bool> IsModerator(string username, string password)
        {
            var html = await _downloadService.DownloadWithUserLogin(
                "https://www.shacknews.com/moderators", username, password);

            return(html.Contains("<div id=\"mod_board_head\">", StringComparison.Ordinal));
        }
Пример #2
0
        public async Task <MessagePage> GetMessagePage(Mailbox folder, string username, string password, int page)
        {
            if (page < 1)
            {
                throw new Api400Exception("Invalid page.");
            }

            var folderName = folder == Mailbox.Inbox ? "inbox" : "sent";

            var messagesPerPage = 50;
            var html            = await _downloadService.DownloadWithUserLogin(
                $"https://www.shacknews.com/messages/{folderName}?page={page}",
                username, password);

            var p = new Parser(html);

            var messagePage =
                new MessagePage
            {
                Messages = new List <MessageModel>()
            };

            p.Seek(1, "class=\"tools\"");
            messagePage.UnreadCount = int.Parse(p.Clip(new string[] { "<span class=\"flag\"", ">" }, "</span>"));

            p.Seek(1, "<h2>Message Center</h2>");

            if (p.Peek(1, "<div class=\"showing-column\">") == -1)
            {
                messagePage.TotalCount = 0;
                messagePage.LastPage   = 1;
            }
            else
            {
                messagePage.TotalCount = int.Parse(p.Clip(
                                                       new[] { "<div class=\"showing-column\">", ">", "of", " " },
                                                       "</div>"));
                messagePage.LastPage = (int)Math.Ceiling((double)messagePage.TotalCount / messagesPerPage);
                p.Seek(1, "<ul id=\"messages\">");
            }

            while (p.Peek(1, "<li class=\"message") != -1)
            {
                var message   = new MessageModel();
                var liClasses = p.Clip(
                    new[] { "<li class=\"message", "\"" },
                    "\"");
                if (!liClasses.Contains("read"))
                {
                    message.Unread = true;
                }

                message.Id = int.Parse(p.Clip(
                                           new[] { "<input type=\"checkbox\" class=\"mid\" name=\"messages[]\"", "value=", "\"" },
                                           "\">"));

                var otherUser = WebUtility.HtmlDecode(p.Clip(
                                                          new[] { "<span class=\"message-username\"", ">" },
                                                          "</span>"));
                if (folder == Mailbox.Inbox)
                {
                    message.From = otherUser;
                    message.To   = username;
                }
                else
                {
                    message.From = username;
                    message.To   = otherUser;
                }

                message.Subject = WebUtility.HtmlDecode(p.Clip(
                                                            new[] { "<span class=\"message-subject\"", ">" },
                                                            "</span>"));
                message.Date = DateParser.Parse(p.Clip(
                                                    new[] { "<span class=\"message-date\"", ">" },
                                                    "</span>"));
                message.Body = p.Clip(
                    new[] { "<div class=\"message-body\"", ">" },
                    "</div>");

                messagePage.Messages.Add(message);
            }

            return(messagePage);
        }
        public async Task Post(string username, string password, int parentId, string body)
        {
            if (body.Length > 0 && body[0] == '@')
            {
                body = " " + body;
            }

            var contentTypeId = 17;
            var contentId     = 17;

            if (parentId != 0)
            {
                try
                {
                    (contentTypeId, contentId) = await _threadParser.GetContentTypeId(parentId);
                }
                catch (ParsingException)
                {
                    throw new Api400Exception(Api400Exception.Codes.INVALID_PARENT, "Invalid parent ID.");
                }
            }

            var query = _downloadService.NewQuery();

            query.Add("parent_id", parentId == 0 ? "" : $"{parentId}");
            query.Add("content_type_id", $"{contentTypeId}");
            query.Add("content_id", $"{contentId}");
            query.Add("page", "");
            query.Add("parent_url", "/chatty");
            query.Add("body", _emojiConverter.ConvertEmojisToEntities(body.Replace("&", "&amp;")));

            var lastEventId = await _eventProvider.GetLastEventId();

            var response = await _downloadService.DownloadWithUserLogin(
                "https://www.shacknews.com/post_chatty.x",
                username, password, query);

            if (response.Contains("You must be logged in to post", StringComparison.Ordinal))
            {
                throw new Api400Exception(Api400Exception.Codes.INVALID_LOGIN,
                                          $"Unable to log into user account [{username}].");
            }
            if (response.Contains("Please wait a few minutes before trying to post again", StringComparison.Ordinal))
            {
                throw new Api400Exception(Api400Exception.Codes.POST_RATE_LIMIT,
                                          "Please wait a few minutes before trying to post again.");
            }
            if (response.Contains("banned", StringComparison.Ordinal))
            {
                throw new Api400Exception(Api400Exception.Codes.BANNED,
                                          "You are banned.");
            }
            if (response.Contains("Trying to post to a nuked thread", StringComparison.Ordinal))
            {
                throw new Api400Exception(Api400Exception.Codes.NUKED,
                                          "You cannot reply to a nuked thread or subthread.");
            }
            if (!response.Contains("fixup_postbox_parent_for_remove(", StringComparison.Ordinal))
            {
                throw new Api500Exception("Unexpected response from server: " + response);
            }

            // wait for the event to appear
            var sw          = Stopwatch.StartNew();
            var maxTimeSpan = TimeSpan.FromSeconds(15);

            while (sw.Elapsed < maxTimeSpan)
            {
                foreach (var ev in await _eventProvider.GetEvents(lastEventId))
                {
                    if (ev.EventType == EventType.NewPost)
                    {
                        var eventData = (NewPostEventDataModel)ev.EventData;
                        if (eventData.Post.Author.Equals(username, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return;
                        }
                    }
                    lastEventId = ev.EventId;
                }
                await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }