Skip to content

Commit

Permalink
Addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ninboy committed Jul 7, 2023
1 parent 847f0bf commit 9d0e392
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 39 deletions.
9 changes: 5 additions & 4 deletions docs/en/social-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,16 @@ Due to Telegram restrictions, some channels involving pornography, copyright, an

### User timeline

<RouteEn author="ninboy" path="/threads/:user/:routeParams?" example="/threads/zuck" radar="1" rssbud="1"/>
cd .
<RouteEn author="ninboy" path="/threads/:user/:routeParams?" example="/threads/zuck" radar="1" rssbud="1">

Specify options (in the format of query string) in parameter `routeParams` to control some extra features for threads

| Key | Description | Accepts | Defaults to |
|---------------------------------|--------------------------------------------------------------------------------------------------------------------------------| ---------------------- |-------------|
| `showAuthorInTitle` | Show author name in title | `0`/`1`/`true`/`false` | `true` |
| `showAuthorInDesc` | Show author name in description (RSS body) | `0`/`1`/`true`/`false` | `true` |
| `showQuotedAuthorAvatarInDesc` | Show avatar of quoted author in description (RSS body) (Not recommended if your RSS reader extracts images from description) | `0`/`1`/`true`/`false` | `true` |
| `showAuthorAvatarInDesc` | Show avatar of author in description (RSS body) (Not recommended if your RSS reader extracts images from description) | `0`/`1`/`true`/`false` | `true` |
| `showQuotedAuthorAvatarInDesc` | Show avatar of quoted author in description (RSS body) (Not recommended if your RSS reader extracts images from description) | `0`/`1`/`true`/`false` | `false` |
| `showAuthorAvatarInDesc` | Show avatar of author in description (RSS body) (Not recommended if your RSS reader extracts images from description) | `0`/`1`/`true`/`false` | `falseP` |
| `showEmojiForQuotesAndReply` | Use "🔁" instead of "QT", "↩️" instead of "Re" | `0`/`1`/`true`/`false` | `true` |
| `showQuotedInTitle` | Show quoted tweet in title | `0`/`1`/`true`/`false` | `true` |
| `replies` | Show replies | `0`/`1`/`true`/`false` | `true` |
Expand All @@ -463,6 +463,7 @@ Specify different option values than default values to improve readability. The
```
https://rsshub.app/threads/zuck/showAuthorInTitle=1&showAuthorInDesc=1&showQuotedAuthorAvatarInDesc=1&showAuthorAvatarInDesc=1&showEmojiForQuotesAndReply=1&showQuotedInTitle=1
```
</RouteEn>

## TikTok

Expand Down
50 changes: 17 additions & 33 deletions lib/v2/threads/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const threadUrl = (code) => `https://www.threads.net/t/${code}`;
const apiUrl = 'https://www.threads.net/api/graphql';
const THREADS_QUERY = 6232751443445612;
const REPLIES_QUERY = 6307072669391286;
const USER_AGENT = 'Barcelona 289.0.0.77.109 Android';

const load = async (url) => {
// user id fetching needs puppeteer
Expand All @@ -21,16 +22,13 @@ const load = async (url) => {
waitUntil: 'domcontentloaded',
});
const response = await page.content();
page.close();
browser.close();

return cheerio.load(response);
};

const extractTokens = async (user, ctx) => {
const $ = await load(profileUrl(user));
const appId = $('script:contains("APP_ID"):first')
.html()
.match(/,"APP_ID":"(\d+)",/)?.[1];
const lsd = $('script:contains("LSD"):first')
.html()
.match(/"LSD",\[],\{"token":"([a-zA-Z0-9@_-]+)"},/)?.[1];
Expand All @@ -40,32 +38,20 @@ const extractTokens = async (user, ctx) => {
.html()
.match(/"user_id":"(\d+)"/)?.[1];

ctx.state.json = { appId, lsd, userId };
return { appId, lsd, userId };
ctx.state.json = { lsd, userId };
return { lsd, userId };
};

const makeHeader = (user, appId, lsd) => ({
const makeHeader = (user, lsd) => ({
Accept: 'application/json',
Host: 'www.threads.net',
'Content-Type': 'application/x-www-form-urlencoded',
Origin: 'https://www.threads.net',
Referer: `https://www.threads.net/@${user}`,
'User-Agent': 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/83.0.4103.122 Safari/537.36',
'User-Agent': USER_AGENT,
'X-FB-LSD': lsd,
'X-IG-App-ID': appId,
'X-IG-App-ID': '238260118697367',
});

const makeBody = (body) => {
let output = '';
Object.entries(body).forEach(([key, value], index) => {
output += `${key}=${value}`;
if (index < Object.keys(body).length - 1) {
output += '&';
}
});
return output;
};

const hasMedia = (post) => post.image_versions2 || post.carousel_media || post.video_versions;
const buildMedia = (post) => {
let html = '';
Expand Down Expand Up @@ -158,39 +144,37 @@ const buildContent = (item, options) => {

module.exports = async (ctx) => {
const { user, routeParams } = ctx.params;
const { appId, lsd, userId } = await extractTokens(user, ctx);
const { lsd, userId } = await extractTokens(user, ctx);

const params = new URLSearchParams(routeParams);
ctx.state.json.params = routeParams;

const options = {
showAuthorInTitle: params.get('showAuthorInTitle') ?? true,
showAuthorInDesc: params.get('showAuthorInDesc') ?? true,
showAuthorAvatarInDesc: params.get('showAuthorAvatarInDesc') ?? true,
showAuthorAvatarInDesc: params.get('showAuthorAvatarInDesc') ?? false,
showQuotedInTitle: params.get('showQuotedInTitle') ?? true,
showQuotedAuthorAvatarInDesc: params.get('showQuotedAuthorAvatarInDesc') ?? true,
showQuotedAuthorAvatarInDesc: params.get('showQuotedAuthorAvatarInDesc') ?? false,
showEmojiForQuotesAndReply: params.get('showEmojiForQuotesAndReply') ?? true,
replies: params.get('replies') ?? false,
};

const headers = makeHeader(user, appId, lsd);
const body = makeBody({
lsd,
variables: encodeURIComponent(JSON.stringify({ userID: userId })),
doc_id: options.replies ? REPLIES_QUERY : THREADS_QUERY,
});

const headers = makeHeader(user, lsd);
const resp = await got.post(apiUrl, {
headers,
body,
form: {
lsd,
variables: JSON.stringify({ userID: userId }),
doc_id: options.replies ? REPLIES_QUERY : THREADS_QUERY,
},
});

ctx.state.json.request = {
headers: resp.request.options.headers,
body: resp.request.options.body,
};

const responseBody = JSON.parse(resp.body);
const responseBody = resp.data;
const threads = responseBody?.data?.mediaData?.threads || [];

const items = threads.flatMap((thread) =>
Expand Down
2 changes: 1 addition & 1 deletion lib/v2/threads/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
'/:user?': ['ninboy'],
'/:user/:routeParams?': ['ninboy'],
};
2 changes: 1 addition & 1 deletion lib/v2/threads/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
{
title: 'Threads',
docs: 'https://docs.rsshub.app/social-media.html#threads',
source: ['/:user'],
source: ['/@:user'],
target: '/threads/:user',
},
],
Expand Down

0 comments on commit 9d0e392

Please sign in to comment.