-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
167 lines (146 loc) · 4.88 KB
/
utils.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
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
const fs = require("fs");
const botUsersFilePath = "./users.json";
const getUserNameFromTxt = (str) => {
//return username from original format
const reg = /twitter.com\/(.*):Mozilla/;
return str.match(reg)[1];
};
const getCookieString = (str) => {
return str.split(":")[5];
};
const decodeCookies = (str, userName) => {
let b64string = str;
let buf = Buffer.from(b64string, "base64"); // Ta-da
let text = buf.toString();
const newText = text.replace(/'/g, '"');
const newText2 = newText.replace(/True/g, "true");
const newText3 = newText2.replace(/False/g, "false");
const newText4 = newText3.replace(/""/g, '"');
let parsedCookies = JSON.parse(newText4);
fs.writeFileSync(
`./cookies/${userName}.json`,
JSON.stringify(parsedCookies)
);
};
const getLinesFromOrderTxt = (file) => {
let eachLine = file.toString().split("\n");
let eachLineNew = [];
eachLine.map((ele, index) => {
if (ele !== "") {
eachLineNew.push(ele);
}
});
return eachLineNew;
};
const sleep = async (timeInMs) => {
return new Promise((resolve) => {
setTimeout(() => resolve(true), timeInMs);
});
};
const getShuffledArr = (arr) => {
const newArr = arr.slice();
for (let i = newArr.length - 1; i > 0; i--) {
const rand = Math.floor(Math.random() * (i + 1));
[newArr[i], newArr[rand]] = [newArr[rand], newArr[i]];
}
return newArr;
};
const getBotUsersObject = () => {
return JSON.parse(fs.readFileSync(botUsersFilePath).toString());
};
const updateBotActionList = (botAction, botTwitHandle, followedUsername) => {
const botUsersObj = getBotUsersObject();
switch (botAction) {
case "follow":
botUsersObj[botTwitHandle].followed[followedUsername] = true;
break;
case "like":
botUsersObj[botTwitHandle].liked[followedUsername] = true;
break;
case "comment":
botUsersObj[botTwitHandle].commented[followedUsername] = true;
break;
case "retweet":
botUsersObj[botTwitHandle].retweeted[followedUsername] = true;
break;
}
// botUsersObj[botTwitHandle].followed[followedUsername] = true;
fs.writeFileSync(botUsersFilePath, JSON.stringify(botUsersObj));
};
const updateNotAllowedBotsList = (
botUsersObj,
botUserArr,
botUserAllowed,
action,
followUserName
) => {
const newArr = botUserArr.filter((val) => !botUserAllowed.includes(val));
switch (action) {
case "like":
newArr.map((e) => {
botUsersObj[e].liked[followUserName] = false;
});
break;
case "comment":
newArr.map((e) => {
botUsersObj[e].commented[followUserName] = false;
});
break;
case "retweet":
newArr.map((e) => {
botUsersObj[e].retweeted[followUserName] = false;
});
break;
}
};
const getAllowedBots = (botUsersObj, botUserArr, action, followUserName) => {
const newBotUserArr = [];
switch (action) {
case "follow":
botUserArr.map((e) => {
if (botUsersObj[e].followed[followUserName] === undefined) {
newBotUserArr.push(e);
}
});
console.log("new user arr is: ", newBotUserArr);
break;
case "like":
botUserArr.map((e) => {
const likedTime = botUsersObj[e].liked[followUserName];
const cooldownTimeInMin = 5;
if (!likedTime || likedTime === undefined) {
newBotUserArr.push(e);
}
});
console.log("new user arr is: ", newBotUserArr);
break;
case "comment":
botUserArr.map((e) => {
const commentedTime = botUsersObj[e].liked[followUserName];
const cooldownTimeInMin = 5;
if (!commentedTime || commentedTime === undefined) {
newBotUserArr.push(e);
}
});
break;
case "retweet":
botUserArr.map((e) => {
const retweetedTime = botUsersObj[e].liked[followUserName];
const cooldownTimeInMin = 5;
if (!retweetedTime || retweetedTime === undefined) {
newBotUserArr.push(e);
}
});
break;
}
return newBotUserArr;
};
exports.updateNotAllowedBotsList = updateNotAllowedBotsList;
exports.getAllowedBots = getAllowedBots;
exports.updateBotActionList = updateBotActionList;
exports.shuffleArray = getShuffledArr;
exports.sleep = sleep;
exports.getLinesFromOrderTxt = getLinesFromOrderTxt;
exports.decodeCookies = decodeCookies;
exports.getUserNameFromTxt = getUserNameFromTxt;
exports.getCookieString = getCookieString;