Skip to content

Instantly share code, notes, and snippets.

@petrstepanov
Last active March 26, 2020 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrstepanov/f617778ac5281edb4e62c00aba6c1e08 to your computer and use it in GitHub Desktop.
Save petrstepanov/f617778ac5281edb4e62c00aba6c1e08 to your computer and use it in GitHub Desktop.
Basic Instagram Bot
// Bot
(function() {
function startBot(){
// Open the modal dialog on the instagram page
var items = document.querySelectorAll('a[href^="/p"]');
items[9].click();
// Define action promises
function loadPost(){
return new Promise(function(resolve, reject) {
var rightButton = document.querySelector('a.coreSpriteRightPaginationArrow');
if (rightButton) rightButton.click();
setTimeout(function(){
console.log("loadPost done");
resolve();
}, (5+Math.random()*10)*1000);
});
}
function likePost(){
const LIKE_PERCENT = 60;
return new Promise(function(resolve, reject) {
if (Math.random() > LIKE_PERCENT/100){
return resolve();
}
if (Math.random()<0.5){
var unlike = document.querySelector('div[role="dialog"]>article header+div+div>section>span:first-child>button [aria-label="Unlike"]');
if (unlike){
console.log("-> Already Liked");
} else {
var likeButton = document.querySelector('div[role="dialog"]>article header+div+div>section>span:first-child>button');
likeButton.click();
console.log("-> Liked");
}
}
else {
var imageButton = document.querySelector('div[role="dialog"]>article header+div>div[role="button"]');
if (imageButton) {
var event = new MouseEvent('dblclick', {
'view': window,
'bubbles': true,
'cancelable': true
});
imageButton.dispatchEvent(event);
}
}
setTimeout(function(){
console.log("likePost done");
resolve();
}, (2+Math.random()*6)*1000);
});
}
function commentPost(){
const COMMENT_PERCENT = 100;
return new Promise(function(resolve, reject) {
if (Math.random() > COMMENT_PERCENT/100){
return resolve();
}
// Create random comment
var comments2d = [["Yo","Hey","Whatup","Sup"],
["man","dude","pal","mate","bro", "how's it going ✌"],
["great looking","nice","dope","cool","gnarly","hot","sick","clean"],
["BMW","bimmer"],
["out there", ", looking good", ", looks clean ✨", ""],
["👍","👀","👆"],
["Good luck with the car!", "Check out our stickers!","Stay cool.","Check our windshield banners some time.","Keep it up!",":blue_car: bimmers for life!", "We make stickers, check our stuff some time!","Interested in custom vinyl stickers?"]];
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var comment="";
for (var words of comments2d){
word = words[getRandomInt(0,words.length-1)];
comment += word;
comment += " ";
}
console.log("Comment:" + comment);
// Populate comment in textarea
var textarea = document.querySelector('div[role="dialog"]>article form textarea');
function appendCharacter(textarea, character){
// No frontend tool to type into textarea found
return new Promise(function(resolve, reject){
textarea.value = textarea.value + character;
setTimeout(function(){
resolve();
}, 100 + Math.random()*100);
});
}
function submitFormAndResolve(){
// Submit form
var submitButton = document.querySelector('div[role="dialog"]>article form button[type="submit"]');
// submitButton.click();
// Wait and resolve
setTimeout(function(){
console.log("commentPost done");
resolve();
}, (3+Math.random()*10)*1000);
}
console.log("Adding characters");
var arrayOfCharacters = comment.split('');
// Resolve promises sequentially
// https://css-tricks.com/why-using-reduce-to-sequentially-resolve-promises-works/
let result = arrayOfCharacters.reduce(function(accumulatorPromise, character){
return accumulatorPromise.then(() => {
console.log(character);
return appendCharacter(textarea, character);
});
}, Promise.resolve());
result.then(e => {
console.log("String entered");
submitFormAndResolve();
});
});
}
(async function loop() {
var i = 0;
var nPosts = 20;
console.log("Liking " + nPosts + " posts...");
while (1) {
console.log("Attempt " + i);
// Sleep at night
var date = new Date();
if (date.getHours() > 22 && date.getHours() < 10){
console.log("Sleeping 3 hours" + i);
await new Promise(function(resolve){
setTimeout(resolve, 3*60*60*1000);
});
}
if (++i < nPosts){
// Load and like post
await loadPost().then(likePost);
} else {
// Wait 30+ minutes
var minutes = Math.round(30 + Math.random()*30);
console.log("Waiting " + minutes + " minutes...");
await new Promise(function(resolve){
setTimeout(resolve, minutes*60*1000);
});
// Select number of posts to like
console.log("Liking " + nPosts + " posts...");
nPosts = i + 10+Math.round(Math.random()*30);
}
}
})();
}
setTimeout(startBot(), 2000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment