Skip to content

OkHttpJsonApi#getMediaList migrated to retrofit #3054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.explore.categories.ExploreActivity;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.media.MediaClient;
import fr.free.nrw.commons.utils.NetworkUtils;
import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -56,7 +57,7 @@ public class CategoryImagesListFragment extends DaggerFragment {
private boolean isLoading = true;
private String categoryName = null;

@Inject CategoryImageController controller;
@Inject MediaClient mediaClient;
@Inject
@Named("default_preferences")
JsonKvStore categoryKvStore;
Expand Down Expand Up @@ -116,7 +117,7 @@ private void initList() {

isLoading = true;
progressBar.setVisibility(VISIBLE);
compositeDisposable.add(controller.getCategoryImages(categoryName)
compositeDisposable.add(mediaClient.getMediaListFromCategory(categoryName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
Expand Down Expand Up @@ -222,7 +223,7 @@ private void fetchMoreImages() {
}

progressBar.setVisibility(VISIBLE);
compositeDisposable.add(controller.getCategoryImages(categoryName)
compositeDisposable.add(mediaClient.getMediaListFromCategory(categoryName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public OkHttpJsonApiClient provideOkHttpJsonApiClient(OkHttpClient okHttpClient,
WIKIDATA_SPARQL_QUERY_URL,
BuildConfig.WIKIMEDIA_CAMPAIGNS_URL,
BuildConfig.WIKIMEDIA_API_HOST,
defaultKvStore,
gson);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,18 @@
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.pedrogomez.renderers.RVRendererAdapter;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
import fr.free.nrw.commons.explore.SearchActivity;
import fr.free.nrw.commons.explore.recentsearches.RecentSearch;
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
import fr.free.nrw.commons.media.MediaClient;
import fr.free.nrw.commons.utils.NetworkUtils;
import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
import timber.log.Timber;

import static android.view.View.GONE;
Expand All @@ -69,7 +61,7 @@ public class SearchImageFragment extends CommonsDaggerSupportFragment {

@Inject RecentSearchesDao recentSearchesDao;
@Inject
OkHttpJsonApiClient okHttpJsonApiClient;
MediaClient mediaClient;
@Inject
@Named("default_preferences")
JsonKvStore defaultKvStore;
Expand Down Expand Up @@ -148,7 +140,7 @@ public void updateImageList(String query) {
bottomProgressBar.setVisibility(GONE);
queryList.clear();
imagesAdapter.clear();
compositeDisposable.add(okHttpJsonApiClient.getMediaList("search", query)
compositeDisposable.add(mediaClient.getMediaListFromSearch(query)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
Expand All @@ -165,7 +157,7 @@ public void addImagesToList(String query) {
this.query = query;
bottomProgressBar.setVisibility(View.VISIBLE);
progressBar.setVisibility(GONE);
compositeDisposable.add(okHttpJsonApiClient.getMediaList("search", query)
compositeDisposable.add(mediaClient.getMediaListFromSearch(query)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
Expand Down Expand Up @@ -228,7 +220,7 @@ private void handleError(Throwable throwable) {
private void initErrorView() {
progressBar.setVisibility(GONE);
imagesNotFoundView.setVisibility(VISIBLE);
imagesNotFoundView.setText(getString(R.string.images_not_found, query));
imagesNotFoundView.setText(getString(R.string.images_not_found));
}

/**
Expand Down
61 changes: 61 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/media/MediaClient.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package fr.free.nrw.commons.media;


import org.wikipedia.dataclient.mwapi.MwQueryResponse;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Singleton;

import fr.free.nrw.commons.Media;
import io.reactivex.Observable;
import io.reactivex.Single;

/**
Expand All @@ -14,9 +24,13 @@ public class MediaClient {

private final MediaInterface mediaInterface;

//OkHttpJsonApiClient used JsonKvStore for this. I don't know why.
private Map<String, Map<String, String>> continuationStore;

@Inject
public MediaClient(MediaInterface mediaInterface) {
this.mediaInterface = mediaInterface;
this.continuationStore = new HashMap<>();
}

/**
Expand All @@ -43,4 +57,51 @@ public Single<Boolean> checkFileExistsUsingSha(String fileSha) {
.query().allImages().size() > 0)
.singleOrError();
}

/**
* This method takes the category 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 category the search category. Must start with "Category:"
* @return
*/
public Single<List<Media>> getMediaListFromCategory(String category) {
return responseToMediaList(
continuationStore.containsKey("category_" + category) ?
mediaInterface.getMediaListFromCategory(category, 10, continuationStore.get("category_" + category)) : //if true
mediaInterface.getMediaListFromCategory(category, 10, Collections.emptyMap()),
"category_" + category); //if false

}

/**
* This method takes a keyword 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 keyword the search keyword
* @return
*/
public Single<List<Media>> getMediaListFromSearch(String keyword) {
return responseToMediaList(
continuationStore.containsKey("search_" + keyword) ?
mediaInterface.getMediaListFromSearch(keyword, 10, continuationStore.get("search_" + keyword)) : //if true
mediaInterface.getMediaListFromSearch(keyword, 10, Collections.emptyMap()), //if false
"search_" + keyword);

}

private Single<List<Media>> responseToMediaList(Observable<MwQueryResponse> response, String key) {
return response.flatMap(mwQueryResponse -> {
if (null == mwQueryResponse
|| null == mwQueryResponse.query()
|| null == mwQueryResponse.query().pages()) {
return Observable.empty();
}
continuationStore.put(key, mwQueryResponse.continuation());
return Observable.fromIterable(mwQueryResponse.query().pages());
})
.map(Media::from)
.collect(ArrayList<Media>::new, List::add);
}

}
38 changes: 38 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/media/MediaInterface.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package fr.free.nrw.commons.media;

import org.jetbrains.annotations.NotNull;
import org.wikipedia.dataclient.mwapi.MwQueryResponse;

import java.util.Map;

import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;

/**
* Interface for interacting with Commons media related APIs
*/
public interface MediaInterface {
/**
* Checks if a page exists or not.
*
* @param title the title of the page to be checked
* @return
*/
Expand All @@ -20,9 +25,42 @@ public interface MediaInterface {

/**
* Check if file exists
*
* @param aisha1 the SHA of the media file to be checked
* @return
*/
@GET("w/api.php?action=query&format=json&formatversion=2&list=allimages")
Observable<MwQueryResponse> checkFileExistsUsingSha(@Query("aisha1") String aisha1);


/**
* This method retrieves a list of Media objects filtered using image generator query
*
* @param category the category name. Must start with "Category:"
* @param itemLimit how many images are returned
* @param continuation the continuation string from the previous query or empty map
* @return
*/
@GET("w/api.php?action=query&format=json&formatversion=2" + //Basic parameters
"&generator=categorymembers&gcmtype=file&gcmsort=timestamp&gcmdir=desc" + //Category parameters
"&prop=imageinfo&iiprop=url|extmetadata&iiurlwidth=640" + //Media property parameters
"&iiextmetadatafilter=DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal" +
"|Artist|LicenseShortName|LicenseUrl")
Observable<MwQueryResponse> getMediaListFromCategory(@Query("gcmtitle") String category, @Query("gcmlimit") int itemLimit, @QueryMap Map<String, String> continuation);

/**
* This method retrieves a list of Media objects filtered using image generator query
*
* @param keyword the searched keyword
* @param itemLimit how many images are returned
* @param continuation the continuation string from the previous query
* @return
*/
@GET("w/api.php?action=query&format=json&formatversion=2" + //Basic parameters
"&generator=search&gsrwhat=text&gsrnamespace=6" + //Search parameters
"&prop=imageinfo&iiprop=url|extmetadata&iiurlwidth=640" + //Media property parameters
"&iiextmetadatafilter=DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal" +
"|Artist|LicenseShortName|LicenseUrl")
Observable<MwQueryResponse> getMediaListFromSearch(@Query("gsrsearch") String keyword, @Query("gsrlimit") int itemLimit, @QueryMap Map<String, String> continuation);

}
Loading