2023-05-09 13:56:08 +00:00
|
|
|
|
|
|
|
|
|
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))
|
|
|
|
|
/**
|
|
|
|
|
* @param text 生成图像的描述
|
|
|
|
|
* @param requestId 请求id,如果不是对话生成图片可以为undefined
|
|
|
|
|
* @param countF 回调函数,获取当前是第几次请求。
|
|
|
|
|
* @return [...{img:url,mImg:url}...] img:图片url mIng:缩略图url
|
|
|
|
|
* */
|
2023-05-09 15:00:28 +00:00
|
|
|
|
async function generateImages(text,requestId,countF){
|
2023-05-09 13:56:08 +00:00
|
|
|
|
let theUrls = new URLSearchParams();
|
|
|
|
|
theUrls.append('re', '1');
|
|
|
|
|
theUrls.append('showselective', '1');
|
|
|
|
|
theUrls.append('sude', '1');
|
|
|
|
|
theUrls.append('kseed', '7500');
|
|
|
|
|
theUrls.append('SFX', '2');
|
|
|
|
|
theUrls.append('q', text);
|
|
|
|
|
theUrls.append('iframeid', requestId);
|
2023-05-09 15:00:28 +00:00
|
|
|
|
let theUrl = magicUrl+`images/create?${theUrls.toString()}`;
|
2023-05-26 07:48:51 +00:00
|
|
|
|
let response = await fetch(theUrl,
|
|
|
|
|
{"sec-fetch-site": "same-origin",
|
|
|
|
|
"referer": "https://www.bing.com/search?q=bingAI"});
|
2023-05-09 13:56:08 +00:00
|
|
|
|
let html = (await response.text());
|
2023-05-10 01:02:04 +00:00
|
|
|
|
//let cookieID = response.headers.get('cookieID');
|
2023-05-09 13:56:08 +00:00
|
|
|
|
|
|
|
|
|
//如果返回的是有错误的页面
|
|
|
|
|
let urr = new RegExp('class="gil_err_mt">([^<>]*)</div>').exec(html);
|
|
|
|
|
if(urr && urr[1]){
|
|
|
|
|
let error = `<h3>${urr[1]}</h3>`;
|
|
|
|
|
urr = new RegExp('class="gil_err_sbt">(([^<>]*<(a|div)[^<>]*>[^<>]*</(a|div)>[^<>]*)*)</div>').exec(html);
|
|
|
|
|
if(urr && urr[1]){
|
|
|
|
|
error = error+`<p>${urr[1]}</p>`;
|
|
|
|
|
}
|
|
|
|
|
throw new Error(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//如果没错误就匹配链接获取图片
|
|
|
|
|
urr = new RegExp('"/(images/create/async/results/(\\S*))"').exec(html);
|
|
|
|
|
if(!urr || !urr[1]){
|
|
|
|
|
console.log(html);
|
|
|
|
|
throw new Error("请求图片返回不正确的页面,无法加载图片。");
|
|
|
|
|
}
|
|
|
|
|
let ur = urr[1];
|
|
|
|
|
ur = ur.replaceAll('&','&');
|
2023-05-09 15:00:28 +00:00
|
|
|
|
let imgPageHtmlUrl = magicUrl + `${ur}`;
|
2023-05-26 07:48:51 +00:00
|
|
|
|
let options = {"sec-fetch-site":"same-origin", "referer":"https://www.bing.com/images/create?partner=sydney&showselective=1&sude=1&kseed=7000"};
|
2023-05-09 13:56:08 +00:00
|
|
|
|
for(let count = 1;count<=20;count++){
|
|
|
|
|
if((!!countF)&&(typeof countF =='function')){
|
|
|
|
|
countF(count);
|
|
|
|
|
}
|
|
|
|
|
await sleep(3000);
|
|
|
|
|
let imgPageHtml;
|
|
|
|
|
try{
|
2023-05-26 07:48:51 +00:00
|
|
|
|
imgPageHtml = (await (await fetch(imgPageHtmlUrl,options)).text());
|
2023-05-09 13:56:08 +00:00
|
|
|
|
}catch(e){
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
if(!imgPageHtml){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//用正则找全部图片
|
|
|
|
|
let allSrc = imgPageHtml.matchAll(/<img[^<>]*src="([^"]*)"[^<>]*>/g);
|
|
|
|
|
let imgs = [];
|
|
|
|
|
for(let src;!(src=allSrc.next()).done;){
|
|
|
|
|
imgs[imgs.length] = {
|
|
|
|
|
img:src.value[1].split('?')[0],
|
|
|
|
|
mImg:src.value[1].replaceAll('&','&')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(imgs.length>0){
|
|
|
|
|
return imgs;
|
|
|
|
|
}else{
|
|
|
|
|
throw new Error("服务器未正常返回图片!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|