Commit bfc79fd2 authored by Patrick van der Leer's avatar Patrick van der Leer
Browse files

Refs #17 - Implemented cache and small bugfix + refactor to make this work

parent 49cec21b
Pipeline #375 passed with stage
in 5 minutes and 4 seconds
......@@ -11,7 +11,7 @@ from safrs import SAFRSJSONEncoder
from eBookHub.config import ProdConfig
from . import extensions as ext
from .extensions import db
from .extensions import db, cache
def init(config_object=ProdConfig):
......@@ -90,8 +90,8 @@ def create_api(app, host='localhost', port=5000, api_prefix=''):
def configure_sources(app):
""" Configure sources """
for source in ext.sources:
source.init_app(app)
from eBookHub.source import init_sources
init_sources(app)
def configure_extensions(app):
......
......@@ -6,7 +6,6 @@ from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from eBookHub.scheduler import Scheduler
from eBookHub.source import GoodreadsClient
bcrypt = Bcrypt()
login_manager = LoginManager()
......@@ -15,7 +14,3 @@ migrate = Migrate()
cache = Cache()
jwt = JWTManager()
scheduler = Scheduler()
goodreads_client = GoodreadsClient()
sources = [
goodreads_client
]
......@@ -5,7 +5,7 @@ from threading import RLock
from eBookHub.parser.exceptions import FilenameParserException, NoResultsException
from eBookHub.parser.filename import FilenameParser
from eBookHub.extensions import sources
from eBookHub.source import sources
from eBookHub.source.abstract import SourceAbstract
from eBookHub.utils import md5sum
......
from flask import current_app
from flask_script import Manager, prompt
from eBookHub.extensions import goodreads_client
from eBookHub.source import goodreads_client
goodreads_manager = Manager(current_app, usage="Goodreads lookup")
......
from .goodreads import GoodreadsClient
goodreads_client = GoodreadsClient()
sources = [
]
def init_sources(app):
global sources
for source in [goodreads_client]:
source.init_app(app)
sources.append(source)
__all__ = [
"GoodreadsClient"
"GoodreadsClient",
"goodreads_client",
"init_sources",
"sources",
]
from collections import OrderedDict
import goodreads_api_client as gr
from requests import HTTPError
from eBookHub import cache
from eBookHub.parser.exceptions import NoResultsException
from eBookHub.source.abstract import SourceAbstract
from eBookHub.source.transformer import GoodreadsTransformer
......@@ -17,18 +19,23 @@ class GoodreadsClient(SourceAbstract):
developer_secret = app.config.get("GOODREADS_DEV_SECRET", None)
self.client = gr.Client(developer_key=developer_key, developer_secret=developer_secret)
def book_id(self, author):
return self.transformer.convert_book(self.client.Book.show(author))
@cache.memoize()
def book_id(self, eid):
return self.transformer.convert_book(self.client.Book.show(eid))
@cache.memoize()
def search_author(self, author):
return self.client.search_author(author)
@cache.memoize()
def search_title(self, title):
return self.parse_search_result(self.client.search_book(q=title, field="title"))
@cache.memoize()
def search_isbn(self, isbn):
return self.client.search_book(q=isbn, field="isbn")
return self.parse_search_result(self.client.search_book(q=isbn, field="isbn"))
@cache.memoize()
def search_title_and_author(self, title, author):
return self.parse_search_result(self.client.search_book("{} - {}".format(title, author)))
......@@ -38,4 +45,11 @@ class GoodreadsClient(SourceAbstract):
work = result['results']['work']
if isinstance(work, (dict, OrderedDict)) and "best_book" in work.keys():
return self.book_id(work['best_book']['id']["#text"])
return False
result = []
for entry in work:
try:
result.append(self.book_id(entry['id']["#text"]))
except HTTPError as e:
if not e.response.status_code == 404: # pragma: no cover
raise
return result
# -*- coding: utf-8 -*-
from flask_testing import TestCase
from eBookHub.extensions import goodreads_client
from eBookHub.source import goodreads_client
from eBookHub.jobs.blackhole import process_file
from eBookHub.parser.filename import FilenameParser
from tests.base import MyTestCase
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment