-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c533975
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.iml | ||
target/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>pl.desz</groupId> | ||
<artifactId>otodom-scrapper</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jsoup</groupId> | ||
<artifactId>jsoup</artifactId> | ||
<version>1.11.2</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>9</source> | ||
<target>9</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module pl.desz.threading { | ||
|
||
requires jdk.incubator.httpclient; | ||
|
||
requires jsoup; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pl.desz.scrapper; | ||
|
||
import jdk.incubator.http.HttpResponse; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
|
||
import java.io.IOException; | ||
|
||
public class Client { | ||
|
||
public static final String OFFERS_COUNT = "div.offers-index.pull-left.text-nowrap strong"; | ||
|
||
public static void main(String[] args) throws IOException, InterruptedException { | ||
|
||
// create criteris for query | ||
OtodomSearchQueryBuilder builder = OtodomSearchQueryBuilder.newBuilder() | ||
.setMinFlatArea(50) | ||
.setMaxFlatArea(80) | ||
.setRooms(2) | ||
.setBalcony(OtodomSearchQueryBuilder.Balcony.YES); | ||
// build search payload | ||
String payload = builder.build(); | ||
System.out.println("Payload to be send:\n" + payload); | ||
|
||
// make a HTTP call to otodom.pl with Java 9 Incubated Http Client | ||
HttpResponse<String> stringHttpResponse = OtodomHttpRequestUtil.getResponse(payload); | ||
String body = stringHttpResponse.body(); | ||
|
||
// parse and show scrapped value | ||
Document doc = Jsoup.parse(body); | ||
System.out.printf("Response:%nFound %s offers based on given criteria.%n", doc.select(OFFERS_COUNT).text()); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pl.desz.scrapper; | ||
|
||
import jdk.incubator.http.HttpClient; | ||
import jdk.incubator.http.HttpRequest; | ||
import jdk.incubator.http.HttpResponse; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
public class OtodomHttpRequestUtil { | ||
|
||
private static HttpClient httpClient = HttpClient.newHttpClient(); | ||
private static final URI uri = URI.create("https://www.otodom.pl/ajax/search/list/"); | ||
|
||
public static HttpResponse<String> getResponse(String payload) throws IOException, InterruptedException { | ||
HttpRequest build = HttpRequest.newBuilder(uri).header("Content-Type", "application/x-www-form-urlencoded") | ||
.POST(HttpRequest.BodyProcessor.fromString(payload)).build(); | ||
|
||
return httpClient.send(build, HttpResponse.BodyHandler.asString()); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/pl/desz/scrapper/OtodomSearchQueryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package pl.desz.scrapper; | ||
|
||
/** | ||
* Creates payload {@link OtodomSearchQueryBuilder#payload} with | ||
* given parameters to search. Dedicated to Otodom.pl site. | ||
*/ | ||
public class OtodomSearchQueryBuilder { | ||
|
||
enum Balcony { | ||
|
||
YES("balcony"), OPTIONAL(""); | ||
|
||
private String queryDesc; | ||
|
||
Balcony(String desc) { | ||
this.queryDesc = desc; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.queryDesc; | ||
} | ||
} | ||
|
||
private static String payload = "search[order]=created_at_first:desc&view=&min_id=&search[street_id]=" + | ||
"&search[district_id]=&search[city_id]=167&search[subregion_id]=311&search[region_id]=12" + | ||
"&search[dist]=0&search[select_all]=&search[category_id]=101&search[filter_float_price:from]=" + | ||
"&search[filter_float_price:to]=&search[filter_float_m:from]=%(minFlatArea)&search[filter_float_m:to]=%(maxFlatArea)" + | ||
"&search[filter_float_price_per_m:from]=&search[filter_float_price_per_m:to]=" + | ||
"&search[filter_float_building_floors_num:from]=&search[filter_float_building_floors_num:to]=" + | ||
"&search[filter_float_build_year:from]=&search[filter_enum_rooms_num][]=%(roomsCount)&search[filter_float_build_year:to]=&search[created_since]=" + | ||
"&search[id]=&q=&search[description]=1&search[filter_enum_extras_types][]=%(balcony)"; | ||
|
||
/** | ||
* minimal area of flat in meters square | ||
*/ | ||
private int minFlatArea; | ||
private int maxFlatArea; | ||
private int roomsCount; | ||
private Balcony balcony = Balcony.OPTIONAL; | ||
|
||
private OtodomSearchQueryBuilder() { | ||
// intended | ||
} | ||
|
||
|
||
public static OtodomSearchQueryBuilder newBuilder() { | ||
return new OtodomSearchQueryBuilder(); | ||
} | ||
|
||
public OtodomSearchQueryBuilder setMinFlatArea(Integer minFlatArea) { | ||
this.minFlatArea = minFlatArea; | ||
return this; | ||
} | ||
|
||
public OtodomSearchQueryBuilder setMaxFlatArea(int maxFlatArea) { | ||
this.maxFlatArea = maxFlatArea; | ||
return this; | ||
} | ||
|
||
public OtodomSearchQueryBuilder setRooms(int roomsCount) { | ||
this.roomsCount = roomsCount; | ||
return this; | ||
} | ||
|
||
public OtodomSearchQueryBuilder setBalcony(Balcony balcony) { | ||
this.balcony = balcony; | ||
return this; | ||
} | ||
|
||
public String build() { | ||
payload = payload.replace("%(minFlatArea)", (this.minFlatArea == 0) ? "" : String.valueOf(this.minFlatArea)); | ||
payload = payload.replace("%(maxFlatArea)", (this.maxFlatArea == 0) ? "" : String.valueOf(this.maxFlatArea)); | ||
payload = payload.replace("%(roomsCount)", (this.roomsCount == 0) ? "" : String.valueOf(this.roomsCount)); | ||
payload = payload.replace("%(balcony)", (this.balcony == null) ? Balcony.OPTIONAL.toString() : this.balcony.toString()); | ||
|
||
return payload; | ||
} | ||
} |