-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
253 lines (184 loc) · 10.1 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
const fs = require('fs')
const crypto = require('crypto');
const myArgs = process.argv.slice(2);
const ENCRYPTION_KEY = Buffer.from(myArgs[0], 'base64');
const IV_LENGTH = 16;
const algorithm = 'aes-256-ctr';
async function Donload(imageUrl, imageName, path) {
// const command = `wget --debug -nc "${imageUrl}" -O picture/${path}/${imageName}.jpg \n`
const command = `aria2c -c "${imageUrl}" -d picture/${path} -o ${imageName}.jpg \n`;
// Path ke file yang ingin diubah atau dibuat jika belum ada
const filePath = `${path}.txt`;
// Teks yang ingin ditambahkan ke file
const textToAdd = command;
// Fungsi untuk menambahkan teks ke dalam file
function appendTextToFile(filePath, textToAdd) {
// Gunakan flag 'a' untuk mode append (menambahkan teks ke akhir file)
fs.writeFile(filePath, textToAdd, { flag: 'a' }, (err) => {
if (err) {
console.error('Terjadi kesalahan:', err);
return;
}
console.log('Teks berhasil ditambahkan ke file.');
});
}
// Panggil fungsi untuk menambahkan teks ke dalam file
appendTextToFile(filePath, textToAdd);
}
function Mkdir(dirName) {
if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName);
}
}
function encrypt(text) {
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv(algorithm, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
function decrypt(text) {
let textParts = text.split(':');
let iv = Buffer.from(textParts.shift(), 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv(algorithm, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
Mkdir('data')
Mkdir('json')
function queueJoB() {
try {
var Createfile = fs.readFileSync('./data/queue.json', { encoding: 'utf8', flag: 'r' });
var FindUser = JSON.parse(Createfile)[0].name
var CurrentFile = 'queue.json'
} catch {
var Createfile = fs.readFileSync('./data/data.json', { encoding: 'utf8', flag: 'r' });
var FindUser = JSON.parse(Createfile)[0].name
var CurrentFile = 'data.json'
}
return { Createfile, FindUser ,CurrentFile}
}
async function GetJob() {
const puppeteer = require('puppeteer');
return new Promise(async (resolve) => {
const GetQueueJob = queueJoB()
// Create a browser instance
const browser = await puppeteer.launch(
{ headless: "new", defaultViewport: null, args: ['--no-sandbox'] }
);
try {
// Create a new page
const page = await browser.newPage();
const saveCookie = async (page) => {
const cookies = await page.cookies();
const cookieJson = JSON.stringify(cookies, null, 2);
fs.writeFileSync('./data/enkrip.txt', encrypt(cookieJson));
// fs.writeFileSync('./kuki.json', cookieJson);
}
const loadCookie = async (page) => {
const adsdas = fs.readFileSync('./data/enkrip.txt', { encoding: 'utf8', flag: 'r' })
// const kuki = fs.readFileSync('./kuki.json', { encoding: 'utf8', flag: 'r' })
const cookies = JSON.parse(
decrypt(adsdas)
// kuki
);
await page.setCookie(...cookies);
}
// Custom user agent
const customUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36';
// Set custom user agent
await page.setUserAgent(customUA);
// Add mouse scroll for up to 20 seconds
let startTime = Date.now();
let scrollInterval = setInterval(async () => {
const timeElapsed = Date.now() - startTime;
if (timeElapsed >= 20000) {
clearInterval(scrollInterval);
} else {
await page.evaluate(() => {
window.scrollBy(0, window.innerHeight); // Scroll down by the viewport height
});
}
}, 200); // Scroll every 200 milliseconds
page.on('response', async response => {
if (response.url().includes("https://www.instagram.com/graphql/query")) {
const data = (await response.json())
if (data.data.xdt_api__v1__feed__user_timeline_graphql_connection !== undefined) {
if (data.data.xdt_api__v1__feed__user_timeline_graphql_connection.edges[0].node.code) {
// console.log(data.data.xdt_api__v1__feed__user_timeline_graphql_connection.edges[0].node.code)
try {
data.data.xdt_api__v1__feed__user_timeline_graphql_connection.edges
const Filename = data.data.xdt_api__v1__feed__user_timeline_graphql_connection.edges[0].node.owner.pk
Mkdir('picture')
Mkdir(`./picture/${Filename}`)
const Isi = data.data.xdt_api__v1__feed__user_timeline_graphql_connection.edges.map(d => {
return {
id: d.node.code,
image: d.node.image_versions2.candidates[0].url,
caption: d.node.caption == null || d.node.caption== undefined ?'':d.node.caption.text,
post_time: d.node.taken_at,
owner: Filename,
cdn_image: `//cdn.jsdelivr.net/gh/pemkotbekasi/photo_gallery/picture/${Filename}/${d.node.code}.jpg`
}
})
if (fs.existsSync(`./json/${Filename}.json`)) {
// Baca isi file secara synchronous
const fileContent = fs.readFileSync(`./json/${Filename}.json`, 'utf8');
const uniqueArray = JSON.parse(fileContent).filter((obj, index, self) =>
index === self.findIndex(o => o.id === obj.id && o.owner === obj.owner)
);
await require('fs').promises.writeFile(`./json/${Filename}.json`, JSON.stringify(uniqueArray.concat(Isi) ));
// console.log('Isi file:', fileContent);
} else {
await require('fs').promises.writeFile(`./json/${Filename}.json`, JSON.stringify(Isi));
}
const asdasdasd = JSON.parse(GetQueueJob.Createfile)
const filteredData = asdasdasd.filter(item => item.name !== GetQueueJob.FindUser)
const WriteFindUserId = fs.readFileSync('./data/data.json', { encoding: 'utf8', flag: 'r' })
const filteredDataWriteFindUserId = JSON.parse(WriteFindUserId).filter(item => item.name !== GetQueueJob.FindUser)
Isi.map(d => Donload(d.image, d.id, Filename))
fs.writeFileSync('./data/queue.json', JSON.stringify(filteredData));
// add user id instagram
fs.writeFileSync('./data/data.json', JSON.stringify(filteredDataWriteFindUserId.concat({ name: GetQueueJob.FindUser, id: Filename })));
await saveCookie(page);
// console.log(GetQueueJob.FindUser + ' sucess');
return resolve(GetQueueJob.FindUser + ' sucess');
} catch (e) {
const asdasdasd = JSON.parse(GetQueueJob.Createfile)
const filteredData = asdasdasd.filter(item => item.name !== GetQueueJob.FindUser)
fs.writeFileSync('./data/queue.json', JSON.stringify(filteredData));
// console.log(FindUser + ' add to queue');
console.log(e)
return resolve(GetQueueJob.FindUser + ' add to queue');
}
}
}
}
});
const website_url = `https://www.instagram.com/${GetQueueJob.FindUser}/?_showcaption=true`;
// Open URL in current page
await loadCookie(page);
await page.goto(website_url, { waitUntil: 'networkidle0' });
// Close the browser instance
// await browser.close();
} catch (error) {
// console.log(error);
} finally {
await browser.close();
if(queueJoB().CurrentFile === 'data.json'){
console.log('finish')
}else{
GetJob().then(console.log)
}
}
})
}
const CurrenJob = queueJoB()
if(CurrenJob.CurrentFile === 'data.json'){
fs.writeFileSync('./data/queue.json', CurrenJob.Createfile);
GetJob()
}else{
GetJob()
}