Skip to content

Commit

Permalink
Implement search for Pocket
Browse files Browse the repository at this point in the history
  • Loading branch information
BHSPitMonkey committed Nov 26, 2023
1 parent 035cfb7 commit 8fa89e9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
32 changes: 32 additions & 0 deletions dist/provider/pocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "node:path";
import querystring from "node:querystring";
import got from "got";
import { OPDSFeed } from "../opds.js";
import { OpenSearchDescription } from "../opensearch.js";
;
export default class PocketProvider {
constructor(app, configDir) {
Expand Down Expand Up @@ -123,6 +124,7 @@ export default class PocketProvider {
self: "/opds/provider/pocket",
start: "/opds",
up: "/opds",
search: "/opds/provider/pocket/search.xml",
},
title: "Pocket",
});
Expand All @@ -136,6 +138,36 @@ export default class PocketProvider {
}));
res.type('application/xml').send(feed.toXmlString());
});
// Search description document
app.get(`/opds/provider/pocket/search.xml`, async (req, res) => {
const searchDescription = new OpenSearchDescription('/opds/provider/pocket/search?q={searchTerms}');
res.type('application/xml').send(searchDescription.toXmlString());
});
// Search handler
app.get(`/opds/provider/pocket/search`, async (req, res) => {
const feed = new OPDSFeed({
id: `pocket-search`,
links: {
self: `/opds/provider/pocket/search`,
start: "/opds",
up: "/opds/provider/pocket",
},
title: "Search",
});
const query = req.query.q;
if (typeof query !== "string") {
console.error("Search endpoint called without a 'q' query param");
res.status(400).send("Search query is required");
return;
}
// Fetch stories
const combinedSearchParams = Object.assign(Object.assign({}, this.BASE_SEARCH_PARAMS), { search: query });
const stories = await this.getStories(combinedSearchParams);
for (const story of stories) {
feed.addArticleAcquisitionEntry(story.url, story.title);
}
res.type('application/xml').send(feed.toXmlString());
});
// Acquisition feeds
for (const entry of this.FEEDS) {
app.get(`/opds/provider/pocket/${entry.id}`, async (req, res) => {
Expand Down
46 changes: 46 additions & 0 deletions src/provider/pocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import querystring from "node:querystring";
import got from "got";
import { Express, Request, Response } from "express";
import { OPDSFeed } from "../opds.js";
import { OpenSearchDescription } from "../opensearch.js";

interface OAuthCodeResponse {
code: string;
Expand All @@ -22,6 +23,7 @@ interface PocketApiSearchParams {
count?: number,
detailType?: "simple",
favorite?: "0" | "1",
search?: string,
sort?: "newest",
state?: "all" | "unread",
}
Expand Down Expand Up @@ -183,6 +185,7 @@ export default class PocketProvider {
self: "/opds/provider/pocket",
start: "/opds",
up: "/opds",
search: "/opds/provider/pocket/search.xml",
},
title: "Pocket",
});
Expand All @@ -199,6 +202,49 @@ export default class PocketProvider {
res.type('application/xml').send(feed.toXmlString());
});

// Search description document
app.get(
`/opds/provider/pocket/search.xml`,
async (req: Request, res: Response) => {
const searchDescription = new OpenSearchDescription('/opds/provider/pocket/search?q={searchTerms}');
res.type('application/xml').send(searchDescription.toXmlString());
}
);

// Search handler
app.get(
`/opds/provider/pocket/search`,
async (req: Request, res: Response) => {
const feed = new OPDSFeed({
id: `pocket-search`,
links: {
self: `/opds/provider/pocket/search`,
start: "/opds",
up: "/opds/provider/pocket",
},
title: "Search",
});

const query = req.query.q;
if (typeof query !== "string") {
console.error("Search endpoint called without a 'q' query param");
res.status(400).send("Search query is required");
return;
}

// Fetch stories
const combinedSearchParams = {
...this.BASE_SEARCH_PARAMS,
search: query,
};
const stories = await this.getStories(combinedSearchParams);
for (const story of stories) {
feed.addArticleAcquisitionEntry(story.url, story.title);
}
res.type('application/xml').send(feed.toXmlString());
}
);

// Acquisition feeds
for (const entry of this.FEEDS) {
app.get(
Expand Down

0 comments on commit 8fa89e9

Please sign in to comment.