mirror of
https://github.com/DustinBrett/daedalOS.git
synced 2025-12-06 00:20:05 +01:00
Compare commits
2 Commits
ae3233407c
...
fa4b92bd46
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa4b92bd46 | ||
|
|
2eca5f56ed |
|
|
@ -115,3 +115,5 @@ export const BASE_VIDEO_SELECTOR = ":scope > video";
|
|||
export const STABLE_DIFFUSION_DELAY_IN_MIN = 10;
|
||||
|
||||
export const PRELOAD_ID = "preloadWallpaper";
|
||||
|
||||
export const MAX_RETRIES = 5;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,15 @@
|
|||
import { MAX_RETRIES } from "components/system/Desktop/Wallpapers/constants";
|
||||
import {
|
||||
type WallpaperHandler,
|
||||
type ApodResponse,
|
||||
type ArtInstituteOfChicagoResponse,
|
||||
} from "components/system/Desktop/Wallpapers/types";
|
||||
import { type WallpaperFit } from "contexts/session/types";
|
||||
import { MILLISECONDS_IN_DAY, MILLISECONDS_IN_HOUR } from "utils/constants";
|
||||
import {
|
||||
HIGH_PRIORITY_REQUEST,
|
||||
MILLISECONDS_IN_DAY,
|
||||
MILLISECONDS_IN_HOUR,
|
||||
} from "utils/constants";
|
||||
import {
|
||||
jsonFetch,
|
||||
viewWidth,
|
||||
|
|
@ -53,60 +58,83 @@ export const wallpaperHandler: Record<string, WallpaperHandler> = {
|
|||
};
|
||||
},
|
||||
ART_INSTITUTE_OF_CHICAGO: async () => {
|
||||
const requestPayload = {
|
||||
boost: false,
|
||||
fields: ["image_id"],
|
||||
limit: 1,
|
||||
query: {
|
||||
function_score: {
|
||||
boost_mode: "replace",
|
||||
query: {
|
||||
bool: {
|
||||
filter: [
|
||||
{
|
||||
term: {
|
||||
is_public_domain: true,
|
||||
// eslint-disable-next-line unicorn/consistent-function-scoping
|
||||
const fetchArtwork = (): Promise<ArtInstituteOfChicagoResponse> =>
|
||||
jsonFetch<ArtInstituteOfChicagoResponse>(
|
||||
API_URL.ART_INSTITUTE_OF_CHICAGO,
|
||||
{
|
||||
body: JSON.stringify({
|
||||
boost: false,
|
||||
fields: ["image_id"],
|
||||
limit: 1,
|
||||
query: {
|
||||
function_score: {
|
||||
boost_mode: "replace",
|
||||
query: {
|
||||
bool: {
|
||||
filter: [
|
||||
{
|
||||
term: {
|
||||
is_public_domain: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
terms: {
|
||||
artwork_type_id: [1], // Painting
|
||||
},
|
||||
},
|
||||
{
|
||||
exists: {
|
||||
field: "image_id",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
terms: {
|
||||
artwork_type_id: [1], // Painting
|
||||
},
|
||||
random_score: {
|
||||
field: "id",
|
||||
seed: Date.now(),
|
||||
},
|
||||
{
|
||||
exists: {
|
||||
field: "image_id",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
random_score: {
|
||||
field: "id",
|
||||
seed: Date.now(),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const response = (await jsonFetch(API_URL.ART_INSTITUTE_OF_CHICAGO, {
|
||||
body: JSON.stringify(requestPayload),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
})) as ArtInstituteOfChicagoResponse;
|
||||
const imageUrl = (isMaxSize: boolean): string =>
|
||||
response?.data?.[0]?.image_id
|
||||
? `https://www.artic.edu/iiif/2/${response.data[0].image_id}/full/${
|
||||
isMaxSize ? "1686" : "843"
|
||||
},/0/default.jpg`
|
||||
: "";
|
||||
method: "POST",
|
||||
}
|
||||
);
|
||||
let wallpaperUrl = "";
|
||||
|
||||
for (let a = 0; a < MAX_RETRIES; a++) {
|
||||
try {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const { data: [{ image_id } = {}] = [] } = await fetchArtwork();
|
||||
|
||||
if (image_id) {
|
||||
const url = `https://www.artic.edu/iiif/2/${image_id}/full/1686,/0/default.jpg`;
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const { ok } = await fetch(url, {
|
||||
...HIGH_PRIORITY_REQUEST,
|
||||
method: "HEAD",
|
||||
});
|
||||
|
||||
if (ok) {
|
||||
wallpaperUrl = url;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Ignore failure to get wallpaper
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
fallbackBackground: imageUrl(false),
|
||||
fallbackBackground: "",
|
||||
newWallpaperFit: "fit",
|
||||
updateTimeout: MILLISECONDS_IN_HOUR,
|
||||
wallpaperUrl: imageUrl(true),
|
||||
wallpaperUrl,
|
||||
};
|
||||
},
|
||||
LOREM_PICSUM: () => ({
|
||||
|
|
|
|||
|
|
@ -1159,12 +1159,12 @@ export const getGifJs = async (): Promise<GIFWithWorkers> => {
|
|||
}) as GIFWithWorkers;
|
||||
};
|
||||
|
||||
export const jsonFetch = async (
|
||||
export const jsonFetch = async <T extends Record<string, unknown>>(
|
||||
url: string,
|
||||
options?: RequestInit
|
||||
): Promise<Record<string, unknown>> => {
|
||||
): Promise<T> => {
|
||||
const response = await fetch(url, { ...HIGH_PRIORITY_REQUEST, ...options });
|
||||
const json = (await response.json()) as Record<string, unknown>;
|
||||
const json = (await response.json()) as T;
|
||||
|
||||
return json || {};
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user