Skip to content

Commit

Permalink
feat: support for Microsoft Spotlight (closes #207)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 4, 2024
1 parent e7f0de6 commit 3e3087e
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.bnyro.wallpaper.api.ow.OwApi
import com.bnyro.wallpaper.api.ps.PsApi
import com.bnyro.wallpaper.api.px.PxApi
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.db.DatabaseHolder
Expand Down Expand Up @@ -53,5 +54,6 @@ class App : Application(), ImageLoaderFactory {
val leApi = LeApi()
val whApi = WhApi()
val pxApi = PxApi()
val spApi = SpApi()
}
}
41 changes: 41 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/sp/SpApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.bnyro.wallpaper.api.sp

import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.api.sp.obj.SpotlightImage
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper
import kotlinx.serialization.decodeFromString
import java.util.Locale

// Credits to https://github.com/ORelio/Spotlight-Downloader/blob/master/SpotlightAPI.md

class SpApi: Api() {
override val name = "Windows Spotlight"
override val baseUrl = "https://fd.api.iris.microsoft.com"
override val filters: Map<String, List<String>> = mapOf(
"country" to listOf("US") + Locale.getISOCountries().toList().filter { it != "US" }.sorted()
)

private val api = RetrofitHelper.create(baseUrl, Spotlight::class.java)

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
val country = getQuery("country")
val apiResp = api.getLatestPage(country = country, locale = "en-${country}")
val images = apiResp.batchrsp.items.map {
RetrofitHelper.json.decodeFromString<SpotlightImage>(it.item)
}
return images.map {
Wallpaper(
title = it.ad.title,
imgSrc = it.ad.portraitImage.asset,
author = it.ad.copyright,
url = it.ad.ctaUri.replaceFirst("microsoft-edge:", "")
)
}
}

override suspend fun getRandomWallpaperUrl(): String {
val country = getQuery("country")
return api.getLatest(country = country, locale = "en-${country}").ad.portraitImage.asset
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/sp/Spotlight.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.bnyro.wallpaper.api.sp

import com.bnyro.wallpaper.api.sp.obj.SpotlightImage
import com.bnyro.wallpaper.api.sp.obj.SpotlightImageItem
import com.bnyro.wallpaper.api.sp.obj.SpotlightPage
import retrofit2.http.GET
import retrofit2.http.Query

private const val PLACEMENT_ID = "88000820"

interface Spotlight {
@GET("v4/api/selection")
suspend fun getLatestPage(
@Query("country") country: String = "US",
@Query("locale") locale: String = "en-US",
@Query("fmt") format: String = "json",
@Query("placement") placement: String = PLACEMENT_ID,
@Query("bcnt") limit: Int = 4
): SpotlightPage

@GET("v4/api/selection")
suspend fun getLatest(
@Query("country") country: String = "US",
@Query("locale") locale: String = "en-US",
@Query("fmt") format: String = "json",
@Query("placement") placement: String = PLACEMENT_ID
): SpotlightImage
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bnyro.wallpaper.api.sp.obj

import kotlinx.serialization.Serializable

@Serializable
data class SpotlightBatchrsp(
val items: List<SpotlightData>,
val ver: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bnyro.wallpaper.api.sp.obj

import kotlinx.serialization.Serializable

@Serializable
data class SpotlightData(
val item: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bnyro.wallpaper.api.sp.obj

import kotlinx.serialization.Serializable

@Serializable
data class SpotlightImage(
val ad: SpotlightInfo,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bnyro.wallpaper.api.sp.obj

import kotlinx.serialization.Serializable

@Serializable
data class SpotlightImageItem(
val asset: String
)
19 changes: 19 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/sp/obj/SpotlightInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bnyro.wallpaper.api.sp.obj

import kotlinx.serialization.Serializable

@Serializable
data class SpotlightInfo(
val copyright: String,
val ctaText: String,
val ctaUri: String,
val description: String,
val dislikeGlyph: String,
val entityId: String,
val iconHoverText: String,
val iconLabel: String,
val landscapeImage: SpotlightImageItem,
val likeGlyph: String,
val portraitImage: SpotlightImageItem,
val title: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bnyro.wallpaper.api.sp.obj

import kotlinx.serialization.Serializable

@Serializable
data class SpotlightPage(
val batchrsp: SpotlightBatchrsp
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.material.icons.filled.Forum
import androidx.compose.material.icons.filled.History
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Landscape
import androidx.compose.material.icons.filled.LightMode
import androidx.compose.material.icons.filled.Nightlight
import androidx.compose.material.icons.filled.Pix
import androidx.compose.material.icons.filled.Settings
Expand All @@ -32,6 +33,7 @@ sealed class DrawerScreens(
object Reddit : DrawerScreens(R.string.reddit, "redd", Icons.Default.Forum)
object Lemmy : DrawerScreens(R.string.lemmy, "le", Icons.Default.Book)
object Pixel : DrawerScreens(R.string.pixel, "px", Icons.Default.Pix)
object Spotlight : DrawerScreens(R.string.spotlight, "sp", Icons.Default.LightMode)
object Favorites : DrawerScreens(R.string.favorites, "favorites", Icons.Default.Favorite, true)
object History : DrawerScreens(R.string.history, "history", Icons.Default.History)
object Settings : DrawerScreens(R.string.settings, "settings", Icons.Default.Settings, true)
Expand All @@ -47,7 +49,8 @@ sealed class DrawerScreens(
BingDaily,
Reddit,
Lemmy,
Pixel
Pixel,
Spotlight
)
}
val screens by lazy {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ object Preferences {
DrawerScreens.Reddit.route -> App.reApi
DrawerScreens.Lemmy.route -> App.leApi
DrawerScreens.Pixel.route -> App.pxApi
DrawerScreens.Spotlight.route -> App.spApi
else -> App.whApi
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object RetrofitHelper {

if (BuildConfig.DEBUG) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC)
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
builder.addInterceptor(loggingInterceptor)
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="reddit" translatable="false">Reddit</string>
<string name="lemmy" translatable="false">Lemmy</string>
<string name="pixel" translatable="false">Google Pixel</string>
<string name="spotlight" translatable="false">Spotlight</string>
<!-- Navigation Drawer -->
<string name="favorites">Favorites</string>
<string name="history">History</string>
Expand Down

0 comments on commit 3e3087e

Please sign in to comment.