Repository: internetarchive/openlibrary

Issue:
is there a JSON API for inside search results?
### Question

Is there a JSON API URL for inside-search requests? I could not find it in the API documentation. Adding ".json" to the end of "inside" does not work here.

The HTML search URL is: [https://openlibrary.org/search/inside?q=%22stocked+with+wine-related+goods%22&mode=ebooks&has_fulltext=true](https://openlibrary.org/search/inside?q=%22stocked+with+wine-related+goods%22&mode=ebooks&has_fulltext=true)

I would have exepected this to work for JSON output: [https://openlibrary.org/search/inside.json?q=%22stocked+with+wine-related+goods%22&mode=ebooks&has_fulltext=true](https://openlibrary.org/search/inside.json?q=%22stocked+with+wine-related+goods%22&mode=ebooks&has_fulltext=true)

Thanks!






Discussion:
It's just a wrapper for the Internet Archive search API, so you could use that.
Are you sure about that? This [query](https://openlibrary.org/search.json?q=%22stocked+with+wine-related+goods%22) returns nothing.

Can you give me a link that returns the JSON results (including the text snippets) for the query "stocked+with+wine-related+goods"?


Resolving patch:
diff --git a/openlibrary/core/fulltext.py b/openlibrary/core/fulltext.py
--- a/openlibrary/core/fulltext.py
+++ b/openlibrary/core/fulltext.py
@@ -5,7 +5,7 @@
 import web
 from infogami import config
 from openlibrary.core.lending import get_availability_of_ocaids
-
+from openlibrary.plugins.openlibrary.home import format_book_data
 
 from six.moves import urllib
 
@@ -29,7 +29,7 @@ def fulltext_search_api(params):
         return {'error': 'Error converting search engine data to JSON'}
 
 
-def fulltext_search(q, page=1, limit=100):
+def fulltext_search(q, page=1, limit=100, js=False):
     offset = (page - 1) * limit
     ia_results = fulltext_search_api({
         'q': q, 'from': offset,
@@ -49,8 +49,8 @@ def fulltext_search(q, page=1, limit=100):
         for ed in editions:
             if ed.ocaid in ocaids:
                 idx = ocaids.index(ed.ocaid)
-                ia_results['hits']['hits'][idx]['edition'] = ed
+                ia_results['hits']['hits'][idx]['edition'] = (
+                    format_book_data(ed) if js else ed
+                )
                 ia_results['hits']['hits'][idx]['availability'] = availability[ed.ocaid]
     return ia_results
-
-
diff --git a/openlibrary/plugins/inside/code.py b/openlibrary/plugins/inside/code.py
--- a/openlibrary/plugins/inside/code.py
+++ b/openlibrary/plugins/inside/code.py
@@ -1,6 +1,6 @@
 from time import time
 
-import simplejson
+import json
 import web
 
 from infogami.utils import delegate
@@ -25,3 +25,19 @@ def GET(self):
 
         return render_template('search/inside.tmpl', query, results, search_time,
                                page=page, results_per_page=RESULTS_PER_PAGE)
+        page.v2 = True  # page is mobile-first
+        return page
+
+
+class search_inside_json(delegate.page):
+    path = "/search/inside"
+    encoding = "json"
+
+    def GET(self):
+        i = web.input(q='', page=1, limit=RESULTS_PER_PAGE)
+        limit = min(i.limit, RESULTS_PER_PAGE) if i.limit else RESULTS_PER_PAGE
+        query = i.q
+        page = int(i.page)
+        results = fulltext_search(query, page=page, limit=limit, js=True)
+        web.header('Content-Type', 'application/json')
+        return delegate.RawText(json.dumps(results, indent=True))


Test patch: