mirror of
https://codeberg.org/teddit/teddit.git
synced 2026-03-05 13:30:33 -05:00
Add suport for subreddit search API
This commit is contained in:
@@ -400,8 +400,50 @@ async function finalizeJsonPost(
|
|||||||
return { post_data: post_data, comments: comments_html };
|
return { post_data: post_data, comments: comments_html };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPostItem(post_json, req) {
|
async function processJsonPostList(posts, mode) {
|
||||||
let protocol = config.https_enabled || config.api_force_https ? 'https' : 'http';
|
let protocol = config.https_enabled || config.api_force_https ? 'https' : 'http';
|
||||||
|
|
||||||
|
for (var i = 0; i < posts.length; i++) {
|
||||||
|
let link = posts[i];
|
||||||
|
let valid_reddit_self_domains = ['reddit.com'];
|
||||||
|
let is_self_link = false;
|
||||||
|
|
||||||
|
if (link.domain) {
|
||||||
|
let tld = link.domain.split('self.');
|
||||||
|
if (tld.length > 1) {
|
||||||
|
if (!tld[1].includes('.')) {
|
||||||
|
is_self_link = true;
|
||||||
|
link.url = teddifyUrl(link.url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
config.valid_media_domains.includes(link.domain) ||
|
||||||
|
valid_reddit_self_domains.includes(link.domain)
|
||||||
|
) {
|
||||||
|
is_self_link = true;
|
||||||
|
link.url = teddifyUrl(link.url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
link.permalink = `${protocol}://${config.domain}${link.permalink}`;
|
||||||
|
|
||||||
|
if (is_self_link) link.url = link.permalink;
|
||||||
|
|
||||||
|
if (link.images) {
|
||||||
|
if (link.images.thumb !== 'self') {
|
||||||
|
link.images.thumb = `${protocol}://${config.domain}${link.images.thumb}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode === 'light') {
|
||||||
|
link.selftext_html = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getPostItem(post_json, req, protocol) {
|
||||||
let thumbnail = '';
|
let thumbnail = '';
|
||||||
let post_image = '';
|
let post_image = '';
|
||||||
let is_self_link = false;
|
let is_self_link = false;
|
||||||
@@ -493,5 +535,6 @@ module.exports = {
|
|||||||
processReplies,
|
processReplies,
|
||||||
processJsonPost,
|
processJsonPost,
|
||||||
finalizeJsonPost,
|
finalizeJsonPost,
|
||||||
|
processJsonPostList,
|
||||||
getPostItem
|
getPostItem
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ module.exports = function () {
|
|||||||
let post = json[0].data.children[0].data;
|
let post = json[0].data.children[0].data;
|
||||||
let comments = json[1].data.children;
|
let comments = json[1].data.children;
|
||||||
|
|
||||||
items += await getPostItem(post, req);
|
items += await getPostItem(post, req, protocol);
|
||||||
|
|
||||||
for (var i = 0; i < comments.length; i++) {
|
for (var i = 0; i < comments.length; i++) {
|
||||||
let comment = comments[i].data;
|
let comment = comments[i].data;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
const processJsonSubreddit = require('../processJsonSubreddit');
|
const processJsonSubreddit = require('../processJsonSubreddit');
|
||||||
const { processJsonSubredditAbout } = require('../processSubredditAbout');
|
const { processJsonSubredditAbout } = require('../processSubredditAbout');
|
||||||
|
const processSearchResults = require('../processSearchResults.js');
|
||||||
|
const { processJsonPostList, getPostItem } = require('../processJsonPost');
|
||||||
|
|
||||||
module.exports = function () {
|
module.exports = function () {
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
@@ -225,4 +227,73 @@ module.exports = function () {
|
|||||||
return res.end(JSON.stringify(subreddit_about));
|
return res.end(JSON.stringify(subreddit_about));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
this.handleTedditApiSubredditSearch = async (
|
||||||
|
json,
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
from,
|
||||||
|
api_type,
|
||||||
|
api_target,
|
||||||
|
subreddit,
|
||||||
|
query,
|
||||||
|
mode
|
||||||
|
) => {
|
||||||
|
if (!config.api_enabled) {
|
||||||
|
res.setHeader('Content-Type', 'application/json');
|
||||||
|
let msg = {
|
||||||
|
info: 'This instance do not support API requests. Please see https://codeberg.org/teddit/teddit#instances for instances that support API, or setup your own instance.',
|
||||||
|
};
|
||||||
|
return res.end(JSON.stringify(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Teddit API request - subreddit search');
|
||||||
|
let _json = json; // Keep the original json
|
||||||
|
if (from === 'redis') json = JSON.parse(json);
|
||||||
|
|
||||||
|
if (api_type === 'rss') {
|
||||||
|
let protocol = config.https_enabled || config.api_force_https ? 'https' : 'http';
|
||||||
|
let items = '';
|
||||||
|
|
||||||
|
for (var i = 0; i < json.data.children.length; i++) {
|
||||||
|
let post = json.data.children[i].data;
|
||||||
|
items += await getPostItem(post, req, protocol);
|
||||||
|
}
|
||||||
|
|
||||||
|
let r_subreddit = '/r/' + subreddit;
|
||||||
|
let title = `${query} - ${r_subreddit}`;
|
||||||
|
let link = `${protocol}://${config.domain}${r_subreddit}/search?q=${query}`;
|
||||||
|
|
||||||
|
let xml_output = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
|
||||||
|
<channel>
|
||||||
|
<atom:link href="${link}&api&type=rss" rel="self" type="application/rss+xml" />
|
||||||
|
<title>${title}</title>
|
||||||
|
<link>${link}</link>
|
||||||
|
<description>Results for: ${query} - ${r_subreddit}</description>
|
||||||
|
${items}
|
||||||
|
</channel>
|
||||||
|
</rss>`;
|
||||||
|
|
||||||
|
res.setHeader('Content-Type', 'application/rss+xml');
|
||||||
|
|
||||||
|
return res.end(xml_output);
|
||||||
|
} else {
|
||||||
|
res.setHeader('Content-Type', 'application/json');
|
||||||
|
if (api_target === 'reddit') {
|
||||||
|
return res.end(JSON.stringify(json));
|
||||||
|
} else {
|
||||||
|
let processed_json = await processSearchResults(
|
||||||
|
json,
|
||||||
|
true,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
req.cookies
|
||||||
|
);
|
||||||
|
|
||||||
|
await processJsonPostList(processed_json.posts, mode);
|
||||||
|
|
||||||
|
return res.end(JSON.stringify(processed_json));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,6 +19,15 @@ const processJsonSubredditsExplore = require('../inc/processSubredditsExplore.js
|
|||||||
subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
||||||
let subreddit = req.params.subreddit;
|
let subreddit = req.params.subreddit;
|
||||||
let q = req.query.q;
|
let q = req.query.q;
|
||||||
|
let api_req = req.query.api;
|
||||||
|
let api_type = req.query.type;
|
||||||
|
let api_target = req.query.target;
|
||||||
|
let api_mode = req.query.mode;
|
||||||
|
|
||||||
|
if (req.query.hasOwnProperty('api')) api_req = true;
|
||||||
|
else api_req = false;
|
||||||
|
|
||||||
|
let raw_json = api_req && req.query.raw_json == '1' ? 1 : 0;
|
||||||
|
|
||||||
if (typeof q === 'undefined') {
|
if (typeof q === 'undefined') {
|
||||||
return res.render('search', {
|
return res.render('search', {
|
||||||
@@ -65,7 +74,7 @@ subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
|||||||
count = '';
|
count = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
let key = `search:${subreddit}:${q}:${restrict_sr}:${sortby}:${past}:${after}:${before}:${nsfw}`;
|
let key = `search:${subreddit}:${q}:${restrict_sr}:${sortby}:${past}:${after}:${before}:${nsfw}:raw_json:${raw_json}`;
|
||||||
redis.get(key, (error, json) => {
|
redis.get(key, (error, json) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error('Error getting the search key from redis.', error);
|
console.error('Error getting the search key from redis.', error);
|
||||||
@@ -78,6 +87,19 @@ subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
|||||||
if (json) {
|
if (json) {
|
||||||
console.log('Got search key from redis.');
|
console.log('Got search key from redis.');
|
||||||
(async () => {
|
(async () => {
|
||||||
|
if (api_req) {
|
||||||
|
return handleTedditApiSubredditSearch(
|
||||||
|
json,
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
'redis',
|
||||||
|
api_type,
|
||||||
|
api_target,
|
||||||
|
subreddit,
|
||||||
|
q,
|
||||||
|
api_mode
|
||||||
|
);
|
||||||
|
} else {
|
||||||
let processed_json = await processSearchResults(
|
let processed_json = await processSearchResults(
|
||||||
json,
|
json,
|
||||||
false,
|
false,
|
||||||
@@ -97,13 +119,14 @@ subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
|||||||
user_preferences: req.cookies,
|
user_preferences: req.cookies,
|
||||||
instance_config: config,
|
instance_config: config,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
} else {
|
} else {
|
||||||
let url = '';
|
let url = '';
|
||||||
if (config.use_reddit_oauth)
|
if (config.use_reddit_oauth)
|
||||||
url = `https://oauth.reddit.com/r/${subreddit}/search?api_type=json&q=${q}&restrict_sr=${restrict_sr}&include_over_18=${nsfw}&sort=${sortby}&t=${past}${count}${d}`;
|
url = `https://oauth.reddit.com/r/${subreddit}/search?api_type=json&q=${q}&restrict_sr=${restrict_sr}&include_over_18=${nsfw}&sort=${sortby}&t=${past}${count}${d}&raw_json=${raw_json}`;
|
||||||
else
|
else
|
||||||
url = `https://reddit.com/r/${subreddit}/search.json?api_type=json&q=${q}&restrict_sr=${restrict_sr}&include_over_18=${nsfw}&sort=${sortby}&t=${past}${count}${d}`;
|
url = `https://reddit.com/r/${subreddit}/search.json?api_type=json&q=${q}&restrict_sr=${restrict_sr}&include_over_18=${nsfw}&sort=${sortby}&t=${past}${count}${d}&raw_json=${raw_json}`;
|
||||||
fetch(encodeURI(url), redditApiGETHeaders())
|
fetch(encodeURI(url), redditApiGETHeaders())
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
if (result.status === 200) {
|
if (result.status === 200) {
|
||||||
@@ -140,6 +163,19 @@ subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
|||||||
} else {
|
} else {
|
||||||
console.log('Fetched search results from Reddit.');
|
console.log('Fetched search results from Reddit.');
|
||||||
(async () => {
|
(async () => {
|
||||||
|
if (api_req) {
|
||||||
|
return handleTedditApiSubredditSearch(
|
||||||
|
json,
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
'from_online',
|
||||||
|
api_type,
|
||||||
|
api_target,
|
||||||
|
subreddit,
|
||||||
|
q,
|
||||||
|
api_mode
|
||||||
|
);
|
||||||
|
} else {
|
||||||
let processed_json = await processSearchResults(
|
let processed_json = await processSearchResults(
|
||||||
json,
|
json,
|
||||||
true,
|
true,
|
||||||
@@ -159,6 +195,7 @@ subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
|||||||
user_preferences: req.cookies,
|
user_preferences: req.cookies,
|
||||||
instance_config: config,
|
instance_config: config,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user