|
1 | 1 | package fr.free.nrw.commons.media;
|
2 | 2 |
|
3 | 3 |
|
| 4 | +import androidx.annotation.NonNull; |
| 5 | +import androidx.annotation.Nullable; |
| 6 | + |
| 7 | +import org.wikipedia.dataclient.mwapi.MwQueryPage; |
| 8 | +import org.wikipedia.dataclient.mwapi.MwQueryResponse; |
| 9 | +import org.wikipedia.dataclient.mwapi.MwQueryResult; |
| 10 | + |
| 11 | +import java.util.Date; |
4 | 12 | import org.wikipedia.dataclient.mwapi.MwQueryResponse;
|
5 | 13 |
|
6 | 14 | import java.util.ArrayList;
|
|
13 | 21 | import javax.inject.Singleton;
|
14 | 22 |
|
15 | 23 | import fr.free.nrw.commons.Media;
|
| 24 | +import fr.free.nrw.commons.utils.CommonsDateUtil; |
| 25 | +import io.reactivex.Observable; |
| 26 | +import io.reactivex.Single; |
| 27 | +import okhttp3.HttpUrl; |
| 28 | +import okhttp3.Request; |
| 29 | +import okhttp3.Response; |
| 30 | +import timber.log.Timber; |
16 | 31 | import io.reactivex.Observable;
|
17 | 32 | import io.reactivex.Single;
|
18 | 33 |
|
@@ -103,5 +118,47 @@ private Single<List<Media>> responseToMediaList(Observable<MwQueryResponse> resp
|
103 | 118 | .map(Media::from)
|
104 | 119 | .collect(ArrayList<Media>::new, List::add);
|
105 | 120 | }
|
| 121 | + |
| 122 | + /** |
| 123 | + * Fetches Media object from the imageInfo API |
| 124 | + * |
| 125 | + * @param titles the tiles to be searched for. Can be filename or template name |
| 126 | + * @return |
| 127 | + */ |
| 128 | + public Single<Media> getMedia(String titles) { |
| 129 | + return mediaInterface.getMedia(titles) |
| 130 | + .flatMap(mwQueryResponse -> { |
| 131 | + if (null == mwQueryResponse |
| 132 | + || null == mwQueryResponse.query() |
| 133 | + || null == mwQueryResponse.query().firstPage()) { |
| 134 | + return Observable.empty(); |
| 135 | + } |
| 136 | + return Observable.just(mwQueryResponse.query().firstPage()); |
| 137 | + }) |
| 138 | + .map(Media::from) |
| 139 | + .single(Media.EMPTY); |
| 140 | + } |
106 | 141 |
|
| 142 | + /** |
| 143 | + * The method returns the picture of the day |
| 144 | + * |
| 145 | + * @return Media object corresponding to the picture of the day |
| 146 | + */ |
| 147 | + @NonNull |
| 148 | + public Single<Media> getPictureOfTheDay() { |
| 149 | + String date = CommonsDateUtil.getIso8601DateFormatShort().format(new Date()); |
| 150 | + Timber.d("Current date is %s", date); |
| 151 | + String template = "Template:Potd/" + date; |
| 152 | + return mediaInterface.getMediaWithGenerator(template) |
| 153 | + .flatMap(mwQueryResponse -> { |
| 154 | + if (null == mwQueryResponse |
| 155 | + || null == mwQueryResponse.query() |
| 156 | + || null == mwQueryResponse.query().firstPage()) { |
| 157 | + return Observable.empty(); |
| 158 | + } |
| 159 | + return Observable.just(mwQueryResponse.query().firstPage()); |
| 160 | + }) |
| 161 | + .map(Media::from) |
| 162 | + .single(Media.EMPTY); |
| 163 | + } |
107 | 164 | }
|
0 commit comments