Skip to content

Instantly share code, notes, and snippets.

@zhepa zhepa/silence.js
Last active Mar 28, 2019

Embed
What would you like to do?
игнор ёбаный
const request = require("request-promise");
const iconv = require("iconv");
const THRESHOLD = 36000;
const USERID = 1;
let previousMessageTime;
let graphItems = [];
(async function() {
let offset = 0;
for (let i = 0; i < 514; i++) {
await getMessages(offset);
await sleep(250);
offset += 100;
console.log(i);
}
console.log(graphItems);
}());
function getMessages(offset) {
return request
.get({
uri: `https://vk.com/al_im.php?act=a_history&al=1&gid=0&im_v=2&offset=${offset}&peer=${USERID}&toend=0&whole=0`,
headers: {
"cookie": "",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
},
encoding: "binary"
})
.then(function (body) {
const { convert } = new iconv.Iconv("windows-1251", "utf8");
let messages = JSON.parse(convert(new Buffer(body, "binary")).toString()
.split("<!>")[6]
.match(/^\<!json>(.*)/)[1]);
messages = Object
.keys(messages)
.reduce((acc, key) => {
acc.push(messages[key])
return acc;
}, [])
.sort((a, b) => b[3] - a[3]);
for (let i = 0; i < messages.length; i++) {
const time = messages[i][3];
if (messages[i + 1]) {
previousMessageTime = messages[i + 1][3];
}
if (i !== messages.length - 1) {
const silence = time - previousMessageTime;
if (silence >= THRESHOLD) {
graphItems.push({
duration: silence / 3600,
date: new Date(previousMessageTime * 1000).toISOString()
});
}
}
}
})
.catch(e => console.log("ОШИБКа", e.message));
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.