-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
145 lines (108 loc) · 3.61 KB
/
index.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
const { IgApiClient } = require("instagram-private-api");
const ig = new IgApiClient();
const cron = require("cron");
require("dotenv").config({ path: "./.env" });
const express = require("express");
const app = express();
const axios = require('axios')
const Travel = require('./src/models/travel')
const Bulldog = require('./src/models/bulldog')
const Frenchie = require('./src/models/frenchies')
const scrap = require('./scrapper')
const mongoose = require('mongoose')
const { IG_USERNAME, IG_PASSWORD, MONGO_URL, PORT, COOKIE_ACCOUNT_PASSWORD, COOKIE_ACCOUNT_USERNAME } = process.env;
let randomMinute = Math.floor(Math.random() * 60);
const login = async () => {
try {
ig.state.generateDevice(IG_USERNAME);
await ig.account.login(IG_USERNAME, IG_PASSWORD);
} catch (err) {
console.error(err);
}
};
const connectToDb = async () => {
await mongoose.connect(MONGO_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
serverSelectionTimeoutMS: 5000,
autoIndex: false, // Don't build indexes
maxPoolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
}).then(
console.log("DB CONNECTED SUCCESS")
).catch(error => {
console.log("DB CONNECTION FAILED ", error)
process.exit(1)
})
}
app.get("/", async (req, res) => {
console.log("tried");
});
app.listen(PORT || 3001, () => {
console.info("Server started.");
if (IG_USERNAME && IG_PASSWORD && COOKIE_ACCOUNT_USERNAME && COOKIE_ACCOUNT_PASSWORD) {
try {
connectToDb()
new cron.CronJob({
cronTime: `${randomMinute} ${Math.floor(Math.random() * 12)} 1-31/3 * *`,
onTick: () => { scrap() },
start: true
});
console.log('STARTED CRON');
console.log(randomMinute)
const postRandom = async () => {
console.log("POSTING STARTED" + randomMinute);
await login()
console.log("SUCCESSFULL LOGGED IN");
const posts = await Travel.find().limit(1).sort({ likes: -1 })
posts.map(async post => {
let mediaBuff = post.links.map(link => {
return new Promise((resolve, reject) => {
resolve(axios.get(link, { responseType: 'arraybuffer' }).then((res) => {
return res.data
}));
});
})
let mediaBuffers = await Promise.all(mediaBuff)
if (post.type === "video") {
await ig.publish.video({
file: mediaBuffers[0],
caption: post.caption
})
} else {
if ( mediaBuffers?.length > 1) {
await ig.publish.album({
items: mediaBuffers.map( item =>({
file: item,
})),
caption: post.caption,
})
} else {
await ig.publish.photo({
file: mediaBuffers[0],
caption: post.caption
})
}
}
})
}
new cron.CronJob({
cronTime: `${randomMinute} 0-23/1 * * *`,
onTick: () => {
// Perform the task here
postRandom();
randomMinute = Math.floor(Math.random() * 60);
},
start: true
});
} catch (error) {
console.log(error);
}
} else {
console.warn(
"IG_USERNAME, IG_PASSWORD are required to run the script"
);
}
});