This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
142 lines (116 loc) · 5.61 KB
/
script.js
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
const SHOULD_UNFOLLOW_MUTUALS = false;
const SHOULD_UNFOLLOW_PRIVATE = false;
let followingArray = new Array();
/** borrowed from https://github.com/qsniyg/maxurl **/
var cookies_str_to_list = function (cookiesstr) {
var cookies = [];
var splitted = cookiesstr.split(/;\s*/);
array_foreach(splitted, function (kv) {
var match = kv.match(/^\s*([^=\s]+)\s*=\s*(?:"([^"]+)"|([^"]\S*))\s*$/);
cookies.push({ name: match[1], value: match[2] || match[3] });
});
return cookies;
}
var array_foreach = function (array, cb, do_shallow_copy) {
if (do_shallow_copy) {
var newarray = [];
for (var i = 0; i < array.length; i++) {
newarray.push(array[i]);
}
array = newarray;
}
for (var k = 0; k < array.length; k++) {
if (cb(array[k], k) === false)
return;
}
};
var headers_list_to_dict = function (headers) {
var dict = {};
for (var i = 0; i < headers.length; i++) {
dict[headers[i].name.toLowerCase()] = headers[i].value;
}
return dict;
};
/******************************** */
function sendRequest(method, endpoint, body, wantsResponse) {
let myXHR = new XMLHttpRequest();
myXHR.open(method, endpoint, false);
method == 'post' && myXHR.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
myXHR.setRequestHeader("Authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA");
myXHR.setRequestHeader("x-csrf-token", headers_list_to_dict(cookies_str_to_list(document.cookie)).ct0);
myXHR.setRequestHeader('x-twitter-active-user', "yes");
method == 'post' ? myXHR.send(body) : myXHR.send();
if (wantsResponse) {
return JSON.parse(myXHR.responseText);
}
}
function doUnfollowUser(id) {
sendRequest('post', 'https://twitter.com/i/api/1.1/friendships/destroy.json', 'user_id=' + id);
console.log("Unfollowing user with id " + id)
}
function getIDbyScreenName(screenname) {
let body = { "screen_name": screenname, "withHighlightedLabel": true };
let req = sendRequest('get', 'https://twitter.com/i/api/graphql/hc-pka9A7gyS3xODIafnrQ/UserByScreenName?variables=' + encodeURIComponent(JSON.stringify(body)), null, true);
return req.data.user.rest_id;
}
let pageNum = 1;
function getGraphQLListAndStoreInArray(username, listName, array, continuation) {
var userId = getIDbyScreenName(username);
if (continuation === undefined && pageNum == 1) {
console.log("Getting first page");
pageNum++;
if (listName == "following") {
let body = { "userId": userId, "count": 20, "withHighlightedLabel": false, "withTweetQuoteCount": false, "includePromotedContent": false, "withTweetResult": false, "withUserResults": false, "withNonLegacyCard": true };
let resp = sendRequest('get', 'https://twitter.com/i/api/graphql/Yv1nkFWinEdXL2oemOSpZA/Following?variables=' + encodeURIComponent(JSON.stringify(body)), null, true);
let entries = resp.data.user.following_timeline.timeline.instructions[2].entries;
Object.entries(entries).forEach(
(value) => {
if (value[1].entryId.includes("user")) {
array.push(value[1])
}
}
);
let continuationToken = entries[entries.length - 2].content.value; // get token for paginating down
console.log(continuationToken);
getGraphQLListAndStoreInArray(username, listName, array, continuationToken)
}
} else {
console.log("Fetching page " + pageNum);
pageNum++;
if (listName == "following") {
let body = { "userId": userId, "count": 20, "cursor": continuation, "withHighlightedLabel": false, "withTweetQuoteCount": false, "includePromotedContent": false, "withTweetResult": false, "withUserResults": false, "withNonLegacyCard": true };
let resp = sendRequest('get', 'https://twitter.com/i/api/graphql/Yv1nkFWinEdXL2oemOSpZA/Following?variables=' + encodeURIComponent(JSON.stringify(body)), null, true);
let entries = resp.data.user.following_timeline.timeline.instructions[0].entries;
Object.entries(entries).forEach(
(value) => {
if (value[1].entryId.includes("user")) {
array.push(value[1])
}
}
);
let continuationToken = entries[entries.length - 2].content.value; // get token for paginating down
console.log(continuationToken);
getGraphQLListAndStoreInArray(username, listName, array, continuationToken);
}
}
}
function unfollowall(followingList) {
if (SHOULD_UNFOLLOW_MUTUALS || SHOULD_UNFOLLOW_PRIVATE) {
followingList.forEach((obj) => {
//console.log(obj.content.itemContent.user.legacy.screen_name);
doUnfollowUser(obj.content.itemContent.user.rest_id);
console.log("Unfollowed " + obj.content.itemContent.user.legacy.screen_name);
})
} else {
followingList.forEach((obj) => {
if (!obj.content.itemContent.user.legacy.protected && !obj.content.itemContent.user.legacy.followed_by) {
//console.log(obj.content.itemContent.user.legacy.screen_name);
doUnfollowUser(obj.content.itemContent.user.rest_id);
console.log("Unfollowed " + obj.content.itemContent.user.legacy.screen_name);
}
})
}
}
// usage:
// getGraphQLListAndStoreInArray('yourusernamehere', 'following', followingArray);
// unfollowall(followingArray);