Skip to content

Commit

Permalink
Merge pull request #440 from Rishikant181/dev
Browse files Browse the repository at this point in the history
v2.5.0
  • Loading branch information
Rishikant181 authored Jan 29, 2024
2 parents f05bec2 + 00a28e0 commit 5133b2f
Show file tree
Hide file tree
Showing 40 changed files with 936 additions and 471 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ module.exports = {
selector: ['variableLike', 'memberLike'],
format: ['camelCase'],
},
{
selector: ['variableLike', 'memberLike'],
modifiers: ['static', 'readonly'],
format: ['UPPER_CASE'],
},
{
selector: 'enumMember',
format: ['UPPER_CASE'],
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18.x'
node-version: '20.x'

# Installing dependencies
- run: yarn
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18.x'
node-version: '20.x'
registry-url: https://registry.npmjs.org/

# Installing dependencies
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ dist
.pnp.*

# Test files
.test/
test

# Documentation
.docs
docs
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"bracketSpacing": true,
"endOfLine": "auto",
"printWidth": 120,
"quoteProps": "consistent",
"quoteProps": "as-needed",
"semi": true,
"singleQuote": true,
"tabWidth": 4,
Expand Down
88 changes: 69 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ By default, Rettiwt-API uses 'guest' authentication. If however, access to the f

Here,

- \<email\> is the email of the Twitter account to be used for authentication.
- \<username\> is the username associtated with the Twitter account.
- \<password\> is the password to the Twitter account.
- `<email>` is the email of the Twitter account to be used for authentication.
- `<username>` is the username associated with the Twitter account.
- `<password>` is the password to the Twitter account.

3. The string returned after running the command is the API_KEY. Store it in a secure place for later use.

Expand All @@ -68,7 +68,7 @@ The API_KEY generated by logging in is what allows Rettiwt-API to authenticate a

- If you have no idea of programming, it's recommended to use the CLI.
- The CLI provides an easy to use interface which does not require any knowledge of JavaScript or programming
- Please skip to 'CLI-Usage' section for details.
- Please skip to [CLI-Usage](https://rishikant181.github.io/Rettiwt-API/#md:cli-usage) for details.

## Usage as a dependency

Expand All @@ -88,19 +88,20 @@ For example, for generating the API_KEY, the command will be modified as follows

## The Rettiwt class

When used as a dependency, the Rettiwt class is entry point for accessing the Twitter API.
When used as a dependency, the [Rettiwt](https://rishikant181.github.io/Rettiwt-API/classes/Rettiwt.html) class is entry point for accessing the Twitter API.

A new Rettiwt instance can be initialized using the following code snippets:

- `const rettiwt = new Rettiwt()` (for 'guest' authentication)
- `const rettiwt = new Rettiwt({ apiKey: API_KEY })` (for 'user' authentication)

The Rettiwt class has two members:
The Rettiwt class has three members:

- 'tweet' member, for accessing resources related to tweets
- 'user' member, for accessing resources related to users
- `auth` memeber, for managing authentication
- `tweet` member, for accessing resources related to tweets
- `user` member, for accessing resources related to users

For details regarding usage of these members for accessing the Twitter API, refer to the 'Features' section.
For details regarding usage of these members for accessing the Twitter API, refer to [Features](https://rishikant181.github.io/Rettiwt-API/#md:features).

## Usage

Expand All @@ -109,7 +110,7 @@ The following examples may help you to get started using the library:
### 1. Getting the details of a target Twitter user

```js
const { Rettiwt } = require('rettiwt-api');
import { Rettiwt } from 'rettiwt-api';

// Creating a new Rettiwt instance
// Note that for accessing user details, 'guest' authentication can be used
Expand All @@ -128,7 +129,7 @@ rettiwt.user.details('<username>')
### 2. Getting the list of tweets that match a given filter

```js
const { Rettiwt } = require('rettiwt-api');
import { Rettiwt } from 'rettiwt-api';

// Creating a new Rettiwt instance using the API_KEY
const rettiwt = new Rettiwt({ apiKey: API_KEY });
Expand All @@ -150,12 +151,14 @@ rettiwt.tweet.search({
});
```

For more information regarding the different available filter options, please refer to [TweetFilter](https://rishikant181.github.io/Rettiwt-API/classes/TweetFilter.html).

### 3. Getting the next batch of data using a cursor

The previous example fetches the the list of tweets matching the given filter. Since no count is specified, in this case, a default of 20 such Tweets are fetched initially. The following example demonstrates how to use the [cursor string](https://rishikant181.github.io/Rettiwt-API/classes/Cursor.html#value) obtained from the [response](https://rishikant181.github.io/Rettiwt-API/classes/CursoredData.html) object's [next](https://rishikant181.github.io/Rettiwt-API/classes/CursoredData.html#next) field, from the previous example, to fetch the next batch of tweets:

```js
const { Rettiwt } = require('rettiwt-api');
import { Rettiwt } from 'rettiwt-api';

// Creating a new Rettiwt instance using the API_KEY
const rettiwt = new Rettiwt({ apiKey: API_KEY });
Expand All @@ -179,7 +182,32 @@ rettiwt.tweet.search({
});
```

For more information regarding the different available filter options, please refer to [TweetFilter](https://rishikant181.github.io/Rettiwt-Core/classes/TweetFilter.html).
### 4. Getting an API_KEY during runtime, using 'user' authentication

Sometimes, you might want to generate an API_KEY on the fly, in situations such as implementing Twitter login in your application. The following example demonstrates how to generate an API_KEY during runtime:

```js
import { Rettiwt } from 'rettiwt-api';

// Creating a new Rettiwt instance
const rettiwt = new Rettiwt();

// Logging in an getting the API_KEY
rettiwt.auth.login('<email>', '<username>', '<password>')
.then(apiKey => {
// Use the API_KEY
...
})
.catch(err => {
console.log(err);
});
```

Where,

- `<email>` is the email associated with the Twitter account to be logged into.
- `<username>` is the username associated with the Twitter account.
- `<password>` is the password to the Twitter account.

## Using a proxy

Expand All @@ -189,11 +217,28 @@ For masking of IP address using a proxy server, use the following code snippet f
/**
* PROXY_URL is the URL or configuration for the proxy server you want to use.`
*/
const rettiwt = Rettiwt({ apiKey: API_KEY, proxyUrl: PROXY_URL });
const rettiwt = new Rettiwt({ apiKey: API_KEY, proxyUrl: PROXY_URL });
```

This creates a Rettiwt instance which uses the given proxy server for making requests to Twitter.

## Cloud environment

When using this library in an application deployed in a cloud environment, the library might throw error 429, even when under rate limits. This happens because Twitter's v1.1 API endpoints seemingly blocks access from cloud services' IP ranges. These v1.1 API endpoints are the ones used for authentication and as such, authentication tasks are blocked while deployed on cloud environments.

This issue can be bypassed by using a proxy only for authentication, using the following code snippet for creating a new Rettiwt instance:

`const rettiwt = new Rettiwt({ authProxyUrl: PROXY_URL });`

Where,

- `PROXY_URL` is the URL to the proxy server to use.

Authentication proxy is required only in the following two scenarios:

1. While using 'guest' authentication.
2. While creating API_KEY by 'user' authentication.

## Debug logs

Sometimes, when the library shows unexpected behaviour, for troubleshooting purposes, debug logs can be enabled which will help in tracking down the issue and working on a potential fix. Currently, debug logs are printed to the console and are enabled by setting the 'logging' property of the config to true, while creating an instance of Rettiwt:
Expand All @@ -202,13 +247,18 @@ Sometimes, when the library shows unexpected behaviour, for troubleshooting purp
/**
* By default, is no value for 'logging' is supplied, logging is disabled.
*/
const rettiwt = Rettiwt({ apiKey: API_KEY, logging: true });
const rettiwt = new Rettiwt({ apiKey: API_KEY, logging: true });
```

## Features

So far, the following operations are supported:

### Authentication

- [Logging in as user](https://rishikant181.github.io/Rettiwt-API/classes/AuthService.html#login)
- [Logging in as guest](https://rishikant181.github.io/Rettiwt-API/classes/AuthService.html#guest)

### Tweets

- [Getting the details of a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#details)
Expand All @@ -235,12 +285,12 @@ Rettiwt-API provides an easy to use command-line interface which does not requir

By default, the CLI operates in 'guest' authentication. If you want to use 'user' authentication:

1. Generate an API_KEY as described in 'Authentication' section.
1. Generate an API_KEY as described in [Authentication](https://rishikant181.github.io/Rettiwt-API/#md:authentication).
2. Store the output API_KEY as an environment variable with the name 'API_KEY'.
- Additionaly, store the API_KEY in a file for later use.
- Additionally, store the API_KEY in a file for later use.
- Make sure to generate an API_KEY only once, and use it every time you need it.
3. The CLI automatically reads this environment variable to authenticate against Twitter.
- Additionaly, the API_KEY can also be passed in manually using the '-k' option as follows: `rettiwt -k <API_KEY> <command>`
- Additionally, the API_KEY can also be passed in manually using the '-k' option as follows: `rettiwt -k <API_KEY> <command>`

Help for the CLI can be obtained from the CLI itself:

Expand All @@ -253,5 +303,5 @@ The complete API reference can be found at [this](https://rishikant181.github.io

## Additional information

- This API uses the cookies of a Twitter account to fetch data from Twitter and as such, there is always a chance (altough a measly one) of getting the account banned by Twitter algorithm.
- This API uses the cookies of a Twitter account to fetch data from Twitter and as such, there is always a chance (although a measly one) of getting the account banned by Twitter algorithm.
- There have been no reports of accounts getting banned, but you have been warned, even though the chances of getting banned is negligible, it is not zero!
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rettiwt-api",
"version": "2.4.2",
"version": "2.5.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"description": "An API for fetching data from TwitterAPI, without any rate limits!",
Expand All @@ -12,7 +12,7 @@
"prepare": "tsc",
"format": "prettier --write .",
"lint": "eslint --max-warnings 0 .",
"docs": "typedoc src/index.ts",
"docs": "typedoc --excludePrivate --excludeProtected --excludeInternal src/index.ts",
"debug": "nodemon ./dist/index.js --inspect=0.0.0.0:9229"
},
"repository": {
Expand All @@ -29,11 +29,11 @@
},
"homepage": "https://rishikant181.github.io/Rettiwt-API/",
"dependencies": {
"axios": "1.3.2",
"axios": "1.6.3",
"commander": "11.1.0",
"https-proxy-agent": "7.0.2",
"rettiwt-auth": "2.0.0",
"rettiwt-core": "3.2.1"
"rettiwt-auth": "2.1.0",
"rettiwt-core": "3.3.0"
},
"devDependencies": {
"@types/node": "20.4.1",
Expand Down
15 changes: 10 additions & 5 deletions src/Rettiwt.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// SERVICES
import { AuthService } from './services/public/AuthService';
import { TweetService } from './services/public/TweetService';
import { UserService } from './services/public/UserService';

// MODELS
import { RettiwtConfig } from './models/internal/RettiwtConfig';
// TYPES
import { IRettiwtConfig } from './types/RettiwtConfig';

/**
* The class for accessing Twitter API.
*
* The created Rettiwt instance can be configured by passing in a configuration object to the constructor.
*
* For details regarding the available configuration options, refer to {@link RettiwtConfig}
* For details regarding the available configuration options, refer to {@link IRettiwtConfig}
*
* @example Creating a Rettiwt instance with 'guest' authentication:
* ```
Expand Down Expand Up @@ -41,12 +42,15 @@ import { RettiwtConfig } from './models/internal/RettiwtConfig';
* import { Rettiwt } from 'rettiwt-api';
*
* // Creating a new Rettiwt instance
* const rettiwt = new Rettiwt({ apiKey: 'API_KEY', loggin: true, proxyUrl: 'URL_TO_PROXY_SERVER' });
* const rettiwt = new Rettiwt({ apiKey: 'API_KEY', logging: true, proxyUrl: 'URL_TO_PROXY_SERVER' });
* ```
*
* @public
*/
export class Rettiwt {
/** The instance used to authenticate. */
public auth: AuthService;

/** The instance used to fetch data related to tweets. */
public tweet: TweetService;

Expand All @@ -58,7 +62,8 @@ export class Rettiwt {
*
* @param config - The config object for configuring the Rettiwt instance.
*/
public constructor(config?: RettiwtConfig) {
public constructor(config?: IRettiwtConfig) {
this.auth = new AuthService(config);
this.tweet = new TweetService(config);
this.user = new UserService(config);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const rettiwt: Rettiwt = new Rettiwt({
// Adding sub-commands
program.addCommand(tweet(rettiwt));
program.addCommand(user(rettiwt));
program.addCommand(auth());
program.addCommand(auth(rettiwt));

// Finalizing the CLI
program.parse();
21 changes: 5 additions & 16 deletions src/commands/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// PACKAGES
import { Command, createCommand } from 'commander';
import { Auth } from 'rettiwt-auth';
import { Rettiwt } from '../Rettiwt';

// UTILITY
import { output } from '../helper/CliUtils';

function createAuthCommand(): Command {
function createAuthCommand(rettiwt: Rettiwt): Command {
// Creating the 'auth' command
const auth = createCommand('auth').description('Manage authentication');

Expand All @@ -16,26 +16,15 @@ function createAuthCommand(): Command {
.argument('<username>', 'The username associated with the Twitter account')
.argument('<password>', 'The password to the Twitter account')
.action(async (email: string, username: string, password: string) => {
// Logging in and getting the credentials
let apiKey: string =
(
await new Auth().getUserCredential({ email: email, userName: username, password: password })
).toHeader().cookie ?? '';

// Converting the credentials to base64 string
apiKey = Buffer.from(apiKey).toString('base64');
const apiKey: string = await rettiwt.auth.login(email, username, password);
output(apiKey);
});

// Guest
auth.command('guest')
.description('Generate a new guest API key')
.description('Generate a new guest key')
.action(async () => {
// Getting a new guest API key
let guestKey: string = (await new Auth().getGuestCredential()).guestToken ?? '';

// Converting the credentials to base64 string
guestKey = Buffer.from(guestKey).toString('base64');
const guestKey: string = await rettiwt.auth.guest();
output(guestKey);
});

Expand Down
Loading

0 comments on commit 5133b2f

Please sign in to comment.