mirror of
https://github.com/zhxycn/BingWallpaperAPI.git
synced 2024-11-30 19:18:47 +00:00
AWS Lambda
This commit is contained in:
commit
89e3013a54
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2021 zhxy-CN
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
99
index.mjs
Normal file
99
index.mjs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
import https from "https";
|
||||||
|
|
||||||
|
const fetchData = async (url) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
https.get(url, (res) => {
|
||||||
|
let body = "";
|
||||||
|
res.on("data", (chunk) => body += chunk);
|
||||||
|
res.on("end", () => {
|
||||||
|
try {
|
||||||
|
resolve(JSON.parse(body));
|
||||||
|
} catch {
|
||||||
|
reject(new Error("Failed to parse response"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).on("error", (error) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const getBingUrl = (ago, region) => {
|
||||||
|
const baseUrl = region === "1"
|
||||||
|
? "https://cn.bing.com/HPImageArchive.aspx"
|
||||||
|
: "https://bing.com/HPImageArchive.aspx";
|
||||||
|
|
||||||
|
return `${baseUrl}?format=js&idx=${ago}&n=1${region === "1" ? "&mkt=zh-CN" : ""}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleError = (error) => {
|
||||||
|
console.error(error);
|
||||||
|
return {
|
||||||
|
statusCode: 500,
|
||||||
|
body: JSON.stringify({ error: error.message || "Internal Server Error" }),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const escapeXML = (str) => {
|
||||||
|
return str
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
.replace(/"/g, """)
|
||||||
|
.replace(/"/g, "'");
|
||||||
|
};
|
||||||
|
|
||||||
|
const jsonToXML = (obj, rootElement) => {
|
||||||
|
let xml = rootElement ? `<${rootElement}>` : "";
|
||||||
|
for (const key in obj) {
|
||||||
|
if (obj.hasOwnProperty(key)) {
|
||||||
|
const value = obj[key];
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
value.forEach(item => {
|
||||||
|
xml += jsonToXML(item, key);
|
||||||
|
});
|
||||||
|
} else if (typeof value === "object" && value !== null) {
|
||||||
|
xml += jsonToXML(value, key);
|
||||||
|
} else {
|
||||||
|
xml += `<${key}>${escapeXML(String(value))}</${key}>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xml += rootElement ? `</${rootElement}>` : "";
|
||||||
|
return xml;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const handler = async (event) => {
|
||||||
|
const ago = event.queryStringParameters?.ago || "0";
|
||||||
|
const regionParam = event.queryStringParameters?.region || "global";
|
||||||
|
const region = regionParam === "cn" ? "1" : "0";
|
||||||
|
|
||||||
|
const url = getBingUrl(ago, region);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await fetchData(url);
|
||||||
|
|
||||||
|
if (event.queryStringParameters?.encode === "json") {
|
||||||
|
return {
|
||||||
|
statusCode: 200,
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
headers: { "Content-Type": "application/json" }
|
||||||
|
};
|
||||||
|
} else if (event.queryStringParameters?.encode === "xml") {
|
||||||
|
const xmlData = jsonToXML(data, "images");
|
||||||
|
return {
|
||||||
|
statusCode: 200,
|
||||||
|
body: xmlData,
|
||||||
|
headers: { "Content-Type": "application/xml" }
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const responseUrl = `https://${region === "0" ? "bing.com" : "cn.bing.com"}${data.images[0].url}`;
|
||||||
|
return {
|
||||||
|
statusCode: 302,
|
||||||
|
headers: { Location: responseUrl }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return handleError(error);
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user