Skip to content

Commit

Permalink
Merge pull request Mishiranu#11 from TrixiEther/fix/search
Browse files Browse the repository at this point in the history
Fix | Search
  • Loading branch information
TrixiEther authored Aug 27, 2022
2 parents a972944 + 3901248 commit 493cbbd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,42 @@ public ReadSearchPostsResult onReadSearchPosts(ReadSearchPostsData data) throws
"text", data.searchQuery);
HttpResponse response = new HttpRequest(uri, data).addCookie(buildCookiesWithCaptchaPass())
.setPostMethod(entity).setRedirectHandler(HttpRequest.RedirectHandler.STRICT).perform();
List<Post> posts = DvachModelMapper.createPostsFromHtml(response.readString());

List<String> postsNumbers = DvachModelMapper.createPostsFromHtml(response.readString());
List<Post> posts = new ArrayList<>();

for (String number : postsNumbers) {

uri = locator.createMobileApiV2Uri("post", data.boardName, number);
response = new HttpRequest(uri, data).addCookie(buildCookiesWithCaptchaPass())
.setGetMethod().setRedirectHandler(HttpRequest.RedirectHandler.STRICT).perform();

try(InputStream input = response.open(); JsonSerial.Reader reader = JsonSerial.reader(input)) {

reader.startObject();
while (!reader.endStruct()) {
switch (reader.nextName()) {
case "post": {
Post post = DvachModelMapper.createPost(reader, this, data.boardName, null,
configuration.isSageEnabled(data.boardName), null);
posts.add(post);
break;
}
default: {
reader.skip();
break;
}
}
}

} catch (ParseException e) {
throw new InvalidResponseException();
} catch (IOException e) {
throw response.fail(e);
}

}

return new ReadSearchPostsResult(posts);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,28 +519,14 @@ public static ArrayList<Post> createPosts(JsonSerial.Reader reader, Object linke
return posts;
}

public static ArrayList<Post> createPostsFromHtml(String html) {
ArrayList<Post> posts = new ArrayList<>();
public static List<String> createPostsFromHtml(String html) {

List<String> posts = new ArrayList<>();
Document doc = Jsoup.parse(html);
Elements elements = doc.select("div.box");

for (Element el : elements) {

Post post = new Post();
post.setPostNumber(el.id().split("post-")[1]);

String parentLink = el.select("span.reflink > a").attr("href");
String p = "\\/res\\/([\\d]+)\\.html";
Pattern r = Pattern.compile(p);
Matcher m = r.matcher(parentLink);
if (m.find())
post.setParentPostNumber(m.group(1));

post.setName(el.select("span.ananimas").text());
post.setComment(el.select("blockquote").text());

posts.add(post);
posts.add(el.id().split("post-")[1]);
}

return posts;
Expand Down

0 comments on commit 493cbbd

Please sign in to comment.