fix fetch data error

This commit is contained in:
wanxp 2024-05-16 01:51:29 +08:00
parent eb25c5be2b
commit 1d74714e6e
9 changed files with 110 additions and 50 deletions

@ -1,7 +1,7 @@
{
"id": "obsidian-douban-plugin",
"name": "Douban",
"version": "2.0.3",
"version": "2.0.4",
"minAppVersion": "0.12.0",
"description": "This is a plugin that can import movies/books/musics/notes/games info data from Douban for Obsidian .",
"author": "Wanxp",

@ -1,6 +1,6 @@
{
"name": "obsidian-douban-plugin",
"version": "2.0.3",
"version": "2.0.4",
"description": "This is a plugin for Obsidian (https://obsidian.md) that can import data from Douban (https://www.douban.com/).",
"main": "main.js",
"scripts": {

@ -34,6 +34,7 @@ import {VariableUtil} from "../../../utils/VariableUtil";
import {DataField} from "../../../utils/model/DataField";
import NumberUtil from "../../../utils/NumberUtil";
import {DoubanHttpUtil} from "../../../utils/DoubanHttpUtil";
import {logger} from "bs-logger";
export default abstract class DoubanAbstractLoadHandler<T extends DoubanSubject> implements DoubanSubjectLoadHandler<T> {
@ -533,6 +534,13 @@ export default abstract class DoubanAbstractLoadHandler<T extends DoubanSubject>
private async handleImage(image: string, folder: string, filename: string, context: HandleContext, showError: boolean, headers?: any) {
if (context.settings.pictureBedFlag) {
//临时限定只支持PicGo
const checked = await context.netFileHandler.downloadDBUploadPicGoByClipboardBefore(context);
if (!checked) {
//TODO 国际化
log.notice('未检测到PicGo软件, 请检查是否已开启PicGo的Server服务或者检查插件中配置地址是否正确现使用默认的下载到本地的方式');
return await context.netFileHandler.downloadDBFile(image, folder, filename, context, false, headers);
}
return await context.netFileHandler.downloadDBUploadPicGoByClipboard(image, filename, context, showError, headers);
}else {
return await context.netFileHandler.downloadDBFile(image, folder, filename, context, false, headers);

@ -2,12 +2,20 @@ import {SupportType} from "../../../../constant/Constsant";
import {SearchResultPageParserInterface} from "./SearchResultPageParserInterface";
import {SearchPage} from "../../model/SearchPage";
import SearchParserHandlerV2 from "../SearchParserV2";
import StringUtil from "../../../../utils/StringUtil";
import {log} from "../../../../utils/Logutil";
export class AllFirstPageSearchResultPageParser implements SearchResultPageParserInterface {
support(type:SupportType, pageNum:number):boolean {
return pageNum == 1 && type == SupportType.ALL;
}
parse(source:string, type:SupportType, pageNum:number, pageSize:number):SearchPage {
if (!source || StringUtil.notJsonString(source)) {
//TODO 国际化
log.notice("Obsidian-Douban:查询结果为空,无匹配结果,请尝试登录获取获取更多数据(已登录则忽略)");
return SearchPage.empty(type);
}
const {subjects} = JSON.parse(source);
if (!subjects) {
return SearchPage.empty(type);

@ -83,6 +83,19 @@ export default class NetFileHandler {
const data = response.textJson as ResultI;
return data;
}
async downloadDBUploadPicGoByClipboardBefore(context: HandleContext) {
//临时限定只支持PicGo
try {
const response = await HttpUtil.httpRequest(
HttpUtil.replaceUrlPath(context.settings.pictureBedSetting.url, 'heartbeat'), {}, context.plugin.settingsManager, {method: "post"});
const data = response.textJson as ResultI;
return data? data.success: false;
}catch (e) {
return false;
}
}
}

@ -77,6 +77,15 @@ export default class HttpUtil {
throw new Error('Invalid URL');
}
public static replaceUrlPath(url: string, newPath: string): string {
const regex = /^(https?:\/\/[^\/]+)(:\d+)?(\/.*)$/;
const matches = url.match(regex);
if (matches && matches.length === 4) {
return matches[1] + (matches[2] || '') + newPath;
}
return url;
}
/**
* url
* @param str

@ -109,6 +109,14 @@ export default class StringUtil {
}
public static notJsonString(str: string) {
try {
JSON.parse(str);
return false;
} catch (error) {
return true;
}
}
}
export const EscapeMap:Map< { [Symbol.replace](string: string, replaceValue: string): string; }, string> = new Map([

@ -48,8 +48,10 @@ export default class DesktopHttpUtil {
'Content-Type': 'application/json'
}
};
let req = null;
try {
const req = DesktopHttpUtil.getHttpClient(url).request(optionsInner, function (response: any) {
req = DesktopHttpUtil.getHttpClient(url).request(optionsInner, function (response: any) {
let chunks: any = [],
size = 0;
if (settingsManager) {
@ -69,6 +71,10 @@ export default class DesktopHttpUtil {
resolve(new HttpResponse(response.statusCode, response.headers, html))
})
});
} catch (e) {
rejects(e);
}
if (req) {
const body = options.body;
if (body) {
req.write(body);
@ -76,7 +82,10 @@ export default class DesktopHttpUtil {
req.write('');
}
req.end();
}
} else {
try {
this.getHttpClient(url).get(url, {...options}, function (response: any) {
let chunks: any = [],
size = 0;
@ -97,9 +106,13 @@ export default class DesktopHttpUtil {
resolve(new HttpResponse(response.statusCode, response.headers, html))
})
});
} catch (e) {
rejects(e);
}
}
}
private static getHttpClient(url?: string) {
if (url && url.startsWith("https")) {
if (!https) {

@ -48,6 +48,7 @@
"2.0.0": "0.12.0",
"2.0.1": "0.12.0",
"2.0.2": "0.12.0",
"2.0.3": "0.12.0"
"2.0.3": "0.12.0",
"2.0.4": "0.12.0"
}