mirror of
https://github.com/Wanxp/obsidian-douban.git
synced 2026-04-06 10:08:42 +08:00
add douban movie handler and settings
This commit is contained in:
parent
e653ffe4a9
commit
9b55b3b88e
@ -1,9 +1,11 @@
|
||||
import { type } from "os";
|
||||
|
||||
interface DoubanPluginSettings {
|
||||
template:string,
|
||||
movieTemplate:string,
|
||||
dateFormat:string,
|
||||
searchUrl:string,
|
||||
searchHeaders?:string
|
||||
arraySpilt:string,
|
||||
searchHeaders?:string,
|
||||
}
|
||||
|
||||
|
||||
@ -14,16 +16,31 @@ export const doubanHeadrs = {
|
||||
};
|
||||
|
||||
export const DEFAULT_SETTINGS:DoubanPluginSettings = {
|
||||
template:
|
||||
movieTemplate:
|
||||
"---\n" +
|
||||
"title: {{title}}" +
|
||||
"cast: {{cast}}" +
|
||||
"doubanId: {{id}}\n" +
|
||||
"title: {{title}}\n" +
|
||||
"type: {{type}}\n" +
|
||||
"score: {{score}}\n" +
|
||||
"---",
|
||||
"datePublished: {{datePublished}}\n" +
|
||||
"director: {{director}}\n" +
|
||||
"actor: {{actor}}\n" +
|
||||
"author: {{author}}\n" +
|
||||
"url: {{url}}\n" +
|
||||
"image: {{image}}\n" +
|
||||
"---\n",
|
||||
searchUrl: 'https://www.douban.com/search?q=',
|
||||
searchHeaders: JSON.stringify(doubanHeadrs)
|
||||
searchHeaders: JSON.stringify(doubanHeadrs),
|
||||
dateFormat: "yyyy_MM_DD",
|
||||
arraySpilt: ", "
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
export class DefaultSettingsContent {
|
||||
|
||||
}
|
||||
|
||||
export type {DoubanPluginSettings}
|
||||
@ -1,6 +1,9 @@
|
||||
import DoubanPlugin from "main";
|
||||
import { App, PluginSettingTab, Setting } from "obsidian";
|
||||
|
||||
import { DEFAULT_SETTINGS } from "./Douban";
|
||||
import DoubanPlugin from "main";
|
||||
import { i18nHelper } from "lang/helper";
|
||||
|
||||
export class DoubanSettingTab extends PluginSettingTab {
|
||||
plugin: DoubanPlugin;
|
||||
|
||||
@ -14,46 +17,143 @@ export class DoubanSettingTab extends PluginSettingTab {
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
containerEl.createEl("h2", { text: "Obsidian Wikipedia" });
|
||||
containerEl.createEl("h2", { text: "Obsidian Douban" });
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Douban Search Url")
|
||||
.setDesc(`full search url with https ahead `)
|
||||
.addText((textField) => {
|
||||
new Setting(containerEl).setName(i18nHelper.getMessage('douban search url'))
|
||||
.then((setting) => {
|
||||
setting.addText((textField) => {
|
||||
setting.descEl.appendChild(
|
||||
createFragment((frag) => {
|
||||
frag.appendText(i18nHelper.getMessage('douban search url desc 1'));
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('douban search url desc 2'));
|
||||
frag.createEl(
|
||||
'a',
|
||||
{
|
||||
text: i18nHelper.getMessage('Douban'),
|
||||
href: 'https://www.douban.com',
|
||||
},
|
||||
(a) => {
|
||||
a.setAttr('target', '_blank');
|
||||
}
|
||||
);
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('douban search url desc 3'));
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('douban search url desc 4'));
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('douban search url desc 5'));
|
||||
frag.createEl('br');
|
||||
})
|
||||
);
|
||||
textField.inputEl.addClass("settings_textField");
|
||||
textField
|
||||
.setPlaceholder(DEFAULT_SETTINGS.searchUrl)
|
||||
.setValue(this.plugin.settings.searchUrl)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.searchUrl = value;
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Douban Request Headers")
|
||||
.setDesc(`full search url with https ahead `)
|
||||
.addText((textField) => {
|
||||
textField
|
||||
.setValue(this.plugin.settings.searchHeaders)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.searchHeaders = value;
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Content Template")
|
||||
.setDesc(
|
||||
`Set markdown template for extract to be inserted.\n
|
||||
Available template variables are {{id}}, {{type}}, {{title}}, {{score}}, {{cast}}, {{desc}} and {{url}}.
|
||||
`
|
||||
)
|
||||
.addTextArea((textarea) =>
|
||||
textarea
|
||||
.setValue(this.plugin.settings.template)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.template = value;
|
||||
await this.plugin.saveSettings();
|
||||
new Setting(containerEl).setName(i18nHelper.getMessage("movie content template")).then((setting) => {
|
||||
setting.addTextArea((textarea) => {
|
||||
setting.descEl.appendChild(
|
||||
createFragment((frag) => {
|
||||
frag.appendText(i18nHelper.getMessage('movie content template desc 1'));
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('movie content template desc 2'));
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('movie content template desc 3'));
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('movie content template desc 4'));
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('movie content template desc 5'));
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('movie content template desc 6'));
|
||||
frag.createEl('br');
|
||||
})
|
||||
);
|
||||
textarea.inputEl.addClass("settings_area");
|
||||
textarea.inputEl.setAttr("rows", 10);
|
||||
textarea.setPlaceholder(DEFAULT_SETTINGS.movieTemplate)
|
||||
.setValue(this.plugin.settings.movieTemplate)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.movieTemplate = value;
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
new Setting(containerEl).setName(i18nHelper.getMessage('Date format')).then((setting) => {
|
||||
setting.addMomentFormat((mf) => {
|
||||
setting.descEl.appendChild(
|
||||
createFragment((frag) => {
|
||||
frag.appendText(
|
||||
i18nHelper.getMessage('This format will be used when available template variables contain date.')
|
||||
);
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('For more syntax, refer to') + ' ');
|
||||
frag.createEl(
|
||||
'a',
|
||||
{
|
||||
text: i18nHelper.getMessage('format reference'),
|
||||
href: 'https://momentjs.com/docs/#/displaying/format/',
|
||||
},
|
||||
(a) => {
|
||||
a.setAttr('target', '_blank');
|
||||
}
|
||||
);
|
||||
frag.createEl('br');
|
||||
frag.appendText(i18nHelper.getMessage('Your current syntax looks like this') + ': ');
|
||||
mf.setSampleEl(frag.createEl('b', { cls: 'u-pop' }));
|
||||
frag.createEl('br');
|
||||
})
|
||||
);
|
||||
mf.setPlaceholder(DEFAULT_SETTINGS.dateFormat);
|
||||
mf.setValue(this.plugin.settings.dateFormat)
|
||||
mf.onChange(async (value) => {
|
||||
this.plugin.settings.dateFormat = value;
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nHelper.getMessage("Array Spilt String"))
|
||||
.setDesc(i18nHelper.getMessage(`string to join between array type, such as author, actor`))
|
||||
.addText((textField) => {
|
||||
textField.setPlaceholder(DEFAULT_SETTINGS.arraySpilt)
|
||||
.setValue(this.plugin.settings.arraySpilt)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.arraySpilt = value;
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
// new Setting(containerEl)
|
||||
// .setName("Douban Request Headers")
|
||||
// .setDesc(`if can not fetch data from douban,\n
|
||||
// please go to douban.com\n and copy headers to this text area `)
|
||||
// .addTextArea((textField) => {
|
||||
// textField
|
||||
// .setPlaceholder(DEFAULT_SETTINGS.searchHeaders)
|
||||
// .setValue(this.plugin.settings.searchHeaders)
|
||||
// .onChange(async (value) => {
|
||||
// this.plugin.settings.searchHeaders = value;
|
||||
// await this.plugin.saveSettings();
|
||||
// });
|
||||
// });
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,39 +1,44 @@
|
||||
import { DoubanPluginSettings } from "douban/Douban";
|
||||
import DoubanSubject from "douban/model/DoubanSubject";
|
||||
import cheerio, { CheerioAPI } from "cheerio";
|
||||
import { get, readStream } from "tiny-network";
|
||||
import { log } from "utils/logutil";
|
||||
import DoubanSubjectLoadHandler from "./DoubanSubjectLoadHandler";
|
||||
|
||||
import DoubanPlugin from "main";
|
||||
import { DoubanPluginSettings } from "douban/Douban";
|
||||
import DoubanSubject from "douban/model/DoubanSubject";
|
||||
import DoubanSubjectLoadHandler from "./DoubanSubjectLoadHandler";
|
||||
import { Editor } from "obsidian";
|
||||
import HttpUtil from "utils/HttpUtil";
|
||||
import { log } from "utils/logutil";
|
||||
|
||||
export default abstract class DoubanAbstractLoadHandler<T extends DoubanSubject> implements DoubanSubjectLoadHandler<T> {
|
||||
|
||||
|
||||
doubanPlugin:DoubanPlugin;
|
||||
public doubanPlugin:DoubanPlugin;
|
||||
|
||||
constructor(doubanPlugin:DoubanPlugin) {
|
||||
this.doubanPlugin = doubanPlugin;
|
||||
}
|
||||
|
||||
handle(url:string):void {
|
||||
Promise
|
||||
.resolve()
|
||||
.then(() => get(url, JSON.parse(this.doubanPlugin.settings.searchHeaders)))
|
||||
abstract parseText(template: string, arraySpilt:string, extract: T): string;
|
||||
|
||||
abstract support(extract: DoubanSubject): boolean;
|
||||
|
||||
handle(url:string, editor:Editor):void {
|
||||
Promise.resolve().then(() => get(url + "/", {headers: JSON.parse(this.doubanPlugin.settings.searchHeaders)}))
|
||||
.then(readStream)
|
||||
.then(log.info)
|
||||
.then(cheerio.load)
|
||||
.then(this.parseSubjectFromHtml);
|
||||
.then(this.parseSubjectFromHtml)
|
||||
.then(content => this.toEditor(editor, content))
|
||||
// .then(content => content ? editor.replaceSelection(content) : content)
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
|
||||
abstract parseSubjectFromHtml(data:CheerioAPI):T | undefined;
|
||||
|
||||
abstract getType(): string | undefined;
|
||||
|
||||
|
||||
|
||||
support(extract: DoubanSubject): boolean {
|
||||
return extract && (this.getType() == extract.type);
|
||||
toEditor(editor:Editor, extract: T):T {
|
||||
this.doubanPlugin.putToEditor(editor, extract);
|
||||
return extract;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,30 +1,63 @@
|
||||
import DoubanPlugin from "main";
|
||||
import { App } from "obsidian";
|
||||
import { App, Editor } from "obsidian";
|
||||
|
||||
import DoubanMovieLoadHandler from "./DoubanMovieLoadHandler";
|
||||
import DoubanOtherLoadHandler from "./DoubanOtherLoadHandler";
|
||||
import DoubanSubjectLoadHandler from "./DoubanSubjectLoadHandler";
|
||||
import DoubanPlugin from "main";
|
||||
import { DoubanPluginSettings } from "douban/Douban";
|
||||
import DoubanSubject from "../model/DoubanSubject";
|
||||
import DoubanSubjectLoadHandler from "./DoubanSubjectLoadHandler";
|
||||
|
||||
export class DoubanEtractHandler {
|
||||
|
||||
private _app:App;
|
||||
private _doubanPlugin:DoubanPlugin;
|
||||
private _doubanSubjectHandlers:DoubanSubjectLoadHandler<DoubanSubject>[];
|
||||
private _doubanSubjectHandlerDefault:DoubanSubjectLoadHandler<DoubanSubject>;
|
||||
|
||||
|
||||
|
||||
constructor(app:App, doubanPlugin:DoubanPlugin) {
|
||||
this._app = app;
|
||||
this._doubanPlugin = doubanPlugin;
|
||||
this._doubanSubjectHandlers = [new DoubanMovieLoadHandler(this._doubanPlugin),
|
||||
new DoubanOtherLoadHandler(this._doubanPlugin)];
|
||||
this._doubanSubjectHandlerDefault = new DoubanOtherLoadHandler(doubanPlugin);
|
||||
this._doubanSubjectHandlers = [new DoubanMovieLoadHandler(doubanPlugin),
|
||||
this._doubanSubjectHandlerDefault];
|
||||
|
||||
}
|
||||
|
||||
public handle(searchExtract:DoubanSubject):void{
|
||||
public handle(searchExtract:DoubanSubject, editor: Editor):void{
|
||||
if(!searchExtract) {
|
||||
return;
|
||||
}
|
||||
this._doubanSubjectHandlers
|
||||
.filter(h => h.support)
|
||||
.forEach(h => h.handle(searchExtract.url));
|
||||
var doubanSubjectHandlers:DoubanSubjectLoadHandler<DoubanSubject>[] = this._doubanSubjectHandlers
|
||||
.filter(h => h.support);
|
||||
if(doubanSubjectHandlers && doubanSubjectHandlers.length > 0) {
|
||||
var result = doubanSubjectHandlers.map(h => h.handle(searchExtract.url, editor))
|
||||
if(result && result.length > 0) {
|
||||
return result[0];
|
||||
}
|
||||
}else {
|
||||
this._doubanSubjectHandlerDefault.handle(searchExtract.url, editor);
|
||||
}
|
||||
}
|
||||
|
||||
public parseText(template: string, arraySpilt:string, extract:DoubanSubject):string {
|
||||
if(!template) {
|
||||
return "";
|
||||
}
|
||||
var doubanSubjectHandlers:DoubanSubjectLoadHandler<DoubanSubject>[] = this._doubanSubjectHandlers
|
||||
.filter(h => h.support);
|
||||
if(doubanSubjectHandlers && doubanSubjectHandlers.length > 0) {
|
||||
var result = doubanSubjectHandlers.map(h => h.parseText(template, arraySpilt, extract));
|
||||
if(result && result.length > 0) {
|
||||
return result[0];
|
||||
}else {
|
||||
return "";
|
||||
}
|
||||
}else {
|
||||
return this._doubanSubjectHandlerDefault.parseText(template, arraySpilt, extract);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,51 +1,68 @@
|
||||
import DoubanMovieSubject from "douban/model/DoubanMoveSubject";
|
||||
import { get, readStream } from "tiny-network";
|
||||
import { log } from "utils/logutil";
|
||||
import DoubanAbstractLoadHandler from "./DoubanAbstractLoadHandler";
|
||||
import { Editor, renderResults } from "obsidian";
|
||||
import cheerio, { CheerioAPI } from 'cheerio';
|
||||
import { DoubanPluginSettings } from "douban/Douban";
|
||||
import DoubanPlugin from "main";
|
||||
import { get, readStream } from "tiny-network";
|
||||
|
||||
import DoubanAbstractLoadHandler from "./DoubanAbstractLoadHandler";
|
||||
import DoubanMovieSubject from "douban/model/DoubanMovieSubject";
|
||||
import DoubanPlugin from "main";
|
||||
import { DoubanPluginSettings } from "douban/Douban";
|
||||
import DoubanSubject from "douban/model/DoubanSubject";
|
||||
import { log } from "utils/logutil";
|
||||
|
||||
export default class DoubanMovieLoadHandler extends DoubanAbstractLoadHandler<DoubanMovieSubject> {
|
||||
|
||||
parseText(template: string, arraySpilt:string, extract: DoubanMovieSubject): string {
|
||||
return template ? template.replace("{{id}}", extract.id)
|
||||
.replace("{{type}}", extract.type ? extract.type : "")
|
||||
.replace("{{title}}", extract.title ? extract.title : "")
|
||||
.replace("{{desc}}", extract.desc ? extract.desc : "")
|
||||
.replace("{{image}}", extract.image ? extract.image : "")
|
||||
.replace("{{director}}", extract.director ? extract.director.join(arraySpilt) : "")
|
||||
.replace("{{actor}}", extract.actor ? extract.actor.join(arraySpilt) : "")
|
||||
.replace("{{author}}", extract.author ? extract.author.join(arraySpilt) : "")
|
||||
.replace("{{datePublished}}", extract.datePublished ? extract.datePublished : "")
|
||||
.replace("{{url}}", extract.url ? extract.url : "")
|
||||
.replace("{{score}}", extract.aggregateRating && extract.aggregateRating.ratingValue ? extract.aggregateRating.ratingValue + "" : "")
|
||||
: undefined; }
|
||||
support(extract: DoubanSubject): boolean {
|
||||
return extract && ('[电影]'==extract.type || 'Movie' == extract.type);
|
||||
}
|
||||
|
||||
|
||||
|
||||
constructor(doubanPlugin:DoubanPlugin) {
|
||||
super(doubanPlugin);
|
||||
}
|
||||
|
||||
parseSubjectFromHtml(data: CheerioAPI): DoubanMovieSubject {
|
||||
return data('.result')
|
||||
return data('script')
|
||||
.get()
|
||||
.map((i:any) => {
|
||||
const item = data(i);
|
||||
.filter(scd => "application/ld+json" == data(scd).attr("type"))
|
||||
.map(i => {
|
||||
var item = data(i).text();
|
||||
var obj = JSON.parse(item);
|
||||
var idPattern = /(\d){5,10}/g;
|
||||
var urlPattern = /(https%3A%2F%2F)\S+(\d){5,10}/g;
|
||||
var linkValue = item.find("div.content > div > h3 > a").text();
|
||||
var ececResult = idPattern.exec(linkValue);
|
||||
var urlResult = urlPattern.exec(linkValue);
|
||||
var cast = item.find(".subject-cast").text();
|
||||
var id = idPattern.exec(obj.url);
|
||||
log.info(item);
|
||||
const result:DoubanMovieSubject = {
|
||||
id: ececResult?ececResult[0]:'',
|
||||
title: item.find("div.content > div > h3 > a").text(),
|
||||
score: item.find(".rating_nums").text(),
|
||||
// duration: item.attr('data-duration'),
|
||||
// region: item.attr('data-region'),
|
||||
// director: item.attr('data-director'),
|
||||
// actors: item.attr('data-actors'),
|
||||
// poster: item.find('.poster img').attr('src'),
|
||||
cast: cast,
|
||||
type: item.find("div.content > div > h3 > span").text(),
|
||||
desc: item.find("div.content > p").text(),
|
||||
url: urlResult?decodeURIComponent(urlResult[0]):'https://www.douban.com',
|
||||
};
|
||||
id: id?id[0]:'',
|
||||
type: 'Movie',
|
||||
title: obj.name,
|
||||
desc: obj.description,
|
||||
url: "https://movie.douban.com" + obj.url,
|
||||
director: obj.director,
|
||||
author: obj.author,
|
||||
actor: obj.actor,
|
||||
aggregateRating: obj.aggregateRating,
|
||||
datePublished:obj.datePublished,
|
||||
image:obj.image
|
||||
}
|
||||
log.info(result);
|
||||
return result;
|
||||
})[0];
|
||||
}
|
||||
|
||||
getType(): string |undefined {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,14 +1,26 @@
|
||||
import { CheerioAPI } from "cheerio";
|
||||
import DoubanSubject from "douban/model/DoubanSubject";
|
||||
import DoubanAbstractLoadHandler from "./DoubanAbstractLoadHandler";
|
||||
import { Editor, Notice } from "obsidian";
|
||||
|
||||
import { CheerioAPI } from "cheerio";
|
||||
import DoubanAbstractLoadHandler from "./DoubanAbstractLoadHandler";
|
||||
import DoubanSubject from "douban/model/DoubanSubject";
|
||||
import { i18nHelper } from "lang/helper";
|
||||
import { log } from "utils/logutil";
|
||||
|
||||
/**
|
||||
* 默认的处理器
|
||||
*/
|
||||
export default class DoubanOtherLoadHandler extends DoubanAbstractLoadHandler<DoubanSubject> {
|
||||
parseSubjectFromHtml(data: CheerioAPI): DoubanSubject | undefined{
|
||||
parseText(template: string, arraySpilt: string, extract: DoubanSubject): string {
|
||||
log.warn(i18nHelper.getMessage('current version not support type'));
|
||||
return "";
|
||||
}
|
||||
support(extract: DoubanSubject): boolean {
|
||||
return false;
|
||||
}
|
||||
parseSubjectFromHtml(data: CheerioAPI): DoubanSubject {
|
||||
return undefined;
|
||||
}
|
||||
getType(): string | undefined{
|
||||
return undefined
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
import DoubanSubject from "douban/model/DoubanSubject";
|
||||
import { Editor } from "obsidian";
|
||||
|
||||
export default interface DoubanSubjectLoadHandler<T extends DoubanSubject> {
|
||||
|
||||
getType():string | undefined;
|
||||
parseText(template: string, arraySpilt:string, extract: DoubanSubject): string;
|
||||
|
||||
support(extract:DoubanSubject):boolean;
|
||||
|
||||
handle(url:string):void;
|
||||
handle(url:string, editor: Editor):void;
|
||||
|
||||
|
||||
}
|
||||
@ -1,6 +0,0 @@
|
||||
import DoubanSubject from "./DoubanSubject";
|
||||
|
||||
export default class DoubanMovieSubject extends DoubanSubject {
|
||||
cast:string;
|
||||
score:string;
|
||||
}
|
||||
12
douban/model/DoubanMovieSubject.ts
Normal file
12
douban/model/DoubanMovieSubject.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import DoubanSubject from "./DoubanSubject";
|
||||
import {AggregateRating, Person, WithContext} from 'schema-dts';
|
||||
|
||||
|
||||
export default class DoubanMovieSubject extends DoubanSubject {
|
||||
director:Person[];
|
||||
author:Person[];
|
||||
actor:Person[];
|
||||
aggregateRating:AggregateRating;
|
||||
datePublished:string;
|
||||
image:string
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
import DoubanSearchResultSubject from "douban/model/DoubanSearchResultSubject";
|
||||
import DoubanPlugin from "main";
|
||||
import { FuzzySuggestModal,App } from "obsidian";
|
||||
import { FuzzySuggestModal,App, Editor } from "obsidian";
|
||||
import { log } from "utils/logutil";
|
||||
|
||||
|
||||
@ -9,13 +9,13 @@ export {DoubanFuzzySuggester}
|
||||
|
||||
class DoubanFuzzySuggester extends FuzzySuggestModal<DoubanSearchResultSubject> {
|
||||
|
||||
public app: App;
|
||||
public editor: Editor;
|
||||
private plugin: DoubanPlugin;
|
||||
private doubanSearchResultExtract:DoubanSearchResultSubject[]
|
||||
|
||||
constructor(app: App, plugin: DoubanPlugin) {
|
||||
constructor(plugin: DoubanPlugin, editor: Editor) {
|
||||
super(app);
|
||||
this.app = app;
|
||||
this.editor = editor;
|
||||
this.plugin = plugin;
|
||||
this.setPlaceholder("Choose an item...");
|
||||
|
||||
@ -28,12 +28,7 @@ class DoubanFuzzySuggester extends FuzzySuggestModal<DoubanSearchResultSubject>
|
||||
|
||||
}
|
||||
|
||||
async reloadSearch() {
|
||||
if(this.inputEl.value) {
|
||||
log.info("reload search")
|
||||
this.doubanSearchResultExtract = await this.plugin.getDoubanSearchList(this.inputEl.value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
getItems(): DoubanSearchResultSubject[] {
|
||||
return this.doubanSearchResultExtract;
|
||||
@ -45,12 +40,12 @@ class DoubanFuzzySuggester extends FuzzySuggestModal<DoubanSearchResultSubject>
|
||||
}
|
||||
|
||||
onChooseItem(item: DoubanSearchResultSubject, evt: MouseEvent | KeyboardEvent): void {
|
||||
this.plugin.doubanEtractHandler.handle(item);
|
||||
log.warn(`choose itme ${JSON.stringify(item)}`);
|
||||
this.plugin.doubanEtractHandler.handle(item, this.editor);
|
||||
}
|
||||
|
||||
public showSearchList(doubanSearchResultExtractList:DoubanSearchResultSubject[]) {
|
||||
this.doubanSearchResultExtract = doubanSearchResultExtractList;
|
||||
log.info("show search result" );
|
||||
this.start();
|
||||
}
|
||||
|
||||
|
||||
@ -44,13 +44,11 @@ export class DoubanSearchModal extends Modal {
|
||||
cancelButton.addEventListener("click", this.close.bind(this));
|
||||
}
|
||||
async search() {
|
||||
log.info("start search :" + this.searchTerm);
|
||||
let { contentEl } = this;
|
||||
contentEl.empty();
|
||||
if (this.searchTerm) {
|
||||
this.close();
|
||||
await this.plugin.search(this.searchTerm);
|
||||
// await this.plugin.pasteIntoEditor(this.editor, null);
|
||||
await this.plugin.search(this.searchTerm, this.editor);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ export default class Searcher {
|
||||
.then(() => get(doubanSettings.searchUrl + searchItem, JSON.parse(doubanSettings.searchHeaders)))
|
||||
.then(ensureStatusCode(200))
|
||||
.then(readStream)
|
||||
.then(log.info)
|
||||
.then(cheerio.load)
|
||||
.then(SearchParserHandler.parseSearch);
|
||||
};
|
||||
|
||||
@ -9,7 +9,7 @@ export default class SearchParserHandler {
|
||||
const item = dataHtml(i);
|
||||
var idPattern = /(\d){5,10}/g;
|
||||
var urlPattern = /(https%3A%2F%2F)\S+(\d){5,10}/g;
|
||||
var linkValue = item.find("div.content > div > h3 > a").text();
|
||||
var linkValue = item.find("div.content > div > h3 > a").attr("href");
|
||||
var ececResult = idPattern.exec(linkValue);
|
||||
var urlResult = urlPattern.exec(linkValue);
|
||||
var cast = item.find(".subject-cast").text();
|
||||
@ -17,11 +17,6 @@ export default class SearchParserHandler {
|
||||
id: ececResult?ececResult[0]:'',
|
||||
title: item.find("div.content > div > h3 > a").text(),
|
||||
score: item.find(".rating_nums").text(),
|
||||
// duration: item.attr('data-duration'),
|
||||
// region: item.attr('data-region'),
|
||||
// director: item.attr('data-director'),
|
||||
// actors: item.attr('data-actors'),
|
||||
// poster: item.find('.poster img').attr('src'),
|
||||
cast: cast,
|
||||
type: item.find("div.content > div > h3 > span").text(),
|
||||
desc: item.find("div.content > p").text(),
|
||||
|
||||
23
lang/helper.ts
Normal file
23
lang/helper.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import en from './locale/en';
|
||||
import zhCN from './locale/zh-cn';
|
||||
|
||||
const localeMap: { [k: string]: Partial<typeof en> } = {
|
||||
en,
|
||||
zh: zhCN,
|
||||
};
|
||||
|
||||
const lang = window.localStorage.getItem('language');
|
||||
const locale = localeMap[lang || 'en'];
|
||||
|
||||
|
||||
export default class I18nHelper {
|
||||
public getMessage(str: keyof typeof en): string {
|
||||
if (!locale) {
|
||||
console.error('Error: obsidian douban locale not found', lang);
|
||||
}
|
||||
|
||||
return (locale && locale[str]) || en[str];
|
||||
}
|
||||
}
|
||||
|
||||
export const i18nHelper:I18nHelper = new I18nHelper();
|
||||
34
lang/locale/en.ts
Normal file
34
lang/locale/en.ts
Normal file
@ -0,0 +1,34 @@
|
||||
//简体中文
|
||||
export default {
|
||||
//main.ts
|
||||
'search douban by current file name':'search douban by current file name',
|
||||
'search douban and input current file':'search douban and input current file',
|
||||
//DoubanSettingTab
|
||||
'douban search url': `Douban Search Url`,
|
||||
'douban search url desc 1': `Douban search page request address. `,
|
||||
'douban search url desc 2': `First go to:`,
|
||||
'douban search url desc 3': `Don't enter anything in the search input box, just click Search,`,
|
||||
'douban search url desc 4': `The redirected web page address is the search address,`,
|
||||
'douban search url desc 5': `Just copy the web address to the current input box.`,
|
||||
|
||||
'movie content template': `Movie Content Template`,
|
||||
'movie content template desc 1': `Set markdown template for extract to be inserted.`,
|
||||
'movie content template desc 2': `Available template variables are :`,
|
||||
'movie content template desc 3': `{{id}}, {{type}}, {{title}},`,
|
||||
'movie content template desc 4': `{{score}}, {{datePublished}}, {{director}},`,
|
||||
'movie content template desc 5': `{{author}}, {{autor}}, {{desc}},`,
|
||||
'movie content template desc 6': `{{image}}, {{url}}`,
|
||||
|
||||
'Date format': `Date Format`,
|
||||
'This format will be used when available template variables contain date.':
|
||||
`This format will be used when available template variables contain date.`,
|
||||
'For more syntax, refer to': `For more syntax, refer to`,
|
||||
'Your current syntax looks like this':`Your current syntax looks like this`,
|
||||
'format reference': `format reference`,
|
||||
'Array Spilt String':`Array Spilt String`,
|
||||
'string to join between array type, such as author, actor':`string to join between array type, such as author, actor`,
|
||||
'Douban Request Headers':`Douban Request Headers`,
|
||||
'current version not support type': `This type of import is not supported temporarily, please go to github to submit issues for help`,
|
||||
'Douban': `Douban`,
|
||||
|
||||
}
|
||||
37
lang/locale/zh-cn.ts
Normal file
37
lang/locale/zh-cn.ts
Normal file
@ -0,0 +1,37 @@
|
||||
//简体中文
|
||||
export default {
|
||||
//main.ts
|
||||
'search in douban by current file name':'用当前文档名搜索豆瓣并写入当前文档',
|
||||
'search douban and input current file':'在豆瓣搜索并写入到当前文档',
|
||||
//DoubanSettingTab
|
||||
'douban search url': `豆瓣搜索地址`,
|
||||
'douban search url desc 1': `豆瓣搜索页面请求地址, 通常是网页搜索的地址. `,
|
||||
'douban search url desc 2': `先访问:`,
|
||||
'douban search url desc 3': `然后在搜索输入框不输入任何内容,直接点击搜索,`,
|
||||
'douban search url desc 4': `所跳转的网页地址即是搜索地址,`,
|
||||
'douban search url desc 5': `将网页地址复制到当前输入框即可,`,
|
||||
|
||||
'movie content template': `电影文本模板`,
|
||||
'movie content template desc 1': `设置选择电影后导入的文本内容模板,`,
|
||||
'movie content template desc 2': `支持以下参数名称 :`,
|
||||
'movie content template desc 3': `{{id}}, {{type}}, {{title}},`,
|
||||
'movie content template desc 4': `{{score}}, {{datePublished}}, {{director}},`,
|
||||
'movie content template desc 5': `{{author}}, {{autor}}, {{desc}},`,
|
||||
'movie content template desc 6': `{{image}}, {{url}}`,
|
||||
|
||||
'Date format': `参数日期格式`,
|
||||
'This format will be used when available template variables contain date.':
|
||||
`这个格式是给上面获取到的参数进行格式化时显示的内容 .`,
|
||||
'For more syntax, refer to': `详细介绍请参考`,
|
||||
'Your current syntax looks like this':`时间参数时间格式预览`,
|
||||
'format reference': `格式参考`,
|
||||
'Array Spilt String':`数组分割字符串`,
|
||||
'string to join between array type, such as author, actor':`当模板中的变量存在数组, 则需要设定数组元素中的分割符号,比如 演员列表等`,
|
||||
'Douban Request Headers':`豆瓣HTTP请求头`,
|
||||
'Douban Request Headers Desc': `如果豆瓣搜索或者获取数据失败,请尝试修改这个参数,\n
|
||||
参数获取方式为:\n
|
||||
1. 访问http://www.douban.com
|
||||
2. 复制请求头,仅复制以下请求头 `,
|
||||
'current version not support type': `暂时不支持该类型导入,请至github提交issuess获取帮助`,
|
||||
'Douban': `豆瓣网`
|
||||
}
|
||||
70
main.ts
70
main.ts
@ -1,59 +1,31 @@
|
||||
import { DEFAULT_SETTINGS, DoubanPluginSettings } from "./douban/Douban";
|
||||
import { Editor, Plugin } from "obsidian";
|
||||
|
||||
import { DoubanEtractHandler } from "douban/handler/DoubanExtractHandler";
|
||||
import { DoubanFuzzySuggester } from "douban/search/DoubanSearchFuzzySuggestModal";
|
||||
import DoubanMovieSubject from "douban/model/DoubanMovieSubject";
|
||||
import { DoubanSearchModal } from "douban/search/DoubanSearchModal";
|
||||
import { DoubanSettingTab } from "douban/DoubanSettingTab";
|
||||
import { DoubanFuzzySuggester } from "douban/search/DoubanSearchFuzzySuggestModal";
|
||||
import { Editor, Notice, Plugin} from "obsidian";
|
||||
import { log } from "utils/logutil";
|
||||
import { DEFAULT_SETTINGS, DoubanPluginSettings } from "./douban/Douban";
|
||||
import DoubanSubject from "douban/model/DoubanSubject";
|
||||
import DoubanSearchResultSubject from "douban/model/DoubanSearchResultSubject";
|
||||
import Searcher from "douban/search/Search";
|
||||
import { i18nHelper } from './lang/helper';
|
||||
|
||||
export default class DoubanPlugin extends Plugin {
|
||||
public settings: DoubanPluginSettings;
|
||||
public fuzzySuggester: DoubanFuzzySuggester;
|
||||
public doubanEtractHandler: DoubanEtractHandler;
|
||||
|
||||
|
||||
formatExtractText(extract: DoubanSubject): string {
|
||||
return this.settings.template ?
|
||||
this.settings.template.replace("{{id}}", extract.id)
|
||||
.replace("{{type}}", extract.type)
|
||||
.replace("{{title}}", extract.title)
|
||||
.replace("{{desc}}", extract.desc)
|
||||
.replace("{{url}}", extract.url) : "";
|
||||
async putToEditor(editor:Editor, extract:DoubanSubject) {
|
||||
var content:string = this.doubanEtractHandler.parseText(this.settings.movieTemplate,
|
||||
this.settings.arraySpilt, extract)
|
||||
if(content) {
|
||||
editor.replaceSelection(content);
|
||||
}
|
||||
|
||||
handleNotFound(searchTerm: string) {
|
||||
log.error(`${searchTerm} not found on Wikipedia.`);
|
||||
}
|
||||
|
||||
handleCouldntResolveDisambiguation() {
|
||||
log.error(`Could not automatically resolve disambiguation.`);
|
||||
}
|
||||
|
||||
async getDoubanSearchList(title: string): Promise<DoubanSearchResultSubject[] | undefined> {
|
||||
return Searcher.search(title, this.settings);
|
||||
}
|
||||
|
||||
|
||||
|
||||
async pasteIntoEditor(editor: Editor, extract: DoubanSubject) {
|
||||
if (!extract) {
|
||||
this.handleNotFound("Not Found Subject");
|
||||
return;
|
||||
}
|
||||
editor.replaceSelection(this.formatExtractText(extract));
|
||||
}
|
||||
|
||||
|
||||
async search(searchTerm:string) {
|
||||
log.info("plugin search :" + searchTerm);
|
||||
const resultListPromise = this.getDoubanSearchList(searchTerm);
|
||||
const resultList = await resultListPromise;
|
||||
// const result = this.parseSearchList(resultList);
|
||||
log.info("plugin search result:" + JSON.stringify(resultList));
|
||||
this.fuzzySuggester.showSearchList(resultList);
|
||||
async search(searchTerm:string, editor: Editor) {
|
||||
const resultList = await Searcher.search(searchTerm, this.settings);
|
||||
new DoubanFuzzySuggester(this, editor).showSearchList(resultList);
|
||||
}
|
||||
|
||||
async getDoubanMovieTextForActiveFile(editor: Editor) {
|
||||
@ -61,13 +33,12 @@ export default class DoubanPlugin extends Plugin {
|
||||
if (activeFile) {
|
||||
const searchTerm = activeFile.basename;
|
||||
if (searchTerm) {
|
||||
await this.search(searchTerm);
|
||||
await this.search(searchTerm, editor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async geDoubanMovieTextForSearchTerm(editor: Editor) {
|
||||
log.info("start open search windows");
|
||||
new DoubanSearchModal(this.app, this, editor).open();
|
||||
}
|
||||
|
||||
@ -75,27 +46,26 @@ export default class DoubanPlugin extends Plugin {
|
||||
await this.loadSettings();
|
||||
|
||||
this.addCommand({
|
||||
id: "douban-movie-for-current-file",
|
||||
name: "get dou ban movie",
|
||||
id: "search-douban-by-current-file-name",
|
||||
name: i18nHelper.getMessage("search douban by current file name"),
|
||||
editorCallback: (editor: Editor) =>
|
||||
this.getDoubanMovieTextForActiveFile(editor),
|
||||
});
|
||||
|
||||
|
||||
this.addCommand({
|
||||
id: "douban-movie-for-search",
|
||||
name: "douban-movie-for-search",
|
||||
id: "search-douban-and-input-current-file",
|
||||
name: i18nHelper.getMessage("search douban and input current file"),
|
||||
editorCallback: (editor: Editor) =>
|
||||
this.geDoubanMovieTextForSearchTerm(editor),
|
||||
});
|
||||
|
||||
this.addSettingTab(new DoubanSettingTab(this.app, this));
|
||||
this.fuzzySuggester = new DoubanFuzzySuggester(this.app, this);
|
||||
this.doubanEtractHandler = new DoubanEtractHandler(this.app, this);
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
this.doubanEtractHandler = new DoubanEtractHandler(this.app, this);
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
|
||||
1249
package-lock.json
generated
1249
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -19,12 +19,14 @@
|
||||
"esbuild": "0.13.12",
|
||||
"obsidian": "latest",
|
||||
"tslib": "2.3.1",
|
||||
"typescript": "4.4.4"
|
||||
"typescript": "4.7.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.27.2",
|
||||
"cheerio": "^1.0.0-rc.11",
|
||||
"douban-search-crack": "^1.0.6",
|
||||
"node-fetch": "^2.6.1",
|
||||
"schema-dts": "^1.1.0",
|
||||
"tiny-network": "0.0.6"
|
||||
}
|
||||
}
|
||||
|
||||
15
styles.css
15
styles.css
@ -1,4 +1,13 @@
|
||||
/* Sets all the text color to red! */
|
||||
body {
|
||||
color: red;
|
||||
.settings_area {
|
||||
margin-left: 5px;
|
||||
margin-right: 5px;
|
||||
font-size: 14px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.settings_textField {
|
||||
margin-left: 5px;
|
||||
margin-right: 5px;
|
||||
font-size: 14px;
|
||||
width: 100%;
|
||||
}
|
||||
33
utils/HttpUtil.ts
Normal file
33
utils/HttpUtil.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import * as https from 'https';
|
||||
import { get } from 'tiny-network';
|
||||
import { log } from './logutil';
|
||||
|
||||
export default class HttpUtil {
|
||||
|
||||
static getHttps(url:string, options:any):Promise<any> {
|
||||
return new Promise<any>(
|
||||
function (resolve, reject) {
|
||||
https.get(url + '/', options, (response) => {
|
||||
console.log('url:', url + '/');
|
||||
console.log('statusCode:', response.statusCode);
|
||||
console.log('headers:', response.headers);
|
||||
if (response.statusCode === 200) {
|
||||
response.on('data', (d) => {
|
||||
resolve(d);
|
||||
});
|
||||
} if (response.statusCode === 301 || response.statusCode === 302 || response.statusCode == 303) {
|
||||
resolve(response.headers.location);
|
||||
} else {
|
||||
reject(new Error(response.statusMessage));
|
||||
}
|
||||
response.on('data', (d) => {
|
||||
process.stdout.write(d);
|
||||
});
|
||||
|
||||
}).on('error', (e) => {
|
||||
reject(new Error('XMLHttpRequest Error: ' + e.message));
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -8,12 +8,12 @@ class Logger {
|
||||
}
|
||||
|
||||
public warn(e:any):any {
|
||||
new Notice("Douban Plugin Warn: " + e);
|
||||
new Notice("Obsidian Douban Plugin Warn: " + e);
|
||||
return e;
|
||||
}
|
||||
|
||||
public info(e:any):any {
|
||||
console.log("Douban Plugin Warn: " + e);
|
||||
console.log("Douban Plugin Info: " + e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user