Skip to content

Commit

Permalink
Search response is checked to be a search response ("fade" query prob…
Browse files Browse the repository at this point in the history
…lem fixed)
  • Loading branch information
yeriomin committed Feb 23, 2017
1 parent 556e2bf commit 4afe666
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/main/java/com/github/yeriomin/playstoreapi/GooglePlayAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,23 @@ public SearchResponse search(String query, Integer offset, Integer numberOfResul
params.put("q", query);

byte[] responseBytes = getClient().get(SEARCH_URL, params, getDefaultHeaders());
return ResponseWrapper.parseFrom(responseBytes).getPayload().getSearchResponse();
return makeSureItsApps(ResponseWrapper.parseFrom(responseBytes).getPayload().getSearchResponse());
}

/**
* Sometimes not a list of apps is returned by search, but a list of content types (musix and apps, for example)
* each of them having a list of items
* In this case we have to find the apps list and return it
*/
private SearchResponse makeSureItsApps(SearchResponse response) {
DocV2 doc = response.getDocList().get(0);
for (DocV2 child: doc.getChildList()) {
if (child.getDocType() == 45 && child.getTitle().equals("Apps")) {
response = SearchResponse.newBuilder(response).setDoc(0, child).build();
break;
}
}
return response;
}

/**
Expand Down Expand Up @@ -614,22 +630,25 @@ public SearchResponse next() {
SearchResponse response;
try {
byte[] responseBytes = getClient().get(url, params, getDefaultHeaders());
response = ResponseWrapper.parseFrom(responseBytes).getPayload().getSearchResponse();
response = makeSureItsApps(ResponseWrapper.parseFrom(responseBytes).getPayload().getSearchResponse());
this.firstQuery = false;
} catch (IOException e) {
throw new RuntimeException(e);
}

this.nextPageUrl = getNextPageUrl(response);
return response;
}

private String getNextPageUrl(SearchResponse response) {
if (null != response
&& response.getDocCount() > 0
&& response.getDocList().get(0).hasContainerMetadata()
&& response.getDocList().get(0).getContainerMetadata().hasNextPageUrl()
) {
this.nextPageUrl = FDFE_URL + response.getDocList().get(0).getContainerMetadata().getNextPageUrl();
} else {
this.nextPageUrl = null;
return FDFE_URL + response.getDocList().get(0).getContainerMetadata().getNextPageUrl();
}
return response;
return null;
}

@Override
Expand Down

0 comments on commit 4afe666

Please sign in to comment.