Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #45 from junhoyeo/junhoyeo/bump-docs
Browse files Browse the repository at this point in the history
[api][docs] Add Usage for Writing Threads in README.md, Fix naming
  • Loading branch information
junhoyeo authored Jul 8, 2023
2 parents 66d7777 + 94b61c7 commit 5c09df8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</p>

<details>
<summary><h3>🚀 Usage</h3></summary>
<summary><h3>🚀 Usage (Read)</h3></summary>

```ts
import { ThreadsAPI } from 'threads-api';
Expand Down Expand Up @@ -56,6 +56,35 @@ main();

</details>

### 🚀 Usage (Write)

```ts
import { ThreadsAPI } from 'threads-api';

const main = async () => {
const threadsAPI = new ThreadsAPI({
username: 'jamel.hammoud', // Your username
password: 'PASSWORD', // Your password
});

await threadsAPI.publish('Hello World');
};

main();
```

You can also provide custom `deviceID` (Default is `android-${(Math.random() * 1e24).toString(36)}`).

```ts
const deviceID = `android-${(Math.random() * 1e24).toString(36)}`;

const threadsAPI = new ThreadsAPI({
username: 'jamel.hammoud',
password: 'PASSWORD',
deviceID,
});
```

<details>
<summary>
<h3>📑 Outputs</h3>
Expand Down Expand Up @@ -595,7 +624,10 @@ import { ThreadsAPI } from 'npm:threads-api';
- [ ] 🚧 Read User Followers
- [ ] 🚧 Read User Followings
- [ ] 🚧 Read private data
- [ ] 🚧 Write data (i.e. write automated Threads)
- [x] ✅ Write data (i.e. write automated Threads)
- [x] ✅ Create new Thread with text
- [ ] 🚧 Create new Thread with media
- [ ] 🚧 Reply to existing Thread
- [x] 🏴‍☠️ Restructure project as an monorepo
- [x] 🏴‍☠ Add Demo App with Next.js
- [ ] Use components in 🏴‍☠️ [junhoyeo/react-threads](https://github.com/junhoyeo/react-threads)
Expand Down
38 changes: 19 additions & 19 deletions threads-api/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,39 @@ program
.alias('id')
.description('get user ID from username')
.action(async (username: string) => {
const userId = await Threads.getUserIDfromUsername(username, { timeout: 10000 });
console.log(`User ID for ${username}: ${userId}`);
const userID = await Threads.getUserIDfromUsername(username, { timeout: 10000 });
console.log(`User ID for ${username}: ${userID}`);
});

program
.command('getUserProfile <username> <userId> [stringify]')
.command('getUserProfile <username> <userID> [stringify]')
.alias('userprofile')
.alias('uprof')
.alias('up')
.description('get user profile')
.action(async (username: string, userId: string, stringify?: boolean) => {
const userProfile = await Threads.getUserProfile(username, userId, { timeout: 10000 });
.action(async (username: string, userID: string, stringify?: boolean) => {
const userProfile = await Threads.getUserProfile(username, userID, { timeout: 10000 });
console.log(stringify ? JSON.stringify(userProfile, null, 5) : userProfile);
});

program
.command('getUserProfileThreads <username> <userId> [stringify]')
.command('getUserProfileThreads <username> <userID> [stringify]')
.alias('uthreads')
.alias('ut')
.description('get user profile threads')
.action(async (username: string, userId: string, stringify?: boolean) => {
const userProfileThreads = await Threads.getUserProfileThreads(username, userId, { timeout: 10000 });
.action(async (username: string, userID: string, stringify?: boolean) => {
const userProfileThreads = await Threads.getUserProfileThreads(username, userID, { timeout: 10000 });
console.log(stringify ? JSON.stringify(userProfileThreads, null, 5) : userProfileThreads);
});

program
.command('getUserProfileReplies <username> <userId> [stringify]')
.command('getUserProfileReplies <username> <userID> [stringify]')
.alias('userreplies')
.alias('ureplies')
.alias('ur')
.description('get user profile replies')
.action(async (username: string, userId: string, stringify?: boolean) => {
const userProfileReplies = await Threads.getUserProfileReplies(username, userId, { timeout: 10000 });
.action(async (username: string, userID: string, stringify?: boolean) => {
const userProfileReplies = await Threads.getUserProfileReplies(username, userID, { timeout: 10000 });
console.log(stringify ? JSON.stringify(userProfileReplies, null, 5) : userProfileReplies);
});

Expand All @@ -63,28 +63,28 @@ program
.alias('p')
.description('get post ID from URL')
.action(async (postURL: string) => {
const postId = await Threads.getPostIDfromURL(postURL, { timeout: 10000 });
console.log(`Post ID for ${postURL}: ${postId}`);
const postID = await Threads.getPostIDfromURL(postURL, { timeout: 10000 });
console.log(`Post ID for ${postURL}: ${postID}`);
});

program
.command('getThreads <postId> [stringify]')
.command('getThreads <postID> [stringify]')
.alias('threads')
.alias('t')
.description('get threads')
.action(async (postId: string, stringify?: boolean) => {
const threads = await Threads.getThreads(postId, { timeout: 10000 });
.action(async (postID: string, stringify?: boolean) => {
const threads = await Threads.getThreads(postID, { timeout: 10000 });
console.log(threads, stringify ? JSON.stringify(threads, null, 5) : threads);
});

program
.command('getThreadLikers <postId> [stringify]')
.command('getThreadLikers <postID> [stringify]')
.alias('threadlikers')
.alias('likers')
.alias('l')
.description('get thread likers')
.action(async (postId: string, stringify?: boolean) => {
const threadLikers = await Threads.getThreadLikers(postId, { timeout: 10000 });
.action(async (postID: string, stringify?: boolean) => {
const threadLikers = await Threads.getThreadLikers(postID, { timeout: 10000 });
console.log(stringify ? JSON.stringify(threadLikers, null, 5) : threadLikers);
});

Expand Down
16 changes: 8 additions & 8 deletions threads-api/src/threads-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export type GetThreadLikersResponse = {
type HTTPAgentType = typeof import('http').Agent;
export type ThreadsAPIOptions = {
fbLSDToken?: string;
deviceId?: string;
deviceID?: string;
verbose?: boolean;
noUpdateLSD?: boolean;
httpAgent?: HTTPAgentType;
Expand All @@ -63,7 +63,7 @@ export const DEFAULT_DEVICE_ID = `android-${(Math.random() * 1e24).toString(36)}

export class ThreadsAPI {
fbLSDToken: string = DEFAULT_LSD_TOKEN;
deviceId: string = DEFAULT_DEVICE_ID;
deviceID: string = DEFAULT_DEVICE_ID;
verbose: boolean = false;
noUpdateLSD: boolean = false;
httpAgent?: HTTPAgentType;
Expand All @@ -72,7 +72,7 @@ export class ThreadsAPI {

constructor(options?: ThreadsAPIOptions) {
if (options?.fbLSDToken) this.fbLSDToken = options.fbLSDToken;
if (options?.deviceId) this.deviceId = options.deviceId;
if (options?.deviceID) this.deviceID = options.deviceID;
if (options?.noUpdateLSD) this.noUpdateLSD = options.noUpdateLSD;
this.verbose = options?.verbose || false;
this.httpAgent = options?.httpAgent;
Expand Down Expand Up @@ -311,11 +311,11 @@ export class ThreadsAPI {
client_input_params: {
password: this.password,
contact_point: this.username,
device_id: `${this.deviceId}`,
device_id: `${this.deviceID}`,
},
server_params: {
credential_type: 'password',
device_id: `${this.deviceId}`,
device_id: `${this.deviceID}`,
},
}),
);
Expand Down Expand Up @@ -352,7 +352,7 @@ export class ThreadsAPI {
}

const token = await this.getToken();
const userId = await this.getUserIDfromUsername(this.username);
const userID = await this.getUserIDfromUsername(this.username);

if (!token) {
return false;
Expand All @@ -367,8 +367,8 @@ export class ThreadsAPI {
text_post_app_info: '{"reply_control":0}',
timezone_offset: '-25200',
source_type: '4',
_uid: userId,
device_id: `${this.deviceId}`,
_uid: userID,
device_id: `${this.deviceID}`,
caption,
upload_id: new Date().getTime(),
device: {
Expand Down

0 comments on commit 5c09df8

Please sign in to comment.