From 9ef51bbd338ffabd77d4c90fb973bb14eb7ad3bb Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 29 Oct 2024 12:04:46 +0100 Subject: [PATCH] feat: support for NASA APOD (closes #212) --- app/src/main/java/com/bnyro/wallpaper/App.kt | 3 +- .../java/com/bnyro/wallpaper/api/na/NaApi.kt | 63 +++++++++++++++++++ .../com/bnyro/wallpaper/api/na/NasaAPOD.kt | 22 +++++++ .../bnyro/wallpaper/api/na/obj/NasaImage.kt | 15 +++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/bnyro/wallpaper/api/na/NaApi.kt create mode 100644 app/src/main/java/com/bnyro/wallpaper/api/na/NasaAPOD.kt create mode 100644 app/src/main/java/com/bnyro/wallpaper/api/na/obj/NasaImage.kt diff --git a/app/src/main/java/com/bnyro/wallpaper/App.kt b/app/src/main/java/com/bnyro/wallpaper/App.kt index 1249ab5..9602503 100644 --- a/app/src/main/java/com/bnyro/wallpaper/App.kt +++ b/app/src/main/java/com/bnyro/wallpaper/App.kt @@ -6,6 +6,7 @@ import coil.ImageLoaderFactory import coil.disk.DiskCache import com.bnyro.wallpaper.api.bi.BiApi import com.bnyro.wallpaper.api.le.LeApi +import com.bnyro.wallpaper.api.na.NaApi import com.bnyro.wallpaper.api.ow.OwApi import com.bnyro.wallpaper.api.ps.PsApi import com.bnyro.wallpaper.api.px.PxApi @@ -47,6 +48,6 @@ class App : Application(), ImageLoaderFactory { companion object { val apis = - listOf(WhApi(), OwApi(), UsApi(), BiApi(), ReApi(), LeApi(), PxApi(), SpApi(), PsApi()) + listOf(WhApi(), OwApi(), UsApi(), BiApi(), ReApi(), LeApi(), PxApi(), SpApi(), NaApi(), PsApi()) } } diff --git a/app/src/main/java/com/bnyro/wallpaper/api/na/NaApi.kt b/app/src/main/java/com/bnyro/wallpaper/api/na/NaApi.kt new file mode 100644 index 0000000..e7d1a42 --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/api/na/NaApi.kt @@ -0,0 +1,63 @@ +package com.bnyro.wallpaper.api.na + +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Star +import androidx.compose.ui.graphics.vector.ImageVector +import com.bnyro.wallpaper.api.Api +import com.bnyro.wallpaper.db.obj.Wallpaper +import com.bnyro.wallpaper.util.RetrofitHelper +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter + +class NaApi : Api() { + override val name: String = "NASA APOD" + override val icon: ImageVector = Icons.Default.Star + override val baseUrl: String = "https://api.nasa.gov/" + + override val filters: Map> + get() = mapOf("order" to listOf("date", "random")) + + private val api = RetrofitHelper.create(baseUrl, NasaAPOD::class.java) + + private var nextEndDate: LocalDateTime? = null + + override suspend fun getWallpapers(page: Int): List { + val sortByDate = getQuery("order") == "date" + + val response = if (sortByDate) { + val endDate = if (page == 1 || nextEndDate == null) { + LocalDateTime.now() + } else { + nextEndDate + } + nextEndDate = endDate!!.minusDays(10) + + api.getImages( + endDate = endDate.format(DateTimeFormatter.ISO_LOCAL_DATE), + startDate = nextEndDate!!.format( + DateTimeFormatter.ISO_LOCAL_DATE + ) + ) + } else { + api.getImages(count = 10) + } + + return response + .filter { it.mediaType == "image" && (it.url ?: it.hdUrl) != null } + .map { + Wallpaper( + imgSrc = it.hdUrl ?: it.url!!, + thumb = it.url, + author = it.copyright, + creationDate = it.date, + title = it.title, + description = it.explanation + ) + } + } + + override suspend fun getRandomWallpaperUrl(): String? { + val image = api.getLatestImage() + return image.hdUrl ?: image.url + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/wallpaper/api/na/NasaAPOD.kt b/app/src/main/java/com/bnyro/wallpaper/api/na/NasaAPOD.kt new file mode 100644 index 0000000..0d217b7 --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/api/na/NasaAPOD.kt @@ -0,0 +1,22 @@ +package com.bnyro.wallpaper.api.na + +import com.bnyro.wallpaper.api.na.obj.NasaImage +import retrofit2.http.GET +import retrofit2.http.Query + +private const val API_KEY = "DEMO_KEY" + +interface NasaAPOD { + @GET("planetary/apod") + suspend fun getImages( + @Query("api_key") apiKey: String = API_KEY, + @Query("count") count: Int? = null, + @Query("start_date") startDate: String? = null, + @Query("end_date") endDate: String? = null + ): List + + @GET("planetary/apod") + suspend fun getLatestImage( + @Query("api_key") apiKey: String = API_KEY, + ): NasaImage +} \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/wallpaper/api/na/obj/NasaImage.kt b/app/src/main/java/com/bnyro/wallpaper/api/na/obj/NasaImage.kt new file mode 100644 index 0000000..6e11105 --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/api/na/obj/NasaImage.kt @@ -0,0 +1,15 @@ +package com.bnyro.wallpaper.api.na.obj + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class NasaImage( + val url: String? = null, + val title: String, + val explanation: String, + val copyright: String? = null, + val date: String, + @SerialName("hdurl") val hdUrl: String? = null, + @SerialName("media_type") val mediaType: String, +) \ No newline at end of file