-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
1829 lines (1525 loc) · 52.1 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package twigo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/arshamalh/twigo/utils"
)
const (
base_route = "https://api.twitter.com/2/"
)
type Client struct {
authorizedClient *http.Client
consumerKey string
consumerSecret string
accessToken string
accessTokenSecret string
bearerToken string
read_only_access bool
userID string
oauth_type OAuthType
}
type Map map[string]interface{}
// ** Requests ** //
func (c *Client) request(method, route string, params Map) (*http.Response, error) {
// OAuth_1a is always true for post and put routes
dataPayload, err := json.Marshal(params)
if err != nil {
return nil, err
}
request, err := http.NewRequest(method, base_route+route, bytes.NewBuffer(dataPayload))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
response, err := c.authorizedClient.Do(request)
return response, err
}
// Sends a get request with specified params
//
// oauth_1a ==> Whether or not to use OAuth 1.0a User context
func (c *Client) get_request(route string, oauth_type OAuthType, params Map, endpoint_parameters []string) (*http.Response, error) {
parsedRoute, err := url.Parse(route)
if err != nil {
return nil, err
}
parsedRoute.RawQuery = utils.QueryMaker(params, endpoint_parameters)
fullRoute := base_route + parsedRoute.String()
if oauth_type == OAuth_1a {
//%% TODO: Should we define authorizedClient here? or tweepy is doing it wrong?
return c.authorizedClient.Get(fullRoute)
} else {
request, err := http.NewRequest("GET", fullRoute, nil)
if err != nil {
return nil, err
}
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
client := http.Client{}
resp, err := client.Do(request)
if resp.StatusCode != 200 {
err := SpecialError{}
json.NewDecoder(resp.Body).Decode(&err)
defer resp.Body.Close()
return nil, err.Error()
}
return resp, err
}
}
func (c *Client) delete_request(route string) (*http.Response, error) {
// OAuth_1a is always true for delete routes
request, err := http.NewRequest("DELETE", base_route+route, nil)
if err != nil {
return nil, err
}
return c.authorizedClient.Do(request)
}
func (c *Client) SetOAuth(oauth_type OAuthType) *Client {
if c.read_only_access {
oauth_type = OAuth_2
}
if c.bearerToken == "" {
oauth_type = OAuth_1a
}
c.oauth_type = oauth_type
return c
}
// TODO: Integrate this to get_request, for each request,
// see if the oauth method is default, set it!, but we need the caller function?
// or maybe we can call this function inside each method
func (c *Client) SetDefaultOAuth(caller string) *Client {
if doa, ok := DefaultOAuthes[caller]; ok {
c.oauth_type = doa
} else {
c.oauth_type = OAuth_2
}
return c
}
// ** Manage Tweets ** //
// Creates a Tweet on behalf of an authenticated user
//
// Parameters
//
// text: Text of the Tweet being created. this field is required if media.media_ids is not present, otherwise pass empty string.
//
// params: A map of parameters.
// you can pass some extra parameters, such as:
// "direct_message_deep_link", "for_super_followers_only", "media", "geo", "poll", "reply", "reply_settings", "quote_tweet_id",
// Some of these parameters are a little special and should be passed like this:
// media := map[string][]string{
// "media_ids": []string{}
// "tagged_user_ids": []string{}
// }
// poll := twigo.Map{
// "options": map[string]string{},
// "duration_minutes": int value,
// }
// reply := twigo.Map{
// "in_reply_to_tweet_id": "",
// "exclude_reply_user_ids": []string{},
// }
// geo := map[string]string{"place_id": value}
// params := twigo.Map{
// "text": text
// "media": media,
// "geo": geo,
// "poll": poll,
// "reply": reply,
// }
//
// Reference
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets
func (c *Client) CreateTweet(text string, params Map) (*TweetResponse, error) {
if params == nil {
params = make(Map)
}
if text != "" {
params["text"] = text
} else if params["media"] == nil {
return nil, fmt.Errorf("text or media is required")
}
response, err := c.request(
"POST",
"tweets",
params,
)
if err != nil {
return nil, err
}
return (&TweetResponse{}).Parse(response)
}
// Allows an authenticated user ID to delete a Tweet
//
// Parameters
//
// tweet_id: The Tweet ID you are deleting.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/delete-tweets-id
func (c *Client) DeleteTweet(tweet_id string) (*DeleteResponse, error) {
route := fmt.Sprintf("tweets/%s", tweet_id)
response, err := c.delete_request(route)
if err != nil {
return nil, err
}
return (&DeleteResponse{}).Parse(response)
}
// ** Likes ** //
// Like a Tweet.
//
// Parameters
//
// tweet_id: The ID of the Tweet that you would like to Like.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/post-users-id-likesx
func (c *Client) Like(tweet_id string) (*LikeResponse, error) {
data := Map{
"tweet_id": tweet_id,
}
route := fmt.Sprintf("users/%s/likes", c.userID)
response, err := c.request(
"POST",
route,
data,
)
if err != nil {
return nil, err
}
return (&LikeResponse{}).Parse(response)
}
// Unlike a Tweet.
//
// The request succeeds with no action when the user sends a request to a
// user they're not liking the Tweet or have already unliked the Tweet.
//
// Parameters
//
// tweet_id: The ID of the Tweet that you would like to unlike.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/delete-users-id-likes-tweet_id
func (c *Client) Unlike(tweet_id string) (*LikeResponse, error) {
route := fmt.Sprintf("users/%s/likes/%s", c.userID, tweet_id)
response, err := c.delete_request(route)
if err != nil {
return nil, err
}
return (&LikeResponse{}).Parse(response)
}
// Allows you to get information about a Tweet’s liking users.
//
// Parameters
//
// tweet_id: Tweet ID of the Tweet to request liking users of.
//
// params (keys):
// "expansions", "media.fields", "place.fields",
// "poll.fields", "tweet.fields", "user.fields",
// "max_results"
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-tweets-id-liking_users
func (c *Client) GetLikingUsers(tweet_id string, params Map) (*UsersResponse, error) {
endpoint_parameters := []string{
"expansions", "media.fields", "place.fields",
"poll.fields", "tweet.fields", "user.fields",
"max_results", "pagination_token",
}
route := fmt.Sprintf("tweets/%s/liking_users", tweet_id)
if params == nil {
params = make(Map)
}
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: tweet_id, Params: params}
users := &UsersResponse{Caller: c.GetLikingUsers, CallerData: caller_data}
return users.Parse(response)
}
// Allows you to get information about a user’s liked Tweets.
//
// The Tweets returned by this endpoint count towards the Project-level `Tweet cap`.
//
// Parameters
//
// tweet_id: User ID of the user to request liked Tweets for.
//
// params (keys):
// "expansions", "media.fields", "place.fields",
// "poll.fields", "tweet.fields", "user.fields",
// "max_results"
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-users-id-liked_tweets
func (c *Client) GetLikedTweets(user_id string, params Map) (*TweetsResponse, error) {
endpoint_parameters := []string{
"expansions", "max_results", "media.fields",
"pagination_token", "place.fields", "poll.fields",
"tweet.fields", "user.fields",
}
route := fmt.Sprintf("users/%s/liked_tweets", user_id)
if params == nil {
params = make(Map)
}
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: user_id, Params: params}
tweets := &TweetsResponse{Caller: c.GetLikedTweets, CallerData: caller_data}
return tweets.Parse(response)
}
// ** Hide replies ** //
// Hides a reply to a Tweet
//
// Parameters
//
// reply_id:
// Unique identifier of the Tweet to hide. The Tweet must belong to a
// conversation initiated by the authenticating user.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/hide-replies/api-reference/put-tweets-id-hidden
func (c *Client) HideReply(reply_id string) (*HideReplyResponse, error) {
data := Map{
"hidden": true,
}
route := fmt.Sprintf("tweets/%s/hidden", reply_id)
response, err := c.request(
"PUT",
route,
data,
)
if err != nil {
return nil, err
}
return (&HideReplyResponse{}).Parse(response)
}
// Unhides a reply to a Tweet
//
// Parameters
//
// reply_id:
// Unique identifier of the Tweet to unhide. The Tweet must belong to
// a conversation initiated by the authenticating user.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/hide-replies/api-reference/put-tweets-id-hidden
func (c *Client) UnHideReply(reply_id string) (*HideReplyResponse, error) {
data := Map{
"hidden": false,
}
route := fmt.Sprintf("tweets/%s/hidden", reply_id)
response, err := c.request(
"PUT",
route,
data,
)
if err != nil {
return nil, err
}
return (&HideReplyResponse{}).Parse(response)
}
// ** Retweets ** //
// Causes the user ID to Retweet the target Tweet.
//
// Parameters
//
// tweet_id: The ID of the Tweet that you would like to Retweet.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/retweets/api-reference/post-users-id-retweets
func (c *Client) Retweet(tweet_id string) (*RetweetResponse, error) {
data := Map{
"tweet_id": tweet_id,
}
route := fmt.Sprintf("users/%s/retweets", c.userID)
response, err := c.request(
"POST",
route,
data,
)
if err != nil {
return nil, err
}
return (&RetweetResponse{}).Parse(response)
}
// Allows an authenticated user ID to remove the Retweet of a Tweet.
//
// The request succeeds with no action when the user sends a request to a
// user they're not Retweeting the Tweet or have already removed the
// Retweet of.
//
// Parameters
//
// tweet_id: The ID of the Tweet that you would like to remove the Retweet of.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/retweets/api-reference/delete-users-id-retweets-tweet_id
func (c *Client) UnRetweet(tweet_id string) (*RetweetResponse, error) {
route := fmt.Sprintf("users/%s/retweets/%s", c.userID, tweet_id)
response, err := c.delete_request(route)
if err != nil {
return nil, err
}
return (&RetweetResponse{}).Parse(response)
}
// Allows you to get information about who has Retweeted a Tweet.
//
// Parameters
//
// tweet_id: Tweet ID of the Tweet to request Retweeting users of.
//
// params (keys):
// "expansions", "media.fields", "place.fields",
// "poll.fields", "tweet.fields", "user.fields",
// "max_results", "pagination_token"
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/retweets/api-reference/get-tweets-id-retweeted_by
func (c *Client) GetRetweeters(tweet_id string, params Map) (*UsersResponse, error) {
endpoint_parameters := []string{
"expansions", "media.fields", "place.fields",
"poll.fields", "tweet.fields", "user.fields",
"max_results", "pagination_token",
}
route := fmt.Sprintf("tweets/%s/retweeted_by", tweet_id)
if params == nil {
params = make(Map)
}
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: tweet_id, Params: params}
users := &UsersResponse{Caller: c.GetRetweeters, CallerData: caller_data}
return users.Parse(response)
}
// Returns Quote Tweets for a Tweet specified by the requested Tweet ID.
//
// The Tweets returned by this endpoint count towards the Project-level
// `Tweet cap`.
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/quote-tweets/api-reference/get-tweets-id-quote_tweets
func (c *Client) GetQuoteTweets(tweet_id string, params Map) (*TweetsResponse, error) {
endpoint_parameters := []string{
"expansions", "media.fields", "place.fields",
"poll.fields", "tweet.fields", "user.fields",
"max_results", "pagination_token",
}
route := fmt.Sprintf("tweets/%s/quoted_tweets", tweet_id)
if params == nil {
params = make(Map)
}
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: tweet_id, Params: params}
tweets := &TweetsResponse{Caller: c.GetQuoteTweets, CallerData: caller_data}
return tweets.Parse(response)
}
// ** Search tweets ** //
// The full-archive search endpoint returns the complete history of public
// Tweets matching a search query; since the first Tweet was created March
// 26, 2006.
//
// This endpoint is only available to those users who have been approved for the `Academic Research product track`
//
// The Tweets returned by this endpoint count towards the Project-level `Tweet cap`.
//
// Parameters
//
// query : str
// One query for matching Tweets. Up to 1024 characters.
// end_time : Union[datetime.datetime, str]
// YYYY-MM-DDTHH:mm:ssZ (ISO 8601/RFC 3339). Used with ``start_time``.
// The newest, most recent UTC timestamp to which the Tweets will be
// provided. Timestamp is in second granularity and is exclusive (for
// example, 12:00:01 excludes the first second of the minute). If used
// without ``start_time``, Tweets from 30 days before ``end_time``
// will be returned by default. If not specified, ``end_time`` will
// default to [now - 30 seconds].
// max_results : int
// The maximum number of search results to be returned by a request. A
// number between 10 and the system limit (currently 500). By default,
// a request response will return 10 results.
// next_token : str
// This parameter is used to get the next 'page' of results. The value
// used with the parameter is pulled directly from the response
// provided by the API, and should not be modified. You can learn more
// by visiting our page on `pagination`_.
// since_id : Union[int, str]
// Returns results with a Tweet ID greater than (for example, more
// recent than) the specified ID. The ID specified is exclusive and
// responses will not include it. If included with the same request as
// a ``start_time`` parameter, only ``since_id`` will be used.
// start_time : Union[datetime.datetime, str]
// YYYY-MM-DDTHH:mm:ssZ (ISO 8601/RFC 3339). The oldest UTC timestamp
// from which the Tweets will be provided. Timestamp is in second
// granularity and is inclusive (for example, 12:00:01 includes the
// first second of the minute). By default, a request will return
// Tweets from up to 30 days ago if you do not include this parameter.
// until_id: string
// Returns results with a Tweet ID less than (that is, older than) the
// specified ID. Used with ``since_id``. The ID specified is exclusive
// and responses will not include it.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-all
//
// Academic Research product track: https://developer.twitter.com/en/docs/projects/overview#product-track
//
// Tweet cap: https://developer.twitter.com/en/docs/projects/overview#tweet-cap
//
// pagination: https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/paginate
func (c *Client) SearchAllTweets(query string, params Map) (*TweetsResponse, error) {
endpoint_parameters := []string{
"end_time", "expansions", "max_results", "media.fields",
"next_token", "place.fields", "poll.fields", "query",
"since_id", "start_time", "tweet.fields", "until_id",
"user.fields",
}
route := "tweets/search/all"
if params == nil {
params = make(Map)
} else if val, ok := params["pagination_token"]; ok {
params["next_token"] = val
delete(params, "pagination_token")
}
params["query"] = query
response, err := c.get_request(route, OAuth_2, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: query, Params: params}
tweets := &TweetsResponse{Caller: c.GetUserTweets, CallerData: caller_data}
return tweets.Parse(response)
}
// The recent search endpoint returns Tweets from the last seven days that match a search query.
//
// The Tweets returned by this endpoint count towards the Project-level
// `Tweet cap`.
//
// Parameters
//
// query : str
// One rule for matching Tweets. If you are using a
// `Standard Project`_ at the Basic `access level`_, you can use the
// basic set of `operators`_ and can make queries up to 512 characters
// long. If you are using an `Academic Research Project`_ at the Basic
// access level, you can use all available operators and can make
// queries up to 1,024 characters long.
// end_time : Union[datetime.datetime, str]
// YYYY-MM-DDTHH:mm:ssZ (ISO 8601/RFC 3339). The newest, most recent
// UTC timestamp to which the Tweets will be provided. Timestamp is in
// second granularity and is exclusive (for example, 12:00:01 excludes
// the first second of the minute). By default, a request will return
// Tweets from as recent as 30 seconds ago if you do not include this
// parameter.
// max_results : int
// The maximum number of search results to be returned by a request. A
// number between 10 and 100. By default, a request response will
// return 10 results.
// next_token : str
// This parameter is used to get the next 'page' of results. The value
// used with the parameter is pulled directly from the response
// provided by the API, and should not be modified.
// since_id : Union[int, str]
// Returns results with a Tweet ID greater than (that is, more recent
// than) the specified ID. The ID specified is exclusive and responses
// will not include it. If included with the same request as a
// ``start_time`` parameter, only ``since_id`` will be used.
// start_time : Union[datetime.datetime, str]
// YYYY-MM-DDTHH:mm:ssZ (ISO 8601/RFC 3339). The oldest UTC timestamp
// (from most recent seven days) from which the Tweets will be
// provided. Timestamp is in second granularity and is inclusive (for
// example, 12:00:01 includes the first second of the minute). If
// included with the same request as a ``since_id`` parameter, only
// ``since_id`` will be used. By default, a request will return Tweets
// from up to seven days ago if you do not include this parameter.
// until_id : Union[int, str]
// Returns results with a Tweet ID less than (that is, older than) the
// specified ID. The ID specified is exclusive and responses will not
// include it.
//
// References
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-recent
//
// Tweet cap: https://developer.twitter.com/en/docs/projects/overview#tweet-cap
//
// Standard Project: https://developer.twitter.com/en/docs/projects
//
// access level: https://developer.twitter.com/en/products/twitter-api/early-access/guide.html#na_1
//
// operators: https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-query
//
// Academic Research Project: https://developer.twitter.com/en/docs/projects
func (c *Client) SearchRecentTweets(query string, params Map) (*TweetsResponse, error) {
endpoint_parameters := []string{
"end_time", "expansions", "max_results", "media.fields",
"next_token", "place.fields", "poll.fields", "query",
"since_id", "start_time", "tweet.fields", "until_id",
"user.fields",
}
route := "tweets/search/recent"
if params == nil {
params = make(Map)
} else if val, ok := params["pagination_token"]; ok {
params["next_token"] = val
delete(params, "pagination_token")
}
params["query"] = query
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: query, Params: params}
tweets := &TweetsResponse{Caller: c.GetUserTweets, CallerData: caller_data}
return tweets.Parse(response)
}
// ** Timelines ** //
// Returns Tweets composed by a single user, specified by the requested
// user ID. By default, the most recent ten Tweets are returned per
// request. Using pagination, the most recent 3,200 Tweets can be
// retrieved.
//
// The Tweets returned by this endpoint count towards the Project-level `Tweet cap`.
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-tweets
func (c *Client) GetUserTweets(user_id string, params Map) (*TweetsResponse, error) {
endpoint_parameters := []string{
"end_time", "exclude", "expansions", "max_results",
"media.fields", "pagination_token", "place.fields",
"poll.fields", "since_id", "start_time", "tweet.fields",
"until_id", "user.fields",
}
route := fmt.Sprintf("users/%s/tweets", user_id)
if params == nil {
params = make(Map)
}
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: user_id, Params: params}
tweets := &TweetsResponse{Caller: c.GetUserTweets, CallerData: caller_data}
return tweets.Parse(response)
}
// Returns Tweets mentioning a single user specified by the requested user
// ID. By default, the most recent ten Tweets are returned per request.
// Using pagination, up to the most recent 800 Tweets can be retrieved.
//
// The Tweets returned by this endpoint count towards the Project-level `Tweet cap`.
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-mentions
func (c *Client) GetUserMentions(user_id string, params Map) (*TweetsResponse, error) {
endpoint_parameters := []string{
"end_time", "expansions", "max_results", "media.fields",
"pagination_token", "place.fields", "poll.fields", "since_id",
"start_time", "tweet.fields", "until_id", "user.fields",
}
route := fmt.Sprintf("users/%s/mentions", user_id)
if params == nil {
params = make(Map)
}
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: user_id, Params: params}
tweets := &TweetsResponse{Caller: c.GetUserMentions, CallerData: caller_data}
return tweets.Parse(response)
}
// ** Tweet counts ** //
// This endpoint is only available to those users who have been approved
// for the `Academic Research product track`_.
//
// The full-archive search endpoint returns the complete history of public
// Tweets matching a search query; since the first Tweet was created March
// 26, 2006.
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/counts/api-reference/get-tweets-counts-all
func (c *Client) GetAllTweetsCount(query string, params Map) (*TweetsCountResponse, error) {
endpoint_parameters := []string{
"end_time", "granularity", "next_token", "query",
"since_id", "start_time", "until_id",
}
if params == nil {
params = make(Map)
}
params["query"] = query
route := "tweets/counts/all"
response, err := c.get_request(route, OAuth_2, params, endpoint_parameters)
if err != nil {
return nil, err
}
return (&TweetsCountResponse{}).Parse(response)
}
// The recent Tweet counts endpoint returns count of Tweets from the last
// seven days that match a search query.
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/counts/api-reference/get-tweets-counts-recent
func (c *Client) GetRecentTweetsCount(query string, params Map) (*TweetsCountResponse, error) {
endpoint_parameters := []string{
"end_time", "granularity", "query",
"since_id", "start_time", "until_id",
}
if params == nil {
params = make(Map)
}
params["query"] = query
response, err := c.get_request("tweets/counts/recent", OAuth_2, params, endpoint_parameters)
if err != nil {
return nil, err
}
return (&TweetsCountResponse{}).Parse(response)
}
// ** Tweet lookup ** //
// Returns a variety of information about a single Tweet specified by
// the requested ID.
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference/get-tweets-id
func (c *Client) GetTweet(tweet_id string, params Map) (*TweetResponse, error) {
endpoint_parameters := []string{
"expansions", "media.fields", "place.fields",
"poll.fields", "tweet.fields", "user.fields",
}
route := fmt.Sprintf("tweets/%s", tweet_id)
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
return (&TweetResponse{}).Parse(response)
}
// Returns a variety of information about the Tweet specified by the
// requested ID or list of IDs.
//
// https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference/get-tweets
func (c *Client) GetTweets(tweet_ids []string, params Map) (*TweetsResponse, error) {
endpoint_parameters := []string{
"ids", "expansions", "media.fields", "place.fields",
"poll.fields", "tweet.fields", "user.fields",
}
if params == nil {
params = make(Map)
}
params["ids"] = tweet_ids
response, err := c.get_request("tweets", c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
// TODO: Needs pagination => It gets an [] of strings, not string like others!
// But it seems this doesn't need pagination, it doesn't accept a pagination token.
return (&TweetsResponse{}).Parse(response)
}
// ** Blocks ** //
func (c *Client) Block(target_user_id string) (*BlockResponse, error) {
data := Map{
"target_user_id": target_user_id,
}
route := fmt.Sprintf("users/%s/blocking", c.userID)
response, err := c.request(
"POST",
route,
data,
)
if err != nil {
return nil, err
}
return (&BlockResponse{}).Parse(response)
}
// The request succeeds with no action when the user sends a request to a
// user they're not blocking or have already unblocked.
//
// https://developer.twitter.com/en/docs/twitter-api/users/blocks/api-reference/delete-users-user_id-blocking
func (c *Client) UnBlock(target_user_id string) (*BlockResponse, error) {
route := fmt.Sprintf("users/%s/blocking/%s", c.userID, target_user_id)
response, err := c.delete_request(route)
if err != nil {
return nil, err
}
return (&BlockResponse{}).Parse(response)
}
// Returns a list of users who are blocked by the authenticating user.
//
// https://developer.twitter.com/en/docs/twitter-api/users/blocks/api-reference/get-users-blocking
func (c *Client) GetBlocked(params Map) (*UsersResponse, error) {
// TODO: PAGINATION PROBLEM
endpoint_parameters := []string{
"expansions", "max_results", "tweet.fields",
"user.fields", "pagination_token",
}
route := fmt.Sprintf("users/%s/blocking", c.userID)
if params == nil {
params = make(Map)
}
response, err := c.get_request(route, OAuth_1a, params, endpoint_parameters)
if err != nil {
return nil, err
}
return (&UsersResponse{}).Parse(response)
}
// ** Follows ** //
// Allows a user ID to follow another user.
//
// If the target user does not have public Tweets, this endpoint will send
// a follow request.
//
// The request succeeds with no action when the authenticated user sends a
// request to a user they're already following, or if they're sending a
// follower request to a user that does not have public Tweets.
//
// https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/post-users-source_user_id-following
func (c *Client) FollowUser(target_user_id string, params Map) (*FollowResponse, error) {
data := Map{
"target_user_id": target_user_id,
}
route := fmt.Sprintf("users/%s/following", c.userID)
response, err := c.request(
"POST",
route,
data,
)
if err != nil {
return nil, err
}
return (&FollowResponse{}).Parse(response)
}
// Allows a user ID to unfollow another user.
//
// The request succeeds with no action when the authenticated user sends a
// request to a user they're not following or have already unfollowed.
//
// https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/delete-users-source_id-following
func (c *Client) UnfollowUser(target_user_id string) (*FollowResponse, error) {
route := fmt.Sprintf("users/%s/following/%s", c.userID, target_user_id)
response, err := c.delete_request(route)
if err != nil {
return nil, err
}
return (&FollowResponse{}).Parse(response)
}
// Returns a list of users who are followers of the specified user ID.
//
// https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-followers
func (c *Client) GetUserFollowers(user_id string, params Map) (*UsersResponse, error) {
endpoint_parameters := []string{
"expansions", "max_results", "tweet.fields",
"user.fields", "pagination_token",
}
route := fmt.Sprintf("users/%s/followers", user_id)
if params == nil {
params = make(Map)
}
response, err := c.get_request(route, c.oauth_type, params, endpoint_parameters)
if err != nil {
return nil, err
}
caller_data := CallerData{ID: user_id, Params: params}
users := &UsersResponse{Caller: c.GetUserFollowers, CallerData: caller_data}
return users.Parse(response)
}
// Returns a list of users the specified user ID is following
//
// https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-following
func (c *Client) GetUserFollowing(user_id string, params Map) (*UsersResponse, error) {
endpoint_parameters := []string{
"expansions", "max_results", "tweet.fields",
"user.fields", "pagination_token",
}
route := fmt.Sprintf("users/%s/following", user_id)
if params == nil {
params = make(Map)