diff --git a/openlibrary/core/bookshelves.py b/openlibrary/core/bookshelves.py index 53ba5b387da..69edc14ffed 100644 --- a/openlibrary/core/bookshelves.py +++ b/openlibrary/core/bookshelves.py @@ -94,6 +94,8 @@ def most_logged_books( since: date | None = None, page: int = 1, fetch: bool = False, + sort_by_count: bool = True, + minimum: int = 0, ) -> list: """Returns a ranked list of work OLIDs (in the form of an integer -- i.e. OL123W would be 123) which have been most logged by @@ -106,8 +108,15 @@ def most_logged_books( where = 'WHERE bookshelf_id' + ('=$shelf_id' if shelf_id else ' IS NOT NULL ') if since: where += ' AND created >= $since' - query = f'select work_id, count(*) as cnt from bookshelves_books {where}' - query += ' group by work_id order by cnt desc limit $limit offset $offset' + group_by = 'group by work_id' + if minimum: + group_by += f" HAVING COUNT(*) > {minimum}" + order_by = 'order by cnt desc' if sort_by_count else '' + query = f""" + select work_id, count(*) as cnt + from bookshelves_books + {where} {group_by} {order_by} + limit $limit offset $offset""" logger.info("Query: %s", query) data = {'shelf_id': shelf_id, 'limit': limit, 'offset': offset, 'since': since} logged_books = list(oldb.query(query, vars=data)) diff --git a/openlibrary/plugins/openlibrary/api.py b/openlibrary/plugins/openlibrary/api.py index b8cd2701287..d3fb9644467 100644 --- a/openlibrary/plugins/openlibrary/api.py +++ b/openlibrary/plugins/openlibrary/api.py @@ -71,7 +71,9 @@ def GET(self, period="/daily"): from openlibrary.views.loanstats import SINCE_DAYS period = period[1:] # remove slash - i = web.input(page=1, limit=100, days=0, hours=0) + i = web.input( + page=1, limit=100, days=0, hours=0, sort_by_count=False, minimum=0 + ) days = SINCE_DAYS.get(period, int(i.days)) works = get_trending_books( since_days=days, @@ -79,6 +81,8 @@ def GET(self, period="/daily"): limit=int(i.limit), page=int(i.page), books_only=True, + sort_by_count=not i.sort_by_count == 'false', + minimum=i.minimum, ) result = { 'query': f"/trending/{period}", diff --git a/openlibrary/templates/home/index.html b/openlibrary/templates/home/index.html index bdcd6aad31c..b8336e5600b 100644 --- a/openlibrary/templates/home/index.html +++ b/openlibrary/templates/home/index.html @@ -20,7 +20,7 @@ $:render_template("home/welcome", test=test) $if not test: - $:render_template("books/custom_carousel", books=get_trending_books(books_only=True), title=_('Trending Books'), url="/trending/daily", test=test, load_more={"url": "/trending/hours.json?hours=1", "mode": "page", "limit": 18}) + $:render_template("books/custom_carousel", books=get_trending_books(books_only=True, since_days=0, since_hours=24, minimum=3, sort_by_count=False), title=_('Trending Books'), url="/trending/daily", test=test, load_more={"url": "/trending/hours.json?hours=24&minimum=3&sort_by_count=false", "mode": "page", "limit": 18}) $:macros.QueryCarousel(query="ddc:8* first_publish_year:[* TO 1950] publish_year:[2000 TO *] NOT public_scan_b:false", title=_('Classic Books'), key="public_domain", url="/read", sort='random.hourly') diff --git a/openlibrary/views/loanstats.py b/openlibrary/views/loanstats.py index e8c8e8a10ad..b50af9b0f96 100644 --- a/openlibrary/views/loanstats.py +++ b/openlibrary/views/loanstats.py @@ -47,7 +47,15 @@ def reading_log_summary(): @public -def get_trending_books(since_days=1, since_hours=0, limit=18, page=1, books_only=False): +def get_trending_books( + since_days=1, + since_hours=0, + limit=18, + page=1, + books_only=False, + sort_by_count=True, + minimum=None, +): logged_books = ( Bookshelves.fetch(get_activity_stream(limit=limit, page=page)) # i.e. "now" if (since_days == 0 and since_hours == 0) @@ -56,6 +64,8 @@ def get_trending_books(since_days=1, since_hours=0, limit=18, page=1, books_only limit=limit, page=page, fetch=True, + sort_by_count=sort_by_count, + minimum=minimum, ) ) return (