@@ -4,7 +4,9 @@ import FormData from 'form-data';
44import _ from 'lodash-es' ;
55import Axios from '../utils/axiosProxy.mjs' ;
66import CQ from '../utils/CQcode.mjs' ;
7+ import { flareSolverr } from '../utils/flaresolverr.mjs' ;
78import { getCqImg64FromUrl , getAntiShieldedCqImg64FromUrl } from '../utils/image.mjs' ;
9+ import { imgAntiShieldingFromArrayBuffer } from '../utils/imgAntiShielding.mjs' ;
810import logError from '../utils/logError.mjs' ;
911import { retryAsync } from '../utils/retry.mjs' ;
1012import { confuseURL } from '../utils/url.mjs' ;
@@ -21,10 +23,9 @@ async function doSearch(img, snLowAcc = false) {
2123 const hosts = global . config . ascii2dHost ;
2224 let host = hosts [ hostsI ++ % hosts . length ] ;
2325 if ( ! / ^ h t t p s ? : \/ \/ / . test ( host ) ) host = `https://${ host } ` ;
24- const callApi = global . config . bot . ascii2dUsePuppeteer ? callAscii2dUrlApiWithPuppeteer : callAscii2dApi ;
2526 const { colorURL, colorDetail } = await retryAsync (
2627 async ( ) => {
27- const ret = await callApi ( host , img ) ;
28+ const ret = await callAscii2dApi ( host , img ) ;
2829 const colorURL = ret . request . res . responseUrl ;
2930 if ( ! colorURL . includes ( '/color/' ) ) {
3031 const $ = Cheerio . load ( ret . data , { decodeEntities : false } ) ;
@@ -41,9 +42,7 @@ async function doSearch(img, snLowAcc = false) {
4142 e => typeof e !== 'string' && String ( _ . get ( e , 'response.data' ) ) . trim ( ) === 'first byte timeout' ,
4243 ) ;
4344 const bovwURL = colorURL . replace ( '/color/' , '/bovw/' ) ;
44- const bovwDetail = await ( global . config . bot . ascii2dUsePuppeteer ? getAscii2dWithPuppeteer : Axios . get ) ( bovwURL ) . then (
45- r => getDetail ( r , host ) ,
46- ) ;
45+ const bovwDetail = await requestGet ( bovwURL ) . then ( r => getDetail ( r , host ) ) ;
4746 const colorRet = await getResult ( colorDetail , snLowAcc ) ;
4847 const bovwRet = await getResult ( bovwDetail , snLowAcc ) ;
4948 return {
@@ -53,10 +52,26 @@ async function doSearch(img, snLowAcc = false) {
5352 } ;
5453}
5554
55+ function throwDeviceImageError ( ) {
56+ // eslint-disable-next-line no-throw-literal
57+ throw '部分图片无法获取,如为转发请尝试保存后再手动发送,或使用其他设备手动发送' ;
58+ }
59+
5660/**
61+ * @param {string } host
5762 * @param {MsgImage } img
5863 */
5964async function callAscii2dApi ( host , img ) {
65+ if ( global . config . flaresolverr . enableForAscii2d ) {
66+ if ( ! img . isUrlValid ) throwDeviceImageError ( ) ;
67+ return flareSolverr . get ( `${ host } /search/url/${ img . url } ` ) ;
68+ }
69+
70+ if ( global . config . bot . ascii2dUsePuppeteer ) {
71+ if ( ! img . isUrlValid ) throwDeviceImageError ( ) ;
72+ return getAscii2dWithPuppeteer ( `${ host } /search/url/${ img . url } ` ) ;
73+ }
74+
6075 if ( global . config . bot . ascii2dLocalUpload || ! img . isUrlValid ) {
6176 const path = await img . getPath ( ) ;
6277 if ( path ) {
@@ -70,19 +85,17 @@ async function callAscii2dApi(host, img) {
7085 return Axios . get ( `${ host } /search/url/${ img . url } ` ) ;
7186 }
7287
73- // eslint-disable-next-line no-throw-literal
74- throw '部分图片无法获取,如为转发请尝试保存后再手动发送,或使用其他设备手动发送' ;
88+ throwDeviceImageError ( ) ;
7589}
7690
77- /**
78- * @param {MsgImage } img
79- */
80- function callAscii2dUrlApiWithPuppeteer ( host , img ) {
81- if ( ! img . isUrlValid ) {
82- // eslint-disable-next-line no-throw-literal
83- throw '部分图片无法获取,如为转发请尝试保存后再手动发送,或使用其他设备手动发送' ;
91+ function requestGet ( url ) {
92+ if ( global . config . flaresolverr . enableForAscii2d ) {
93+ return flareSolverr . get ( url ) ;
8494 }
85- return getAscii2dWithPuppeteer ( `${ host } /search/url/${ img . url } ` ) ;
95+ if ( global . config . bot . ascii2dUsePuppeteer ) {
96+ return getAscii2dWithPuppeteer ( url ) ;
97+ }
98+ return Axios . get ( url ) ;
8699}
87100
88101async function getAscii2dWithPuppeteer ( url ) {
@@ -140,8 +153,12 @@ async function getResult({ url, title, author, thumbnail, author_url }, snLowAcc
140153 const texts = [ CQ . escape ( author ? `「${ title } 」/「${ author } 」` : title ) ] ;
141154 if ( thumbnail && ! ( global . config . bot . hideImg || ( snLowAcc && global . config . bot . hideImgWhenLowAcc ) ) ) {
142155 const mode = global . config . bot . antiShielding ;
143- if ( mode > 0 ) texts . push ( await getAntiShieldedCqImg64FromUrl ( thumbnail , mode ) ) ;
144- else texts . push ( await getCqImg64FromUrl ( thumbnail ) ) ;
156+ if ( global . config . flaresolverr . enableForAscii2d ) {
157+ const img = await flareSolverr . getImage ( thumbnail ) ;
158+ texts . push ( CQ . img64 ( mode > 0 ? await imgAntiShieldingFromArrayBuffer ( img , mode ) : img ) ) ;
159+ } else {
160+ texts . push ( mode > 0 ? await getAntiShieldedCqImg64FromUrl ( thumbnail , mode ) : await getCqImg64FromUrl ( thumbnail ) ) ;
161+ }
145162 }
146163 if ( url ) texts . push ( CQ . escape ( confuseURL ( url ) ) ) ;
147164 if ( author_url ) texts . push ( `Author: ${ CQ . escape ( confuseURL ( author_url ) ) } ` ) ;
0 commit comments