forked from Zequez/reddit-placebot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.js
executable file
·163 lines (137 loc) · 4.07 KB
/
run.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
const fs = require('fs')
const qs = require('querystring')
const axios = require('axios')
const Jimp = require('jimp')
if (!fs.existsSync('./cookies.json')) fs.writeFileSync('cookies.json', '{}')
if (!fs.existsSync('./queues.json')) fs.writeFileSync('queues.json', '{}')
const config = require('./config')
const boardDownloader = require('./board_downloader')
const targetDownloader = require('./target_downloader')
const boardDiffer = require('./board_differ')
const users = require('./users')
const cookies = require('./cookies')
const queues = require('./queues')
// boardDownloader.loadFromRawFile('_bitmap.bmp')
// boardDownloader.load()
// return
for (let user in users) { if (!queues[user]) scheduleUser(user) }
startQueue()
function authenticateAll () {
for (let user in users) {
if (!cookies[user]) {
authenticateUser(user)
}
}
}
function authenticateUser (user) {
let passwd = users[user]
console.log('Getting modhash and cookies for ', user)
return axios.post('https://www.reddit.com/api/login/' + user, qs.stringify({
op: 'login',
user: user,
passwd: passwd,
api_type: 'json'
}))
.then((response) => {
let modhash = response.data.json.data.modhash
let cookie = response.headers['set-cookie'].map((c) => c.split(';')[0]).join('; ')
cookies[user] = cookies[user] || {}
cookies[user].cookie = cookie
cookies[user].modhash = modhash
saveCookieJar()
})
}
function saveCookieJar () {
fs.writeFile('cookies.json', JSON.stringify(cookies, null, '\t'), function () {
console.log('Cookie jar saved!')
})
}
function userRun (user) {
console.log('Running user ', user)
return Promise.all([boardDownloader.load(), targetDownloader.load()])
.then((buffers) => {
let action = boardDiffer(buffers[0], buffers[1])
if (action) {
if (action.color === -1) {
throw `Invalid color at X: ${action.x} Y: ${action.y}`
}
return userPaint(user, action.x, action.y, action.color)
} else {
console.log('Nothing to do')
return Promise.resolve(scheduleUser(user, 30))
}
})
}
function userPaint (user, x, y, color) {
if (!cookies[user]) return authenticateUser(user)
console.log('Painting ', {x: x, y: y, color: color})
// console.log('Mock painting!')
// scheduleUser(user, 30)
// return Promise.resolve()
return axios({
method: 'POST',
url: config.DRAW_URL,
data: qs.stringify({
x: x,
y: y,
color: color
}),
headers: {
cookie: cookies[user].cookie,
'x-modhash': cookies[user].modhash,
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).then((response) => {
scheduleUser(user, response.data.wait_seconds)
}).catch((error) => {
let seconds = error.response.data.wait_seconds
if (seconds) {
console.log('Could not paint, user available in ', seconds, ' seconds')
scheduleUser(user, seconds)
} else {
console.log('Could not paint, some unknown error, forcing re-authentication')
delete cookies[user]
}
})
}
function scheduleUser (user, seconds = -1) {
queues[user] = new Date().valueOf() + seconds * 1000
saveQueue()
}
function saveQueue () {
fs.writeFileSync('queues.json', JSON.stringify(queues, null, '\t'))
console.log('Queue saved!')
}
function startQueue () {
let nextOneIn = printCountdowns()
authenticateAll()
let ran = false
for (let user in queues) {
if (queues[user] < new Date().valueOf()) {
ran = true
userRun(user).then(() => {
startQueue()
})
break
}
}
if (!ran) {
if (nextOneIn <= 0) nextOneIn = 0
console.log(`Next one in ${nextOneIn} seconds`)
setTimeout(startQueue, Math.min(30, nextOneIn) * 1000 + 1000)
}
}
function printCountdowns () {
let countdowns = []
let timeNow = new Date().valueOf()
let nextOne = Infinity
for (let user in queues) {
let secs = (queues[user] - timeNow) / 1000
if (secs < nextOne) {
nextOne = secs
}
countdowns.push(`${user} ${Math.round(secs)}s`)
}
console.log(countdowns.join('; '))
return nextOne
}