@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/src/index.ts ADDED
@@ -0,0 +1,128 @@
1
+ // MAIN
2
+ export * from './Rettiwt';
3
+
4
+ // ENUMS
5
+ export * from './enums/raw/Analytics';
6
+ export * from './enums/raw/Media';
7
+ export * from './enums/raw/Notification';
8
+ export * from './enums/raw/Tweet';
9
+ export * from './enums/Api';
10
+ export * from './enums/Authentication';
11
+ export * from './enums/Data';
12
+ export * from './enums/Logging';
13
+ export * from './enums/Media';
14
+ export * from './enums/Notification';
15
+ export * from './enums/Resource';
16
+ export * from './enums/Tweet';
17
+
18
+ // MODELS
19
+ export * from './models/args/FetchArgs';
20
+ export * from './models/args/PostArgs';
21
+ export * from './models/args/ProfileArgs';
22
+ export * from './models/data/BookmarkFolder';
23
+ export * from './models/data/Conversation';
24
+ export * from './models/data/CursoredData';
25
+ export * from './models/data/DirectMessage';
26
+ export * from './models/data/Inbox';
27
+ export * from './models/data/List';
28
+ export * from './models/data/Notification';
29
+ export * from './models/data/Tweet';
30
+ export * from './models/data/User';
31
+ export * from './models/errors/TwitterError';
32
+
33
+ // REQUESTS
34
+ export * from './requests/DirectMessage';
35
+ export * from './requests/List';
36
+ export * from './requests/Media';
37
+ export * from './requests/Tweet';
38
+ export * from './requests/User';
39
+
40
+ // SERVICES
41
+ export * from './services/public/DirectMessageService';
42
+ export * from './services/public/FetcherService';
43
+ export * from './services/public/ListService';
44
+ export * from './services/public/TweetService';
45
+ export * from './services/public/UserService';
46
+
47
+ // TYPES
48
+ export * from './types/args/FetchArgs';
49
+ export * from './types/args/PostArgs';
50
+ export * from './types/args/ProfileArgs';
51
+ export * from './types/data/BookmarkFolder';
52
+ export * from './types/data/Conversation';
53
+ export * from './types/data/CursoredData';
54
+ export * from './types/data/DirectMessage';
55
+ export * from './types/data/Inbox';
56
+ export * from './types/data/List';
57
+ export * from './types/data/Notification';
58
+ export * from './types/data/Tweet';
59
+ export * from './types/data/User';
60
+ export * from './types/errors/TwitterError';
61
+ export * from './types/params/Variables';
62
+ export { IAnalytics as IRawAnalytics } from './types/raw/base/Analytic';
63
+ export { IBookmarkFolder as IRawBookmarkFolder } from './types/raw/base/BookmarkFolder';
64
+ export { ICursor as IRawCursor } from './types/raw/base/Cursor';
65
+ export { IErrorData as IRawErrorData, IErrorDetails as IRawErrorDetails } from './types/raw/base/Error';
66
+ export { ILimitedVisibilityTweet as IRawLimitedVisibilityTweet } from './types/raw/base/LimitedVisibilityTweet';
67
+ export { IList as IRawList } from './types/raw/base/List';
68
+ export { IMedia as IRawMedia } from './types/raw/base/Media';
69
+ export { IMessage as IRawMessage } from './types/raw/base/Message';
70
+ export { INotification as IRawNotification } from './types/raw/base/Notification';
71
+ export { ISpace as IRawSpace } from './types/raw/base/Space';
72
+ export { ITweet as IRawTweet } from './types/raw/base/Tweet';
73
+ export { IUser as IRawUser } from './types/raw/base/User';
74
+ export { IDataResult as IRawDataResult } from './types/raw/composite/DataResult';
75
+ export { ITimelineTweet as IRawTimelineTweet } from './types/raw/composite/TimelineTweet';
76
+ export { ITimelineUser as IRawTimelineUser } from './types/raw/composite/TimelineUser';
77
+ export { IResponse as IRawResponse } from './types/raw/generic/Response';
78
+ export { IListMemberAddResponse as IRawListMemberAddResponse } from './types/raw/list/AddMember';
79
+ export { IListMemberRemoveResponse as IRawListMemberRemoveResponse } from './types/raw/list/RemoveMember';
80
+ export { IListDetailsResponse as IRawListDetailsResponse } from './types/raw/list/Details';
81
+ export { IListMembersResponse as IRawListMembersResponse } from './types/raw/list/Members';
82
+ export { IListTweetsResponse as IRawListTweetsResponse } from './types/raw/list/Tweets';
83
+ export { IMediaFinalizeUploadResponse as IRawMediaFinalizeUploadResponse } from './types/raw/media/FinalizeUpload';
84
+ export { IMediaInitializeUploadResponse as IRawMediaInitializeUploadResponse } from './types/raw/media/InitalizeUpload';
85
+ export { IMediaLiveVideoStreamResponse as IRawMediaLiveVideoStreamResponse } from './types/raw/media/LiveVideoStream';
86
+ export { ITweetDetailsResponse as IRawTweetDetailsResponse } from './types/raw/tweet/Details';
87
+ export { ITweetDetailsBulkResponse as IRawTweetDetailsBulkResponse } from './types/raw/tweet/DetailsBulk';
88
+ export { ITweetLikeResponse as IRawTweetLikeResponse } from './types/raw/tweet/Like';
89
+ export { ITweetLikersResponse as IRawTweetLikersResponse } from './types/raw/tweet/Likers';
90
+ export { ITweetPostResponse as IRawTweetPostResponse } from './types/raw/tweet/Post';
91
+ export { ITweetRepliesResponse as IRawTweetRepliesResponse } from './types/raw/tweet/Replies';
92
+ export { ITweetRetweetResponse as IRawTweetRetweetResponse } from './types/raw/tweet/Retweet';
93
+ export { ITweetRetweetersResponse as IRawTweetRetweetersResponse } from './types/raw/tweet/Retweeters';
94
+ export { ITweetScheduleResponse as IRawTweetScheduleResponse } from './types/raw/tweet/Schedule';
95
+ export { ITweetSearchResponse as IRawTweetSearchResponse } from './types/raw/tweet/Search';
96
+ export { ITweetUnlikeResponse as IRawTweetUnlikeResponse } from './types/raw/tweet/Unlike';
97
+ export { ITweetUnpostResponse as IRawTweetUnpostResponse } from './types/raw/tweet/Unpost';
98
+ export { ITweetUnretweetResponse as IRawTweetUnretweetResponse } from './types/raw/tweet/Unretweet';
99
+ export { ITweetUnscheduleResponse as ITRawTweetUnscheduleResponse } from './types/raw/tweet/Unschedule';
100
+ export { IUserAffiliatesResponse as IRawUserAffiliatesResponse } from './types/raw/user/Affiliates';
101
+ export { IUserAnalyticsResponse as IRawUserAnalyticsResponse } from './types/raw/user/Analytics';
102
+ export { IUserBookmarkFoldersResponse as IRawUserBookmarkFoldersResponse } from './types/raw/user/BookmarkFolders';
103
+ export { IUserBookmarkFolderTweetsResponse as IRawUserBookmarkFolderTweetsResponse } from './types/raw/user/BookmarkFolderTweets';
104
+ export { IUserBookmarksResponse as IRawUserBookmarksResponse } from './types/raw/user/Bookmarks';
105
+ export { IUserDetailsResponse as IRawUserDetailsResponse } from './types/raw/user/Details';
106
+ export { IUserDetailsBulkResponse as IRawUserDetailsBulkResponse } from './types/raw/user/DetailsBulk';
107
+ export { IUserFollowResponse as IRawUserFollowResponse } from './types/raw/user/Follow';
108
+ export { IUserFollowedResponse as IRawUserFollowedResponse } from './types/raw/user/Followed';
109
+ export { IUserFollowersResponse as IRawUserFollowersResponse } from './types/raw/user/Followers';
110
+ export { IUserFollowingResponse as IRawUserFollowingResponse } from './types/raw/user/Following';
111
+ export { IUserHighlightsResponse as IRawUserHighlightsResponse } from './types/raw/user/Highlights';
112
+ export { IUserLikesResponse as IRawUserLikesResponse } from './types/raw/user/Likes';
113
+ export { IUserMediaResponse as IRawUserMediaResponse } from './types/raw/user/Media';
114
+ export { IUserNotificationsResponse as IRawUserNotificationsResponse } from './types/raw/user/Notifications';
115
+ export { IUserRecommendedResponse as IRawUserRecommendedResponse } from './types/raw/user/Recommended';
116
+ export { IUserSearchResponse as IRawUserSearchResponse } from './types/raw/user/Search';
117
+ export { IUserScheduledResponse as IRawUserScheduledResponse } from './types/raw/user/Scheduled';
118
+ export { IUserSubscriptionsResponse as IRawUserSubscriptionsResponse } from './types/raw/user/Subscriptions';
119
+ export { IUserTweetsResponse as IRawUserTweetsResponse } from './types/raw/user/Tweets';
120
+ export { IUserTweetsAndRepliesResponse as IRawUserTweetsAndRepliesResponse } from './types/raw/user/TweetsAndReplies';
121
+ export { IUserUnfollowResponse as IRawUserUnfollowResponse } from './types/raw/user/Unfollow';
122
+ export { IUserProfileUpdateResponse as IRawUserProfileUpdateResponse } from './types/raw/user/ProfileUpdate';
123
+ export * from './types/ErrorHandler';
124
+ export * from './types/RettiwtConfig';
125
+ export { IConversationTimelineResponse as IRawConversationTimelineResponse } from './types/raw/dm/Conversation';
126
+ export { IInboxInitialResponse as IRawInboxInitialResponse } from './types/raw/dm/InboxInitial';
127
+ export { IInboxTimelineResponse as IRawInboxTimelineResponse } from './types/raw/dm/InboxTimeline';
128
+ export { IUserUpdatesResponse as IRawUserUpdatesResponse } from './types/raw/dm/UserUpdates';
@@ -0,0 +1,101 @@
1
+ import { Agent } from 'https';
2
+
3
+ import { HttpsProxyAgent } from 'https-proxy-agent';
4
+
5
+ import { AuthService } from '../services/internal/AuthService';
6
+ import { IErrorHandler } from '../types/ErrorHandler';
7
+ import { IRettiwtConfig } from '../types/RettiwtConfig';
8
+
9
+ /**
10
+ * The default headers.
11
+ *
12
+ * @public
13
+ */
14
+ const DefaultHeaders = {
15
+ /* eslint-disable @typescript-eslint/naming-convention */
16
+
17
+ Authority: 'x.com',
18
+ 'Accept-Language': 'en-US,en;q=0.9',
19
+ 'Cache-Control': 'no-cache',
20
+ Referer: 'https://x.com',
21
+ 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0',
22
+ 'X-Twitter-Active-User': 'yes',
23
+ 'X-Twitter-Client-Language': 'en',
24
+
25
+ /* eslint-enable @typescript-eslint/naming-convention */
26
+ };
27
+
28
+ /**
29
+ * The configuration for initializing a new Rettiwt instance.
30
+ *
31
+ * @public
32
+ */
33
+ export class RettiwtConfig implements IRettiwtConfig {
34
+ // Parameters for internal use
35
+ private _apiKey?: string;
36
+ private _headers: { [key: string]: string };
37
+ private _httpsAgent: Agent;
38
+ private _userId: string | undefined;
39
+
40
+ // Parameters that can be set once, upon initialization
41
+ public readonly delay?: number | (() => number | Promise<number>);
42
+ public readonly errorHandler?: IErrorHandler;
43
+ public readonly logging?: boolean;
44
+ public readonly maxRetries: number;
45
+ public readonly timeout?: number;
46
+
47
+ /**
48
+ * @param config - The config for Rettiwt of type {@link IRettiwtConfig}.
49
+ */
50
+ public constructor(config?: IRettiwtConfig) {
51
+ this._apiKey = config?.apiKey;
52
+ this._httpsAgent = config?.proxyUrl ? new HttpsProxyAgent(config?.proxyUrl) : new Agent();
53
+ this._userId = config?.apiKey ? AuthService.getUserId(config?.apiKey) : undefined;
54
+ this.delay = config?.delay ?? 0;
55
+ this.maxRetries = config?.maxRetries ?? 0;
56
+ this.errorHandler = config?.errorHandler;
57
+ this.logging = config?.logging;
58
+ this.timeout = config?.timeout;
59
+ this.apiKey = config?.apiKey;
60
+ this._headers = {
61
+ ...DefaultHeaders,
62
+ ...config?.headers,
63
+ };
64
+ }
65
+
66
+ public get apiKey(): string | undefined {
67
+ return this._apiKey;
68
+ }
69
+
70
+ public get headers(): { [key: string]: string } {
71
+ return this._headers;
72
+ }
73
+
74
+ /** The HTTPS agent instance to use. */
75
+ public get httpsAgent(): Agent {
76
+ return this._httpsAgent;
77
+ }
78
+
79
+ /** The ID of the user associated with the API key, if any. */
80
+ public get userId(): string | undefined {
81
+ return this._userId;
82
+ }
83
+
84
+ public set apiKey(apiKey: string | undefined) {
85
+ this._apiKey = apiKey;
86
+ this._userId = apiKey ? AuthService.getUserId(apiKey) : undefined;
87
+ }
88
+
89
+ public set headers(headers: { [key: string]: string } | undefined) {
90
+ this._headers = {
91
+ ...DefaultHeaders,
92
+ ...headers,
93
+ };
94
+ }
95
+
96
+ public set proxyUrl(proxyUrl: URL | undefined) {
97
+ this._httpsAgent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : new Agent();
98
+ }
99
+ }
100
+
101
+ export { DefaultHeaders as DefaultRettiwtHeaders };
@@ -0,0 +1,169 @@
1
+ import { TweetRepliesSortType } from '../../enums/Tweet';
2
+ import { IFetchArgs, ITweetFilter } from '../../types/args/FetchArgs';
3
+
4
+ import type { RawAnalyticsGranularity, RawAnalyticsMetric } from '../../enums/raw/Analytics';
5
+ /**
6
+ * Options specifying the data that is to be fetched.
7
+ *
8
+ * @public
9
+ */
10
+ export class FetchArgs implements IFetchArgs {
11
+ public activeConversationId?: string;
12
+ public conversationId?: string;
13
+ public count?: number;
14
+ public cursor?: string;
15
+ public filter?: TweetFilter;
16
+ public fromTime?: Date;
17
+ public granularity?: RawAnalyticsGranularity;
18
+ public id?: string;
19
+ public ids?: string[];
20
+ public maxId?: string;
21
+ public metrics?: RawAnalyticsMetric[];
22
+ public showVerifiedFollowers?: boolean;
23
+ public sortBy?: TweetRepliesSortType;
24
+ public toTime?: Date;
25
+
26
+ /**
27
+ * @param args - Additional user-defined arguments for fetching the resource.
28
+ */
29
+ public constructor(args: IFetchArgs) {
30
+ this.id = args.id;
31
+ this.ids = args.ids;
32
+ this.count = args.count;
33
+ this.cursor = args.cursor;
34
+ this.filter = args.filter ? new TweetFilter(args.filter) : undefined;
35
+ this.sortBy = args.sortBy;
36
+ this.fromTime = args.fromTime;
37
+ this.toTime = args.toTime;
38
+ this.granularity = args.granularity;
39
+ this.metrics = args.metrics;
40
+ this.showVerifiedFollowers = args.showVerifiedFollowers;
41
+ this.activeConversationId = args.activeConversationId;
42
+ this.conversationId = args.conversationId;
43
+ this.maxId = args.maxId;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * The filter to be used for searching tweets.
49
+ *
50
+ * @public
51
+ */
52
+ export class TweetFilter implements ITweetFilter {
53
+ public endDate?: Date;
54
+ public excludeWords?: string[];
55
+ public fromUsers?: string[];
56
+ public hashtags?: string[];
57
+ public includePhrase?: string;
58
+ public includeWords?: string[];
59
+ public language?: string;
60
+ public list?: string;
61
+ public maxId?: string;
62
+ public mentions?: string[];
63
+ public minLikes?: number;
64
+ public minReplies?: number;
65
+ public minRetweets?: number;
66
+ public onlyLinks?: boolean;
67
+ public onlyOriginal?: boolean;
68
+ public onlyReplies?: boolean;
69
+ public onlyText?: boolean;
70
+ public optionalWords?: string[];
71
+ public quoted?: string;
72
+ public sinceId?: string;
73
+ public startDate?: Date;
74
+ public toUsers?: string[];
75
+ public top?: boolean;
76
+
77
+ /**
78
+ * @param filter - The filter configuration.
79
+ */
80
+ public constructor(filter: ITweetFilter) {
81
+ this.endDate = filter.endDate;
82
+ this.excludeWords = filter.excludeWords;
83
+ this.fromUsers = filter.fromUsers;
84
+ this.hashtags = filter.hashtags;
85
+ this.includePhrase = filter.includePhrase;
86
+ this.language = filter.language;
87
+ this.list = filter.list;
88
+ this.mentions = filter.mentions;
89
+ this.quoted = filter.quoted;
90
+ this.sinceId = filter.sinceId;
91
+ this.maxId = filter.maxId;
92
+ this.minLikes = filter.minLikes;
93
+ this.minReplies = filter.minReplies;
94
+ this.minRetweets = filter.minRetweets;
95
+ this.onlyLinks = filter.onlyLinks;
96
+ this.onlyOriginal = filter.onlyOriginal;
97
+ this.onlyReplies = filter.onlyReplies;
98
+ this.onlyText = filter.onlyText;
99
+ this.optionalWords = filter.optionalWords;
100
+ this.startDate = filter.startDate;
101
+ this.toUsers = filter.toUsers;
102
+ this.top = filter.top;
103
+ this.includeWords = filter.includeWords;
104
+ }
105
+
106
+ /**
107
+ * Convert Date object to Twitter string representation.
108
+ * eg - 2023-06-23_11:21:06_UTC
109
+ *
110
+ * @param date - The date object to convert.
111
+ * @returns The Twitter string representation of the date.
112
+ */
113
+ private static _dateToTwitterString(date: Date): string {
114
+ // Converting localized date to UTC date
115
+ const utc = new Date(
116
+ Date.UTC(
117
+ date.getUTCFullYear(),
118
+ date.getUTCMonth(),
119
+ date.getUTCDate(),
120
+ date.getUTCHours(),
121
+ date.getUTCMinutes(),
122
+ date.getUTCSeconds(),
123
+ ),
124
+ );
125
+
126
+ /**
127
+ * To convert ISO 8601 date string to Twitter date string:
128
+ *
129
+ * - 'T' between date and time substring is replace with '_'.
130
+ * - Milliseconds substring is omitted.
131
+ * - '_UTC' is appended as suffix.
132
+ */
133
+ return utc.toISOString().replace(/T/, '_').replace(/\..+/, '') + '_UTC';
134
+ }
135
+
136
+ /**
137
+ * @returns The string representation of 'this' filter.
138
+ */
139
+ public toString(): string {
140
+ return (
141
+ [
142
+ this.includeWords ? this.includeWords.join(' ') : '',
143
+ this.includePhrase ? `"${this.includePhrase}"` : '',
144
+ this.optionalWords ? `(${this.optionalWords.join(' OR ')})` : '',
145
+ this.excludeWords ? `${this.excludeWords.map((word) => '-' + word).join(' ')}` : '',
146
+ this.hashtags ? `(${this.hashtags.map((hashtag) => '#' + hashtag).join(' OR ')})` : '',
147
+ this.fromUsers ? `(${this.fromUsers.map((user) => `from:${user}`).join(' OR ')})` : '',
148
+ this.toUsers ? `(${this.toUsers.map((user) => `to:${user}`).join(' OR ')})` : '',
149
+ this.list ? `list:${this.list}` : '',
150
+ this.mentions ? `(${this.mentions.map((mention) => '@' + mention).join(' OR ')})` : '',
151
+ this.minReplies ? `min_replies:${this.minReplies}` : '',
152
+ this.minLikes ? `min_faves:${this.minLikes}` : '',
153
+ this.minRetweets ? `min_retweets:${this.minRetweets}` : '',
154
+ this.language ? `lang:${this.language}` : '',
155
+ this.startDate ? `since:${TweetFilter._dateToTwitterString(this.startDate)}` : '',
156
+ this.endDate ? `until:${TweetFilter._dateToTwitterString(this.endDate)}` : '',
157
+ this.sinceId ? `since_id:${this.sinceId}` : '',
158
+ this.maxId ? `max_id:${this.maxId}` : '',
159
+ this.quoted ? `quoted_tweet_id:${this.quoted}` : '',
160
+ ]
161
+ .filter((item) => item !== '()' && item !== '')
162
+ .join(' ') +
163
+ (this.onlyText === true ? ' -filter:links' : '') +
164
+ (this.onlyOriginal === true ? ' -filter:replies' : '') +
165
+ (this.onlyLinks === true ? ' filter:links' : '') +
166
+ (this.onlyReplies === true ? ' filter:replies' : '')
167
+ );
168
+ }
169
+ }
@@ -0,0 +1,93 @@
1
+ import { INewTweet, INewTweetMedia, IPostArgs, IUploadArgs } from '../../types/args/PostArgs';
2
+
3
+ import { ProfileUpdateOptions } from './ProfileArgs';
4
+
5
+ /**
6
+ * Options specifying the data that is to be posted.
7
+ *
8
+ * @public
9
+ */
10
+ export class PostArgs implements IPostArgs {
11
+ public conversationId?: string;
12
+ public id?: string;
13
+ public profileOptions?: ProfileUpdateOptions;
14
+ public tweet?: NewTweet;
15
+ public upload?: UploadArgs;
16
+ public userId?: string;
17
+
18
+ /**
19
+ * @param resource - The resource to be posted.
20
+ * @param args - Additional user-defined arguments for posting the resource.
21
+ */
22
+ public constructor(args: IPostArgs) {
23
+ this.id = args.id;
24
+ this.tweet = args.tweet ? new NewTweet(args.tweet) : undefined;
25
+ this.upload = args.upload ? new UploadArgs(args.upload) : undefined;
26
+ this.userId = args.userId;
27
+ this.conversationId = args.conversationId;
28
+ this.profileOptions = args.profileOptions ? new ProfileUpdateOptions(args.profileOptions) : undefined;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Configuration for the new tweet to be posted.
34
+ *
35
+ * @public
36
+ */
37
+ export class NewTweet implements INewTweet {
38
+ public media?: NewTweetMedia[];
39
+ public quote?: string;
40
+ public replyTo?: string;
41
+ public scheduleFor?: Date;
42
+ public text: string;
43
+
44
+ /**
45
+ * @param newTweet - The args specifying the new tweet to be posted.
46
+ */
47
+ public constructor(newTweet: INewTweet) {
48
+ this.media = newTweet.media;
49
+ this.quote = newTweet.quote;
50
+ this.replyTo = newTweet.replyTo;
51
+ this.scheduleFor = newTweet.scheduleFor;
52
+ this.text = newTweet.text ?? '';
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Configuration for the media to be uploaded.
58
+ *
59
+ * @public
60
+ */
61
+ export class NewTweetMedia implements INewTweetMedia {
62
+ public id: string;
63
+ public tags?: string[];
64
+
65
+ /**
66
+ * @param newTweetMedia - The args specifying the new media to be posted along with the tweet.
67
+ */
68
+ public constructor(newTweetMedia: INewTweetMedia) {
69
+ this.id = newTweetMedia.id;
70
+ this.tags = newTweetMedia.tags;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Options specifying the media file to be uploaded.
76
+ *
77
+ * @public
78
+ */
79
+ export class UploadArgs implements IUploadArgs {
80
+ public id?: string;
81
+ public media?: string | ArrayBuffer;
82
+ public size?: number;
83
+
84
+ /**
85
+ * @param step - The upload step.
86
+ * @param args - The upload arguments for uploading the media file.
87
+ */
88
+ public constructor(args: IUploadArgs) {
89
+ this.size = args.size;
90
+ this.media = args.media;
91
+ this.id = args.id;
92
+ }
93
+ }
@@ -0,0 +1,68 @@
1
+ import { IProfileUpdateOptions } from '../../types/args/ProfileArgs';
2
+
3
+ /**
4
+ * Configuration for profile update.
5
+ *
6
+ * @public
7
+ */
8
+ export class ProfileUpdateOptions implements IProfileUpdateOptions {
9
+ public description?: string;
10
+ public location?: string;
11
+ public name?: string;
12
+ public url?: string;
13
+
14
+ /**
15
+ * @param options - The options specifying the profile fields to update.
16
+ */
17
+ public constructor(options: IProfileUpdateOptions) {
18
+ this.description = options.description;
19
+ this.location = options.location;
20
+ this.name = options.name;
21
+ this.url = options.url;
22
+
23
+ // At least one field must be provided
24
+ if (
25
+ this.name === undefined &&
26
+ this.url === undefined &&
27
+ this.location === undefined &&
28
+ this.description === undefined
29
+ ) {
30
+ throw new Error('At least one profile field must be provided');
31
+ }
32
+
33
+ // Name validation
34
+ if (this.name !== undefined) {
35
+ if (this.name.trim().length === 0) {
36
+ throw new Error('Name cannot be empty');
37
+ }
38
+ if (this.name.length > 50) {
39
+ throw new Error('Name cannot exceed 50 characters');
40
+ }
41
+ }
42
+
43
+ // URL validation (minimal - just check if not empty when provided)
44
+ if (this.url !== undefined && this.url.trim().length === 0) {
45
+ throw new Error('URL cannot be empty');
46
+ }
47
+
48
+ // Location validation
49
+ if (this.location !== undefined) {
50
+ if (this.location.trim().length === 0) {
51
+ throw new Error('Location cannot be empty');
52
+ }
53
+ if (this.location.length > 30) {
54
+ throw new Error('Location cannot exceed 30 characters');
55
+ }
56
+ }
57
+
58
+ // Description validation
59
+ if (this.description !== undefined) {
60
+ if (this.description.trim().length === 0) {
61
+ throw new Error('Description cannot be empty');
62
+ }
63
+ if (this.description.length > 160) {
64
+ throw new Error('Description cannot exceed 160 characters');
65
+ }
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,58 @@
1
+ import { Cookie } from 'cookiejar';
2
+
3
+ import { IAuthCookie } from '../../types/auth/AuthCookie';
4
+
5
+ /**
6
+ * The cookie containing the tokens that are used to authenticate against Twitter.
7
+ *
8
+ * @internal
9
+ */
10
+ export class AuthCookie implements IAuthCookie {
11
+ /* eslint-disable @typescript-eslint/naming-convention */
12
+
13
+ public auth_token: string;
14
+ public ct0: string;
15
+ public kdt: string;
16
+ public twid: string;
17
+
18
+ /* eslint-enable @typescript-eslint/naming-convention */
19
+
20
+ /**
21
+ * @param cookies - The cookie list obtained from the browser.
22
+ */
23
+ public constructor(cookies: Cookie[]) {
24
+ // Initializing defaults
25
+ this.auth_token = '';
26
+ this.ct0 = '';
27
+ this.kdt = '';
28
+ this.twid = '';
29
+
30
+ // Parsing the cookies
31
+ for (const cookie of cookies) {
32
+ if (cookie.name == 'kdt') {
33
+ this.kdt = cookie.value;
34
+ } else if (cookie.name == 'twid') {
35
+ this.twid = cookie.value;
36
+ } else if (cookie.name == 'ct0') {
37
+ this.ct0 = cookie.value;
38
+ } else if (cookie.name == 'auth_token') {
39
+ this.auth_token = cookie.value;
40
+ }
41
+ }
42
+ }
43
+
44
+ /**
45
+ * @returns the string representation of 'this' object.
46
+ */
47
+ public toString(): string {
48
+ /** The string representation of 'this' object. */
49
+ let outStr = '';
50
+
51
+ // Iterating through the (key, value) pairs of this cookie
52
+ for (const [key, value] of Object.entries(this)) {
53
+ outStr += `${key}=${value as string};`;
54
+ }
55
+
56
+ return outStr;
57
+ }
58
+ }