Skip to content

Commit dfa710c

Browse files
authored
Refactor OPDS links and update catalog metadata
Switch to pyopds2_openlibrary.STORED_SEARCHES which uses pyopds2.Search
1 parent b03df2c commit dfa710c

File tree

1 file changed

+37
-73
lines changed
  • openlibrary/plugins/openlibrary

1 file changed

+37
-73
lines changed

openlibrary/plugins/openlibrary/api.py

Lines changed: 37 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -827,18 +827,8 @@ def GET(self):
827827
href=f"{provider.BASE_URL}/opds/search{{?query}}",
828828
type="application/opds+json",
829829
templated=True,
830-
),
831-
Link(
832-
rel="http://opds-spec.org/shelf",
833-
href="https://archive.org/services/loans/loan/?action=user_bookshelf",
834-
type="application/opds+json",
835-
),
836-
Link(
837-
rel="profile",
838-
href="https://archive.org/services/loans/loan/?action=user_profile",
839-
type="application/opds-profile+json",
840-
),
841-
],
830+
)
831+
] + [provider.bookshelf_link(), provider.profile_link],
842832
)
843833
web.header('Content-Type', 'application/opds+json')
844834
return delegate.RawText(json.dumps(catalog.model_dump()))
@@ -860,21 +850,39 @@ def GET(self, edition_olid: str):
860850
)
861851

862852
pub = resp.records[0].to_publication()
863-
pub.links += [
864-
Link(
865-
rel="http://opds-spec.org/shelf",
866-
href="https://archive.org/services/loans/loan/?action=user_bookshelf",
867-
type="application/opds+json",
868-
),
869-
Link(
870-
rel="profile",
871-
href="https://archive.org/services/loans/loan/?action=user_profile",
872-
type="application/opds-profile+json",
873-
),
874-
]
853+
pub.links += [provider.bookshelf_link(), provider.profile_link]
875854
return delegate.RawText(json.dumps(pub.model_dump()))
876855

856+
class opds_subjects(delegate.page):
857+
path = r"/opds/subjects/(.*)"
858+
859+
def GET(self, subject_key):
860+
from pyopds2 import Catalog, Link, Metadata
861+
from pyopds2.helpers import build_url
862+
i = web.input(limit=25, page=1)
863+
provider = get_opds_data_provider()
864+
readable = 'ebook_access:[borrowable TO *]'
865+
sfw = '-subject:"content_warning:cover"'
866+
params = {
867+
'sort': 'trending',
868+
'query': f"subject_key:{subject_key} {readable} {sfw}',
869+
}
870+
self_url = build_url(f"{provider.BASE_URL}/opds/subjects/{subject_key}", params)
871+
title = subject_key.replace("_", " ").title()
872+
catalog = Catalog.create(
873+
self_url=self_url,
874+
metadata=Metadata(title=_(title)),
875+
response=provider.search(
876+
query=params.query,
877+
sort=params.sort,
878+
limit=int(i.limit),
879+
offset=(int(i.page) - 1) * int(i.limit),
880+
),
881+
)
882+
web.header('Content-Type', 'application/opds+json')
883+
return delegate.RawText(json.dumps(catalog.model_dump()))
877884

885+
878886
class opds_home(delegate.page):
879887
path = r"/opds"
880888

@@ -884,65 +892,21 @@ def build_homepage():
884892

885893
provider = get_opds_data_provider()
886894
catalog = Catalog(
887-
metadata=Metadata(title=_("Welcome to Open Library")),
895+
metadata=Metadata(title=_("Browsing the Open Library")),
888896
publications=[],
889897
navigation=[
890898
Navigation(
891899
type="application/opds+json",
892900
title=subject['presentable_name'],
893-
href=f'{provider.BASE_URL}{provider.SEARCH_URL}?sort=trending&query=subject_key:{subject['key'].split('/')[-1]} -subject:"content_warning:cover" ebook_access:[borrowable TO *]', # noqa: E501
901+
href=f"{provider.BASE_URL}/subjects/{subject['key'].split('/')[-1]}"
894902
)
895903
for subject in get_cached_featured_subjects()
896904
],
897905
groups=[
898906
Catalog.create(
899-
metadata=Metadata(title=_("Trending Books")),
900-
response=provider.search(
901-
query='trending_score_hourly_sum:[1 TO *] -subject:"content_warning:cover" ebook_access:[borrowable TO *]',
902-
sort='trending',
903-
limit=25,
904-
),
905-
),
906-
Catalog.create(
907-
metadata=Metadata(title=_("Classic Books")),
908-
response=provider.search(
909-
query='ddc:8* first_publish_year:[* TO 1950] publish_year:[2000 TO *] NOT public_scan_b:false -subject:"content_warning:cover"',
910-
sort='trending',
911-
limit=25,
912-
),
913-
),
914-
Catalog.create(
915-
metadata=Metadata(title=_("Romance")),
916-
response=provider.search(
917-
query='subject:romance ebook_access:[borrowable TO *] first_publish_year:[1930 TO *] trending_score_hourly_sum:[1 TO *] -subject:"content_warning:cover"', # noqa: E501
918-
sort='trending,trending_score_hourly_sum',
919-
limit=25,
920-
),
921-
),
922-
Catalog.create(
923-
metadata=Metadata(title=_("Kids")),
924-
response=provider.search(
925-
query='ebook_access:[borrowable TO *] trending_score_hourly_sum:[1 TO *] (subject_key:(juvenile_audience OR children\'s_fiction OR juvenile_nonfiction OR juvenile_encyclopedias OR juvenile_riddles OR juvenile_poetry OR juvenile_wit_and_humor OR juvenile_limericks OR juvenile_dictionaries OR juvenile_non-fiction) OR subject:("Juvenile literature" OR "Juvenile fiction" OR "pour la jeunesse" OR "pour enfants"))', # noqa: E501
926-
sort='random.hourly',
927-
limit=25,
928-
),
929-
),
930-
Catalog.create(
931-
metadata=Metadata(title=_("Thrillers")),
932-
response=provider.search(
933-
query='subject:thrillers ebook_access:[borrowable TO *] trending_score_hourly_sum:[1 TO *] -subject:"content_warning:cover"',
934-
sort='trending,trending_score_hourly_sum',
935-
limit=25,
936-
),
937-
),
938-
Catalog.create(
939-
metadata=Metadata(title=_("Textbooks")),
940-
response=provider.search(
941-
query='subject_key:textbooks publish_year:[1990 TO *] ebook_access:[borrowable TO *]',
942-
sort='trending',
943-
limit=25,
944-
),
945-
),
907+
metadata=Metadata(title=_(title)),
908+
response=provider.search(**search)
909+
) for (title, search) in provider.STORED_SEARCHES
946910
],
947911
facets=None,
948912
links=[

0 commit comments

Comments
 (0)