use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.
the class DisambigListAdapter method fetchDescriptions.
/**
* Start getting Wikidata descriptions (directly from the current Wikipedia wiki).
*/
private void fetchDescriptions() {
final List<PageTitle> titleList = new ArrayList<>();
for (DisambigResult r : items) {
titleList.add(r.getTitle());
}
if (titleList.isEmpty()) {
return;
}
new DescriptionClient().request(wiki, titleList, new DescriptionClient.Callback() {
@Override
public void success(@NonNull Call<MwQueryResponse> call, @NonNull List<MwQueryPage> results) {
for (MwQueryPage page : results) {
PageTitle pageTitle = new PageTitle(null, page.title(), wiki);
for (PageTitle title : titleList) {
if (title.getPrefixedText().equals(pageTitle.getPrefixedText()) || title.getDisplayText().equals(pageTitle.getDisplayText())) {
title.setDescription(page.description());
break;
}
}
}
notifyDataSetChanged();
}
@Override
public void failure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable caught) {
// descriptions are expendable
}
});
}
use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.
the class DescriptionClientTest method testRequestSuccess.
@Test
public void testRequestSuccess() throws Throwable {
enqueueFromFile("reading_list_page_info.json");
DescriptionClient.Callback cb = mock(DescriptionClient.Callback.class);
Call<MwQueryResponse> call = request(cb);
server().takeRequest();
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(cb).success(eq(call), captor.capture());
List<MwQueryPage> result = captor.getValue();
MwQueryPage biden = result.get(0);
MwQueryPage obama = result.get(1);
assertThat(biden.title(), is("Joe Biden"));
assertThat(biden.description(), is("47th Vice President of the United States"));
assertThat(obama.title(), is("Barack Obama"));
assertThat(obama.description(), is("44th President of the United States of America"));
}
use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.
the class ShareHandler method shareSnippet.
private void shareSnippet(@NonNull CharSequence input) {
final String selectedText = StringUtil.sanitizeText(input.toString());
final PageTitle title = fragment.getTitle();
final String leadImageNameText = fragment.getPage().getPageProperties().getLeadImageName() != null ? fragment.getPage().getPageProperties().getLeadImageName() : "";
final PageTitle imageTitle = new PageTitle(Namespace.FILE.toLegacyString(), leadImageNameText, title.getWikiSite());
disposables.add(ServiceFactory.get(title.getWikiSite()).getImageExtMetadata(imageTitle.getPrefixedText()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).map(response -> {
// noinspection ConstantConditions
MwQueryPage page = response.query().pages().get(0);
return page.imageInfo() != null && page.imageInfo().getMetadata() != null ? new ImageLicense(page.imageInfo().getMetadata()) : new ImageLicense();
}).subscribe(imageLicense -> {
final Bitmap snippetBitmap = SnippetImage.getSnippetImage(fragment.requireContext(), fragment.getLeadImageBitmap(), title.getDisplayText(), fragment.getPage().isMainPage() ? "" : StringUtils.capitalize(title.getDescription()), selectedText, imageLicense);
fragment.showBottomSheet(new PreviewDialog(fragment.getContext(), snippetBitmap, title, selectedText, funnel));
}, caught -> {
// If we failed to get license info for the lead image, just share the text
PreviewDialog.shareAsText(fragment.requireContext(), title, selectedText, funnel);
L.e("Error fetching image license info for " + title.getDisplayText(), caught);
}));
}
use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.
the class ReadingListPageInfoClientTest method testRequestSuccess.
@Test
@SuppressWarnings("checkstyle:magicnumber")
public void testRequestSuccess() throws Throwable {
enqueueFromFile("reading_list_page_info.json");
ReadingListPageInfoClient.Callback cb = mock(ReadingListPageInfoClient.Callback.class);
Call<MwQueryResponse> call = request(cb);
server().takeRequest();
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(cb).success(eq(call), captor.capture());
List<MwQueryPage> result = captor.getValue();
MwQueryPage biden = result.get(0);
MwQueryPage obama = result.get(1);
assertThat(biden.title(), is("Joe Biden"));
assertThat(biden.description(), is("47th Vice President of the United States"));
assertThat(biden.thumbUrl(), is("https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Official_portrait_of_Vice_President_Joe_Biden.jpg/255px-Official_portrait_of_Vice_President_Joe_Biden.jpg"));
assertThat(obama.title(), is("Barack Obama"));
assertThat(obama.description(), is("44th President of the United States of America"));
assertThat(obama.thumbUrl(), is("https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/256px-President_Barack_Obama.jpg"));
}
use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.
the class ImageLicenseFetchClient method request.
@VisibleForTesting
Call<MwQueryResponse> request(@NonNull Service service, @NonNull final PageTitle title, @NonNull final Callback cb) {
Call<MwQueryResponse> call = service.request(title.toString());
call.enqueue(new retrofit2.Callback<MwQueryResponse>() {
@Override
public void onResponse(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
if (response.body().success()) {
// noinspection ConstantConditions
MwQueryPage page = response.body().query().pages().get(0);
cb.success(call, page.imageInfo() != null && page.imageInfo().getMetadata() != null ? new ImageLicense(page.imageInfo().getMetadata()) : new ImageLicense());
} else if (response.body().hasError()) {
// noinspection ConstantConditions
cb.failure(call, new MwException(response.body().getError()));
} else {
cb.failure(call, new IOException("An unknown error occurred."));
}
}
@Override
public void onFailure(Call<MwQueryResponse> call, Throwable t) {
cb.failure(call, t);
}
});
return call;
}
Aggregations