Skip to content

Instantly share code, notes, and snippets.

@franciscocorrales
Last active January 18, 2020 16:53
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 franciscocorrales/cc5250255518cca32f50e173c56a3981 to your computer and use it in GitHub Desktop.
Save franciscocorrales/cc5250255518cca32f50e173c56a3981 to your computer and use it in GitHub Desktop.
likeAllPost.js
/*
Works for tags and locations.
How to use:
Open an URL.
Tag Example: https://www.instagram.com/explore/tags/loremipsum/
Location Example: https://www.instagram.com/explore/locations/219976434/
Open the browser console.
Copy and run this code.
References:
https://gist.github.com/LoranKloeze/cf6d08fa04b5371980442f92209d045e
TODO:
How to get an profile information: object window._sharedData
example inside a profile page:
window._sharedData.entry_data.ProfilePage[0].graphql.user
example inside a tag page:
?
example inside a location page: here's the profile data:
?
*/
(function () {
'use strict';
var maxLikesPerRun = 1500;
var likesInThisRun = 0;
var amountLikesRequestFailed = 0;
var maxTriesOnLikes = 3;
var maxLikesOnPost = 100;
var beginTime = undefined;
var endTime = undefined;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getWaitTime() {
var minSeconds = 60 * 1000; // 1 minute
var maxSeconds = 600 * 1000; // 10 minutes
var random = Math.floor(Math.random() * (+maxSeconds - +minSeconds)) + +minSeconds;
console.log("Bot will wait " + random.toString().substring(0, 3) + " seconds before liking the next post. Liked count: " + likesInThisRun);
return random;
}
function getLikesCount() {
var postLikes = 0;
try {
// Possible formats: "1 like", "like this", "7 likes", "346 others", "1,771 likes"
postLikes = document.querySelector("article a time").parentElement.parentElement.parentElement.children[1].lastElementChild.lastElementChild.lastElementChild.textContent;
postLikes = postLikes.replace(/,/g,'');
postLikes = parseInt(postLikes);
if (isNaN(postLikes)) postLikes = 0;
}
catch(error) {}
return postLikes;
}
async function loadNextPost() {
// Get next post
document.querySelector(".coreSpriteRightPaginationArrow").click();
// Wait for the UI load.
await sleep(2000);
// Let's filter some posts. Dont' give likes on low-return posts.
if (getLikesCount() > maxLikesOnPost) {
console.log("Bot will skip this post. Reason: it has " + getLikesCount() + " likes.");
await loadNextPost();
}
}
async function likeLoadedPost() {
var likeHeartSvg = document.querySelector("article button svg[aria-label='Like'][width='24']");
if (!likeHeartSvg) return;
var likeBtn = likeHeartSvg.parentElement;
if (!likeBtn) return;
likeBtn.click();
likesInThisRun++;
await sleep(getWaitTime());
}
function openFirstPost() {
document.querySelector("article a div div img").click();
// Skip the Top post, and set the head to the Most Recent posts.
document.querySelector(".coreSpriteRightPaginationArrow").click();
document.querySelector(".coreSpriteRightPaginationArrow").click();
document.querySelector(".coreSpriteRightPaginationArrow").click();
document.querySelector(".coreSpriteRightPaginationArrow").click();
document.querySelector(".coreSpriteRightPaginationArrow").click();
document.querySelector(".coreSpriteRightPaginationArrow").click();
document.querySelector(".coreSpriteRightPaginationArrow").click();
document.querySelector(".coreSpriteRightPaginationArrow").click();
document.querySelector(".coreSpriteRightPaginationArrow").click();
}
function startInspectionOfXMLHttpRequest() {
XMLHttpRequest.prototype.realSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(value) {
this.addEventListener("progress", function(){
if (this.status == 400 && this.responseURL.includes("/web/likes/")) amountLikesRequestFailed++;
}, false);
this.realSend(value);
};
}
function finishedReport() {
var elapsedTime = new Date(endTime - beginTime).toISOString().slice(11, -1);
console.log("Bot has ended. Total elapsed running time: " + elapsedTime + ". Likes failed: " + amountLikesRequestFailed + ". Likes given: " + likesInThisRun);
}
function initUI() {
var style = "";
style += "#lokl_guiContainer {z-index: 2; position: fixed; width: 100px; height: 100px; top: 5px; left: 5px; background-color: rgba(255, 193, 7, 0.31); padding: 15px;";
style += " font-family: -apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,Helvetica,Arial,sans-serif; font-size: 14px; font-weight: 600; border: solid 1px rgb(255, 193, 7); border-radius: 3px;} ";
style += "#lokl_toggleScroll {padding: 5px 2px; border-radius: 3px; text-align: center; background-color: #35b13d; color: white;}";
style += "#lokl_toggleScroll:hover {background-color: #2e9835; cursor: pointer;}";
style += "#lokl_toggleScroll.active {background-color: #bb3a30; }";
style += "#lokl_toggleScroll.active:hover {background-color: #a9342b; }";
var styleEl = document.createElement("style");
styleEl.innerHTML = style;
document.body.appendChild(styleEl);
var guiContainer = document.createElement('div');
document.body.append(guiContainer);
guiContainer.id = 'lokl_guiContainer';
btnToggle = document.createElement('a');
guiContainer.append(btnToggle);
btnToggle.id = 'lokl_toggleScroll';
btnToggle.innerHTML = 'Fran!';
}
async function main() {
beginTime = performance.now();
//initUI();
startInspectionOfXMLHttpRequest();
openFirstPost();
await sleep(3000);
while (maxLikesPerRun > likesInThisRun && amountLikesRequestFailed < maxTriesOnLikes) {
console.log("\n");
await likeLoadedPost();
await loadNextPost();
}
endTime = performance.now();
finishedReport();
}
main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment