@glagan/rettiwt-api 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/.eslintrc.js +166 -0
  2. package/.gitattributes +3 -0
  3. package/.github/FUNDING.yml +4 -0
  4. package/.github/ISSUE_TEMPLATE/bug-report.yml +57 -0
  5. package/.github/ISSUE_TEMPLATE/feature-request.yml +20 -0
  6. package/.github/ISSUE_TEMPLATE/question.yml +15 -0
  7. package/.github/PULL_REQUEST_TEMPLATE.md +32 -0
  8. package/.github/workflows/ci.yml +32 -0
  9. package/.github/workflows/publish.yml +23 -0
  10. package/.nvmrc +1 -0
  11. package/.prettierignore +3 -0
  12. package/.prettierrc +13 -0
  13. package/LICENSE +21 -0
  14. package/README.md +566 -0
  15. package/dist/cli.js +43 -0
  16. package/eslint.config.mjs +17 -0
  17. package/package.json +50 -0
  18. package/src/Rettiwt.ts +97 -0
  19. package/src/cli.ts +48 -0
  20. package/src/collections/Extractors.ts +155 -0
  21. package/src/collections/Groups.ts +81 -0
  22. package/src/collections/Requests.ts +89 -0
  23. package/src/collections/Tweet.ts +17 -0
  24. package/src/commands/DirectMessage.ts +62 -0
  25. package/src/commands/List.ts +90 -0
  26. package/src/commands/Tweet.ts +437 -0
  27. package/src/commands/User.ts +367 -0
  28. package/src/enums/Api.ts +10 -0
  29. package/src/enums/Authentication.ts +10 -0
  30. package/src/enums/Data.ts +13 -0
  31. package/src/enums/Logging.ts +14 -0
  32. package/src/enums/Media.ts +10 -0
  33. package/src/enums/Notification.ts +12 -0
  34. package/src/enums/Resource.ts +69 -0
  35. package/src/enums/Tweet.ts +8 -0
  36. package/src/enums/raw/Analytics.ts +32 -0
  37. package/src/enums/raw/Media.ts +10 -0
  38. package/src/enums/raw/Notification.ts +11 -0
  39. package/src/enums/raw/Tweet.ts +20 -0
  40. package/src/helper/CliUtils.ts +17 -0
  41. package/src/helper/JsonUtils.ts +70 -0
  42. package/src/index.ts +128 -0
  43. package/src/models/RettiwtConfig.ts +101 -0
  44. package/src/models/args/FetchArgs.ts +169 -0
  45. package/src/models/args/PostArgs.ts +93 -0
  46. package/src/models/args/ProfileArgs.ts +68 -0
  47. package/src/models/auth/AuthCookie.ts +58 -0
  48. package/src/models/auth/AuthCredential.ts +83 -0
  49. package/src/models/data/Analytics.ts +97 -0
  50. package/src/models/data/BookmarkFolder.ts +73 -0
  51. package/src/models/data/Conversation.ts +344 -0
  52. package/src/models/data/CursoredData.ts +64 -0
  53. package/src/models/data/DirectMessage.ts +335 -0
  54. package/src/models/data/Inbox.ts +124 -0
  55. package/src/models/data/List.ts +113 -0
  56. package/src/models/data/Notification.ts +84 -0
  57. package/src/models/data/Tweet.ts +388 -0
  58. package/src/models/data/User.ts +187 -0
  59. package/src/models/errors/TwitterError.ts +65 -0
  60. package/src/models/params/Variables.ts +62 -0
  61. package/src/requests/DirectMessage.ts +229 -0
  62. package/src/requests/List.ts +203 -0
  63. package/src/requests/Media.ts +67 -0
  64. package/src/requests/Tweet.ts +607 -0
  65. package/src/requests/User.ts +1191 -0
  66. package/src/services/internal/AuthService.ts +115 -0
  67. package/src/services/internal/ErrorService.ts +41 -0
  68. package/src/services/internal/LogService.ts +34 -0
  69. package/src/services/public/DirectMessageService.ts +159 -0
  70. package/src/services/public/FetcherService.ts +366 -0
  71. package/src/services/public/ListService.ts +241 -0
  72. package/src/services/public/TweetService.ts +886 -0
  73. package/src/services/public/UserService.ts +1154 -0
  74. package/src/types/ErrorHandler.ts +13 -0
  75. package/src/types/Fetch.ts +3 -0
  76. package/src/types/RettiwtConfig.ts +48 -0
  77. package/src/types/args/FetchArgs.ts +233 -0
  78. package/src/types/args/PostArgs.ts +142 -0
  79. package/src/types/args/ProfileArgs.ts +33 -0
  80. package/src/types/auth/AuthCookie.ts +22 -0
  81. package/src/types/auth/AuthCredential.ts +28 -0
  82. package/src/types/auth/TransactionHeader.ts +8 -0
  83. package/src/types/data/Analytics.ts +58 -0
  84. package/src/types/data/BookmarkFolder.ts +12 -0
  85. package/src/types/data/Conversation.ts +44 -0
  86. package/src/types/data/CursoredData.ts +24 -0
  87. package/src/types/data/DirectMessage.ts +33 -0
  88. package/src/types/data/Inbox.ts +23 -0
  89. package/src/types/data/List.ts +33 -0
  90. package/src/types/data/Notification.ts +26 -0
  91. package/src/types/data/Tweet.ts +99 -0
  92. package/src/types/data/User.ts +54 -0
  93. package/src/types/errors/TwitterError.ts +37 -0
  94. package/src/types/params/Variables.ts +41 -0
  95. package/src/types/raw/base/Analytic.ts +32 -0
  96. package/src/types/raw/base/BookmarkFolder.ts +12 -0
  97. package/src/types/raw/base/Cursor.ts +13 -0
  98. package/src/types/raw/base/Error.ts +38 -0
  99. package/src/types/raw/base/LimitedVisibilityTweet.ts +40 -0
  100. package/src/types/raw/base/List.ts +50 -0
  101. package/src/types/raw/base/Media.ts +53 -0
  102. package/src/types/raw/base/Message.ts +22 -0
  103. package/src/types/raw/base/Notification.ts +66 -0
  104. package/src/types/raw/base/Space.ts +35 -0
  105. package/src/types/raw/base/Tweet.ts +139 -0
  106. package/src/types/raw/base/User.ts +182 -0
  107. package/src/types/raw/composite/DataResult.ts +8 -0
  108. package/src/types/raw/composite/TimelineList.ts +10 -0
  109. package/src/types/raw/composite/TimelineTweet.ts +14 -0
  110. package/src/types/raw/composite/TimelineUser.ts +13 -0
  111. package/src/types/raw/dm/Conversation.ts +59 -0
  112. package/src/types/raw/dm/InboxInitial.ts +155 -0
  113. package/src/types/raw/dm/InboxTimeline.ts +301 -0
  114. package/src/types/raw/dm/UserUpdates.ts +46 -0
  115. package/src/types/raw/generic/Response.ts +10 -0
  116. package/src/types/raw/list/AddMember.ts +175 -0
  117. package/src/types/raw/list/Details.ts +176 -0
  118. package/src/types/raw/list/Members.ts +154 -0
  119. package/src/types/raw/list/RemoveMember.ts +174 -0
  120. package/src/types/raw/list/Tweets.ts +2296 -0
  121. package/src/types/raw/media/FinalizeUpload.ts +20 -0
  122. package/src/types/raw/media/InitalizeUpload.ts +12 -0
  123. package/src/types/raw/media/LiveVideoStream.ts +21 -0
  124. package/src/types/raw/space/Details.ts +359 -0
  125. package/src/types/raw/tweet/Bookmark.ts +14 -0
  126. package/src/types/raw/tweet/Details.ts +210 -0
  127. package/src/types/raw/tweet/DetailsBulk.ts +338 -0
  128. package/src/types/raw/tweet/Like.ts +14 -0
  129. package/src/types/raw/tweet/Likers.ts +200 -0
  130. package/src/types/raw/tweet/Post.ts +150 -0
  131. package/src/types/raw/tweet/Replies.ts +539 -0
  132. package/src/types/raw/tweet/Retweet.ts +31 -0
  133. package/src/types/raw/tweet/Retweeters.ts +208 -0
  134. package/src/types/raw/tweet/Schedule.ts +18 -0
  135. package/src/types/raw/tweet/Search.ts +597 -0
  136. package/src/types/raw/tweet/Unbookmark.ts +14 -0
  137. package/src/types/raw/tweet/Unlike.ts +14 -0
  138. package/src/types/raw/tweet/Unpost.ts +20 -0
  139. package/src/types/raw/tweet/Unretweet.ts +31 -0
  140. package/src/types/raw/tweet/Unschedule.ts +14 -0
  141. package/src/types/raw/user/Affiliates.ts +179 -0
  142. package/src/types/raw/user/Analytics.ts +23 -0
  143. package/src/types/raw/user/BookmarkFolderTweets.ts +53 -0
  144. package/src/types/raw/user/BookmarkFolders.ts +41 -0
  145. package/src/types/raw/user/Bookmarks.ts +637 -0
  146. package/src/types/raw/user/Details.ts +185 -0
  147. package/src/types/raw/user/DetailsBulk.ts +104 -0
  148. package/src/types/raw/user/Follow.ts +280 -0
  149. package/src/types/raw/user/Followed.ts +1942 -0
  150. package/src/types/raw/user/Followers.ts +215 -0
  151. package/src/types/raw/user/Following.ts +215 -0
  152. package/src/types/raw/user/Highlights.ts +1287 -0
  153. package/src/types/raw/user/Likes.ts +1254 -0
  154. package/src/types/raw/user/Lists.ts +378 -0
  155. package/src/types/raw/user/Media.ts +1738 -0
  156. package/src/types/raw/user/Notifications.ts +499 -0
  157. package/src/types/raw/user/ProfileUpdate.ts +80 -0
  158. package/src/types/raw/user/Recommended.ts +2319 -0
  159. package/src/types/raw/user/Scheduled.ts +37 -0
  160. package/src/types/raw/user/Search.ts +230 -0
  161. package/src/types/raw/user/Subscriptions.ts +176 -0
  162. package/src/types/raw/user/Tweets.ts +1254 -0
  163. package/src/types/raw/user/TweetsAndReplies.ts +1254 -0
  164. package/src/types/raw/user/Unfollow.ts +280 -0
  165. package/tsconfig.json +97 -0
package/README.md ADDED
@@ -0,0 +1,566 @@
1
+ # Rettiwt-API
2
+
3
+ A CLI tool and an API for fetching data from Twitter for free!
4
+
5
+ ## Prerequisites
6
+
7
+ - NodeJS 22
8
+ - A working Twitter account (optional)
9
+
10
+ ## Installation
11
+
12
+ It is recommended to install the package globally, if you want to use it from the CLI. Use the following steps to install the package and ensure it's installed correctly:
13
+
14
+ 1. Open a terminal.
15
+ 2. Install the package using the command `npm install -g rettiwt-api`.
16
+ 3. Check if the package is installed correctly using the command `rettiwt help`.
17
+
18
+ For using the package in your own project, you can install it as a [dependency](https://rishikant181.github.io/Rettiwt-API/#md:usage-as-a-dependency).
19
+
20
+ ## Authentication
21
+
22
+ Rettiwt-API can be used with or without logging in to Twitter. As such, the two authentication strategies are:
23
+
24
+ - 'Guest' authentication (without logging in) grants access to the following resources/actions:
25
+
26
+ - Tweet Details
27
+ - User Details (by username)
28
+ - User Timeline
29
+
30
+ - 'User' authentication (logging in) grants access to the following resources/actions:
31
+
32
+ - Direct Message Inbox
33
+ - Direct Message Conversations
34
+ - Direct Message Delete Conversation
35
+ - List Add Member
36
+ - List Details
37
+ - List Members
38
+ - List Remove Member
39
+ - List Tweets
40
+ - Tweet Details - Single and Bulk
41
+ - Tweet Bookmark
42
+ - Tweet Like
43
+ - Tweet Likers
44
+ - Tweet Media Upload
45
+ - Tweet Post
46
+ - Tweet Replies
47
+ - Tweet Retweet
48
+ - Tweet Retweeters
49
+ - Tweet Schedule
50
+ - Tweet Search
51
+ - Tweet Stream
52
+ - Tweet Unbookmark
53
+ - Tweet Unlike
54
+ - Tweet Unpost
55
+ - Tweet Unretweet
56
+ - Tweet Unschedule
57
+ - User Affiliates
58
+ - User Analytics (Only for Premium accounts)
59
+ - User Bookmarks
60
+ - User Bookmark Folders
61
+ - User Bookmark Folder Tweets
62
+ - User Details - Single (by ID and Username) and Bulk (by ID only)
63
+ - User Follow
64
+ - User Followed Feed
65
+ - User Followers
66
+ - User Following
67
+ - User Highlights
68
+ - User Likes
69
+ - User Lists
70
+ - User Media
71
+ - User Notification
72
+ - User Recommended Feed
73
+ - User Replies Timeline
74
+ - User Search
75
+ - User Subscriptions
76
+ - User Timeline
77
+ - User Unfollow
78
+ - User Profile Update
79
+
80
+ By default, Rettiwt-API uses 'guest' authentication. If however, access to the full set of resources is required, 'user' authentication can be used. This is done by using the cookies associated with your Twitter/X account, and encoding them into an `API_KEY` for convenience. The said `API_KEY` can be obtained by using a browser extension, as follows:
81
+
82
+ ### A. For Chrome/Chromium-based browsers:
83
+
84
+ 1. Install the [X Auth Helper extension](https://chromewebstore.google.com/detail/x-auth-helper/igpkhkjmpdecacocghpgkghdcmcmpfhp) from the Chrome Web Store, and allow it to run it in incognito mode.
85
+ 2. Switch to incognito mode and login to Twitter/X.
86
+ 3. After successful login, while still being on Twitter/X, click on the extension which will open the extension popup.
87
+ 4. Click on the `Get Key` button, this will generate the `API_KEY` and will show up in the text-area.
88
+ 5. Copy the `API_KEY` by either clicking on the `Copy Key` button or manually from the text-area.
89
+ 6. You may close the browser, but don't log out. Remember, since it's incognito mode, you didn't explicity 'log out', so, while the session will be erased from the browser, the `API_KEY` still remains valid.
90
+ 7. Save the `API_KEY` for use.
91
+
92
+ ### B. For Firefox/Firefox-based browsers:
93
+
94
+ 1. Install the [Rettiwt Auth Helper extension](https://addons.mozilla.org/en-US/firefox/addon/rettiwt-auth-helper) from Firefox Add-Ons, and allow it to run it in in-private mode.
95
+ 2. Switch to in-private mode and login to Twitter/X.
96
+ 3. After successful login, while still being on Twitter/X, click on the extension which will open the extension popup.
97
+ 4. Click on the `Get API Key` button, this will generate the `API_KEY` and will show up in the text-area.
98
+ 5. Copy the `API_KEY` by either clicking on the `Copy API Key` button or manually from the text-area.
99
+ 6. You may close the browser, but don't log out. Remember, since it's in-private mode, you didn't explicity 'log out', so, while the session will be erased from the browser, the `API_KEY` still remains valid.
100
+ 7. Save the `API_KEY` for use.
101
+
102
+ #### Notes:
103
+
104
+ - `API_KEY` created in this way should last 5 years from the date of login, as long as the credentials to the account aren't changed.
105
+ - This approach can also be done without going into incognito/in-private mode, in which case you can either login as usual or skip the login step if you're already logged in, and continue from the steps after login. However, this makes the `API_KEY` to last only as long as the Twitter/X account isn't logged out of (you may exit the browser as usual) or 5 years, whichever comes first. That's why it's recommended to use incognito/in-private mode, so that the `API_KEY` isn't accidentially revoked by logging out.
106
+
107
+ ## The API_KEY
108
+
109
+ The API_KEY generated by logging in is what allows Rettiwt-API to authenticate as a logged in user while interacting with the Twitter API ('user' authentication). As such it is a very sensitive information and therefore, must be stored securely. The following points must be kept in mind while using the API_KEY for 'user' authentication:
110
+
111
+ - The API_KEY is actually a base64 encoding of the account's cookies.
112
+ - The API_KEY provides the same level of authorization as any standard Twitter account, nothing more, nothing less.
113
+
114
+ ## Notes for non-programmers
115
+
116
+ - If you have no idea of programming, it's recommended to use the CLI.
117
+ - The CLI provides an easy to use interface which does not require any knowledge of JavaScript or programming
118
+ - Please skip to [CLI-Usage](https://rishikant181.github.io/Rettiwt-API/#md:cli-usage) for details.
119
+
120
+ ## Usage as a dependency
121
+
122
+ Rettiwt-API can be used as a dependency for your NodeJS project. In such a case, it is not required to install Rettiwt-API globally and you may install it locally in the root of your project using the command:
123
+
124
+ - `npm install --save rettiwt-api` (using npm)
125
+
126
+ or
127
+
128
+ - `yarn add rettiwt-api` (using yarn)
129
+
130
+ However, in this case, for accessing the CLI, you will be required to prepend the CLI commands with `npx` in order to tell NodeJS to use the locally installed package.
131
+
132
+ ## The Rettiwt class
133
+
134
+ 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.
135
+
136
+ A new Rettiwt instance can be initialized using the following code snippets:
137
+
138
+ - `const rettiwt = new Rettiwt()` (for 'guest' authentication)
139
+ - `const rettiwt = new Rettiwt({ apiKey: API_KEY })` (for 'user' authentication)
140
+
141
+ The Rettiwt class has four members:
142
+
143
+ - `dm` member, for accessing resources related to direct messages.
144
+ - `list` member, for accessing resources related to lists.
145
+ - `tweet` member, for accessing resources related to tweets.
146
+ - `user` member, for accessing resources related to users.
147
+
148
+ For details regarding usage of these members for accessing the Twitter API, refer to [features](https://rishikant181.github.io/Rettiwt-API/#md:features).
149
+
150
+ ## Rettiwt Configuration
151
+
152
+ When initializing a new Rettiwt instance, it can be configures using various parameters, namely:
153
+
154
+ - `apiKey` (string) - The API key to use for `user` authentication.
155
+ - `proxyUrl` (URL) - The URL to the proxy server to use.
156
+ - `timeout` (number) - The timeout to use for HTTP requests used by Rettiwt.
157
+ - `logging` (boolean) - Whether to enable logging or not.
158
+ - `errorHandler` (interface) - The custom error handler to use.
159
+ - `tidProvider` (interface) - The custom TID provider to use for generating transaction token.
160
+ - `headers` (object) - Custom HTTP headers to append to the default headers.
161
+ - `delay` (number/function) - The delay to use between concurrent requests, can either be a number in milliseconds, or a function that returns the number. Default is 0 (no delay).
162
+ - `maxRetries` (number) - The maximum number of retries to use in case when a random error 404 is encountered. Default is 0 (no retries).
163
+
164
+ Of these parameters, the following are hot-swappable, using their respective setters:
165
+
166
+ - `apiKey`
167
+ - `headers`
168
+ - `proxyUrl`
169
+
170
+ The following example demonstrates changing the API key on the fly:
171
+
172
+ ```ts
173
+ import { Rettiwt } from 'rettiwt-api';
174
+
175
+ // Initializing a new Rettiwt instance with API key 1
176
+ const rettiwt = new Rettiwt({ apiKey: '<API_KEY_1>' });
177
+
178
+ rettiwt.user.details().then((res) => {
179
+ console.log(res); // Returns details of the user associated with API_KEY_1
180
+ });
181
+
182
+ // Changing API key to API key 2
183
+ rettiwt.apiKey = '<API_KEY_2>';
184
+
185
+ rettiwt.user.details().then((res) => {
186
+ console.log(res); // Returns details of the user associated with API_KEY_2
187
+ });
188
+ ```
189
+
190
+ ## Usage
191
+
192
+ The following examples may help you to get started using the library:
193
+
194
+ ### 1. Getting the details of a target Twitter user
195
+
196
+ ```ts
197
+ import { Rettiwt } from 'rettiwt-api';
198
+
199
+ // Creating a new Rettiwt instance
200
+ // Note that for accessing user details, 'guest' authentication can be used
201
+ const rettiwt = new Rettiwt();
202
+
203
+ // Fetching the details of the user whose username is <username>
204
+ rettiwt.user.details('<username>')
205
+ .then(details => {
206
+ ...
207
+ })
208
+ .catch(error => {
209
+ ...
210
+ });
211
+ ```
212
+
213
+ ### 2. Getting the list of tweets that match a given filter
214
+
215
+ ```ts
216
+ import { Rettiwt } from 'rettiwt-api';
217
+
218
+ // Creating a new Rettiwt instance using the API_KEY
219
+ const rettiwt = new Rettiwt({ apiKey: API_KEY });
220
+
221
+ /**
222
+ * Fetching the list of tweets that:
223
+ * - are made by a user with username <username>,
224
+ * - contain the words <word1> and <word2>
225
+ */
226
+ rettiwt.tweet.search({
227
+ fromUsers: ['<username>'],
228
+ includeWords: ['<word1>', '<word2>']
229
+ })
230
+ .then(data => {
231
+ ...
232
+ })
233
+ .catch(err => {
234
+ ...
235
+ });
236
+ ```
237
+
238
+ For more information regarding the different available filter options, please refer to [TweetFilter](https://rishikant181.github.io/Rettiwt-API/classes/TweetFilter.html).
239
+
240
+ ### 3. Getting the next batch of data using a cursor
241
+
242
+ 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:
243
+
244
+ ```ts
245
+ import { Rettiwt } from 'rettiwt-api';
246
+
247
+ // Creating a new Rettiwt instance using the API_KEY
248
+ const rettiwt = new Rettiwt({ apiKey: API_KEY });
249
+
250
+ /**
251
+ * Fetching the list of tweets that:
252
+ * - are made by a user with username <username>,
253
+ * - contain the words <word1> and <word2>
254
+ *
255
+ * 'data' is the response object received in the previous example.
256
+ *
257
+ * 'count' is a number less or equal to 20 (the quantity of tweets to return)
258
+ */
259
+ rettiwt.tweet.search({
260
+ fromUsers: ['<username>'],
261
+ includeWords: ['<word1>', '<word2>']
262
+ }, count, data.next.value)
263
+ .then(data => {
264
+ ...
265
+ })
266
+ .catch(err => {
267
+ ...
268
+ });
269
+ ```
270
+
271
+ ### 4. Getting an API_KEY during runtime, using 'user' authentication (Borked)
272
+
273
+ 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:
274
+
275
+ ```ts
276
+ import { Rettiwt } from 'rettiwt-api';
277
+
278
+ // Creating a new Rettiwt instance
279
+ const rettiwt = new Rettiwt();
280
+
281
+ // Logging in an getting the API_KEY
282
+ rettiwt.auth.login('<email>', '<username>', '<password>')
283
+ .then(apiKey => {
284
+ // Use the API_KEY
285
+ ...
286
+ })
287
+ .catch(err => {
288
+ console.log(err);
289
+ });
290
+ ```
291
+
292
+ Where,
293
+
294
+ - `<email>` is the email associated with the Twitter account to be logged into.
295
+ - `<username>` is the username associated with the Twitter account.
296
+ - `<password>` is the password to the Twitter account.
297
+
298
+ ## Using a custom error handler
299
+
300
+ Out of the box, `Rettiwt`'s error handling is bare-minimum, only able to parse basic error messages. For advanced scenarios, where full error response might be required, in order to diagnose error reason, it's recommended to use a custom error handler, by implementing the `IErrorHandler` interface, as follows:
301
+
302
+ ```ts
303
+ import { Rettiwt, IErrorHandler } from 'rettiwt-api';
304
+
305
+ // Implementing an error handler
306
+ class CustomErrorHandler implements IErrorHandler {
307
+ /**
308
+ * This is where you handle the error yourself.
309
+ */
310
+ public handler(error: unknown): void {
311
+ // The 'error' variable has the full, raw error response returned from Twitter.
312
+ /**
313
+ * You custom error handling logic goes here
314
+ */
315
+
316
+ console.log(`Raw Twitter Error: ${JSON.stringify(error)}`);
317
+ }
318
+ }
319
+
320
+ // Now we'll use the implemented error handler while initializing Rettiwt
321
+ const rettiwt = new Rettiwt({ apiKey: '<API_KEY>', errorHandler: CustomErrorHandler });
322
+ ```
323
+
324
+ You can then use the created `rettiwt` instance and your custom error handler will handler all the error responses, bypassing `Rettiwt`'s error handling logic.
325
+
326
+ ## Using a proxy
327
+
328
+ For masking of IP address using a proxy server, use the following code snippet for instantiation of Rettiwt:
329
+
330
+ ```ts
331
+ /**
332
+ * PROXY_URL is the URL or configuration for the proxy server you want to use.`
333
+ */
334
+ const rettiwt = new Rettiwt({ apiKey: API_KEY, proxyUrl: PROXY_URL });
335
+ ```
336
+
337
+ This creates a Rettiwt instance which uses the given proxy server for making requests to Twitter.
338
+
339
+ ## Debug logs
340
+
341
+ 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:
342
+
343
+ ```ts
344
+ /**
345
+ * By default, is no value for 'logging' is supplied, logging is disabled.
346
+ */
347
+ const rettiwt = new Rettiwt({ apiKey: API_KEY, logging: true });
348
+ ```
349
+
350
+ ## Accessing raw response
351
+
352
+ For getting the raw data instead of the parsed results, all data models provide a getter `raw` which returns the raw data entity as returned by Twitter, instead of parsing them to Rettiwt's own data entity formats. The following example demonstrates the use of the `raw` getter:
353
+
354
+ ```ts
355
+ import { Rettiwt } from 'rettiwt-api';
356
+
357
+ // Creating a new Rettiwt instance
358
+ // Note that for accessing user details, 'guest' authentication can be used
359
+ const rettiwt = new Rettiwt();
360
+
361
+ // Fetching the details of the user whose username is <username>
362
+ rettiwt.user.details('<username>')
363
+ .then(details => {
364
+ console.log(details);
365
+ // {
366
+ // "createdAt": "2021-07-24T14:25:32.000Z",
367
+ // "description": "Coder, Gamer and Tech Enthusiast",
368
+ // "followersCount": 3,
369
+ // "followingsCount": 44,
370
+ // "fullName": "Rishikant Sahu",
371
+ // "id": "1418940387037782018",
372
+ // "isVerified": false,
373
+ // "likeCount": 762,
374
+ // "profileImage": "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png",
375
+ // "statusesCount": 5,
376
+ // "userName": "negmatico"
377
+ // }
378
+
379
+ console.log(details.raw);
380
+ // {
381
+ // "__typename": "User",
382
+ // "id": "VXNlcjoxNDE4OTQwMzg3MDM3NzgyMDE4",
383
+ // "rest_id": "1418940387037782018",
384
+ // "affiliates_highlighted_label": {},
385
+ // "has_graduated_access": true,
386
+ // "is_blue_verified": false,
387
+ // "legacy": {
388
+ // "following": false,
389
+ // "can_dm": true,
390
+ // "can_media_tag": true,
391
+ // "created_at": "Sat Jul 24 14:25:32 +0000 2021",
392
+ // "default_profile": true,
393
+ // "default_profile_image": true,
394
+ // "description": "Coder, Gamer and Tech Enthusiast",
395
+ // "entities": { "description": { "urls": [] } },
396
+ // "fast_followers_count": 0,
397
+ // "favourites_count": 762,
398
+ // "followers_count": 3,
399
+ // "friends_count": 44,
400
+ // "has_custom_timelines": false,
401
+ // "is_translator": false,
402
+ // "listed_count": 0,
403
+ // "location": "",
404
+ // "media_count": 0,
405
+ // "name": "Rishikant Sahu",
406
+ // "needs_phone_verification": false,
407
+ // "normal_followers_count": 3,
408
+ // "pinned_tweet_ids_str": [],
409
+ // "possibly_sensitive": false,
410
+ // "profile_image_url_https": "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png",
411
+ // "profile_interstitial_type": "",
412
+ // "screen_name": "negmatico",
413
+ // "statuses_count": 5,
414
+ // "translator_type": "none",
415
+ // "verified": false,
416
+ // "want_retweets": false,
417
+ // "withheld_in_countries": []
418
+ // },
419
+ // "parody_commentary_fan_label": "None",
420
+ // "profile_image_shape": "Circle",
421
+ // "tipjar_settings": {},
422
+ // "verified_phone_status": false,
423
+ // "legacy_extended_profile": {
424
+ // "birthdate": { "day": 18, "month": 1, "year": 2001, "visibility": "Self", "year_visibility": "Self" }
425
+ // },
426
+ // "is_profile_translatable": false,
427
+ // "has_hidden_subscriptions_on_profile": false,
428
+ // "verification_info": { "is_identity_verified": false },
429
+ // "highlights_info": { "can_highlight_tweets": false, "highlighted_tweets": "0" },
430
+ // "user_seed_tweet_count": 0,
431
+ // "premium_gifting_eligible": true,
432
+ // "business_account": {},
433
+ // "creator_subscriptions_count": 0
434
+ // }
435
+
436
+ })
437
+ .catch(error => {
438
+ ...
439
+ });
440
+ ```
441
+
442
+ However, if further control over the raw response is required, Rettiwt-API provides the [`FetcherService`](https://rishikant181.github.io/Rettiwt-API/classes/FetcherService.html) class which provides direct access to the raw response, but keep in mind, this delegates the task of parsing and filtering the results to the consumer of the library. The following example demonstrates using the `FetcherService` class:
443
+
444
+ ```ts
445
+ import { RettiwtConfig, FetcherService, ResourceType, IUserDetailsResponse } from 'rettiwt-api';
446
+
447
+ // Creating the configuration for Rettiwt
448
+ const config = new RettiwtConfig({ apiKey: '<API_KEY>' });
449
+
450
+ // Creating a new FetcherService instance using the config
451
+ const fetcher = new FetcherService(config);
452
+
453
+ // Fetching the details of the given user
454
+ fetcher
455
+ .request<IUserDetailsResponse>(ResourceType.USER_DETAILS_BY_USERNAME, { id: 'user1' })
456
+ .then((res) => {
457
+ console.log(res);
458
+ })
459
+ .catch((err) => {
460
+ console.log(err);
461
+ });
462
+ ```
463
+
464
+ As demonstrated by the example, the raw data can be accessed by using the `request` method of the `FetcherService` class, which takes two parameters. The first parameter is the name of the requested resource, while the second is an object specifying the associated arguments required for the given resource. The complete list of resource type can be checked [here](https://rishikant181.github.io/Rettiwt-API/enums/AuthService.html#ResourceType). As for the resource specific argurments, they are the same as that of the methods of `Rettiwt` class' methods for the respective resources, but structured as an object. Notice how the `FetcherService` class takes the same arguments as the `Rettiwt` class, and the arguments have the same effects as they have in case of `Rettiwt` class.
465
+
466
+ #### Notes:
467
+
468
+ - For for hot-swapping in case of using `FetcherService`, the setters are accessed from the `config` object as `config.apiKey = ...`, `config.proxyUrl = ...`, etc.
469
+
470
+ ## Data serialization
471
+
472
+ The data returned by all functions of `Rettiwt` are complex objects, containing non-serialized fields like `raw`. In order to get JSON-serializable data, all data objects returned by `Rettiwt` provide a function `toJSON()` which converts the data into a serializable JSON, whose type is described by their respective interfaces i.e, `ITweet` for `Tweet`, `IUser` for `User` and so on.
473
+
474
+ For handling and processing of data returned by the functions, it's always advisable to serialize them using the `toJSON()` function.
475
+
476
+ ## Features
477
+
478
+ So far, the following operations are supported:
479
+
480
+ ### Direct Messages
481
+
482
+ - [Getting the DM inbox](https://rishikant181.github.io/Rettiwt-API/classes/DirectMessageService.html#inbox)
483
+ - [Getting a specific conversation with full message history](https://rishikant181.github.io/Rettiwt-API/classes/DirectMessageService.html#conversation)
484
+ - [Deleting a conversation](https://rishikant181.github.io/Rettiwt-API/classes/DirectMessageService.html#deleteConversation)
485
+
486
+ ### List
487
+
488
+ - [Adding a member to a given Twitter list](https://rishikant181.github.io/Rettiwt-API/classes/ListService.html#addMember)
489
+ - [Getting the details of a given Twitter list](https://rishikant181.github.io/Rettiwt-API/classes/ListService.html#details)
490
+ - [Getting the members of a given Twitter list](https://rishikant181.github.io/Rettiwt-API/classes/ListService.html#members)
491
+ - [Removing a member from a given Twitter list](https://rishikant181.github.io/Rettiwt-API/classes/ListService.html#removeMember)
492
+ - [Getting the list of tweets from a given Twitter list](https://rishikant181.github.io/Rettiwt-API/classes/ListService.html#tweets)
493
+
494
+ ### Tweets
495
+
496
+ - [Bookmarking a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#bookmark)
497
+ - [Getting the details of a tweet/multiple tweets](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#details)
498
+ - [Liking a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#like)
499
+ - [Getting the list of users who liked your tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#likers)
500
+ - [Posting a new tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#post)
501
+ - [Getting the list of replies to a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#replies)
502
+ - [Retweeting a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#retweet)
503
+ - [Getting the list of users who retweeted a given tweet by the logged-in user](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#retweeters)
504
+ - [Scheduling a new tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#schedule)
505
+ - [Searching for the list of tweets that match a given filter](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#search)
506
+ - [Streaming filtered tweets in pseudo-realtime](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#stream)
507
+ - [Unbookmarking a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#unbookmark)
508
+ - [Unliking a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#unlike)
509
+ - [Unposting a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#unpost)
510
+ - [Unretweeting a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#unretweet)
511
+ - [Unscheduling a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#unschedule)
512
+ - [Uploading a media file for a tweet](https://rishikant181.github.io/Rettiwt-API/classes/TweetService.html#upload)
513
+
514
+ ### Users
515
+
516
+ - [Getting the list of users affiliated with the given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#affiliates)
517
+ - [Getting the analytics of the logged-in user (premium accounts only)](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#analytics)
518
+ - [Getting the list of tweets bookmarked by the logged-in user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#bookmarks)
519
+ - [Getting the list of bookmark folders of the logged-in user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#bookmarkFolders)
520
+ - [Getting the list of tweets in a specific bookmark folder](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#bookmarkFolderTweets)
521
+ - [Getting the details of a user/multiple users](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#details)
522
+ - [Following a given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#follow)
523
+ - [Getting the followed feed of the logged-in user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#followed)
524
+ - [Getting the list of users who follow the given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#followers)
525
+ - [Getting the list of users who are followed by the given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#following)
526
+ - [Getting the list of highlighted tweets of the given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#highlights)
527
+ - [Getting the list of tweets liked by the logged-in user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#likes)
528
+ - [Getting the lists of the logged-in user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#lists)
529
+ - [Getting the media timeline of the given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#media)
530
+ - [Streaming notifications of the logged-in user in pseudo-realtime](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#notifications)
531
+ - [Getting the recommended feed of the logged-in user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#recommended)
532
+ - [Getting the replies timeline of the given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#replies)
533
+ - [Searching for a username](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#search)
534
+ - [Getting the tweet timeline of the given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#timeline)
535
+ - [Unfollowing a given user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#unfollow)
536
+ - [Updating the profile of the logged-in user](https://rishikant181.github.io/Rettiwt-API/classes/UserService.html#updateProfile)
537
+
538
+ ## CLI Usage
539
+
540
+ Rettiwt-API provides an easy to use command-line interface which does not require any programming knowledge.
541
+
542
+ By default, the CLI operates in 'guest' authentication. If you want to use 'user' authentication:
543
+
544
+ 1. Generate an API_KEY as described in [Authentication](https://rishikant181.github.io/Rettiwt-API/#md:authentication).
545
+ 2. Store the output API_KEY as an environment variable with the name 'API_KEY'.
546
+ - Additionally, store the API_KEY in a file for later use.
547
+ - Make sure to generate an API_KEY only once, and use it every time you need it.
548
+ 3. The CLI automatically reads this environment variable to authenticate against Twitter.
549
+ - Additionally, the API_KEY can also be passed in manually using the '-k' option as follows: `rettiwt -k <API_KEY> <command>`
550
+
551
+ Help for the CLI can be obtained from the CLI itself:
552
+
553
+ - For help regarding the available commands, use the command `rettiwt help`
554
+ - For help regarding a specific command, use the command `rettiwt help <command_name>`
555
+
556
+ ## API Reference
557
+
558
+ The complete API reference can be found at [this](https://rishikant181.github.io/Rettiwt-API/modules) page.
559
+
560
+ ## Additional information
561
+
562
+ - 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.
563
+
564
+ ## Donation
565
+
566
+ Support this project by donating at my [PayPal](https://paypal.me/Rishikant181?country.x=IN&locale.x=en_GB).
package/dist/cli.js ADDED
@@ -0,0 +1,43 @@
1
+ #! /usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const commander_1 = require("commander");
8
+ const DirectMessage_1 = __importDefault(require("./commands/DirectMessage"));
9
+ const List_1 = __importDefault(require("./commands/List"));
10
+ const Tweet_1 = __importDefault(require("./commands/Tweet"));
11
+ const User_1 = __importDefault(require("./commands/User"));
12
+ const Rettiwt_1 = require("./Rettiwt");
13
+ // Creating a new commandline program
14
+ const Program = (0, commander_1.createCommand)('rettiwt')
15
+ .description('A CLI tool for accessing the Twitter API for free!')
16
+ .passThroughOptions()
17
+ .enablePositionalOptions();
18
+ // Adding options
19
+ Program.option('-k, --key <string>', 'The API key to use for authentication')
20
+ .option('-l, --log', 'Enable logging to console')
21
+ .option('-p, --proxy <string>', 'The URL to the proxy to use')
22
+ .option('-t, --timeout <number>', 'The timout (in milli-seconds) to use for requests')
23
+ .option('-r, --retries <number>', 'The maximum number of retries to use, a value of 5 combined with a delay of 1000 is recommended')
24
+ .option('-d, --delay <number>', 'The delay in milliseconds to use in-between successive requests');
25
+ // Parsing the program to get supplied options
26
+ Program.parse();
27
+ // Initializing Rettiwt instance using the given options
28
+ const RettiwtInstance = new Rettiwt_1.Rettiwt({
29
+ apiKey: process.env.API_KEY ?? Program.opts().key,
30
+ logging: Program.opts().log ? true : false,
31
+ proxyUrl: Program.opts().proxy,
32
+ timeout: Program.opts().timeout ? Number(Program.opts().timeout) : undefined,
33
+ maxRetries: Program.opts().retries,
34
+ delay: Program.opts().delay,
35
+ });
36
+ // Adding sub-commands
37
+ Program.addCommand((0, DirectMessage_1.default)(RettiwtInstance));
38
+ Program.addCommand((0, List_1.default)(RettiwtInstance));
39
+ Program.addCommand((0, Tweet_1.default)(RettiwtInstance));
40
+ Program.addCommand((0, User_1.default)(RettiwtInstance));
41
+ // Finalizing the CLI
42
+ Program.parse();
43
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1,17 @@
1
+ import { FlatCompat } from '@eslint/eslintrc';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = path.dirname(__filename);
7
+
8
+ const compat = new FlatCompat({
9
+ baseDirectory: __dirname,
10
+ });
11
+
12
+ export default [
13
+ {
14
+ ignores: ['dist/', 'node_modules/', 'playground/'],
15
+ },
16
+ ...compat.extends('.eslintrc.js'),
17
+ ];