This repository has been archived by the owner on Nov 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
157 lines (135 loc) · 7.63 KB
/
bot.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
const config = require('./config.json');
const Raven = require('raven');
const fs = require('fs');
const request = require('request');
const log = require('winston');
const temp = require('temp').track();
const ffmpeg = require('fluent-ffmpeg');
const getDuration = require('get-video-duration');
const sharp = require('sharp');
const Modules = require ('./lib/Modules');
const schedule = require('node-schedule');
const Client = require('instagram-private-api').V1;
const device = new Client.Device(config.credentials.username);
const storage = new Client.CookieFileStorage(`${config.settings.storage}${config.credentials.username}.json`);
Raven.config(config.settings.raven).install();
log.addColors({error:"red",warning:"yellow",info:"green",verbose:"white",debug:"blue",silly:"gray"});
log.remove(log.transports.Console);
log.add(log.transports.Console, {
level: config.settings.debug_level,
prettyPrint: true,
colorize: true,
timestamp: true
});
log.add(log.transports.File, {
level: config.settings.debug_level,
prettyPrint: true,
filename: config.settings.storage + config.credentials.username + '.log',
timestamp: true
});
var IG;
Client.Session.create(device, storage, config.credentials.username, config.credentials.password)
.then((session) => {
session.getAccount()
.then(account => log.info(`Successfully logged in as ${account.params.username}`));
IG = session;
log.info('Scheduled jobs for uploads');
schedule.scheduleJob('0 13 * * *', uploadIF);
schedule.scheduleJob('30 14 * * *', uploadIF);
schedule.scheduleJob('0 16 * * *', uploadIF);
schedule.scheduleJob('30 17 * * *', uploadIF);
schedule.scheduleJob('0 19 * * *', uploadIF);
});
function uploadIF() {
Modules.IF.getMemesFromProfile('rumblefrog', (err, memes) => {
if (err) return;
let IF_Cache = new Modules.Cache(config.settings.storage + config.settings.caches.ifunny);
for (i = 0; i <= memes.length; i++) {
let meta = Modules.IF.parseRawMeta(memes[i].raw_meta_data);
if (!IF_Cache.existsSync(meta.contentId)) {
log.debug('Post candidate found');
IF_Cache.push(meta.contentId, (err) => {
if (err) return;
log.debug('Candidate type: ' + meta.contentType);
let dl_src = (meta.contentType == 'video_clip') ? memes[i].video : memes[i].image;
let dl_ext = Modules.IF.getContentTypeExt(meta.contentType);
let stream = temp.createWriteStream({prefix:'ifunny',suffix:dl_ext});
request(dl_src).pipe(stream).on('close', () => {
stream.end();
let final_type = (meta.contentType == 'pic') ? 'image' : 'video';
if (final_type == 'image') {
let cropped = temp.createWriteStream({prefix:'ifunny',suffix:'.jpg'});
log.debug('Cropping IF watermark for image upload');
const image = sharp(fs.readFileSync(stream.path));
image
.metadata()
.then((metadata) => {
return image
.resize(metadata.width, metadata.height - 20)
.crop(sharp.gravity.north)
.toFile(cropped.path)
})
.then((data) => {
cropped.end();
log.debug('Uploading');
Client.Upload.photo(IG, cropped.path)
.then((upload) => {
return Client.Media.configurePhoto(IG, upload.params.uploadId, `Follow @${config.credentials.username} for more contents`);
})
.then((medium) => {
Client.Comment.create(IG, medium.params.id, Modules.IF.generateTags(memes[i].tags, config.credentials.username));
})
})
} else {
if (dl_ext == '.gif') {
let convert = temp.createWriteStream({prefix:'ifunny',suffix:'.mp4'});
log.debug('Converting gif -> video');
ffmpeg(stream.path)
.output(convert)
.on('end', () => {
convert.end();
})
.on('codecData', (data) => {
let thumbnail = temp.createWriteStream({prefix:'ifunny',suffix:'.jpg'});
log.debug('Converting thumbnail for gif -> video upload');
ffmpeg(stream.path)
.frames(1)
.output(thumbnail)
.on('end', () => {
thumbnail.end();
log.debug('Uploading');
Client.Upload.video(IG, convert.path, thumbnail.path)
.then((upload) => {
return Client.Media.configureVideo(IG, upload.uploadId, `Follow @${config.credentials.username} for more contents`, data.duration);
})
.then((medium) => {
Client.Comment.create(IG, medium.params.id, Modules.IF.generateTags(memes[i].tags, config.credentials.username));
})
})
})
} else {
let thumbnail = temp.createWriteStream({prefix:'ifunny',suffix:'.jpg'});
log.debug('Downloading thumbnail for video upload');
request(memes[i].image).pipe(thumbnail).on('close', () => {
thumbnail.end();
getDuration(stream.path).then((duration) => {
log.debug('Uploading');
Client.Upload.video(IG, stream.path, thumbnail.path)
.then((upload) => {
return Client.Media.configureVideo(IG, upload.uploadId, `Follow @${config.credentials.username} for more contents`, duration);
})
.then((medium) => {
Client.Comment.create(IG, medium.params.id, Modules.IF.generateTags(memes[i].tags, config.credentials.username));
})
});
});
}
}
});
});
log.info('Schedule ran');
break;
}
}
});
}