-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoundcloudReposter.js
80 lines (76 loc) · 2.02 KB
/
soundcloudReposter.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
var fs = require('fs');
var http = require('http');
var Promise = require('promise');
var mongoose = require('mongoose');
var scWrapper = require("./server/app/SCWrapper/SCWrapper.js");
mongoose.connect('mongodb://localhost/bots');
var schema = new mongoose.Schema({
token: String,
clientID: String,
clientSecret: String,
username: String,
id: Number,
url: String,
description: String,
full_name: String,
city: String,
email: String,
password: String
});
var Bot = mongoose.model("Bot", schema);
var db = mongoose.connection;
db.on('error', function(err) {
console.log('connection error', err);
});
db.once('open', function() {
console.log('connected.');
setTimeout(function() {
scheduleReposts();
}, parseFloat(process.env.HOURS_DELAY) * 60 * 60 * 1000)
});
var timeBetween = 60 * 60 * 1000 * parseFloat(process.env.HOURS_SPAN) / parseFloat(process.env.NUMBER_REPOSTS);
var totalReposts = 0;
console.log(timeBetween);
function scheduleReposts() {
setTimeout(function() {
if (totalReposts < process.env.NUMBER_REPOSTS) {
scheduleReposts();
}
}, timeBetween);
performRepost();
}
function performRepost() {
if (totalReposts >= process.env.NUMBER_REPOSTS) return;
Bot.find({})
.then(function(bots) {
var bot = bots[Math.floor(Math.random() * bots.length)];
console.log(bot);
scWrapper.init({
id: bot.clientID,
secret: bot.clientSecret,
uri: 'https://localhost:1337',
accessToken: bot.token
});
var reqObj = {
method: 'PUT',
path: '/e1/me/track_reposts/' + process.env.TRACK_ID,
qs: {
oauth_token: bot.token
}
};
scWrapper.request(reqObj, function(err, response) {
if (err) {
console.log(err)
console.log(response);
performRepost();
} else {
console.log(response);
if (response.status.includes('200 - OK')) {
performRepost();
} else {
totalReposts++;
}
}
});
})
}