Skip to content

Commit

Permalink
chore: add neodb store
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Jul 22, 2023
1 parent 234e9d9 commit 06b6191
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ configParser({
notion_token: {
usage: 'Notion 机器人 Token',
env: 'PLUGIN_NOTION_TOKEN,DOUBAN_NOTION_TOKEN,NOTION_TOKEN,INPUT_NOTION_TOKEN',
},
neodb_token: {
usage: 'NeoDB Token',
env: 'PLUGIN_NEODB_TOKEN,DOUBAN_NEODB_TOKEN,NEODB_TOKEN,INPUT_NEODB_TOKEN',
}
})(exec);
4 changes: 2 additions & 2 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ module.exports = {
}
return fn => fn(ret);
},
async exec({ id, type, status, format, dir, notion_token }) {
async exec({ id, type, status, format, dir, notion_token, neodb_token }) {
const Store = Stores[format];
if(!Store) {
throw new Error(`当前选择的存储类型为 ${storeType},它尚未被支持`);
}
const store = new Store({ type, dir, notion_token });
const store = new Store({ type, dir, status, notion_token, neodb_token });

// 读取之前的 mark 记录
const local = await store.get();
Expand Down
3 changes: 2 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exports.csv = require('./csv');
exports.json = require('./json');
exports.notion = require('./notion');
exports.notion = require('./notion');
exports.neodb = require('./neodb');
101 changes: 101 additions & 0 deletions src/store/neodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const { request } = require('undici');
const dayjs = require('dayjs');
const customParseFormat = require('dayjs/plugin/customParseFormat');
dayjs.extend(customParseFormat);
const { sleep } = require('../helper');

const STATUS_MAP = {
'done': 'complete',
'doing': 'progress',
'mark': 'wishlist',
'complete': 'done',
'progress': 'doing',
'wishlist': 'mark',
};

module.exports = class NeoDBStore {
constructor({type, status, neodb_token}) {
this.type = type;
this.status = status;
this.token = neodb_token;
}

request({ method, url, data }) {
url = 'https://neodb.social' + url;
if (method === 'GET' && data) {
url += '?' + (new URLSearchParams(data)).toString();
}

return request(url, {
method,
body: method !== 'GET' ? JSON.stringify(data) : undefined,
headers: {
authorization: `Bearer ${this.token}`,
accept: 'application/json',
'content-type': method !== 'GET' ? 'application/json' : undefined,
}
}).then(({ body }) => body.text());
}

async fetchSubjectUUID(item) {
const neodbItem = JSON.parse(await this.request({
url: '/api/catalog/fetch',
method: 'GET',
data: {
url: item.subject.url
}
}));

if (!neodbItem.uuid) {
await sleep(1);
return this.fetchSubjectUUID(item);
}

return neodbItem;
}

async markItemNeodb(neodbItem, item) {
return this.request({
url: `/api/me/shelf/item/${neodbItem.uuid}`,
method: 'POST',
data: {
shelf_type: STATUS_MAP[this.status],
visibility: 0,
comment_text: item.comment,
rating_grade: item.rating ? item.rating.star_count * 2 : undefined,
created_time: dayjs(item.create_time, 'YYYY-MM-DD HH:mm:ss'),
post_to_fediverse: false,
tags: item.tags,
}
});
}

format(item) {
return item;
}

async get() {
const resp = JSON.parse(await this.request({
method: 'GET',
url: `/api/me/shelf/${STATUS_MAP[this.status]}`,
data: {
category: this.type,
page: 1
}
}));

resp.data.forEach(item => {
item.star_time = (new Date(item.cteated_time)).getTime();
});

return resp.data;
}

async set(data) {
for(let i = 0; i < data.length; i++) {
const subject = data[i];
const neodbItem = await this.fetchSubjectUUID(subject);
await this.markItemNeodb(neodbItem, subject);
}
}
}

0 comments on commit 06b6191

Please sign in to comment.