Skip to content

Commit

Permalink
Made search URL construciton more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Nov 11, 2015
1 parent c78cac6 commit 8542b3f
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions app/src/main/java/io/plaidapp/data/api/dribbble/DribbbleSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.support.annotation.WorkerThread;
import android.text.TextUtils;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
Expand All @@ -31,7 +32,6 @@
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -49,19 +49,33 @@
*/
public class DribbbleSearch {

public static final String SORT_POPULAR = "s=";
public static final String SORT_RECENT = "s=latest";
private static final String SEARCH_ENDPOINT = "/search?q=";
private static final String HOST = "https://dribbble.com";
private static final Pattern PATTERN_PLAYER_ID = Pattern.compile("users/(\\d+?)/", Pattern
.DOTALL);
public static final String SORT_POPULAR = "";
public static final String SORT_RECENT = "latest";
private static final Pattern PATTERN_PLAYER_ID =
Pattern.compile("users/(\\d+?)/", Pattern.DOTALL);

@WorkerThread
public static List<Shot> search(String query, @SortOrder String sort, int page) {

String html = null;
// e.g https://dribbble.com/search?q=material+design&page=7&per_page=12
String html = downloadPage(HOST + SEARCH_ENDPOINT + URLEncoder.encode(query) + "&" + sort
+ "&page=" + page + "&per_page=12");
HttpUrl url = new HttpUrl.Builder()
.scheme("https")
.host("dribbble.com")
.addPathSegment("search")
.addQueryParameter("q", query)
.addQueryParameter("page", String.valueOf(page))
.addQueryParameter("per_page", "12")
.build();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
try {
Response response = client.newCall(request).execute();
html = response.body().string();
} catch (IOException ioe) {
return null;
}

if (html == null) return null;
Elements shotElements = Jsoup.parse(html, HOST).select("li[id^=screenshot]");
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
Expand All @@ -88,8 +102,7 @@ private static Shot parseShot(Element element, SimpleDateFormat dateFormat) {
}
Date createdAt = null;
try {
createdAt
= dateFormat.parse(descriptionBlock.select("em.timestamp").first().text());
createdAt = dateFormat.parse(descriptionBlock.select("em.timestamp").first().text());
} catch (ParseException e) { }

return new Shot.Builder()
Expand Down Expand Up @@ -132,17 +145,6 @@ private static User parsePlayer(Element element) {
.build();
}

private static String downloadPage(String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException ioe) {
return null;
}
}

@Retention(RetentionPolicy.SOURCE)
@StringDef({
SORT_POPULAR,
Expand Down

0 comments on commit 8542b3f

Please sign in to comment.