Advertisement
Guest User

instaLikeBot

a guest
Apr 2nd, 2019
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. import time
  4. import random
  5. import sys
  6. from settings import user_login, user_password
  7.  
  8.  
  9. def print_same_line(text):
  10.     sys.stdout.write('\r')
  11.     sys.stdout.flush()
  12.     sys.stdout.write(text)
  13.     sys.stdout.flush()
  14.  
  15.  
  16. class InstagramBot:
  17.  
  18.     def __init__(self, username, password):
  19.         self.username = username
  20.         self.password = password
  21.         self.driver = webdriver.Chrome()
  22.  
  23.     def closeBrowser(self):
  24.         self.driver.close()
  25.  
  26.     def login(self):
  27.         driver = self.driver
  28.         driver.get("https://www.instagram.com/")
  29.         time.sleep(2)
  30.         login_button = driver.find_element_by_xpath("//a[@href='/accounts/login/?source=auth_switcher']")
  31.         login_button.click()
  32.         time.sleep(2)
  33.         user_name_elem = driver.find_element_by_xpath("//input[@name='username']")
  34.         user_name_elem.clear()
  35.         user_name_elem.send_keys(self.username)
  36.         password_elem = driver.find_element_by_xpath("//input[@name='password']")
  37.         password_elem.clear()
  38.         password_elem.send_keys(self.password)
  39.         password_elem.send_keys(Keys.RETURN)
  40.         time.sleep(2)
  41.  
  42.     def like_photo(self, tag):
  43.         driver = self.driver
  44.         driver.get("https://www.instagram.com/explore/tags/" + tag + "/")
  45.         time.sleep(2)
  46.  
  47.         pic_hrefs = []
  48.         for i in range(1, 2):
  49.             try:
  50.                 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  51.                 time.sleep(2)
  52.  
  53.                 hrefs_in_view = driver.find_elements_by_tag_name('a')
  54.                 hrefs_in_view = [elem.get_attribute('href') for elem in hrefs_in_view if '.com/p/' in elem.get_attribute('href')]
  55.                 [pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
  56.             except Exception:
  57.                 continue
  58.  
  59.         unique_photos = len(pic_hrefs)
  60.         for pic_href in pic_hrefs:
  61.             driver.get(pic_href)
  62.             time.sleep(2)
  63.             driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  64.             try:
  65.                 time.sleep(random.randint(2, 4))
  66.                 like_button = lambda: driver.find_element_by_xpath('//span[@aria-label="Нравится"]').click()
  67.                 like_button().click()
  68.                 for second in reversed(range(0, random.randint(18, 28))):
  69.                     print_same_line("#" + tag + ': unique photos left: ' + str(unique_photos)
  70.                                     + " | Sleeping " + str(second))
  71.                     time.sleep(1)
  72.             except Exception as e:
  73.                 time.sleep(2)
  74.             unique_photos -= 1
  75.  
  76.  
  77. bot = InstagramBot(user_login, user_password)
  78. bot.login()
  79.  
  80. tags = ['воркаут', 'workout', 'выходнадве', 'мотивация', 'motivation', 'спорт', 'sport', 'roadtothedream', 'roadtothedream71']
  81.  
  82. while True:
  83.     try:
  84.         tag = random.choice(tags)
  85.         bot.like_photo(tag)
  86.     except Exception:
  87.         bot.closeBrowser()
  88.         time.sleep(60)
  89.         InstagramBot(user_login, user_password)
  90.         bot.login()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement