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", "id": "obsidian-douban-plugin",
"name": "Douban", "name": "Douban",
"version": "2.0.3", "version": "2.0.4",
"minAppVersion": "0.12.0", "minAppVersion": "0.12.0",
"description": "This is a plugin that can import movies/books/musics/notes/games info data from Douban for Obsidian .", "description": "This is a plugin that can import movies/books/musics/notes/games info data from Douban for Obsidian .",
"author": "Wanxp", "author": "Wanxp",

@ -1,6 +1,6 @@
{ {
"name": "obsidian-douban-plugin", "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/).", "description": "This is a plugin for Obsidian (https://obsidian.md) that can import data from Douban (https://www.douban.com/).",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {

@ -34,6 +34,7 @@ import {VariableUtil} from "../../../utils/VariableUtil";
import {DataField} from "../../../utils/model/DataField"; import {DataField} from "../../../utils/model/DataField";
import NumberUtil from "../../../utils/NumberUtil"; import NumberUtil from "../../../utils/NumberUtil";
import {DoubanHttpUtil} from "../../../utils/DoubanHttpUtil"; import {DoubanHttpUtil} from "../../../utils/DoubanHttpUtil";
import {logger} from "bs-logger";
export default abstract class DoubanAbstractLoadHandler<T extends DoubanSubject> implements DoubanSubjectLoadHandler<T> { 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) { private async handleImage(image: string, folder: string, filename: string, context: HandleContext, showError: boolean, headers?: any) {
if (context.settings.pictureBedFlag) { 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); return await context.netFileHandler.downloadDBUploadPicGoByClipboard(image, filename, context, showError, headers);
}else { }else {
return await context.netFileHandler.downloadDBFile(image, folder, filename, context, false, headers); 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 {SearchResultPageParserInterface} from "./SearchResultPageParserInterface";
import {SearchPage} from "../../model/SearchPage"; import {SearchPage} from "../../model/SearchPage";
import SearchParserHandlerV2 from "../SearchParserV2"; import SearchParserHandlerV2 from "../SearchParserV2";
import StringUtil from "../../../../utils/StringUtil";
import {log} from "../../../../utils/Logutil";
export class AllFirstPageSearchResultPageParser implements SearchResultPageParserInterface { export class AllFirstPageSearchResultPageParser implements SearchResultPageParserInterface {
support(type:SupportType, pageNum:number):boolean { support(type:SupportType, pageNum:number):boolean {
return pageNum == 1 && type == SupportType.ALL; return pageNum == 1 && type == SupportType.ALL;
} }
parse(source:string, type:SupportType, pageNum:number, pageSize:number):SearchPage { 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); const {subjects} = JSON.parse(source);
if (!subjects) { if (!subjects) {
return SearchPage.empty(type); return SearchPage.empty(type);

@ -83,6 +83,19 @@ export default class NetFileHandler {
const data = response.textJson as ResultI; const data = response.textJson as ResultI;
return data; 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'); 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 * url
* @param str * @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([ export const EscapeMap:Map< { [Symbol.replace](string: string, replaceValue: string): string; }, string> = new Map([

@ -5,8 +5,8 @@ import DoubanHumanCheckModel from "../../douban/component/DoubanHumanCheckModel"
import HttpUtil from "../HttpUtil"; import HttpUtil from "../HttpUtil";
import {HttpResponse} from "../model/HttpResponse"; import {HttpResponse} from "../model/HttpResponse";
var https:any = null; var https: any = null;
var http:any = null; var http: any = null;
export default class DesktopHttpUtil { export default class DesktopHttpUtil {
@ -29,7 +29,7 @@ export default class DesktopHttpUtil {
}) })
} }
private static httpRequest(url: string, options: any, times:number, resolve:any, rejects:any, settingsManager?: SettingsManager) { private static httpRequest(url: string, options: any, times: number, resolve: any, rejects: any, settingsManager?: SettingsManager) {
settingsManager.debug(`Obsidian-Douban:从网络获取json开始:\nurl:${url}\nheaders:${JSON.stringify(options)}`); settingsManager.debug(`Obsidian-Douban:从网络获取json开始:\nurl:${url}\nheaders:${JSON.stringify(options)}`);
var method = options.method; var method = options.method;
@ -48,8 +48,10 @@ export default class DesktopHttpUtil {
'Content-Type': 'application/json' '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 = [], let chunks: any = [],
size = 0; size = 0;
if (settingsManager) { if (settingsManager) {
@ -69,15 +71,22 @@ export default class DesktopHttpUtil {
resolve(new HttpResponse(response.statusCode, response.headers, html)) resolve(new HttpResponse(response.statusCode, response.headers, html))
}) })
}); });
} catch (e) {
rejects(e);
}
if (req) {
const body = options.body; const body = options.body;
if (body) { if (body) {
req.write(body); req.write(body);
}else { } else {
req.write(''); req.write('');
} }
req.end(); req.end();
}else { }
this.getHttpClient(url).get(url, { ...options }, function (response:any) { } else {
try {
this.getHttpClient(url).get(url, {...options}, function (response: any) {
let chunks: any = [], let chunks: any = [],
size = 0; size = 0;
if (settingsManager) { if (settingsManager) {
@ -97,16 +106,20 @@ export default class DesktopHttpUtil {
resolve(new HttpResponse(response.statusCode, response.headers, html)) resolve(new HttpResponse(response.statusCode, response.headers, html))
}) })
}); });
} catch (e) {
rejects(e);
} }
} }
}
private static getHttpClient(url?: string) { private static getHttpClient(url?: string) {
if (url && url.startsWith("https")) { if (url && url.startsWith("https")) {
if (!https) { if (!https) {
https = require("follow-redirects").https; https = require("follow-redirects").https;
} }
return https; return https;
}else { } else {
if (!http) { if (!http) {
http = require("follow-redirects").http; http = require("follow-redirects").http;
} }
@ -131,7 +144,7 @@ export default class DesktopHttpUtil {
}) })
} }
private static httpRequestGetBufferInner(url: string, options: any, times:number, resolve:any, rejects:any, settingsManager?: SettingsManager) { private static httpRequestGetBufferInner(url: string, options: any, times: number, resolve: any, rejects: any, settingsManager?: SettingsManager) {
if (settingsManager) { if (settingsManager) {
settingsManager.debug(`Obsidian-Douban:从网络获取文件开始:\n${url}\nheaders:${JSON.stringify(options)}`); settingsManager.debug(`Obsidian-Douban:从网络获取文件开始:\n${url}\nheaders:${JSON.stringify(options)}`);

@ -48,6 +48,7 @@
"2.0.0": "0.12.0", "2.0.0": "0.12.0",
"2.0.1": "0.12.0", "2.0.1": "0.12.0",
"2.0.2": "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"
} }