Last active
December 11, 2016 12:32
-
-
Save lkLeonov/ab538a119c7039dd5083aaa0de4b5140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPublics(userId,callback) { | |
var xhr = new XMLHttpRequest(); | |
var body = 'act=load_idols&al=1&oid=' + userId; | |
xhr.open("POST", 'https://vk.com/al_fans.php', true) | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
xhr.onreadystatechange = function() { | |
if (this.readyState == 4) { | |
if (this.status == 200) { | |
var vkData = this.responseText; | |
console.log('Got DATA:' + vkData.substring(0,100) + '...'); | |
if (vkData.indexOf('<!json>') !== -1) { | |
var publics = vkData.substr(vkData.indexOf('<!json>') + 7); | |
var publicsArr = JSON.parse(publics); | |
if (Array.isArray(publicsArr)) | |
callback(publicsArr) | |
else { | |
callback('data parse error') | |
} | |
} else { | |
callback('no json error') | |
} | |
} else { | |
callback('response error') | |
} | |
}; | |
} | |
xhr.send(body); | |
} | |
var users = []; | |
var userId = 1; | |
var lastId = 1000; | |
var interval = (min, max) => | |
Math.floor(Math.random() * (max - min + 1)) + min; //in ms | |
var t0, t1; | |
function storeUsersPublics() { | |
if (userId === 1) t0 = performance.now(); | |
setTimeout(() => { | |
getPublics(userId, (data) => { | |
if (typeof data !== 'string') { // not error | |
users.push({ | |
id: userId, | |
publics: data | |
}); | |
console.log('Successfully stored data of id' + userId); | |
if (userId < lastId) { | |
userId++; | |
storeUsersPublics(); | |
} else { | |
t1 = performance.now(); | |
console.log("Time took " + (t1 - t0) + " milliseconds.") | |
} | |
} else { // error | |
users.push({ | |
id: userId, | |
publics: data | |
}); | |
console.log('ERROR: ' + data + ' of id' + userId); | |
if (userId < lastId) { | |
userId++; | |
storeUsersPublics(); | |
} | |
} | |
}); | |
}, interval(10, 100)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment