mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
parent
422890cac4
commit
43b753dfe9
2 changed files with 119 additions and 101 deletions
|
|
@ -1,101 +0,0 @@
|
|||
package fr.free.nrw.commons;
|
||||
|
||||
import static fr.free.nrw.commons.depictions.Media.DepictedImagesFragment.PAGE_ID_PREFIX;
|
||||
|
||||
import androidx.core.text.HtmlCompat;
|
||||
import fr.free.nrw.commons.media.Depictions;
|
||||
import fr.free.nrw.commons.media.MediaClient;
|
||||
import io.reactivex.Single;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
* Fetch additional media data from the network that we don't store locally.
|
||||
*
|
||||
* This includes things like category lists and multilingual descriptions,
|
||||
* which are not intrinsic to the media and may change due to editing.
|
||||
*/
|
||||
@Singleton
|
||||
public class MediaDataExtractor {
|
||||
|
||||
private final MediaClient mediaClient;
|
||||
|
||||
@Inject
|
||||
public MediaDataExtractor(final MediaClient mediaClient) {
|
||||
this.mediaClient = mediaClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simplified method to extract all details required to show media details.
|
||||
* It fetches media object, deletion status, talk page and captions for the filename
|
||||
* @param filename for which the details are to be fetched
|
||||
* @return full Media object with all details including deletion status and talk page
|
||||
*/
|
||||
public Single<Media> fetchMediaDetails(final String filename, final String pageId) {
|
||||
return Single.zip(getMediaFromFileName(filename),
|
||||
mediaClient.checkPageExistsUsingTitle("Commons:Deletion_requests/" + filename),
|
||||
getDiscussion(filename),
|
||||
pageId != null ? getCaption(PAGE_ID_PREFIX + pageId)
|
||||
: Single.just(MediaClient.NO_CAPTION),
|
||||
getDepictions(filename),
|
||||
this::combineToMedia);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Media combineToMedia(final Media media, final Boolean deletionStatus, final String discussion,
|
||||
final String caption, final Depictions depictions) {
|
||||
media.setDiscussion(discussion);
|
||||
media.setCaption(caption);
|
||||
media.setDepictions(depictions);
|
||||
if (deletionStatus) {
|
||||
media.setRequestedDeletion(true);
|
||||
}
|
||||
return media;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains captions using filename
|
||||
* @param wikibaseIdentifier
|
||||
*
|
||||
* @return caption for the image in user's locale
|
||||
* Ex: "a nice painting" (english locale) and "No Caption" in case the caption is not available for the image
|
||||
*/
|
||||
private Single<String> getCaption(final String wikibaseIdentifier) {
|
||||
return mediaClient.getCaptionByWikibaseIdentifier(wikibaseIdentifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch depictions from the MediaWiki API
|
||||
* @param filename the filename we will return the caption for
|
||||
* @return Depictions
|
||||
*/
|
||||
private Single<Depictions> getDepictions(final String filename) {
|
||||
return mediaClient.getDepictions(filename)
|
||||
.doOnError(throwable -> Timber.e(throwable, "error while fetching depictions"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method can be used to fetch media for a given filename
|
||||
* @param filename Eg. File:Test.jpg
|
||||
* @return return data rich Media object
|
||||
*/
|
||||
public Single<Media> getMediaFromFileName(final String filename) {
|
||||
return mediaClient.getMedia(filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch talk page from the MediaWiki API
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
private Single<String> getDiscussion(final String filename) {
|
||||
return mediaClient.getPageHtml(filename.replace("File", "File talk"))
|
||||
.map(discussion -> HtmlCompat.fromHtml(discussion, HtmlCompat.FROM_HTML_MODE_LEGACY).toString())
|
||||
.onErrorReturn(throwable -> {
|
||||
Timber.e(throwable, "Error occurred while fetching discussion");
|
||||
return "";
|
||||
});
|
||||
}
|
||||
}
|
||||
119
app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.kt
Normal file
119
app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.kt
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
package fr.free.nrw.commons
|
||||
|
||||
import androidx.core.text.HtmlCompat
|
||||
import fr.free.nrw.commons.depictions.Media.DepictedImagesFragment
|
||||
import fr.free.nrw.commons.media.Depictions
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.functions.Function5
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Fetch additional media data from the network that we don't store locally.
|
||||
*
|
||||
* This includes things like category lists and multilingual descriptions,
|
||||
* which are not intrinsic to the media and may change due to editing.
|
||||
*/
|
||||
@Singleton
|
||||
class MediaDataExtractor @Inject constructor(private val mediaClient: MediaClient) {
|
||||
|
||||
/**
|
||||
* Simplified method to extract all details required to show media details.
|
||||
* It fetches media object, deletion status, talk page and captions for the filename
|
||||
* @param filename for which the details are to be fetched
|
||||
* @return full Media object with all details including deletion status and talk page
|
||||
*/
|
||||
fun fetchMediaDetails(filename: String, pageId: String?): Single<Media> {
|
||||
return Single.zip(
|
||||
getMediaFromFileName(filename),
|
||||
mediaClient.checkPageExistsUsingTitle("Commons:Deletion_requests/$filename"),
|
||||
getDiscussion(filename),
|
||||
if (pageId != null)
|
||||
getCaption(DepictedImagesFragment.PAGE_ID_PREFIX + pageId)
|
||||
else Single.just(MediaClient.NO_CAPTION),
|
||||
getDepictions(filename),
|
||||
Function5 { media: Media, deletionStatus: Boolean, discussion: String, caption: String, depictions: Depictions ->
|
||||
combineToMedia(
|
||||
media,
|
||||
deletionStatus,
|
||||
discussion,
|
||||
caption,
|
||||
depictions
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun combineToMedia(
|
||||
media: Media,
|
||||
deletionStatus: Boolean,
|
||||
discussion: String,
|
||||
caption: String,
|
||||
depictions: Depictions
|
||||
): Media {
|
||||
media.discussion = discussion
|
||||
media.caption = caption
|
||||
media.depictions = depictions
|
||||
if (deletionStatus) {
|
||||
media.isRequestedDeletion = true
|
||||
}
|
||||
return media
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains captions using filename
|
||||
* @param wikibaseIdentifier
|
||||
*
|
||||
* @return caption for the image in user's locale
|
||||
* Ex: "a nice painting" (english locale) and "No Caption" in case the caption is not available for the image
|
||||
*/
|
||||
private fun getCaption(wikibaseIdentifier: String): Single<String> {
|
||||
return mediaClient.getCaptionByWikibaseIdentifier(wikibaseIdentifier)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch depictions from the MediaWiki API
|
||||
* @param filename the filename we will return the caption for
|
||||
* @return Depictions
|
||||
*/
|
||||
private fun getDepictions(filename: String): Single<Depictions> {
|
||||
return mediaClient.getDepictions(filename)
|
||||
.doOnError { throwable: Throwable? ->
|
||||
Timber.e(
|
||||
throwable,
|
||||
"error while fetching depictions"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method can be used to fetch media for a given filename
|
||||
* @param filename Eg. File:Test.jpg
|
||||
* @return return data rich Media object
|
||||
*/
|
||||
fun getMediaFromFileName(filename: String?): Single<Media> {
|
||||
return mediaClient.getMedia(filename)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch talk page from the MediaWiki API
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
private fun getDiscussion(filename: String): Single<String> {
|
||||
return mediaClient.getPageHtml(filename.replace("File", "File talk"))
|
||||
.map { discussion: String? ->
|
||||
HtmlCompat.fromHtml(
|
||||
discussion!!,
|
||||
HtmlCompat.FROM_HTML_MODE_LEGACY
|
||||
).toString()
|
||||
}
|
||||
.onErrorReturn { throwable: Throwable? ->
|
||||
Timber.e(throwable, "Error occurred while fetching discussion")
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue