feat(twitter): only display tweets that contains a media (#17310)

* feat: only display tweets that contains a media

* Use lambda to simplify filtering

* wording: media is uncountable

---------
This commit is contained in:
Thomas 2024-10-26 20:56:48 +02:00 committed by GitHub
parent b4217bf892
commit a71410fdd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 26 additions and 7 deletions

View File

@ -40,7 +40,7 @@ export const route: Route = {
async function handler(ctx) {
// For compatibility
const { count, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
const { count, include_rts, only_media } = utils.parseRouteParams(ctx.req.param('routeParams'));
const params = count ? { count } : {};
await api.init();
@ -48,6 +48,9 @@ async function handler(ctx) {
if (!include_rts) {
data = utils.excludeRetweet(data);
}
if (only_media) {
data = utils.keepOnlyMedia(data);
}
return {
title: `Twitter following timeline`,

View File

@ -40,7 +40,7 @@ export const route: Route = {
async function handler(ctx) {
// For compatibility
const { count, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
const { count, include_rts, only_media } = utils.parseRouteParams(ctx.req.param('routeParams'));
const params = count ? { count } : {};
await api.init();
@ -48,6 +48,9 @@ async function handler(ctx) {
if (!include_rts) {
data = utils.excludeRetweet(data);
}
if (only_media) {
data = utils.keepOnlyMedia(data);
}
return {
title: `Twitter following timeline`,

View File

@ -27,7 +27,7 @@ export const route: Route = {
async function handler(ctx) {
const id = ctx.req.param('id');
const { count, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
const { count, include_rts, only_media } = utils.parseRouteParams(ctx.req.param('routeParams'));
const params = count ? { count } : {};
await api.init();
@ -35,6 +35,9 @@ async function handler(ctx) {
if (!include_rts) {
data = utils.excludeRetweet(data);
}
if (only_media) {
data = utils.keepOnlyMedia(data);
}
return {
title: `Twitter Likes - ${id}`,

View File

@ -33,7 +33,7 @@ export const route: Route = {
async function handler(ctx) {
const id = ctx.req.param('id');
const { count, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
const { count, include_rts, only_media } = utils.parseRouteParams(ctx.req.param('routeParams'));
const params = count ? { count } : {};
await api.init();
@ -41,6 +41,9 @@ async function handler(ctx) {
if (!include_rts) {
data = utils.excludeRetweet(data);
}
if (only_media) {
data = utils.keepOnlyMedia(data);
}
return {
title: `Twitter List - ${id}`,

View File

@ -27,6 +27,7 @@ export const namespace: Namespace = {
| \`includeRts\` | Include retweets, only available in \`/twitter/user\` | \`0\`/\`1\`/\`true\`/\`false\` | \`true\` |
| \`forceWebApi\` | Force using Web API even if Developer API is configured, only available in \`/twitter/user\` and \`/twitter/keyword\` | \`0\`/\`1\`/\`true\`/\`false\` | \`false\` |
| \`count\` | \`count\` parameter passed to Twitter API, only available in \`/twitter/user\` | Unspecified/Integer | Unspecified |
| \`onlyMedia\` | Only get tweets with a media | \`0\`/\`1\`/\`true\`/\`false\` | \`false\` |
Specify different option values than default values to improve readability. The URL

View File

@ -451,7 +451,7 @@ if (config.twitter.consumer_key && config.twitter.consumer_secret) {
}
const parseRouteParams = (routeParams) => {
let count, exclude_replies, include_rts;
let count, exclude_replies, include_rts, only_media;
let force_web_api = false;
switch (routeParams) {
case 'exclude_rts_replies':
@ -479,9 +479,10 @@ const parseRouteParams = (routeParams) => {
exclude_replies = fallback(undefined, queryToBoolean(parsed.get('excludeReplies')), false);
include_rts = fallback(undefined, queryToBoolean(parsed.get('includeRts')), true);
force_web_api = fallback(undefined, queryToBoolean(parsed.get('forceWebApi')), false);
only_media = fallback(undefined, queryToBoolean(parsed.get('onlyMedia')), false);
}
}
return { count, exclude_replies, include_rts, force_web_api };
return { count, exclude_replies, include_rts, force_web_api, only_media };
};
export const excludeRetweet = function (tweets) {
@ -495,4 +496,9 @@ export const excludeRetweet = function (tweets) {
return excluded;
};
export default { ProcessFeed, getAppClient, parseRouteParams, excludeRetweet };
export const keepOnlyMedia = function (tweets) {
const excluded = tweets.filter((t) => t.extended_entities && t.extended_entities.media);
return excluded;
};
export default { ProcessFeed, getAppClient, parseRouteParams, excludeRetweet, keepOnlyMedia };