Skip to content

Convert wikidata/mwapi to kotlin (part 3) #6004

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import fr.free.nrw.commons.wikidata.model.Entities
import fr.free.nrw.commons.wikidata.model.gallery.ExtMetadata
import fr.free.nrw.commons.wikidata.model.gallery.ImageInfo
import fr.free.nrw.commons.wikidata.mwapi.MwQueryPage
import org.apache.commons.lang3.StringUtils
import java.text.ParseException
import java.util.Date
import javax.inject.Inject
Expand All @@ -24,24 +23,24 @@ class MediaConverter
entity: Entities.Entity,
imageInfo: ImageInfo,
): Media {
val metadata = imageInfo.metadata
val metadata = imageInfo.getMetadata()
requireNotNull(metadata) { "No metadata" }
// Stores mapping of title attribute to hidden attribute of each category
val myMap = mutableMapOf<String, Boolean>()
page.categories()?.forEach { myMap[it.title()] = (it.hidden()) }

return Media(
page.pageId().toString(),
imageInfo.thumbUrl.takeIf { it.isNotBlank() } ?: imageInfo.originalUrl,
imageInfo.originalUrl,
imageInfo.getThumbUrl().takeIf { it.isNotBlank() } ?: imageInfo.getOriginalUrl(),
imageInfo.getOriginalUrl(),
page.title(),
metadata.imageDescription(),
safeParseDate(metadata.dateTime()),
metadata.licenseShortName(),
metadata.prefixedLicenseUrl,
getAuthor(metadata),
getAuthor(metadata),
MediaDataExtractorUtil.extractCategoriesFromList(metadata.categories),
MediaDataExtractorUtil.extractCategoriesFromList(metadata.categories()),
metadata.latLng,
entity.labels().mapValues { it.value.value() },
entity.descriptions().mapValues { it.value.value() },
Expand Down Expand Up @@ -104,9 +103,5 @@ private val ExtMetadata.prefixedLicenseUrl: String
}

private val ExtMetadata.latLng: LatLng?
get() =
if (!StringUtils.isBlank(gpsLatitude) && !StringUtils.isBlank(gpsLongitude)) {
LatLng(gpsLatitude.toDouble(), gpsLongitude.toDouble(), 0.0f)
} else {
null
}
get() = LatLng.latLongOrNull(gpsLatitude(), gpsLongitude())

7 changes: 7 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/location/LatLng.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ data class LatLng(
* Accepts a non-null [Location] and converts it to a [LatLng].
*/
companion object {
fun latLongOrNull(latitude: String?, longitude: String?): LatLng? =
if (!latitude.isNullOrBlank() && !longitude.isNullOrBlank()) {
LatLng(latitude.toDouble(), longitude.toDouble(), 0.0f)
} else {
null
}

/**
* gets the latitude and longitude of a given non-null location
* @param location the non-null location of the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class NotificationClient
return Notification(
notificationType = notificationType,
notificationText = notificationText,
date = DateUtil.getMonthOnlyDateString(timestamp),
link = contents?.links?.primary?.url ?: "",
date = DateUtil.getMonthOnlyDateString(getTimestamp()),
link = contents?.links?.getPrimary()?.url ?: "",
iconUrl = "",
notificationId = id().toString(),
)
Expand Down

This file was deleted.

25 changes: 25 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/wikidata/model/edit/Edit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fr.free.nrw.commons.wikidata.model.edit

import fr.free.nrw.commons.wikidata.mwapi.MwPostResponse

class Edit : MwPostResponse() {
private val edit: Result? = null

fun edit(): Result? = edit

class Result {
private val result: String? = null
private val code: String? = null
private val info: String? = null
private val warning: String? = null

fun editSucceeded(): Boolean =
"Success" == result

fun code(): String? = code

fun info(): String? = info

fun warning(): String? = warning
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package fr.free.nrw.commons.wikidata.model.gallery

import com.google.gson.annotations.SerializedName
import org.apache.commons.lang3.StringUtils

class ExtMetadata {
@SerializedName("DateTime") private val dateTime: Values? = null
@SerializedName("ObjectName") private val objectName: Values? = null
@SerializedName("CommonsMetadataExtension") private val commonsMetadataExtension: Values? = null
@SerializedName("Categories") private val categories: Values? = null
@SerializedName("Assessments") private val assessments: Values? = null
@SerializedName("GPSLatitude") private val gpsLatitude: Values? = null
@SerializedName("GPSLongitude") private val gpsLongitude: Values? = null
@SerializedName("ImageDescription") private val imageDescription: Values? = null
@SerializedName("DateTimeOriginal") private val dateTimeOriginal: Values? = null
@SerializedName("Artist") private val artist: Values? = null
@SerializedName("Credit") private val credit: Values? = null
@SerializedName("Permission") private val permission: Values? = null
@SerializedName("AuthorCount") private val authorCount: Values? = null
@SerializedName("LicenseShortName") private val licenseShortName: Values? = null
@SerializedName("UsageTerms") private val usageTerms: Values? = null
@SerializedName("LicenseUrl") private val licenseUrl: Values? = null
@SerializedName("AttributionRequired") private val attributionRequired: Values? = null
@SerializedName("Copyrighted") private val copyrighted: Values? = null
@SerializedName("Restrictions") private val restrictions: Values? = null
@SerializedName("License") private val license: Values? = null

fun licenseShortName(): String = licenseShortName?.value ?: ""

fun licenseUrl(): String = licenseUrl?.value ?: ""

fun license(): String = license?.value ?: ""

fun imageDescription(): String = imageDescription?.value ?: ""

fun imageDescriptionSource(): String = imageDescription?.source ?: ""

fun objectName(): String = objectName?.value ?: ""

fun usageTerms(): String = usageTerms?.value ?: ""

fun dateTimeOriginal(): String = dateTimeOriginal?.value ?: ""

fun dateTime(): String = dateTime?.value ?: ""

fun artist(): String = artist?.value ?: ""

fun categories(): String = categories?.value ?: ""

fun gpsLatitude(): String = gpsLatitude?.value ?: ""

fun gpsLongitude(): String = gpsLongitude?.value ?: ""

fun credit(): String = credit?.value ?: ""

class Values {
val value: String? = null
val source: String? = null
val hidden: String? = null
}
}
Loading
Loading