package org.wikipedia.commons

import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import org.wikipedia.dataclient.Service
import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.dataclient.WikiSite
import java.util.*

object ImageTagsProvider {
    fun getImageTagsObservable(pageId: Long, langCode: String): Observable<Map<String, List<String>>> {
        return ServiceFactory.get(WikiSite(Service.COMMONS_URL)).getClaims("M$pageId")
                .subscribeOn(Schedulers.io())
                .flatMap { claims ->
                    val depicts = claims.claims()["P180"]
                    val ids = mutableListOf<String?>()
                    depicts?.forEach {
                        ids.add(it.mainSnak?.dataValue?.value?.id)
                    }
                    if (ids.isEmpty()) {
                        Observable.empty()
                    } else {
                        ServiceFactory.get(WikiSite(Service.WIKIDATA_URL)).getWikidataLabels(ids.joinToString(separator = "|"), langCode)
                    }
                }
                .subscribeOn(Schedulers.io())
                .map { entities ->
                    val tags = HashMap<String, MutableList<String>>()
                    entities.entities().forEach {
                        it.value.labels().values.forEach { label ->
                            if (tags[label.language()].isNullOrEmpty()) {
                                tags[label.language()] = mutableListOf(label.value())
                            } else {
                                tags[label.language()]!!.add(label.value())
                            }
                        }
                    }
                    tags
                }
    }
}


package org.wikipedia.commons

import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import org.wikipedia.dataclient.Service
import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.dataclient.wikidata.Claims
import org.wikipedia.dataclient.wikidata.Entities
import org.wikipedia.util.log.L
import java.util.*

object ImageTagsProvider {
    fun getImageTagsObservable(pageId: Int, langCode: String): Observable<Map<String, List<String>>> {
        return ServiceFactory.get(WikiSite(Service.COMMONS_URL)).getClaims("M$pageId")
                .subscribeOn(Schedulers.io())
                .onErrorReturnItem(Claims())
                .flatMap { claims ->
                    val depicts = claims.claims()["P180"]
                    val ids = mutableListOf<String?>()
                    depicts?.forEach {
                        ids.add(it.mainSnak?.dataValue?.value?.id)
                    }
                    if (ids.isEmpty()) {
                        Observable.just(Entities())
                    } else {
                        ServiceFactory.get(WikiSite(Service.WIKIDATA_URL)).getWikidataLabels(ids.joinToString(separator = "|"), langCode)
                    }
                }
                .subscribeOn(Schedulers.io())
                .map { entities ->
                    val tags = HashMap<String, MutableList<String>>()
                    entities.entities().forEach {
                        it.value.labels().values.forEach { label ->
                            if (tags[label.language()].isNullOrEmpty()) {
                                tags[label.language()] = mutableListOf(label.value())
                            } else {
                                tags[label.language()]!!.add(label.value())
                            }
                        }
                    }
                    tags
                }
    }
}


Handle MW error message and change pageId from Long to Int
Handle MW error message and change pageId from Long to Int

wikimedia/apps-android-wikipedia,wikimedia/apps-android-wikipedia,wikimedia/apps-android-wikipedia,dbrant/apps-android-wikipedia,dbrant/apps-android-wikipedia,wikimedia/apps-android-wikipedia,dbrant/apps-android-wikipedia,dbrant/apps-android-wikipedia,dbrant/apps-android-wikipedia