Created
July 22, 2017 12:22
-
-
Save mik-laj/21f5cb8babab8ba26548e96cc6cf59ed to your computer and use it in GitHub Desktop.
WykopApiV2
This file contains hidden or 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
package entity; | |
import com.fasterxml.jackson.annotation.JsonFormat; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import java.util.Date; | |
@JsonIgnoreProperties(ignoreUnknown=true) | |
public class Entry { | |
@JsonProperty("id") | |
public int id; | |
@JsonProperty("author") | |
public User author; | |
@JsonProperty("vote_count") | |
public int vote_count; | |
@JsonProperty("comments_count") | |
public int comments_count; | |
@JsonProperty("date") | |
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "CET") | |
public Date date; | |
@JsonProperty("user_vote") | |
public int user_vote; | |
@JsonProperty("favorite") | |
public boolean favorite; | |
@JsonProperty("can_comment") | |
public boolean can_comment; | |
@JsonProperty("app") | |
public String app; | |
@JsonProperty("violation_url") | |
public String violation_url; | |
@JsonProperty("status") | |
public String status; | |
@JsonProperty("body") | |
public String body; | |
@JsonProperty("survey") | |
public Survey survey; | |
// comments ArrayList<Comment> | |
} |
This file contains hidden or 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
package entity; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class PaginationData { | |
@JsonProperty("next") | |
public String prev; | |
@JsonProperty("prev") | |
public String next; | |
@Override | |
public String toString() { | |
return "PaginationData{" + | |
"prev='" + prev + '\'' + | |
", next='" + next + '\'' + | |
'}'; | |
} | |
} |
This file contains hidden or 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
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
@JsonIgnoreProperties(ignoreUnknown=true) | |
public class Response<T> { | |
@JsonProperty("data") | |
T data; | |
@JsonProperty("pagination") | |
PaginationData pagination; | |
@Override | |
public String toString() { | |
return "Response{" + | |
"data=" + data + | |
", pagination=" + pagination + | |
'}'; | |
} | |
} |
This file contains hidden or 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
package entity; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import java.util.ArrayList; | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class Survey { | |
@JsonProperty("question") | |
public String text; | |
@JsonProperty("answers") | |
public ArrayList<SurveyAnswer> answers; | |
} |
This file contains hidden or 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
package entity; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class SurveyAnswer { | |
@JsonProperty("id") | |
public int id; | |
@JsonProperty("answer") | |
public String answer; | |
@JsonProperty("count") | |
public int count; | |
@JsonProperty("percentage") | |
public float percentage; | |
} |
This file contains hidden or 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
package entity; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class User { | |
@JsonProperty("login") | |
public String login; | |
@Override | |
public String toString() { | |
return "User{" + | |
"login='" + login + '\'' + | |
'}'; | |
} | |
} |
This file contains hidden or 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
import entity.*; | |
import okhttp3.*; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.jackson.JacksonConverterFactory; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.Proxy; | |
import java.util.ArrayList; | |
public class Main { | |
public static void main(String... args) throws IOException { | |
OkHttpClient client = new OkHttpClient.Builder() | |
.addInterceptor(new Interceptor() { | |
@Override | |
public okhttp3.Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
HttpUrl url = request.url().newBuilder() | |
.addPathSegment("appkey") | |
.addPathSegment("aNd401dAPp") | |
.build(); | |
Request newRequest = chain.request().newBuilder().url(url).build(); | |
System.out.println(url); | |
// System.exit(0); | |
return chain.proceed(newRequest); | |
} | |
}) | |
// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost",8888))) | |
.build(); | |
Retrofit retrofit = new Retrofit.Builder() | |
.client(client) | |
.baseUrl("https://a2.wykop.pl/") | |
// .addConverterFactory(ScalarsConverterFactory.create()) | |
.addConverterFactory(JacksonConverterFactory.create()) | |
.build(); | |
WykopApiV2 service = retrofit.create(WykopApiV2.class); | |
retrofit2.Call<Response<ArrayList<Entry>>> a = service.getEntryForTag("ankieta"); | |
retrofit2.Response<Response<ArrayList<Entry>>> r = a.execute(); | |
ArrayList<Entry> data = r.body().data; | |
for (Entry entry : data) { | |
System.out.println("Author: " + entry.author.login); | |
System.out.println("Date: " + entry.date); | |
System.out.println(); | |
System.out.println(stripTags(entry.body)); | |
if (entry.survey != null) { | |
Survey survey = entry.survey; | |
System.out.println(); | |
System.out.println(survey.text); | |
for (SurveyAnswer answer: survey.answers) { | |
System.out.println(String.format("%s (%3d) %s", bar(answer.percentage), answer.count, answer.answer)); | |
} | |
} | |
System.out.println("============="); | |
} | |
} | |
public static final String bar(float percentage){ | |
StringBuilder sb = new StringBuilder(); | |
for(int i = 0; i < percentage / 10;i++){ | |
sb.append("#"); | |
} | |
for(int i = 0; i < (100-percentage) / 10;i++){ | |
sb.append(" "); | |
} | |
return sb.toString(); | |
} | |
public static final String stripTags(String text){ | |
return text.replaceAll("\\<[^>]*>",""); | |
} | |
} |
This file contains hidden or 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
import entity.*; | |
import retrofit2.Call; | |
import retrofit2.http.*; | |
import java.util.ArrayList; | |
public interface WykopApiV2 { | |
@GET("tags/entries/{tag}") | |
Call<Response<ArrayList<Entry>>> getEntryForTag(@Path("tag") String a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment