Skip to content

Commit

Permalink
feat: add wikipedia picture of the day
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Feb 3, 2025
1 parent 0f6fc32 commit 90c1cad
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.bnyro.wallpaper.api.re.ReApi
import com.bnyro.wallpaper.api.sp.SpApi
import com.bnyro.wallpaper.api.us.UsApi
import com.bnyro.wallpaper.api.wh.WhApi
import com.bnyro.wallpaper.api.wi.WiAPi
import com.bnyro.wallpaper.db.DatabaseHolder
import com.bnyro.wallpaper.util.Preferences

Expand Down Expand Up @@ -48,6 +49,6 @@ class App : Application(), ImageLoaderFactory {

companion object {
val apis =
listOf(WhApi(), OwApi(), UsApi(), BiApi(), ReApi(), LeApi(), PxApi(), SpApi(), NaApi(), PsApi())
listOf(WhApi(), OwApi(), UsApi(), BiApi(), ReApi(), LeApi(), PxApi(), SpApi(), NaApi(), WiAPi(), PsApi())
}
}
57 changes: 57 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/wi/WiAPi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.bnyro.wallpaper.api.wi

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Today
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.LocalDate
import java.time.format.DateTimeFormatter

class WiAPi : Api() {
override val name = "Wikipedia POTD"
override val icon: ImageVector = Icons.Default.Today
override val baseUrl = "https://en.wikipedia.org"

private val api = RetrofitHelper.create(baseUrl, WikiPOTD::class.java)
private val dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd")

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
val now = LocalDate.now()
val dates = mutableListOf<String>()

for (i in 0 until PAGE_SIZE) {
val offset = (page - 1) * PAGE_SIZE + i
val date = now.minusDays(offset.toLong())
dates.add(date.format(dateFormat))
}

val titles = dates.map { POTD_ARTICLE_PREFIX + it }
return api.getImages(titles = titles.joinToString("|")).query.pages.values
.filter { it.imageInfo.isNotEmpty() }
.map { image ->
val imageInfo = image.imageInfo.first()

Wallpaper(
imgSrc = imageInfo.url,
url = imageInfo.descriptionUrl,
title = image.title.substringAfter(":").substringBeforeLast("."),
resolution = "${imageInfo.width}x${imageInfo.height}",
thumb = imageInfo.thumbUrl
)
}
}

override suspend fun getRandomWallpaperUrl(): String? {
val today = LocalDate.now().format(dateFormat)

return api.getImages(titles = POTD_ARTICLE_PREFIX + today).query.pages.values.firstOrNull()
?.imageInfo?.firstOrNull()?.url
}

companion object {
private const val POTD_ARTICLE_PREFIX = "Template:POTD/"
private const val PAGE_SIZE = 10
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/wi/WikiPOTD.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.bnyro.wallpaper.api.wi

import com.bnyro.wallpaper.api.wi.obj.WikiImagesResponse
import retrofit2.http.GET
import retrofit2.http.Query

interface WikiPOTD {
@GET("w/api.php")
suspend fun getImages(
@Query("action") action: String = "query",
@Query("format") format: String = "json",
@Query("generator") generator: String = "images",
@Query("prop") prop: String = "imageinfo",
@Query("iiprop") iiProp: String = "url|size",
@Query("iiurlwidth") iiUrlWidth: String = "300",
@Query("titles") titles: String, // separated by "|"
): WikiImagesResponse
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/wi/obj/WikiImageInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bnyro.wallpaper.api.wi.obj

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class WikiImageInfo(
@SerialName("descriptionshorturl") val descriptionShortUrl: String,
@SerialName("descriptionurl") val descriptionUrl: String,
val url: String,
val width: Int,
val height: Int,
@SerialName("thumburl") val thumbUrl: String? = null
)
13 changes: 13 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/wi/obj/WikiImagePage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bnyro.wallpaper.api.wi.obj

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class WikiImagePage(
@SerialName("imageinfo") val imageInfo: List<WikiImageInfo>,
@SerialName("imagerepository") val imageRepository: String,
@SerialName("pageid") val pageId: Int,
val title: String,
val ns: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bnyro.wallpaper.api.wi.obj

import kotlinx.serialization.Serializable

@Serializable
data class WikiImageQuery(
val pages: Map<String, WikiImagePage>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bnyro.wallpaper.api.wi.obj

import kotlinx.serialization.Serializable

@Serializable
data class WikiImagesResponse(
val query: WikiImageQuery
)

0 comments on commit 90c1cad

Please sign in to comment.