-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add wikipedia picture of the day
- Loading branch information
Showing
7 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
app/src/main/java/com/bnyro/wallpaper/api/wi/obj/WikiImageInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
app/src/main/java/com/bnyro/wallpaper/api/wi/obj/WikiImagePage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/bnyro/wallpaper/api/wi/obj/WikiImageQuery.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/bnyro/wallpaper/api/wi/obj/WikiImagesResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |