-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestUtils.js
More file actions
1033 lines (913 loc) · 35.6 KB
/
RestUtils.js
File metadata and controls
1033 lines (913 loc) · 35.6 KB
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
var hmacsha1 = require('hmacsha1');
const Constants = {
//Dev Parse keys
TWITTER_COMSUMER_KEY: "<Your twitter-app's consumer key>",
TWITTER_CONSUMER_SECRET: "<Your twitter-app's consumer secret>",
};
export function _getSignature(user_auth_token, accesstoken_secret, data){
// let signing_key = encodeURIComponent(Constants.TWITTER_CONSUMER_SECRET)+'&'+
// encodeURIComponent(Constants.ACCESS_TOKEN_SECRET);
let signing_key = encodeURIComponent(Constants.TWITTER_CONSUMER_SECRET)+'&'+
encodeURIComponent(accesstoken_secret);
console.log('signing data');
console.log(data);
console.log('signing key');
console.log(signing_key);
return hmacsha1(signing_key, data);
}
/*
let include_entities_key = encodeURIComponent('include_entities');
let include_entities_val = encodeURIComponent('false');
let oauth_consumer_key_key = encodeURIComponent('oauth_consumer_key');
let oauth_consumer_key_val = encodeURIComponent(Constants.TWITTER_COMSUMER_KEY);
let oauth_nonce_key = encodeURIComponent('oauth_nonce');
let oauth_nonce_val = encodeURIComponent(_getNonce());
let oauth_signature_method_key = encodeURIComponent('oauth_signature_method');
let oauth_signature_method_val = encodeURIComponent('HMAC-SHA1');
let oauth_timestamp_key = encodeURIComponent('oauth_timestamp');
var val = Math.round(Date.now() / 1000);
let oauth_timestamp_val = encodeURIComponent(val);
let oauth_token_key = encodeURIComponent('oauth_token');
// let oauth_token_val = encodeURIComponent(Constants.ACCESS_TOKEN);
let oauth_token_val = encodeURIComponent(user_auth_token);
let oauth_version_key = encodeURIComponent('oauth_version');
let oauth_version_val = encodeURIComponent('1.0');
*/
export function _buildRequestHeader(user_auth_token, accesstoken_secret, url, parameter_dict, include_entities_bool, post){
// https://dev.twitter.com/oauth/overview/creating-signatures
// https://dev.twitter.com/oauth/overview/authorizing-requests
// encodeURI componests when creating
let params = {};
if(include_entities_bool){
params.include_entities = include_entities_bool;
}
params.oauth_consumer_key = Constants.TWITTER_COMSUMER_KEY;
params.oauth_nonce = _getNonce();
params.oauth_signature_method = 'HMAC-SHA1';
var val = Math.round(Date.now() / 1000);
params.oauth_timestamp = val;
params.oauth_token = user_auth_token;
params.oauth_version = '1.0';
if(parameter_dict){
Object.keys(parameter_dict).forEach(function(key) {
// console.log(key, obj[key]);
params[key] = parameter_dict[key];
});
}
let parameter_keys = Object.keys(params).sort();
let parameter_string = '';
let i = 0;
parameter_keys.forEach(key=>{
if(i != parameter_keys.length-1){
parameter_string += encodeURIComponent(key)+'='+encodeURIComponent(params[key])+'&';
}else{
parameter_string += encodeURIComponent(key)+'='+encodeURIComponent(params[key]);
}
i++
});
let data = _getBaseString(post?'POST':'GET', url,
parameter_string);
let signature = _getSignature(user_auth_token, accesstoken_secret, data)
console.log('signature'+signature);
params.oauth_signature = signature;
parameter_keys = Object.keys(params).sort();
// let oauth_signature_key = encodeURIComponent('oauth_signature');
// let oauth_signature_val = encodeURIComponent(signature);
let request_header_string = 'OAuth ';
i=0;
parameter_keys.forEach(key=>{
if(i != parameter_keys.length-1){
request_header_string += encodeURIComponent(key)+'="'+encodeURIComponent(params[key])+'", ';
}else{
request_header_string += encodeURIComponent(key)+'="'+encodeURIComponent(params[key])+'"';
}
i++;
});
console.log(request_header_string);
return request_header_string;
}
export function _getBaseString(method, url, parameter_string){
return method+'&'+
encodeURIComponent(url)+'&'+encodeURIComponent(parameter_string);
}
export function _getNonce(){
// https://stackoverflow.com/questions/10051494/oauth-nonce-value
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < 42; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
export function b64EncodeUnicode(str) {
// first we use encodeURIComponent to get percent-encoded UTF-8,
// then we convert the percent encodings into raw bytes which
// can be fed into btoa.
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function toSolidBytes(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
export async function fetchTimeline(tweet_count, success_callback, failure_callback){
try {
console.log('calling')
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';//?count=60'
let url = base_url+'?count='+tweet_count;
let parameter_dict = {count: tweet_count};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict);
return fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return null;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return null;
}
}
export async function fetchUserTweets(screen_name, tweet_count, success_callback, failure_callback){
// https://dev.twitter.com/rest/reference/get/statuses/user_timeline
// GET https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
let url = base_url+'?screen_name='+screen_name+'&count='+tweet_count;
let parameter_dict = {screen_name: screen_name, count: tweet_count};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict, false);
fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return null;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return null;
}
}
export async function fetchUserTweetsAfter(screen_name, tweet_id, tweet_count, success_callback, failure_callback){
// https://dev.twitter.com/rest/reference/get/statuses/user_timeline
// GET https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
try {
// max_id optional Returns results with an ID less than (that is, older than) or equal to the specified ID.
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
let url = base_url+'?screen_name='+
screen_name+'&count='+tweet_count+'&max_id'+tweet_id;
parameter_dict={screen_name: screen_name, count: tweet_count, max_id: tweet_id};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict);
fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return null;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return null;
}
}
export async function likeStatus(tweet_id, success_callback, failure_callback){
// https://dev.twitter.com/rest/reference/post/favorites/destroy
// POST https://api.twitter.com/1.1/favorites/create.json?id=243138128959913986
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
console.log(tweet_id);
let base_url = 'https://api.twitter.com/1.1/favorites/create.json'
let url = base_url+'?id='+tweet_id;
parameter_dict = {id: tweet_id};
console.log(url);
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict, false, true);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json)
return json;
});
}else{
console.log('no data')
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function unlikeStatus(tweet_id, success_callback, failure_callback){
// https://dev.twitter.com/rest/reference/post/favorites/destroy
// POST https://api.twitter.com/1.1/favorites/destroy.json?id=243138128959913986
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/favorites/destroy.json'
let url = base_url+'?id='+tweet_id;
parameter_dict = {id: tweet_id};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data')
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function retweetStatus(tweet_id, success_callback, failure_callback){
// POST https://api.twitter.com/1.1/statuses/retweet/243149503589400576.json
try {
console.log('calling')
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let url = 'https://api.twitter.com/1.1/statuses/retweet/'+tweet_id+'.json';
console.log(url);
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, url, {}, false, true);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function unRetweet(tweet_id, success_callback, failure_callback){
// POST https://api.twitter.com/1.1/statuses/unretweet/241259202004267009.json
try {
console.log('calling')
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let url = 'https://api.twitter.com/1.1/statuses/unretweet/'+tweet_id+'.json';
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function postStatus(status, success_callback, failure_callback){
// https://api.twitter.com/1.1/statuses/update.json
// POST https://api.twitter.com/1.1/statuses/update.json?status=Maybe%20he%27ll%20finally%20find%20his%20keys.%20%23peterfalk
// body: form?
try {
console.log('calling')
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/statuses/update.json';
let url = base_url+'?status='+encodeURIComponent(status);
let parameter_dict = {status: status};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict, false, true);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
},
// body: 'status='+encodeURIComponent(status)
}).then((response) =>{
console.log(response);
return response})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data')
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function quoteStatus(status, quoted_handle, quoted_status_id, success_callback, failure_callback){
// https://twittercommunity.com/t/how-to-quote-some-text-with-a-retweet/69679
/*
Note: URLs for Quote Tweet or DM deep links that are typed or pasted into a Tweet
will still count against the character limit. The new attachment_url parameter
on the POST statuses/update endpoint will enable valid link formats to be attached to a Tweet.
They will not count against the character limit when this method is used.
The new attachment_url parameter
*/
/*
quote tweet is just an ordinary tweet with a permalink to another tweet at the end. So a call to https://dev.twitter.com/rest/reference/post/statuses/update21 with the text:
This is a quote tweet https://twitter.com/TwitterDev/status/740196895784570880
Will quote the tweet 740196895784570880 with your comment
1 Like
Reply
BurleyBrianJul '16
Thanks for your reply Igor. I tried it and it is working as expected.
I just want to clarify one more thing. When we append the link of parent tweet while retweeting, it (link of parent tweet) won't be accounted in 140 chars of tweet once twitter introduces the char count changes on 24th july.
Reply
IgorBrigadirRegularJul '16
As far as i can tell, according to https://dev.twitter.com/overview/api/upcoming-changes-to-tweets19 you'll need to specify the tweet permalink url in attachment_url parameter, then it won't count. But if you append the url to the end of the text, like above it will count towards 140 characters.
*/
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let attachment_url = 'https://twitter.com/'+quoted_handle+'/status/'+quoted_status_id;
let base_url = 'https://api.twitter.com/1.1/statuses/update.json'
let url = base_url+'?status='+
encodeURIComponent(status)+'&'+
'attachment_url='+attachment_url;
parameter_dict = {status: status, attachment_url: attachment_url};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict, true);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data')
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function replyTweet(status, in_reply_to_status_id, success_callback, failure_callback){
try {
console.log('calling')
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/statuses/update.json';
let url = base_url+'?status='+
encodeURIComponent(status)+'&'+
'in_reply_to_status_id='+in_reply_to_status_id;
let parameter_dict = {status: status, in_reply_to_status_id: in_reply_to_status_id};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict, true);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data')
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function follow(user_id, success_callback, failure_callback){
// POST https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true
try {
console.log('calling')
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/friendships/create.json'
let url = base_url+'?user_id='+user_id+
'&follow=true';
let parameter_dict = {user_id: user_id, follow: true};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict, true);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function unfollow(user_id, success_callback, failure_callback){
// Object https://api.twitter.com/1.1/friendships/destroy.json?user_id=1401881
try {
console.log('calling')
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/friendships/destroy.json';
let url = base_url+'?user_id='+user_id;
let parameter_dict = {user_id: user_id};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict, true);
fetch(url, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data')
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function fetchFriendship(source_screen_name, target_screen_name, success_callback, failure_callback){
// Returns detailed information about the relationship between two arbitrary users.
// GET https://api.twitter.com/1.1/friendships/show.json?source_screen_name=bert&target_screen_name=ernie
/*
{
"relationship": {
"target": {
"id_str": "12148",
"id": 12148,
"screen_name": "ernie",
"following": false,
"followed_by": false
},
"source": {
"can_dm": false,
"blocking": null,
"muting": null,
"id_str": "8649302",
"all_replies": null,
"want_retweets": null,
"id": 8649302,
"marked_spam": null,
"screen_name": "bert",
"following": false,
"followed_by": false,
"notifications_enabled": null
}
}
}
*/
try {
// https://api.twitter.com/1.1/friendships/show.json?source_screen_name=bert&target_screen_name=ernie
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url='https://api.twitter.com/1.1/friendships/show.json'
let url = base_url+'?source_screen_name='+
source_screen_name+'&target_screen_name='+target_screen_name;
let parameter_dict = {
target_screen_name: target_screen_name,
source_screen_name: source_screen_name
};
console.log(url);
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict);
fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
},
qs: {target_screen_name: target_screen_name, source_screen_name: source_screen_name}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function getUserFriends(screen_name, success_callback, failure_callback){
// GET https://api.twitter.com/1.1/friends/list.json?cursor=-1&screen_name=twitterapi&skip_status=true&include_user_entities=false
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/users/friends/list.json'
let url = base_url+ '?cursor=-1&screen_name='+
screen_name+'&skip_status=true&include_user_entities=false';
let parameter_dict = {
cursor: -1,
screen_name: screen_name,
skip_status: true,
include_user_entities: false
}
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict);
fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function getUserFollowers(screen_name, success_callback, failure_callback){
// GET https://api.twitter.com/1.1/followers/list.json?cursor=-1&screen_name=twitterdev&skip_status=true&include_user_entities=false
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/users/followers/list.json';
let url = base_url+'?cursor=-1&screen_name='+
screen_name+'&skip_status=true&include_user_entities=false';
let parameter_dict={
screen_name: screen_name,
skip_status: true,
include_user_entities: false
}
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict);
fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function fetchProfile(screen_name, success_callback, failure_callback){
// GET https://api.twitter.com/1.1/users/show.json?screen_name=twitterdev
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let base_url = 'https://api.twitter.com/1.1/users/show.json';
let url = base_url+'?screen_name='+screen_name;
let parameter_dict = {screen_name: screen_name}
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict);
fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
failure_callback();
console.log('no data')
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export async function fetchTweetByID(tweet_id, success_callback, failure_callback){
// GET https://api.twitter.com/1.1/statuses/show.json?id=210462857140252672
// This request could also be obtained with: GET https://api.twitter.com/1.1/statuses/show/210462857140252672.json
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
let url = 'https://api.twitter.com/1.1/statuses/show/'+tweet_id+'.json';
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, url);
fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{ return response.json()})
.then((json) => {
console.log(json);
success_callback(json);
return json;
});
}else{
console.log('no data');
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}
export function fetchTwitterThreadAncestory(tweet_id){
// i think this is just a nested in replyto thing
//recursive
let tweets = [];
return fetchTweetAncestor(tweet_id, tweets);
}
export function fetchTweetAncestor(tweet_id, tweets){
tweets.unshift()
fetchTweetByID(tweet_id).then(res=>{
tweets.unshift(res);
if(res.in_reply_to_status_id != null){
fetchTweetAncestor(res.in_reply_to_status_id, tweet)
}else{
return tweets;
}
}).catch(err=>{
return null;
});
}
export async function fetchTweetReplies(tweet_id, handle, success_callback, failure_callback){
/* GET https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4
There is a workaround using the REST API.
You will need the id_str and @username of the author of the original tweet
you want to find replies to.
You should use the Search API for the "@username" of the author.
Go through the results looking for the 'in_reply_to_status_id'
field to compare to the id_str of the specific tweet you want replies for.
*/
let tweets = [];
try {
const twitter_token = "<Your method to get the user's twitter token>";
const twitter_tokenSecret = "<Your method to get the user's twitter token-secret>";
if (twitter_tokenSecret !== null && twitter_token !== null){
// You will need the id_str and @username of the author of the original tweet
// you want to find replies to.
// You should use the Search API for the "@username" of the author.
// when you fetch the tweet store the tweetId ie., id_str
// using twitter search api do the following query [q="to:$tweeterusername", sinceId = $tweetId]
// Loop all the results , the results matching the in_reply_to_status_id_str to $tweetid is the replies for the post.
let query = '@'+handle;
let base_url = 'https://api.twitter.com/1.1/search/tweets.json';
let url = base_url + '?q='+encodeURIComponent(query)+'&since_id='+tweet_id;
let parameter_dict = {q:query, since_id: tweet_id};
let header = _buildRequestHeader(twitter_token, twitter_tokenSecret, base_url, parameter_dict);
console.log(url);
fetch(url, {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': header
}
}).then((response) =>{
console.log(response);
return response.json()}
).then((json) => {
console.log(json);
let statuses = json.statuses;
// Go through the results looking for the 'in_reply_to_status_id'
// field to compare to the id_str of the specific tweet you want replies for.
statuses.forEach((tweet)=>{
console.log(tweet);
if(tweet.in_reply_to_status_id == tweet_id){
tweets.push(tweet);
}
})
if(callback){
success_callback(tweets);
}
});
}else{
console.log('no data');
failure_callback();
return false;
}
} catch (error) {
// Error retrieving data
console.log('there was an error retrieving timeline');
console.log(error);
failure_callback();
return true;
}
}