Skip to content

Commit

Permalink
Add user threads fetch function
Browse files Browse the repository at this point in the history
  • Loading branch information
dvque committed Jan 31, 2024
1 parent dfb54ef commit e9de633
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export const GRAPHQL_ENDPOINT = 'https://www.threads.net/api/graphql';
export const THREADS_APP_ID = '238260118697367';

export const ENDPOINTS_DOCUMENT_ID = {
USER_THREADS: '6451898791498605',
USER_PROFILE: '23996318473300828'
}
2 changes: 2 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const IS_DEBUG = Boolean(Bun.env.DEBUG)
// export const IS_DEBUG = Boolean(false)
19 changes: 19 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ENDPOINTS_DOCUMENT_ID, GRAPHQL_ENDPOINT, THREADS_APP_ID } from "./consts";
import { IS_DEBUG } from "./env";

const fetchBase = ({ documentId, variables } : {documentId: string, variables: string} ) => {
return fetch (GRAPHQL_ENDPOINT, {
Expand All @@ -14,7 +15,25 @@ const fetchBase = ({ documentId, variables } : {documentId: string, variables: s
.then(res => res.json())
}

export const fetchUserIdByName = ({ userName }: { userName: string }) => {
if(IS_DEBUG) console.info(`https://www.threads.net/@${userName}`)

return fetch(`https://www.threads.net/@${userName}`)
.then(res => res.text())
.then(html => {
// "props":{"user_id":"8242141302"}
const regex = /"user_id":"(\d+)"/g;
const [[,userId]] = html.matchAll(regex) ?? [];
return userId
})
}

export const fetchUserProfile = ({ userId }: { userId: string }) => {
const variables = JSON.stringify({ userID: userId });
return fetchBase({ documentId: ENDPOINTS_DOCUMENT_ID.USER_PROFILE, variables })
}

export const fetchUserThreads = ({ userId }: {userId: string}) => {
const variables = JSON.stringify({ userID: userId });
return fetchBase({ documentId: ENDPOINTS_DOCUMENT_ID.USER_THREADS, variables })
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { fetchUserProfile } from "./fetch";
import { fetchUserIdByName, fetchUserProfile, fetchUserThreads } from "./fetch";

const midudevId = '8242141302';

const response = await fetchUserProfile({ userId: midudevId });
const response = await fetchUserIdByName({ userName: 'midu.dev' });
console.log(response);

0 comments on commit e9de633

Please sign in to comment.