Guest User

Untitled

a guest
Nov 18th, 2018
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. def like_photo(self, hashtag):
  2. driver = self.driver
  3. driver.get("https://www.instagram.com/explore/tags/"+ hashtag +"/")
  4. time.sleep(2)
  5.  
  6. #gathering photos
  7. pic_hrefs = []
  8. for i in range (1,7):
  9. try:
  10. driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  11. time.sleep(2)
  12. #get tags
  13. hrefs_in_view = driver.find_elements_by_tag_name('a')
  14. #finding relevant hrefs
  15. hrefs_in_view = [elem.get_attribute('href') for elem in hrefs_in_view
  16. if '.com/p/' in elem.get_attribute('href')]
  17. # building list of unique photos
  18. [pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
  19. # print("Check: pic href length " + str(len(pic_hrefs)))
  20. except Exeption:
  21. continue
  22.  
  23. #liking photos
  24. unique_photos = len(pic_hrefs)
  25. for pic_href in pic_hrefs:
  26. driver.get(pic_href)
  27. time.sleep(2)
  28. driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  29. try:
  30. time.sleep(random.randint(2,4))
  31. like_button = lambda:driver.find_element_by_xpathh('/html/body/span/section/main/section/div[1]/div[1]/div/article[1]/div[2]/section[1]/span[1]/button').click()
  32. like_button().click()
  33. for second in reversed(range(0, random.randint(18, 28))):
  34. print("#" + hashtag + ': unique photos left: ' + str(unique_photos)
  35. + " | Sleeping " + str(second))
  36. time.sleep(1)
  37. except Exception as e:
  38. time.sleep(2)
  39. unique_photos -= 1
  40.  
  41.  
  42.  
  43. username = "username"
  44. password = "passw"
  45.  
  46. ig = InstagramBot(username, password)
  47. ig.login()
  48.  
  49. hashtags = ['kittens']
  50.  
  51. while True:
  52. try:
  53. # Choose a random tag from the list of tags
  54. tag = random.choice(hashtags)
  55. ig.like_photo(tag)
  56. except Exception:
  57. ig.closeBrowser()
  58. time.sleep(60)
  59. ig = InstagramBot(username, password)
  60. ig.login()
Add Comment
Please, Sign In to add comment