Skip to content

Commit

Permalink
feat(twitter): only display tweets that contains a media (#17310)
Browse files Browse the repository at this point in the history
* feat: only display tweets that contains a media

* Use lambda to simplify filtering

* wording: media is uncountable

---------
  • Loading branch information
Rakambda authored Oct 26, 2024
1 parent b4217bf commit a71410f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/routes/twitter/home-latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ 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();
let data = await api.getHomeLatestTimeline('', params);
if (!include_rts) {
data = utils.excludeRetweet(data);
}
if (only_media) {
data = utils.keepOnlyMedia(data);
}

return {
title: `Twitter following timeline`,
Expand Down
5 changes: 4 additions & 1 deletion lib/routes/twitter/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ 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();
let data = await api.getHomeTimeline('', params);
if (!include_rts) {
data = utils.excludeRetweet(data);
}
if (only_media) {
data = utils.keepOnlyMedia(data);
}

return {
title: `Twitter following timeline`,
Expand Down
5 changes: 4 additions & 1 deletion lib/routes/twitter/likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ 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();
let data = await api.getUserLikes(id, params);
if (!include_rts) {
data = utils.excludeRetweet(data);
}
if (only_media) {
data = utils.keepOnlyMedia(data);
}

return {
title: `Twitter Likes - ${id}`,
Expand Down
5 changes: 4 additions & 1 deletion lib/routes/twitter/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ 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();
let data = await api.getList(id, params);
if (!include_rts) {
data = utils.excludeRetweet(data);
}
if (only_media) {
data = utils.keepOnlyMedia(data);
}

return {
title: `Twitter List - ${id}`,
Expand Down
1 change: 1 addition & 0 deletions lib/routes/twitter/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/routes/twitter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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) {
Expand All @@ -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 };

0 comments on commit a71410f

Please sign in to comment.