Created
April 28, 2020 23:40
-
-
Save innateessence/363bab8cb1db7224f99a06e7093f8ba5 to your computer and use it in GitHub Desktop.
A freerice bot written in Javascript using puppeteer
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
#!/usr/bin/env node | |
const puppeteer = require("puppeteer"); | |
function parse_args(args){ | |
let headless = false; | |
let proxy = null | |
while (args.length != 0){ | |
switch (args[0]) { | |
case '--headless': | |
headless = true; | |
console.log("Running in headless mode"); | |
args.shift(); | |
break; | |
case '--proxy': | |
proxy = args[1]; | |
console.log("Proxy: ", args[1]); | |
args.shift(); | |
args.shift(); | |
break; | |
default: | |
if (args[0].includes("node") | args[0].includes('.js')) { | |
args.shift(); | |
break; | |
} | |
else { | |
console.log("Unrecognized argument: ", args[0]); | |
args.shift(); | |
break; | |
} | |
} | |
} | |
return {headless: headless, proxy: proxy} | |
} | |
function random_css_selector(){ | |
let css_selectors = []; | |
for (let i = 2; i < 6; i++) { | |
css_selectors.push(`div.card-button:nth-child(${i})`) | |
}; | |
let css_selector = css_selectors[Math.floor(Math.random() * css_selectors.length)]; | |
return css_selector; | |
} | |
async function freerice(headless, proxy) { | |
let browser = null; | |
if (proxy){ | |
browser = await puppeteer.launch({headless: headless, args: [ `--proxy-server=http://${proxy}`]}); | |
} | |
else{ | |
browser = await puppeteer.launch({headless: headless}); | |
} | |
let page = await browser.newPage(); | |
await page.goto("https://freerice.com"); | |
await page.waitFor(5 * 1000); | |
await page.evaluate(() => { | |
document.querySelector(".as-oil__btn-optin").click(); | |
}); | |
while (true){ | |
let css_selector = random_css_selector() | |
points = await page.evaluate((css_selector) => { | |
// let css_selector = random_css_selector(); | |
document.querySelector(css_selector).click(); | |
return document.querySelector('.rice-counter__value > span:nth-child(1)').textContent | |
}, css_selector); | |
process.stdout.write(`Points: ${points}`); | |
await page.waitFor(2000); | |
process.stdout.clearLine(); | |
process.stdout.cursorTo(0); | |
}; | |
}; | |
(async () => { | |
let args = parse_args(process.argv); | |
await freerice(args.headless, args.proxy) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment