From 5af3f07b9626d8df2c8fa86605afac71bc174256 Mon Sep 17 00:00:00 2001 From: Gary Fu Date: Thu, 21 Oct 2021 16:53:46 +0800 Subject: [PATCH] =?UTF-8?q?calibre-web=E7=9A=84=E8=B1=86=E7=93=A3api=20pro?= =?UTF-8?q?vider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++- requirements.txt | 1 + src/cps/services/Metadata.py | 27 +++++++++++++++++++++++++ src/douban.py | 39 ++++++++++++++++++++++++++++++++++++ tests/DoubanTest.py | 6 ++++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 requirements.txt create mode 100644 src/cps/services/Metadata.py create mode 100644 src/douban.py create mode 100644 tests/DoubanTest.py diff --git a/README.md b/README.md index 649c490..1c82574 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,12 @@ # calibre-web-douban-api -新版calibre-web已经移除douban-api了,添加一个豆瓣api实现,调用simple-boot-douban-api服务 + +新版calibre-web已经移除douban-api了,而且把从get_meta.js中直接发起请求获取数据改成了从服务端使用python获取数据。 + +此项目是添加一个豆瓣api provider实现,需要放到metadata_provider目录下,调用simple-boot-douban-api服务 + +### 使用方法 + +复制`src/douban.py`到`calibre-web/cps/metadata_provider/`目录下,重启项目。 + +**注意:由于豆瓣api已经不开放使用了,这个豆瓣api需要连接`simple-boot-douban-api`使用** + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..22999c6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests>=2.11.1,<2.25.0 \ No newline at end of file diff --git a/src/cps/services/Metadata.py b/src/cps/services/Metadata.py new file mode 100644 index 0000000..4456cfa --- /dev/null +++ b/src/cps/services/Metadata.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web) +# Copyright (C) 2021 OzzieIsaacs +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# 从calibre-web复制出来,方便测试使用 +class Metadata(): + __name__ = "Generic" + + def __init__(self): + self.active = True + + def set_status(self, state): + self.active = state diff --git a/src/douban.py b/src/douban.py new file mode 100644 index 0000000..d53021b --- /dev/null +++ b/src/douban.py @@ -0,0 +1,39 @@ +import requests + +from cps.services.Metadata import Metadata + + +class Douban(Metadata): + __name__ = "Douban Books" + __id__ = "douban" + doubanUrl = "http://YOUR_NAS_IP:8085" + headers = { + 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36' + } + + def search(self, query, generic_cover=""): + if self.active: + val = list() + result = requests.get(self.doubanUrl + "/v2/book/search?q=" + query.replace(" ", "+"), headers=self.headers) + for r in result.json()['books']: + v = dict() + v['id'] = r['id'] + v['title'] = r['title'] + v['authors'] = r.get('authors', []) + v['description'] = r.get('summary', "") + v['publisher'] = r.get('publisher', "") + v['publishedDate'] = r.get('pubdate', "") + v['tags'] = [tag.get('name', '') for tag in r.get('tags', [])] + v['rating'] = r['rating'].get('average', 0) + if r.get('image'): + v['cover'] = r.get('image') + else: + v['cover'] = generic_cover + v['source'] = { + "id": self.__id__, + "description": self.__name__, + "link": "https://book.douban.com/" + } + v['url'] = "https://book.douban.com/subject/" + r['id'] + val.append(v) + return val diff --git a/tests/DoubanTest.py b/tests/DoubanTest.py new file mode 100644 index 0000000..c79398d --- /dev/null +++ b/tests/DoubanTest.py @@ -0,0 +1,6 @@ +from douban import Douban + +douban = Douban() +result = douban.search('人民的名义') +for book in result: + print(book.get('title'), book.get('url'))