-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
348 lines (303 loc) · 12.7 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
const express = require("express");
const app = express();
const axios = require("axios");
const contentful = require("contentful-management");
const fs = require("fs");
const Jimp = require("jimp");
const Instagram = require("./instagram-web-api/index");
const FileCookieStore = require("tough-cookie-filestore2");
const cron = require("node-cron");
const imaps = require("imap-simple");
const _ = require("lodash");
const simpleParser = require("mailparser").simpleParser;
const dayjs = require("dayjs");
require("dotenv").config();
const port = process.env.PORT || 4000;
// Upload new Inky Doodle to Instagram every day at 4:00 PM
cron.schedule("59 15 * * *", async () => {
const instagramLoginFunction = async () => {
// Persist cookies after Instagram client log in
const cookieStore = new FileCookieStore("./cookies.json");
const client = new Instagram(
{
username: process.env.INSTAGRAM_USERNAME,
password: process.env.INSTAGRAM_PASSWORD,
cookieStore,
},
{
language: "en-US",
}
);
const instagramPostPictureFunction = async () => {
await client
.getPhotosByUsername({ username: process.env.INSTAGRAM_USERNAME })
.then((res) => {
const allCaptions = res.user.edge_owner_to_timeline_media.edges.map(
(item) => item.node.edge_media_to_caption.edges[0]
);
const allCaptionsExisting = allCaptions.filter((caption) => caption);
return {
mostRecent:
allCaptions[allCaptions.length - allCaptionsExisting.length].node
.text,
offset: allCaptions.length - allCaptionsExisting.length,
};
})
.then(({ mostRecent, offset }) => {
return {
latestNumber: Number(mostRecent.split(" - ")[0]),
offset: offset,
};
})
.then(({ latestNumber, offset }) => {
const updatedNumber = latestNumber + (offset + 1);
const inkyDoodleQuery = `
query {
inkyDoodleCollection(where: {number: ${updatedNumber}}) {
items {
sys {
id
}
number
generation
name
wave
parents
image {
url
}
}
}
}
`;
axios({
url: `https://graphql.contentful.com/content/v1/spaces/${process.env.CONTENTFUL_SPACE_ID}`,
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.CONTENTFUL_ACCESS_TOKEN}`,
},
data: {
query: inkyDoodleQuery,
},
})
.then((res) => res.data)
.then(async ({ data, errors }) => {
if (errors) {
console.error(errors);
}
const updatedInkyDoodle = data.inkyDoodleCollection.items[0];
if (updatedInkyDoodle) {
const updatedCaption = `${updatedNumber} - ${
updatedInkyDoodle.name
}\n${
updatedInkyDoodle.parents
? updatedInkyDoodle.parents.length > 0
? updatedInkyDoodle.parents
.map((parent) => "#" + parent)
.join(" + ") + " \n"
: ""
: ""
}#inkydoodle #gen${updatedInkyDoodle.generation}${
updatedInkyDoodle.wave
? ` #wave${updatedInkyDoodle.wave}`
: ""
}`;
Jimp.read(updatedInkyDoodle.image.url)
.then((lenna) => {
return lenna
.resize(405, 405, Jimp.RESIZE_NEAREST_NEIGHBOR)
.quality(100)
.write(`./${updatedInkyDoodle.name}.jpg`, async () => {
// Upload converted and resized JPG to Instagram feed
await client
.uploadPhoto({
photo: `${updatedInkyDoodle.name}.jpg`,
caption: updatedCaption,
post: "feed",
})
.then(async ({ media }) => {
console.log(
`https://www.instagram.com/p/${media.code}/`
);
await client.addComment({
mediaId: media.id,
text: "#nftcollectors #nftcollectibles #dibujo #dibujodigital #pixel #pixelart #digitalart #cute #artist #instadaily #artdaily #pixelartist #dailyart #nfts #nft #16bitart #8bitart #8bit #32bit #arteespanol #artebrasil #gatoslindos #gatosdeinstagram #nycart",
});
const contentfulClient = contentful.createClient({
accessToken:
process.env.CONTENTFUL_MANAGEMENT_TOKEN,
});
contentfulClient
.getSpace(process.env.CONTENTFUL_SPACE_ID)
.then((space) => {
space
.getEnvironment("master")
.then((environment) => {
environment
.getEntry(updatedInkyDoodle.sys.id)
.then((entry) => {
entry.fields.instagram = {
"en-US": {
url: `https://www.instagram.com/p/${media.code}/`,
date: dayjs().format(
"MMMM D, YYYY"
),
},
};
entry.update().then(() => {
environment
.getEntry(updatedInkyDoodle.sys.id)
.then((updatedEntry) => {
updatedEntry.publish();
console.log(
`Entry updated successfully and published. New updated entry Instagram link is ${updatedEntry.fields.instagram["en-US"].url}`
);
});
});
});
});
});
// Remove Local JPG File
fs.unlinkSync(`${updatedInkyDoodle.name}.jpg`);
});
});
})
.catch((err) => {
console.log(err);
});
}
});
});
};
try {
console.log("Logging in...");
await client.login();
console.log("Login successful!");
const delayedInstagramPostFunction = async (timeout) => {
setTimeout(async () => {
await instagramPostPictureFunction();
}, timeout);
};
await delayedInstagramPostFunction(50000);
} catch (err) {
console.log("Login failed!");
const delayedLoginFunction = async (timeout) => {
setTimeout(async () => {
await client.login().then(() => instagramPostPictureFunction());
}, timeout);
};
if (err.statusCode === 403 || err.statusCode === 429) {
console.log("Throttled!");
await delayedLoginFunction(60000);
}
console.log(err);
// Instagram has thrown a checkpoint error
if (err.error && err.error.message === "checkpoint_required") {
const challengeUrl = err.error.checkpoint_url;
await client.updateChallenge({ challengeUrl, choice: 1 });
const emailConfig = {
imap: {
user: `${process.env.INKY_DOODLE_EMAIL}`,
password: `${process.env.INKY_DOODLE_EMAIL_PASSWORD}`,
host: "imap.gmail.com",
port: 993,
tls: true,
tlsOptions: {
servername: "imap.gmail.com",
rejectUnauthorized: false,
},
authTimeout: 30000,
},
};
// Connect to email and solve Instagram challenge after delay
const delayedEmailFunction = async (timeout) => {
setTimeout(() => {
imaps.connect(emailConfig).then(async (connection) => {
return connection.openBox("INBOX").then(async () => {
// Fetch emails from the last hour
const delay = 1 * 3600 * 1000;
let lastHour = new Date();
lastHour.setTime(Date.now() - delay);
lastHour = lastHour.toISOString();
const searchCriteria = ["ALL", ["SINCE", lastHour]];
const fetchOptions = {
bodies: [""],
};
return connection
.search(searchCriteria, fetchOptions)
.then((messages) => {
messages.forEach((item) => {
const all = _.find(item.parts, { which: "" });
const id = item.attributes.uid;
const idHeader = "Imap-Id: " + id + "\r\n";
simpleParser(idHeader + all.body, async (err, mail) => {
if (err) {
console.log(err);
}
console.log(mail.subject);
const answerCodeArr = mail.text
.split("\n")
.filter(
(item) =>
item && /^\S+$/.test(item) && !isNaN(Number(item))
);
if (mail.text.includes("Instagram")) {
if (answerCodeArr.length > 0) {
// Answer code must be kept as string type and not manipulated to a number type to preserve leading zeros
const answerCode = answerCodeArr[0];
console.log(answerCode);
await client.updateChallenge({
challengeUrl,
securityCode: answerCode,
});
console.log(
`Answered Instagram security challenge with answer code: ${answerCode}`
);
await client.login();
await instagramPostPictureFunction();
}
}
});
});
});
});
});
}, timeout);
};
await delayedEmailFunction(40000);
}
// Delete stored cookies, if any, and log in again
console.log("Logging in again and setting new cookie store");
fs.unlinkSync("./cookies.json");
const newCookieStore = new FileCookieStore("./cookies.json");
const newClient = new Instagram(
{
username: process.env.INSTAGRAM_USERNAME,
password: process.env.INSTAGRAM_PASSWORD,
cookieStore: newCookieStore,
},
{
language: "en-US",
}
);
const delayedNewLoginFunction = async (timeout) => {
setTimeout(async () => {
console.log("Logging in again");
await newClient
.login()
.then(() => instagramPostPictureFunction())
.catch((err) => {
console.log(err);
console.log("Login failed again!");
});
}, timeout);
};
await delayedNewLoginFunction(10000);
}
};
await instagramLoginFunction();
});
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});