diff --git a/src/consts.ts b/src/consts.ts index 4f193ca..8fa8f41 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -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' } \ No newline at end of file diff --git a/src/env.ts b/src/env.ts new file mode 100644 index 0000000..0401466 --- /dev/null +++ b/src/env.ts @@ -0,0 +1,2 @@ +export const IS_DEBUG = Boolean(Bun.env.DEBUG) +// export const IS_DEBUG = Boolean(false) \ No newline at end of file diff --git a/src/fetch.ts b/src/fetch.ts index 07aee88..4e19296 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -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, { @@ -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 }) } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a15eb98..5f1e03b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); \ No newline at end of file