Skip to content

Commit

Permalink
fill license, alter axios to Obsidian api
Browse files Browse the repository at this point in the history
  • Loading branch information
hucorz committed Aug 29, 2023
1 parent 72fbe33 commit 0e6ea91
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 37 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) [year] [fullname]
Copyright (c) [2023] [Chao Hui]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 7 additions & 6 deletions autoliter/spiders/arxivInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import parser from 'rss-parser';
import unidecode from 'unidecode';
import { assert } from 'autoliter/utils';
import type { Dict } from 'autoliter/types';
import {requestUrl} from 'obsidian'

class ArxivInfo {
private base_url: string;
Expand All @@ -14,14 +14,15 @@ class ArxivInfo {
}

async getInfoByArxivId(arxivId: string): Promise<Dict> {
// const params = "?search_query=id:" + encodeURIComponent(unidecode(arxivId));
const params = `?search_query=id:${arxivId}`;
try {
const results = await this.feedParser.parseURL(this.base_url + params);
assert(results.items.length === 1, "ArxivId should return only one result");
// use rss-parser cannot get the doi info
// const results = await this.feedParser.parseURL(this.base_url + params);
const respnse = await requestUrl(this.base_url + params);
const data = await this.feedParser.parseString(respnse.text); // response format is xml
assert(data.items.length === 1, "ArxivId should return only one result");
// TODO: get doi info
return this.extractInfo(results.items[0]);
// It seems that arxiv api can not get the doi info
return this.extractInfo(data.items[0]);
} catch (error) {
throw new Error(`Error in getInfoByArxivId: ${error.message}`);
}
Expand Down
10 changes: 6 additions & 4 deletions autoliter/spiders/bmInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios";
import CrossrefInfo from "./crossrefInfo";
import { Dict } from "autoliter/types";
import {requestUrl} from 'obsidian'


class BMInfo {
private base_url: string;
Expand All @@ -15,9 +16,10 @@ class BMInfo {
const urls = this.server.map(server => `${this.base_url}${server}/${bmId}`);
for (let url of urls) {
try {
const response = await axios.get(url);
if (response.data['collection'].length > 0) {
const data = response.data['collection'][0];
// const response = await axios.get(url);
const response = await requestUrl(url).json; // user obsidian's requestUrl to avoid cors
if (response['collection'].length > 0) {
const data = response['collection'][0];
if (data['published'] !== "NA") {
return new CrossrefInfo().getInfoByDoi(data['published']);
}
Expand Down
7 changes: 4 additions & 3 deletions autoliter/spiders/crossrefInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import type { Dict } from 'autoliter/types';
import {requestUrl} from 'obsidian'

// const HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/43.0'};

Expand All @@ -13,8 +13,9 @@ class CrossrefInfo {
async getInfoByDoi(doi: string): Promise<Dict> {
const url = `${this.base_url}works/${doi}`;
try {
const response = await axios.get(url);
return this.extractInfo(response['data']['message']);
// const response = await axios.get(url);
const response = await requestUrl(url).json; // user obsidian's requestUrl to avoid cors
return this.extractInfo(response['message']);
} catch (error) {
throw new Error(`Error in getInfoByDoi: ${error.message}`);
}
Expand Down
21 changes: 0 additions & 21 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,8 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Set
import PatternRecognizer from 'autoliter/patternRecognizer';
import {getReplaceDict} from 'autoliter/utils'

// Remember to rename these classes and interfaces!

interface MyPluginSettings {
mySetting: string;
}

const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
}

export default class AutoLiter extends Plugin {
settings: MyPluginSettings;

async onload() {
await this.loadSettings();
const paperRecognizer = new PatternRecognizer(/- \{.{3,}\}/g); // g is must

// This creates an icon in the left ribbon.
Expand All @@ -31,14 +18,6 @@ export default class AutoLiter extends Plugin {
onunload() {
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}

async updateNote(file: TFile, paperRecognizer: PatternRecognizer) {
const content = await this.app.vault.read(file);
const m = paperRecognizer.findAll(content);
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "auto-literature",
"name": "autoLiterature",
"version": "1.0.8",
"version": "1.0.9",
"minAppVersion": "0.15.0",
"description": "Assist you in taking notes for your literature.",
"author": "hucorz",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "autoLiterature",
"version": "1.0.8",
"version": "1.0.9",
"description": "Assist you in taking notes for your literature.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 0e6ea91

Please sign in to comment.