-
Notifications
You must be signed in to change notification settings - Fork 491
/
Twitter_Unfollowers.js
114 lines (96 loc) · 3.41 KB
/
Twitter_Unfollowers.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
const prompt = require("prompt-sync")();
const db = require("./dbFunc")
const api = require("./apiTokens")
const twt = require("./twitterFunc")
const dbFileLoc = "db.json";
// api data
const bearerToken = api.data.bearer;
const getUserData = async (username) => {
let user = new Object();
user.name = username;
let res = await twt.getUserId(user.name, bearerToken)
user.id = res.data[0].id;
user.followers = await twt.getFollowers(user.id, bearerToken);
return user;
}
const displayUser = (user) => {
console.log("\nUser Info :");
console.log(`Username : ${user.name} \nuserID : ${user.id}`);
}
const getUnfollowers = (current, fromDB) => {
// separate usernames from the json array using map
tempCurrent = current.map((user) => {
return user.name;
})
tempFromDB = fromDB.map((user) => {
return user.name;
})
// new list to store users that have unfollowed the user
let unfollows = [];
tempFromDB.forEach(user => {
if (tempCurrent.indexOf(user) == -1) {
unfollows.push(user);
}
});
return unfollows;
}
const getDate = () => {
let d = new Date();
return `${d.getDate()}/${d.getMonth() + 1}/${d.getFullYear()}`
}
const updateDB = (dbData, currentData, unfollows) => {
// assign current new user followers changes to db
dbData[currentData.name]["followers"] = currentData["followers"];
if (unfollows > 0) {
let d = getDate();
let newCheck = {
"On-Date": d,
"People-unfollowed": unfollows
}
// update time
dbData[currentData.name].time = d;
// make history of checking
dbData[currentData.name]["history"].push(newCheck);
}
// write the data into the db
db.write(dbFileLoc, dbData);
return dbData;
}
const init = async () => {
let username = prompt("Enter Twitter Username : "); //this info will be taken through input
let user = await getUserData(username)
let dbData = db.read(dbFileLoc);
dbData = (dbData === "" || dbData.length == 0) ? "{}" : dbData;
dbData = JSON.parse(dbData);
if (Object.prototype.hasOwnProperty.call(dbData, user.name)) {
// display basic info
displayUser(user);
// get list of people that have unfollowed you
let people = getUnfollowers(user.followers, dbData[user.name]["followers"]);
// displaying their number
console.log(`\nNOTE : ${people.length} people unfollowed you(him/her) After ${dbData[user.name].time}`);
// display their names
if (people.length > 0) {
console.log(`\nThose are(is) : `);
console.table(people);
}
// to update the db with new data
dbData = updateDB(dbData, user, people.length);
if (dbData[user.name]["history"].length > 0) {
console.log(`\Query history is as follows :`);
console.table(dbData[user.name]["history"]);
}
} else {
// assign current new user to db
dbData[user.name] = user;
// add date of addition
dbData[user.name].time = getDate();
// make history of checking
dbData[user.name].history = [];
// write the data into the db
db.write(dbFileLoc, dbData);
// inform us about the user not being in the db
console.log("\nNOTE : User didn't exist! Now it has been added to DB.\nCheck after some time!!!");
}
}
init();