Repository: internetarchive/openlibrary

Issue:
Change homepage trending carousel from 1-day to either 1 or 6 hours (whichever is better)
In our homepage, we currently render a carousel which shows the books most "trending today":
https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/home/index.html#L23

We want to update the plugins/openlibrary/api.py API to allow `/custom?days=&hours=`

https://github.com/internetarchive/openlibrary/blob/628ca04c4f345dfacc44a205f40028af5c60f9f3/openlibrary/views/loanstats.py#L55

get_trending_books will also need to be updated to include hours=0
https://github.com/internetarchive/openlibrary/blob/628ca04c4f345dfacc44a205f40028af5c60f9f3/openlibrary/views/loanstats.py#L50

Proposal, update `dateutil.date_n_days_ago` to be a function that takes (day=0 and hours=0)

If the `period` is `custom` then directly pass in the `days` to  `since_days` and `hours` to `since_hours` (new field).

https://github.com/internetarchive/openlibrary/blob/628ca04c4f345dfacc44a205f40028af5c60f9f3/openlibrary/plugins/openlibrary/api.py#L74-L78

Let's go for **2 hours** as the limit and try this on testing.

### Stakeholders
<!-- @ tag stakeholders of this bug -->





Discussion:
Might want to change what the header links to

Resolving patch:
diff --git a/openlibrary/plugins/openlibrary/api.py b/openlibrary/plugins/openlibrary/api.py
--- a/openlibrary/plugins/openlibrary/api.py
+++ b/openlibrary/plugins/openlibrary/api.py
@@ -70,9 +70,11 @@ def GET(self, period="/daily"):
         from openlibrary.views.loanstats import SINCE_DAYS
 
         period = period[1:]  # remove slash
-        i = web.input(page=1, limit=100)
+        i = web.input(page=1, limit=100, days=0, hours=0)
+        days = SINCE_DAYS.get(period, int(i.days))
         works = get_trending_books(
-            since_days=SINCE_DAYS[period],
+            since_days=days,
+            since_hours=int(i.hours),
             limit=int(i.limit),
             page=int(i.page),
             books_only=True,
@@ -80,6 +82,8 @@ def GET(self, period="/daily"):
         result = {
             'query': f"/trending/{period}",
             'works': [dict(work) for work in works],
+            'days': days,
+            'hours': i.hours,
         }
         return delegate.RawText(json.dumps(result), content_type="application/json")
 
diff --git a/openlibrary/utils/dateutil.py b/openlibrary/utils/dateutil.py
--- a/openlibrary/utils/dateutil.py
+++ b/openlibrary/utils/dateutil.py
@@ -21,6 +21,10 @@ def days_in_current_month():
     return calendar.monthrange(now.year, now.month)[1]
 
 
+def todays_date_minus(**kwargs):
+    return datetime.date.today() - datetime.timedelta(**kwargs)
+
+
 def date_n_days_ago(n=None, start=None):
     """
     Args:
diff --git a/openlibrary/views/loanstats.py b/openlibrary/views/loanstats.py
--- a/openlibrary/views/loanstats.py
+++ b/openlibrary/views/loanstats.py
@@ -47,12 +47,12 @@ def reading_log_summary():
 
 
 @public
-def get_trending_books(since_days=1, limit=18, page=1, books_only=False):
+def get_trending_books(since_days=1, since_hours=0, limit=18, page=1, books_only=False):
     logged_books = (
         Bookshelves.fetch(get_activity_stream(limit=limit, page=page))  # i.e. "now"
-        if since_days == 0
+        if (since_days == 0 and since_hours == 0)
         else Bookshelves.most_logged_books(
-            since=dateutil.date_n_days_ago(since_days),
+            since=dateutil.todays_date_minus(days=since_days, hours=since_hours),
             limit=limit,
             page=page,
             fetch=True,


Test patch: