-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateFollowers.js
72 lines (65 loc) · 2.09 KB
/
updateFollowers.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
const fs = require("fs")
const firebase = require("firebase")
require("firebase/firestore")
firebase.initializeApp(JSON.parse(fs.readFileSync("secret/secretFirebase.json", "utf-8")))
var db = firebase.firestore()
const followers = JSON.parse(fs.readFileSync("followers.json", "utf-8"))
const date = new Date().toString()
var followList = new Array()
var unfollowList = new Array()
db.collection("userIds").get().then(function(querySnapshot) {
var savedList = []
querySnapshot.forEach(function(doc) {
savedList.push(doc.data().id)
})
// 新たなフォローの時
getDiff(followers.ids, savedList, item => followList.push(item))
if (followList.length !== 0) {
console.log('followList', followList)
loopSlowly(setFirebase, followList.length, 1000)
} else {
console.log('no follow')
}
// アンフォローの時
getDiff(savedList, followers.ids, item => unfollowList.push(item))
if (unfollowList.length !== 0) {
console.log('unfollowList', unfollowList)
loopSlowly(delFirebase, unfollowList.length, 1000)
} else {
console.log("no unfollow")
}
})
function getDiff(x, y, op){
x.forEach(item => {
if (!y.includes(item)) op(item)
})
}
var followIndex = -1
const setFirebase = function() {
followIndex++
console.log('set', followIndex, followList[followIndex])
db.collection("userIds").doc(followList[followIndex].toString()).set({
id: followList[followIndex],
followed_date: date
}).then(() => {
console.log("Document successfully setted!")
}).catch(function(error) {
console.error("Error adding document: ", error)
})
}
var unfollowIndex = -1
const delFirebase = function() {
unfollowIndex++
console.log('set', unfollowIndex, unfollowList[unfollowIndex])
db.collection("userIds").doc(unfollowList[unfollowIndex].toString()).delete().then(function() {
console.log("Document successfully deleted!")
}).catch(function(error) {
console.error("Error removing document: ", error)
})
}
function loopSlowly(func, loop, interval) {
console.log('start...')
for(let i = 0; i < loop; i++) {
setTimeout(func, i * interval)
}
}