mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 13:23:58 +01:00
Replace remaining AsyncTask with RxAndroid (#2681)
This commit is contained in:
parent
a62aaadf90
commit
0bf63f50b3
28 changed files with 1096 additions and 1153 deletions
|
|
@ -246,11 +246,11 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean pageExists(String pageName) throws IOException {
|
||||
return Double.parseDouble( api.action("query")
|
||||
public Single<Boolean> pageExists(String pageName) {
|
||||
return Single.fromCallable(() -> Double.parseDouble(api.action("query")
|
||||
.param("titles", pageName)
|
||||
.get()
|
||||
.getString("/api/query/pages/page/@_idx")) != -1;
|
||||
.getString("/api/query/pages/page/@_idx")) != -1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -305,42 +305,44 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String findThumbnailByFilename(String filename) throws IOException {
|
||||
return api.action("query")
|
||||
public Single<String> findThumbnailByFilename(String filename) {
|
||||
return Single.fromCallable(() -> api.action("query")
|
||||
.param("format", "xml")
|
||||
.param("prop", "imageinfo")
|
||||
.param("iiprop", "url")
|
||||
.param("iiurlwidth", THUMB_SIZE)
|
||||
.param("titles", filename)
|
||||
.get()
|
||||
.getString("/api/query/pages/page/imageinfo/ii/@thumburl");
|
||||
.getString("/api/query/pages/page/imageinfo/ii/@thumburl"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parseWikicode(String source) throws IOException {
|
||||
return api.action("flow-parsoid-utils")
|
||||
public Single<String> parseWikicode(String source) {
|
||||
return Single.fromCallable(() -> api.action("flow-parsoid-utils")
|
||||
.param("from", "wikitext")
|
||||
.param("to", "html")
|
||||
.param("content", source)
|
||||
.param("title", "Main_page")
|
||||
.get()
|
||||
.getString("/api/flow-parsoid-utils/@content");
|
||||
.getString("/api/flow-parsoid-utils/@content"));
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public MediaResult fetchMediaByFilename(String filename) throws IOException {
|
||||
CustomApiResult apiResult = api.action("query")
|
||||
.param("prop", "revisions")
|
||||
.param("titles", filename)
|
||||
.param("rvprop", "content")
|
||||
.param("rvlimit", 1)
|
||||
.param("rvgeneratexml", 1)
|
||||
.get();
|
||||
public Single<MediaResult> fetchMediaByFilename(String filename) {
|
||||
return Single.fromCallable(() -> {
|
||||
CustomApiResult apiResult = api.action("query")
|
||||
.param("prop", "revisions")
|
||||
.param("titles", filename)
|
||||
.param("rvprop", "content")
|
||||
.param("rvlimit", 1)
|
||||
.param("rvgeneratexml", 1)
|
||||
.get();
|
||||
|
||||
return new MediaResult(
|
||||
apiResult.getString("/api/query/pages/page/revisions/rev"),
|
||||
apiResult.getString("/api/query/pages/page/revisions/rev/@parsetree"));
|
||||
return new MediaResult(
|
||||
apiResult.getString("/api/query/pages/page/revisions/rev"),
|
||||
apiResult.getString("/api/query/pages/page/revisions/rev/@parsetree"));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -33,9 +33,9 @@ public interface MediaWikiApi {
|
|||
|
||||
boolean fileExistsWithName(String fileName) throws IOException;
|
||||
|
||||
boolean pageExists(String pageName) throws IOException;
|
||||
Single<Boolean> pageExists(String pageName);
|
||||
|
||||
String findThumbnailByFilename(String filename) throws IOException;
|
||||
Single<String> findThumbnailByFilename(String filename);
|
||||
|
||||
boolean logEvents(LogBuilder[] logBuilders);
|
||||
|
||||
|
|
@ -69,10 +69,10 @@ public interface MediaWikiApi {
|
|||
@Nullable
|
||||
boolean addWikidataEditTag(String revisionId) throws IOException;
|
||||
|
||||
String parseWikicode(String source) throws IOException;
|
||||
Single<String> parseWikicode(String source);
|
||||
|
||||
@NonNull
|
||||
MediaResult fetchMediaByFilename(String filename) throws IOException;
|
||||
Single<MediaResult> fetchMediaByFilename(String filename);
|
||||
|
||||
@NonNull
|
||||
Observable<String> searchCategories(String filterValue, int searchCatsLimit);
|
||||
|
|
|
|||
|
|
@ -42,6 +42,9 @@ import okhttp3.Request;
|
|||
import okhttp3.Response;
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
* Test methods in ok http api client
|
||||
*/
|
||||
@Singleton
|
||||
public class OkHttpJsonApiClient {
|
||||
|
||||
|
|
@ -219,18 +222,30 @@ public class OkHttpJsonApiClient {
|
|||
@Nullable
|
||||
public Single<Media> getPictureOfTheDay() {
|
||||
String template = "Template:Potd/" + DateUtils.getCurrentDate();
|
||||
return getMedia(template, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches Media object from the imageInfo API
|
||||
*
|
||||
* @param titles the tiles to be searched for. Can be filename or template name
|
||||
* @param useGenerator specifies if a image generator parameter needs to be passed or not
|
||||
* @return
|
||||
*/
|
||||
public Single<Media> getMedia(String titles, boolean useGenerator) {
|
||||
HttpUrl.Builder urlBuilder = HttpUrl
|
||||
.parse(commonsBaseUrl)
|
||||
.newBuilder()
|
||||
.addQueryParameter("action", "query")
|
||||
.addQueryParameter("generator", "images")
|
||||
.addQueryParameter("format", "json")
|
||||
.addQueryParameter("titles", template)
|
||||
.addQueryParameter("prop", "imageinfo")
|
||||
.addQueryParameter("iiprop", "url|extmetadata");
|
||||
.addQueryParameter("titles", titles);
|
||||
|
||||
if (useGenerator) {
|
||||
urlBuilder.addQueryParameter("generator", "images");
|
||||
}
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(urlBuilder.build())
|
||||
.url(appendMediaProperties(urlBuilder).build())
|
||||
.build();
|
||||
|
||||
return Single.fromCallable(() -> {
|
||||
|
|
@ -238,17 +253,38 @@ public class OkHttpJsonApiClient {
|
|||
if (response.body() != null && response.isSuccessful()) {
|
||||
String json = response.body().string();
|
||||
MwQueryResponse mwQueryPage = gson.fromJson(json, MwQueryResponse.class);
|
||||
return Media.from(mwQueryPage.query().firstPage());
|
||||
if (mwQueryPage.success() && mwQueryPage.query().firstPage() != null) {
|
||||
return Media.from(mwQueryPage.query().firstPage());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Whenever imageInfo is fetched, these common properties can be specified for the API call
|
||||
* https://www.mediawiki.org/wiki/API:Imageinfo
|
||||
* @param builder
|
||||
* @return
|
||||
*/
|
||||
private HttpUrl.Builder appendMediaProperties(HttpUrl.Builder builder) {
|
||||
builder.addQueryParameter("prop", "imageinfo")
|
||||
.addQueryParameter("iiprop", "url|extmetadata")
|
||||
.addQueryParameter("iiextmetadatafilter", "DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName|LicenseUrl");
|
||||
|
||||
String language = Locale.getDefault().getLanguage();
|
||||
if (!StringUtils.isNullOrWhiteSpace(language)) {
|
||||
builder.addQueryParameter("iiextmetadatalanguage", language);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method takes the keyword and queryType as input and returns a list of Media objects filtered using image generator query
|
||||
* It uses the generator query API to get the images searched using a query, 10 at a time.
|
||||
* @param queryType queryType can be "search" OR "category"
|
||||
* @param keyword
|
||||
* @param keyword the search keyword. Can be either category name or search query
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
|
|
@ -294,29 +330,10 @@ public class OkHttpJsonApiClient {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Whenever imageInfo is fetched, these common properties can be specified for the API call
|
||||
* https://www.mediawiki.org/wiki/API:Imageinfo
|
||||
* @param builder
|
||||
* @return
|
||||
*/
|
||||
private HttpUrl.Builder appendMediaProperties(HttpUrl.Builder builder) {
|
||||
builder.addQueryParameter("prop", "imageinfo")
|
||||
.addQueryParameter("iiprop", "url|extmetadata")
|
||||
.addQueryParameter("iiextmetadatafilter", "DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal|Artist|LicenseShortName");
|
||||
|
||||
String language = Locale.getDefault().getLanguage();
|
||||
if (!StringUtils.isNullOrWhiteSpace(language)) {
|
||||
builder.addQueryParameter("iiextmetadatalanguage", language);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append params for search query.
|
||||
* @param query
|
||||
* @param urlBuilder
|
||||
* @param query the search query to be sent to the API
|
||||
* @param urlBuilder builder for HttpUrl
|
||||
*/
|
||||
private void appendSearchParam(String query, HttpUrl.Builder urlBuilder) {
|
||||
urlBuilder.addQueryParameter("generator", "search")
|
||||
|
|
@ -340,6 +357,11 @@ public class OkHttpJsonApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Append parameters for category image generator
|
||||
* @param categoryName name of the category
|
||||
* @param urlBuilder HttpUrl builder
|
||||
*/
|
||||
private void appendCategoryParams(String categoryName, HttpUrl.Builder urlBuilder) {
|
||||
urlBuilder.addQueryParameter("generator", "categorymembers")
|
||||
.addQueryParameter("gcmtype", "file")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue