Search in sources :

Example 1 with EditPoll

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

the class EditPost method createPoll.

private EditPoll createPoll(Matcher matcher) {
    EditPoll poll = new EditPoll();
    Matcher jsonMatcher = fckngInvalidJsonPattern.matcher(matcher.group(2));
    while (jsonMatcher.find()) {
        EditPoll.Question question = new EditPoll.Question();
        int questionIndex = Integer.parseInt(jsonMatcher.group(1));
        if (questionIndex > poll.getBaseIndexOffset()) {
            poll.setBaseIndexOffset(questionIndex);
        }
        question.setIndex(questionIndex);
        question.setTitle(ApiUtils.fromHtml(jsonMatcher.group(3)));
        poll.addQuestion(question);
    }
    jsonMatcher = jsonMatcher.reset(matcher.group(3));
    while (jsonMatcher.find()) {
        int questionIndex = Integer.parseInt(jsonMatcher.group(1));
        EditPoll.Question question = EditPoll.findQuestionByIndex(poll, questionIndex);
        if (question != null) {
            EditPoll.Choice choice = new EditPoll.Choice();
            int choiceIndex = Integer.parseInt(jsonMatcher.group(2));
            if (choiceIndex > question.getBaseIndexOffset()) {
                question.setBaseIndexOffset(choiceIndex);
            }
            choice.setIndex(choiceIndex);
            choice.setTitle(ApiUtils.fromHtml(jsonMatcher.group(3)));
            question.addChoice(choice);
        }
    }
    jsonMatcher = jsonMatcher.reset(matcher.group(4));
    while (jsonMatcher.find()) {
        int questionIndex = Integer.parseInt(jsonMatcher.group(1));
        EditPoll.Question question = EditPoll.findQuestionByIndex(poll, questionIndex);
        if (question != null) {
            int choiceIndex = Integer.parseInt(jsonMatcher.group(2));
            EditPoll.Choice choice = EditPoll.findChoiceByIndex(question, choiceIndex);
            if (choice != null) {
                choice.setVotes(Integer.parseInt(jsonMatcher.group(3)));
            }
        }
    }
    jsonMatcher = jsonMatcher.reset(matcher.group(5));
    while (jsonMatcher.find()) {
        int questionIndex = Integer.parseInt(jsonMatcher.group(1));
        EditPoll.Question question = EditPoll.findQuestionByIndex(poll, questionIndex);
        if (question != null) {
            question.setMulti(jsonMatcher.group(3).equals("1"));
        }
    }
    poll.setMaxQuestions(Integer.parseInt(matcher.group(6)));
    poll.setMaxChoices(Integer.parseInt(matcher.group(7)));
    poll.setTitle(ApiUtils.fromHtml(matcher.group(8)));
    return poll;
}
Also used : Matcher(java.util.regex.Matcher) EditPoll(forpdateam.ru.forpda.api.theme.editpost.models.EditPoll)

Example 2 with EditPoll

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

the class EditPost method loadForm.

public EditPostForm loadForm(int postId) throws Exception {
    EditPostForm form = new EditPostForm();
    String url = "https://4pda.ru/forum/index.php?act=post&do=edit_post&p=".concat(Integer.toString(postId));
    // url = url.concat("&t=").concat(Integer.toString(topicId)).concat("&f=").concat(Integer.toString(forumId));
    NetworkResponse response = Api.getWebClient().get(url);
    if (response.getBody().equals("nopermission")) {
        form.setErrorCode(EditPostForm.ERROR_NO_PERMISSION);
        return form;
    }
    Matcher matcher = postPattern.matcher(response.getBody());
    if (matcher.find()) {
        form.setMessage(ApiUtils.fromHtml(ApiUtils.escapeNewLine(matcher.group(1))));
        form.setEditReason(matcher.group(2));
    }
    matcher = pollInfoPattern.matcher(response.getBody());
    if (matcher.find()) {
        EditPoll poll = createPoll(matcher);
        form.setPoll(poll);
        EditPost.printPoll(poll);
    }
    response = Api.getWebClient().get("https://4pda.ru/forum/index.php?act=attach&index=1&relId=" + postId + "&maxSize=134217728&allowExt=&code=init&unlinked=");
    matcher = attachmentsPattern.matcher(response.getBody());
    while (matcher.find()) {
        form.addAttachment(fillAttachmentV2(new AttachmentItem(), matcher));
    }
    return form;
}
Also used : EditPostForm(forpdateam.ru.forpda.api.theme.editpost.models.EditPostForm) Matcher(java.util.regex.Matcher) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) EditPoll(forpdateam.ru.forpda.api.theme.editpost.models.EditPoll) AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)

Example 3 with EditPoll

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

the class EditPost method sendPost.

public ThemePage sendPost(EditPostForm form) throws Exception {
    String url = "https://4pda.ru/forum/index.php";
    Map<String, String> headers = new HashMap<>();
    NetworkRequest.Builder builder = new NetworkRequest.Builder().url(url).formHeaders(headers).multipart().formHeader("act", "Post").formHeader("CODE", form.getType() == EditPostForm.TYPE_NEW_POST ? "03" : "9").formHeader("f", "" + form.getForumId()).formHeader("t", "" + form.getTopicId()).formHeader("auth_key", Api.getWebClient().getAuthKey()).formHeader("Post", form.getMessage()).formHeader("enablesig", "yes").formHeader("enableemo", "yes").formHeader("st", "" + form.getSt()).formHeader("removeattachid", "0").formHeader("MAX_FILE_SIZE", "0").formHeader("parent_id", "0").formHeader("ed-0_wysiwyg_used", "0").formHeader("editor_ids[]", "ed-0").formHeader("iconid", "0").formHeader("_upload_single_file", "1");
    EditPoll poll = form.getPoll();
    if (poll != null) {
        EditPost.printPoll(poll);
        builder.formHeader("poll_question", poll.getTitle().replaceAll("\n", " "));
        for (int i = 0; i < poll.getQuestions().size(); i++) {
            EditPoll.Question question = poll.getQuestion(i);
            int q_index = i + 1;
            builder.formHeader("question[" + q_index + "]", question.getTitle().replaceAll("\n", " "));
            builder.formHeader("multi[" + q_index + "]", question.isMulti() ? "1" : "0");
            for (int j = 0; j < question.getChoices().size(); j++) {
                EditPoll.Choice choice = question.getChoice(j);
                int c_index = j + 1;
                builder.formHeader("choice[" + q_index + '_' + c_index + "]", choice.getTitle().replaceAll("\n", " "));
            }
        }
    }
    // .formHeader("file-list", addedFileList);
    if (form.getType() == EditPostForm.TYPE_EDIT_POST)
        builder.formHeader("post_edit_reason", form.getEditReason());
    StringBuilder ids = new StringBuilder();
    if (form.getAttachments() != null && !form.getAttachments().isEmpty()) {
        for (int i = 0; i < form.getAttachments().size(); i++) {
            int id = form.getAttachments().get(i).getId();
            ids.append(id);
            if (i < form.getAttachments().size() - 1) {
                ids.append(",");
            }
        }
    }
    builder.formHeader("file-list", ids.toString());
    if (form.getPostId() != 0)
        builder.formHeader("p", "" + form.getPostId());
    return Api.Theme().parsePage(url, Api.getWebClient().request(builder.build()), false, false);
}
Also used : HashMap(java.util.HashMap) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) EditPoll(forpdateam.ru.forpda.api.theme.editpost.models.EditPoll)

Aggregations

EditPoll (forpdateam.ru.forpda.api.theme.editpost.models.EditPoll)3 Matcher (java.util.regex.Matcher)2 NetworkRequest (forpdateam.ru.forpda.api.NetworkRequest)1 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)1 AttachmentItem (forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)1 EditPostForm (forpdateam.ru.forpda.api.theme.editpost.models.EditPostForm)1 HashMap (java.util.HashMap)1