Search in sources :

Example 1 with PollQuestion

use of forpdateam.ru.forpda.api.theme.models.PollQuestion in project ForPDA by RadiationX.

the class ThemeRx method transform.

public static ThemePage transform(ThemePage page, boolean withHtml) throws Exception {
    if (withHtml) {
        List<ForumUser> forumUsers = new ArrayList<>();
        for (ThemePost post : page.getPosts()) {
            ForumUser forumUser = new ForumUser();
            forumUser.setId(post.getUserId());
            forumUser.setNick(post.getNick());
            forumUser.setAvatar(post.getAvatar());
        }
        ForumUsersCache.saveUsers(forumUsers);
        int memberId = ClientHelper.getUserId();
        MiniTemplator t = App.get().getTemplate(App.TEMPLATE_THEME);
        App.setTemplateResStrings(t);
        boolean authorized = ClientHelper.getAuthState();
        boolean prevDisabled = page.getPagination().getCurrent() <= 1;
        boolean nextDisabled = page.getPagination().getCurrent() == page.getPagination().getAll();
        t.setVariableOpt("style_type", App.get().getCssStyleType());
        t.setVariableOpt("topic_title", ApiUtils.htmlEncode(page.getTitle()));
        t.setVariableOpt("topic_description", ApiUtils.htmlEncode(page.getDesc()));
        t.setVariableOpt("topic_url", page.getUrl());
        t.setVariableOpt("all_pages_int", page.getPagination().getAll());
        t.setVariableOpt("posts_on_page_int", page.getPagination().getPerPage());
        t.setVariableOpt("current_page_int", page.getPagination().getCurrent());
        t.setVariableOpt("authorized_bool", Boolean.toString(authorized));
        t.setVariableOpt("is_curator_bool", Boolean.toString(page.isCurator()));
        t.setVariableOpt("member_id_int", ClientHelper.getUserId());
        t.setVariableOpt("elem_to_scroll", page.getAnchor());
        t.setVariableOpt("body_type", "topic");
        t.setVariableOpt("navigation_disable", getDisableStr(prevDisabled && nextDisabled));
        t.setVariableOpt("first_disable", getDisableStr(prevDisabled));
        t.setVariableOpt("prev_disable", getDisableStr(prevDisabled));
        t.setVariableOpt("next_disable", getDisableStr(nextDisabled));
        t.setVariableOpt("last_disable", getDisableStr(nextDisabled));
        t.setVariableOpt("in_favorite_bool", Boolean.toString(page.isInFavorite()));
        boolean isEnableAvatars = Preferences.Theme.isShowAvatars(null);
        t.setVariableOpt("enable_avatars_bool", Boolean.toString(isEnableAvatars));
        t.setVariableOpt("enable_avatars", isEnableAvatars ? "show_avatar" : "hide_avatar");
        t.setVariableOpt("avatar_type", Preferences.Theme.isCircleAvatars(null) ? "circle_avatar" : "square_avatar");
        int hatPostId = 0;
        if (!page.getPosts().isEmpty()) {
            hatPostId = page.getPosts().get(0).getId();
        }
        Matcher letterMatcher = null;
        for (ThemePost post : page.getPosts()) {
            t.setVariableOpt("user_online", post.isOnline() ? "online" : "");
            t.setVariableOpt("post_id", post.getId());
            t.setVariableOpt("user_id", post.getUserId());
            // Post header
            // t.setVariableOpt("avatar", post.getAvatar().isEmpty() ? "file:///android_asset/av.png" : "https://s.4pda.to/forum/uploads/".concat(post.getAvatar()));
            t.setVariableOpt("avatar", post.getAvatar());
            t.setVariableOpt("none_avatar", post.getAvatar().isEmpty() ? "none_avatar" : "");
            if (letterMatcher != null) {
                letterMatcher = letterMatcher.reset(post.getNick());
            } else {
                letterMatcher = firstLetter.matcher(post.getNick());
            }
            String letter = null;
            if (letterMatcher.find()) {
                letter = letterMatcher.group(1);
            } else {
                letter = post.getNick().substring(0, 1);
            }
            t.setVariableOpt("nick_letter", letter);
            t.setVariableOpt("nick", ApiUtils.htmlEncode(post.getNick()));
            t.setVariableOpt("curator", post.isCurator() ? "curator" : "");
            t.setVariableOpt("group_color", post.getGroupColor());
            t.setVariableOpt("group", post.getGroup());
            t.setVariableOpt("reputation", post.getReputation());
            t.setVariableOpt("date", post.getDate());
            t.setVariableOpt("number", post.getNumber());
            // Post body
            if (page.getPosts().size() > 1 && hatPostId == post.getId()) {
                boolean hatOpened = prevDisabled || page.isHatOpen();
                t.setVariableOpt("hat_state_class", prevDisabled || page.isHatOpen() ? "open" : "close");
                // t.setVariableOpt("hat_body_state", prevDisabled || page.isHatOpen() ? "" : "hidden");
                t.addBlockOpt("hat_button");
                t.addBlockOpt("hat_content_start");
                t.addBlockOpt("hat_content_end");
            } else {
                t.setVariableOpt("hat_state_class", "");
            }
            t.setVariableOpt("body", post.getBody());
            if (post.canReport() && authorized)
                t.addBlockOpt("report_block");
            if (page.canQuote() && authorized && post.getUserId() != memberId)
                t.addBlockOpt("reply_block");
            if (authorized && post.getUserId() != memberId)
                t.addBlockOpt("vote_block");
            if (post.canDelete() && authorized)
                t.addBlockOpt("delete_block");
            if (post.canEdit() && authorized)
                t.addBlockOpt("edit_block");
            t.addBlockOpt("post");
        }
        // Poll block
        if (page.getPoll() != null) {
            t.setVariableOpt("poll_state_class", page.isPollOpen() ? "open" : "close");
            // t.setVariableOpt("poll_body_state", page.isPollOpen() ? "" : "hidden");
            Poll poll = page.getPoll();
            boolean isResult = poll.isResult();
            t.setVariableOpt("poll_type", isResult ? "result" : "default");
            t.setVariableOpt("poll_title", poll.getTitle().isEmpty() || poll.getTitle().equals("-") ? App.get().getString(R.string.poll) : poll.getTitle());
            for (PollQuestion question : poll.getQuestions()) {
                t.setVariableOpt("question_title", question.getTitle());
                for (PollQuestionItem questionItem : question.getQuestionItems()) {
                    t.setVariableOpt("question_item_title", questionItem.getTitle());
                    if (isResult) {
                        t.setVariableOpt("question_item_votes", questionItem.getVotes());
                        t.setVariableOpt("question_item_percent", Float.toString(questionItem.getPercent()));
                        t.addBlockOpt("poll_result_item");
                    } else {
                        t.setVariableOpt("question_item_type", questionItem.getType());
                        t.setVariableOpt("question_item_name", questionItem.getName());
                        t.setVariableOpt("question_item_value", questionItem.getValue());
                        t.addBlockOpt("poll_default_item");
                    }
                }
                t.addBlockOpt("poll_question_block");
            }
            t.setVariableOpt("poll_votes_count", poll.getVotesCount());
            if (poll.haveButtons()) {
                if (poll.haveVoteButton())
                    t.addBlockOpt("poll_vote_button");
                if (poll.haveShowResultsButton())
                    t.addBlockOpt("poll_show_results_button");
                if (poll.haveShowPollButton())
                    t.addBlockOpt("poll_show_poll_button");
                t.addBlockOpt("poll_buttons");
            }
            t.addBlockOpt("poll_block");
        }
        page.setHtml(t.generateOutput());
        t.reset();
    }
    return page;
}
Also used : ThemePost(forpdateam.ru.forpda.api.theme.models.ThemePost) PollQuestion(forpdateam.ru.forpda.api.theme.models.PollQuestion) ForumUser(forpdateam.ru.forpda.api.others.user.ForumUser) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Poll(forpdateam.ru.forpda.api.theme.models.Poll) PollQuestionItem(forpdateam.ru.forpda.api.theme.models.PollQuestionItem) MiniTemplator(biz.source_code.miniTemplator.MiniTemplator)

Example 2 with PollQuestion

use of forpdateam.ru.forpda.api.theme.models.PollQuestion in project ForPDA by RadiationX.

the class Theme method parsePage.

public ThemePage parsePage(String url, NetworkResponse response, boolean hatOpen, boolean pollOpen) throws Exception {
    ThemePage page = new ThemePage();
    page.setHatOpen(hatOpen);
    page.setPollOpen(pollOpen);
    String redirectUrl = response.getRedirect();
    if (redirectUrl == null)
        redirectUrl = url;
    page.setUrl(redirectUrl);
    long time = System.currentTimeMillis();
    Matcher matcher = elemToScrollPattern.matcher(redirectUrl);
    while (matcher.find()) {
        page.addAnchor(matcher.group(1));
    }
    matcher = themeIdPattern.matcher(response.getBody());
    if (matcher.find()) {
        page.setForumId(Integer.parseInt(matcher.group(1)));
        page.setId(Integer.parseInt(matcher.group(2)));
    }
    page.setPagination(Pagination.parseForum(response.getBody()));
    matcher = titlePattern.matcher(response.getBody());
    if (matcher.find()) {
        page.setTitle(ApiUtils.fromHtml(matcher.group(1)));
        page.setDesc(ApiUtils.fromHtml(matcher.group(2)));
    }
    matcher = alreadyInFavPattern.matcher(response.getBody());
    if (matcher.find()) {
        page.setInFavorite(true);
        matcher = favIdPattern.matcher(response.getBody());
        if (matcher.find()) {
            page.setFavId(Integer.parseInt(matcher.group(1)));
        }
    }
    matcher = universalForumPosts.matcher(response.getBody());
    Matcher attachMatcher = null;
    while (matcher.find()) {
        ThemePost item = new ThemePost();
        item.setTopicId(page.getId());
        item.setForumId(page.getForumId());
        item.setId(Integer.parseInt(matcher.group(1)));
        item.setDate(matcher.group(5));
        item.setNumber(Integer.parseInt(matcher.group(6)));
        item.setOnline(matcher.group(7).contains("green"));
        String avatar = matcher.group(8);
        if (!avatar.isEmpty()) {
            avatar = "https://s.4pda.to/forum/uploads/".concat(avatar);
        }
        item.setAvatar(avatar);
        item.setNick(ApiUtils.fromHtml(matcher.group(9)));
        item.setUserId(Integer.parseInt(matcher.group(10)));
        item.setCurator(matcher.group(11) != null);
        item.setGroupColor(matcher.group(12));
        item.setGroup(matcher.group(13));
        item.setCanMinus(!matcher.group(14).isEmpty());
        item.setReputation(matcher.group(15));
        item.setCanPlus(!matcher.group(16).isEmpty());
        item.setCanReport(!matcher.group(17).isEmpty());
        item.setCanEdit(!matcher.group(18).isEmpty());
        item.setCanDelete(!matcher.group(19).isEmpty());
        page.setCanQuote(!matcher.group(20).isEmpty());
        item.setCanQuote(page.canQuote());
        item.setBody(matcher.group(21));
        if (attachMatcher == null) {
            attachMatcher = attachImagesPattern.matcher(item.getBody());
        } else {
            attachMatcher.reset(item.getBody());
        }
        while (attachMatcher.find()) {
            item.addAttachImage(attachMatcher.group(1), attachMatcher.group(2));
        }
        if (item.isCurator() && item.getUserId() == ClientHelper.getUserId())
            page.setCurator(true);
        page.addPost(item);
    }
    matcher = pollMainPattern.matcher(response.getBody());
    if (matcher.find()) {
        Poll poll = new Poll();
        final boolean isResult = matcher.group().contains("<img");
        poll.setIsResult(isResult);
        poll.setTitle(ApiUtils.fromHtml(matcher.group(1)));
        Matcher matcher1 = pollQuestions.matcher(matcher.group(2));
        while (matcher1.find()) {
            PollQuestion pollQuestion = new PollQuestion();
            pollQuestion.setTitle(ApiUtils.fromHtml(matcher1.group(1)));
            Matcher itemsMatcher = pollQuestionItems.matcher(matcher1.group(2));
            while (itemsMatcher.find()) {
                PollQuestionItem questionItem = new PollQuestionItem();
                if (!isResult) {
                    questionItem.setType(itemsMatcher.group(1));
                    questionItem.setName(ApiUtils.fromHtml(itemsMatcher.group(2)));
                    questionItem.setValue(Integer.parseInt(itemsMatcher.group(3)));
                    questionItem.setTitle(ApiUtils.fromHtml(itemsMatcher.group(4)));
                } else {
                    questionItem.setTitle(ApiUtils.fromHtml(itemsMatcher.group(5)));
                    questionItem.setVotes(Integer.parseInt(itemsMatcher.group(6)));
                    questionItem.setPercent(Float.parseFloat(itemsMatcher.group(7).replace(",", ".")));
                }
                pollQuestion.addItem(questionItem);
            }
            poll.addQuestion(pollQuestion);
        }
        matcher1 = pollButtons.matcher(matcher.group(4));
        while (matcher1.find()) {
            String value = matcher1.group(1);
            if (value.contains("Голосовать")) {
                poll.setVoteButton();
            } else if (value.contains("результаты")) {
                poll.setShowResultButton();
            } else if (value.contains("пункты опроса")) {
                poll.setShowPollButton();
            }
        }
        poll.setVotesCount(Integer.parseInt(matcher.group(3)));
        page.setPoll(poll);
    }
    return page;
}
Also used : ThemePost(forpdateam.ru.forpda.api.theme.models.ThemePost) ThemePage(forpdateam.ru.forpda.api.theme.models.ThemePage) PollQuestion(forpdateam.ru.forpda.api.theme.models.PollQuestion) Matcher(java.util.regex.Matcher) Poll(forpdateam.ru.forpda.api.theme.models.Poll) PollQuestionItem(forpdateam.ru.forpda.api.theme.models.PollQuestionItem)

Aggregations

Poll (forpdateam.ru.forpda.api.theme.models.Poll)2 PollQuestion (forpdateam.ru.forpda.api.theme.models.PollQuestion)2 PollQuestionItem (forpdateam.ru.forpda.api.theme.models.PollQuestionItem)2 ThemePost (forpdateam.ru.forpda.api.theme.models.ThemePost)2 Matcher (java.util.regex.Matcher)2 MiniTemplator (biz.source_code.miniTemplator.MiniTemplator)1 ForumUser (forpdateam.ru.forpda.api.others.user.ForumUser)1 ThemePage (forpdateam.ru.forpda.api.theme.models.ThemePage)1 ArrayList (java.util.ArrayList)1