├── README.md ├── cookies.txt └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # instagram-bot-puppeteer 2 | A bot that dowloads memes from reddit and post them on instagram 3 | 4 | # Stargazers 5 | 6 | [![Stargazers repo roster for @Anyesh/instagram-bot-puppeteer](https://reporoster.com/stars/Anyesh/instagram-bot-puppeteer)](https://github.com/Anyesh/instagram-bot-puppeteer/stargazers) 7 | 8 | # Forkers 9 | 10 | [![Stargazers repo roster for @Anyesh/instagram-bot-puppeteer](https://reporoster.com/forks/Anyesh/instagram-bot-puppeteer)](https://github.com/Anyesh/instagram-bot-puppeteer/network/members) 11 | -------------------------------------------------------------------------------- /cookies.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anyesh/instagram-bot-puppeteer/0531d1832bf0848be696d866d0ddec7342a9e730/cookies.txt -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const puppeteer = require("puppeteer"); 2 | const fs = require("fs"); 3 | const fetch = require("node-fetch"); 4 | const device = require("puppeteer/DeviceDescriptors"); 5 | const iphone = device["iPhone 6"]; 6 | 7 | const cookiesPath = "cookies.txt"; 8 | let hour = new Array(12); 9 | let picNum = 1; 10 | 11 | const sub_reddit = "https://www.reddit.com/r/dankmemes/hot.json"; 12 | 13 | async function timeout(ms) { 14 | return new Promise((resolve) => setTimeout(resolve, ms)); 15 | } 16 | 17 | // Downloads top 12 memes from r/dankmemes/hot. 18 | function download() { 19 | let num = 1; 20 | fetch(sub_reddit) 21 | .then((res) => res.json()) 22 | .then((json) => { 23 | const children = json.data.children; 24 | children.slice(1, 14).forEach((element) => { 25 | //console.log(element.data.url) 26 | let memePic = fs.createWriteStream("./meme/" + num + ".jpg"); 27 | fetch(element.data.url).then((res) => { 28 | res.body.pipe(memePic); 29 | }); 30 | 31 | num += 1; 32 | }); 33 | }) 34 | .catch((error) => console.error(error)); 35 | } 36 | 37 | // loads cookies with username and password 38 | async function loadCookies(page) { 39 | const previousSession = fs.existsSync(cookiesPath); 40 | if (previousSession) { 41 | // read cookies 42 | const content = fs.readFileSync(cookiesPath); 43 | const cookiesArr = JSON.parse(content); 44 | if (cookiesArr.length !== 0) { 45 | for (let cookie of cookiesArr) { 46 | await page.setCookie(cookie); // set cookies in puppeteer 47 | } 48 | console.log("cookies loaded in\n"); 49 | } 50 | } 51 | } 52 | 53 | async function upload(num) { 54 | const selectors = { 55 | post: 56 | "#react-root > section > nav.NXc7H.f11OC > div > div > div.KGiwt > div > div > div.q02Nz._0TPg", 57 | next: 58 | "#react-root > section > div.Scmby > header > div > div.mXkkY.KDuQp > button", 59 | expand: 60 | "#react-root > section > div.gH2iS > div.N7f6u.Bc-AD > div > div > div > button.pHnkA", 61 | wait: 62 | "#react-root > section > nav.NXc7H.f11OC > div > div > div.KGiwt > div > div > div:nth-child(1) > a > span", 63 | }; 64 | 65 | const browser = await puppeteer.launch(); 66 | const page = await browser.newPage(); 67 | await page.emulate(iphone); // emulate mobile version of the website. 68 | 69 | // Loads cookies 70 | loadCookies(page); 71 | 72 | await page.goto("https://instagram.com"); 73 | 74 | // POST BUTTON 75 | try { 76 | await page.waitForSelector(selectors.post); 77 | } catch (err) { 78 | await page.screenshot({ path: "error.jpg" }); 79 | console.log(err); 80 | } 81 | await page.click(selectors.post); 82 | console.log("Post button"); 83 | 84 | // UPPLOAD MEME 85 | const fileinput = await page.$("input[type=file]"); 86 | await fileinput.uploadFile("./meme/" + num + ".jpg"); 87 | console.log("uploads image"); 88 | 89 | // EXPAND AND NEXT BUTTONS 90 | 91 | try { 92 | // this checks if you can zoom out the picture or not 93 | await page.waitForSelector(selectors.expand); 94 | await page.click(selectors.expand); 95 | console.log("expand button found"); 96 | } catch (err) { 97 | console.log("expand button not found"); 98 | } 99 | 100 | await page.click(selectors.next); 101 | console.log("Next button"); 102 | 103 | await timeout(2000); // The next button and share button have the same selector. I delay here to be sure that the SHARE button is there. 104 | 105 | // SHARE BUTTON (same SELECTOR as NEXT) 106 | console.log("Share button"); 107 | try { 108 | await page.waitForSelector(selectors.next); 109 | } catch (err) { 110 | await page.screenshot({ path: "error.jpg" }); 111 | console.log(err); 112 | } 113 | await page.click(selectors.next); 114 | 115 | // Waits for the picture to be shared 116 | try { 117 | await page.waitForSelector(selectors.wait); 118 | } catch (err) { 119 | await page.screenshot({ path: "error.jpg" }); 120 | console.log(err); 121 | } 122 | await browser.close(); 123 | } 124 | 125 | console.log( 126 | "startar upp..", 127 | new Date().getHours() + ":" + new Date().getMinutes() 128 | ); 129 | let dwnld = false; 130 | 131 | async function bot() { 132 | console.log("i loopen..."); 133 | if (dwnld == false) { 134 | await download(); 135 | console.log("memes downloaded."); 136 | dwnld = true; 137 | } 138 | for (const item of hour) { 139 | console.log("hour and picture", picNum); 140 | await upload(picNum); 141 | picNum++; 142 | await timeout(3600000); 143 | } 144 | // after 12 hours download again 145 | await download(); 146 | console.log("memes downloaded. "); 147 | picNum = 1; 148 | bot(); 149 | } 150 | 151 | bot(); 152 | --------------------------------------------------------------------------------