From 89e3013a548a0e2d4a56af70fb4bc2d6a2d05dcb Mon Sep 17 00:00:00 2001 From: zhxy-CN Date: Wed, 2 Oct 2024 13:19:11 +0800 Subject: [PATCH] AWS Lambda --- LICENSE | 21 ++++++++++++ index.mjs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 LICENSE create mode 100644 index.mjs diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9f49bd0 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..e447e4c --- /dev/null +++ b/index.mjs @@ -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, "'"); +}; + +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))}`; + } + } + } + xml += 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); + } +};