|
| 1 | +from selenium import webdriver |
| 2 | +from selenium.common.exceptions import NoSuchElementException |
| 3 | +from selenium.webdriver import ActionChains |
| 4 | +from selenium.webdriver.common.keys import Keys |
| 5 | +from selenium.webdriver.support import expected_conditions as EC |
| 6 | +from selenium.webdriver.common.by import By |
| 7 | +from selenium.webdriver.support.wait import WebDriverWait |
| 8 | +import time |
| 9 | +import random |
| 10 | +from cred import username as usr, password as psw |
| 11 | + |
| 12 | + |
| 13 | +def check_exists_by_xpath(driver, xpath): |
| 14 | + try: |
| 15 | + driver.find_element_by_xpath(xpath) |
| 16 | + except NoSuchElementException: |
| 17 | + return True |
| 18 | + |
| 19 | + return False |
| 20 | + |
| 21 | + |
| 22 | +def random_comment(): |
| 23 | + with open(r'comments.txt', 'r') as f: |
| 24 | + commentsl = [line.strip() for line in f] |
| 25 | + comments = commentsl |
| 26 | + comment = random.choice(comments) |
| 27 | + return comment |
| 28 | + |
| 29 | + |
| 30 | +class Bot: |
| 31 | + def __init__(self, username, password): |
| 32 | + self.username = username |
| 33 | + self.password = password |
| 34 | + self.bot = webdriver.Firefox() |
| 35 | + with open(r'tags.txt', 'r') as f: |
| 36 | + tags1 = [line.strip() for line in f] |
| 37 | + self.tags = tags1 |
| 38 | + self.urls = [] |
| 39 | + |
| 40 | + def exit(self): |
| 41 | + bot = self.bot |
| 42 | + bot.quit() |
| 43 | + |
| 44 | + def login(self): |
| 45 | + bot = self.bot |
| 46 | + bot.get('https://instagram.com/') |
| 47 | + time.sleep(3) |
| 48 | + |
| 49 | + username_f = WebDriverWait(bot, 10).until( |
| 50 | + EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))) |
| 51 | + password_f = WebDriverWait(bot, 10).until( |
| 52 | + EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))) |
| 53 | + username_f.clear() |
| 54 | + username_f.send_keys(self.username) |
| 55 | + password_f.clear() |
| 56 | + password_f.send_keys(self.password) |
| 57 | + login_button = WebDriverWait(bot, 2).until( |
| 58 | + EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))) |
| 59 | + login_button.click() |
| 60 | + time.sleep(4) |
| 61 | + try: |
| 62 | + not_now = WebDriverWait(bot, 6).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/section/main/div/div/div/div/button'))) |
| 63 | + not_now.click() |
| 64 | + time.sleep(2) |
| 65 | + except: |
| 66 | + print("no pop up 1") |
| 67 | + |
| 68 | + try: |
| 69 | + not_now2 = WebDriverWait(bot, 6).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.aOOlW:nth-child(2)'))) |
| 70 | + not_now2.click() |
| 71 | + time.sleep(2) |
| 72 | + except: |
| 73 | + print("no pop up 2") |
| 74 | + time.sleep(4) |
| 75 | + |
| 76 | + def get_post(self): |
| 77 | + print("Searching posts by tags....") |
| 78 | + bot = self.bot |
| 79 | + tags = self.tags |
| 80 | + tag = tags.pop() |
| 81 | + |
| 82 | + # generating the link |
| 83 | + link = 'https://www.instagram.com/explore/tags/' + tag |
| 84 | + bot.get(link) |
| 85 | + |
| 86 | + time.sleep(4) |
| 87 | + for i in range(4): |
| 88 | + ActionChains(bot).send_keys(Keys.END).perform() |
| 89 | + time.sleep(2) |
| 90 | + |
| 91 | + divs = bot.find_elements_by_xpath("//a[@href]") |
| 92 | + |
| 93 | + first_urls = [] |
| 94 | + |
| 95 | + for i in divs: |
| 96 | + if i.get_attribute('href') is not None: |
| 97 | + first_urls.append(i.get_attribute('href')) |
| 98 | + else: |
| 99 | + continue |
| 100 | + |
| 101 | + for url in first_urls: |
| 102 | + if url.startswith('https://www.instagram.com/p/'): |
| 103 | + self.urls.append(url) |
| 104 | + return go.comment(random_comment()) |
| 105 | + |
| 106 | + def comment(self, comment): |
| 107 | + |
| 108 | + if len(self.urls) == 0: |
| 109 | + print('Finished tag jumping to next one...') |
| 110 | + return go.get_post() |
| 111 | + |
| 112 | + bot = self.bot |
| 113 | + url = self.urls.pop() |
| 114 | + print('commenting...') |
| 115 | + bot.get(url) |
| 116 | + bot.implicitly_wait(1) |
| 117 | + |
| 118 | + bot.execute_script("window.scrollTo(0, window.scrollY + 300)") |
| 119 | + time.sleep(2) |
| 120 | + |
| 121 | + bot.find_element_by_xpath('/html/body/div[1]/section/main/div/div/article/div[3]/section[1]/span[1]/button').click() |
| 122 | + time.sleep(2) |
| 123 | + |
| 124 | + bot.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[1]/article/div[3]/section[1]/span[2]/button').click() |
| 125 | + |
| 126 | + if check_exists_by_xpath(bot, '/html/body/div[1]/section/main/div/div[1]/article/div[3]/section[3]/div'): |
| 127 | + print("skiped") |
| 128 | + return go.comment(random_comment()) |
| 129 | + |
| 130 | + #find_comment_box = WebDriverWait(bot, 6).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[3]/div/form/textarea'))) |
| 131 | + print("yes") |
| 132 | + #find_comment_box.click() |
| 133 | + #find_comment_box.clear() |
| 134 | + c = bot.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[3]/section[3]/div/form/textarea").click() |
| 135 | + #commentb = WebDriverWait(bot, 6).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[3]/div/form/textarea'))) |
| 136 | + print("yes2") |
| 137 | + c2 = bot.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[3]/section[3]/div/form/textarea") |
| 138 | + c2.send_keys(comment) |
| 139 | + |
| 140 | + post_btn = WebDriverWait(bot, 6).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/section/main/div/div[1]/article/div[3]/section[3]/div/form/button[2]'))) |
| 141 | + post_btn.click() |
| 142 | + # edit this line to make bot faster |
| 143 | + time.sleep(5) |
| 144 | + # --------------------------------- |
| 145 | + |
| 146 | + return go.comment(random_comment()) |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +go = Bot(usr, psw) |
| 154 | +go.login() |
| 155 | +if __name__ == '__main__': |
| 156 | + if go.tags == []: |
| 157 | + print("Finished") |
| 158 | + else: |
| 159 | + go.get_post() |
0 commit comments