Skip to content

Commit

Permalink
Instagram following bot
Browse files Browse the repository at this point in the history
  • Loading branch information
artur.sniegowski committed Sep 8, 2022
0 parents commit bbfaac6
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Numerous always-ignore extensions
*.diff
*.err
*.orig
*.log
*.rej
*.swo
*.swp
*.vi
*~
*.sass-cache
node_modules/
.tmp/

# OS or Editor folders
.DS_Store
Thumbs.db
.cache
.project
.settings
.tmproj
*.esproj
nbproject
*.sublime-project
*.sublime-workspace
*.komodoproject
.komodotools
_notes
dwsync.xml

# Environmental variables
.env

# Virtual enviroment
ENV/

### Django ###
*.sqlite3

#passwords
passwords.txt

# spotipy lib
token.txt
.cache

# selenium - chrom driver
ChromeDriver_browser/
2 changes: 2 additions & 0 deletions Instagram_follower_bot/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MY_INSTAGRAM_EMAIL = "your.email.for_Instagram@no-replay.com"
MY_INSTAGRAM_PASSWORD = "Your_Instagram_password"
40 changes: 40 additions & 0 deletions Instagram_follower_bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Instagram_follower_bot

It is a python selenium based bot that will boost your followers by following simple steps.
First, it will automatically log in to your Instagram account. Secondly, it is going to go to the account you want to replicate followers for and check its followers. In the last step, the bot will follow these individual accounts one by one automatically.
This way, the accounts you followed might notice your account and follow you back.
So basically, follow the followers of your competitors in your given market should boost your own follower count (given that your content is of similar nature).</br>

---

Necessary steps to make the program work:</br>
1. Install the Chrome web browser https://www.google.com/intl/en_uk/chrome/ </br>
2. Download Chrome Driver (don't forget to match the version of your Chrome with the version of the Chrome Driver) https://chromedriver.storage.googleapis.com/index.html?path=104.0.5112.79/, and unzip the file for your OS.
Mark the DIR to the chromedriver.exe file and adjust the *CHROME_DRIVER_PATH* in main.py. </br>
3. You will need an Instagram account. Sign up for Instagram, https://www.instagram.com/. </br>
4. After creating the Instagram account, we have to change the name of .env.example to .env and define the environmental variables according to the created account:</br>
*MY_INSTAGRAM_EMAIL* = "your.email.for_Instagram@no-replay.com"</br>
*MY_INSTAGRAM_PASSWORD* = "Your_Instagram_password"</br>
5. The user has to adjust the starting variables in the main.py:</br>
*SIMILAR_INSTAGRAM_ACCOUNT* - account you want to replicate followers for. Figure out which account you would like to target. (Pick a large account that has a lot of followers). This user has to exist in Instagram otherwise the bot will terminate the porgram with an Error: Maybe the user doesn't exist!</br>
*CHROME_DRIVER_PATH* - as stated in point 2.</br>
6. Install the required libraries from the requirements.txt using the following command: </br>
*pip install -r requirements.txt*</br>

---

**The bot automatically logs in.**</br>
![Screenshot](docs/img/01_automatically_logs_in_to_instagram.png)</br>

**The bot automatically opens the given user Instagram and starts following the given user followers.**</br>
![Screenshot](docs/img/02_after_log_in_opens_users_followers_and_start_follwoing_them.png)</br>

**The bot will log out at when done with adding followers.**</br>
![Screenshot](docs/img/03_logs_out_after_finishing_with_following_users.png)</br>

---

**The program was developed using python 3.10.6, selenium**


In order to run the program, you have to execute main.py.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
203 changes: 203 additions & 0 deletions Instagram_follower_bot/instagram_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# class for handling the instagram bot

from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException, NoSuchElementException, TimeoutException
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import time

# env variables !
from dotenv import load_dotenv
load_dotenv()
import os


################################################################################
## sensitive data ###
#####################
# user defined email and app password for your Instagram account !
# env variables !! - dont change here !!
MY_INSTAGRAM_EMAIL = os.environ.get('MY_INSTAGRAM_EMAIL')
MY_INSTAGRAM_PASSWORD = os.environ.get('MY_INSTAGRAM_PASSWORD')
################################################################################


class InstagramBotSettings:
"""
settings for the Instagram bot
"""
WEBSITE_URL_INSTAGRAM_LOGIN = "https://www.instagram.com/accounts/login/"
WEBSITE_URL_INSTAGRAM_LOGOUT = "https://www.instagram.com/accounts/logout/"
WEBSITE_URL_INSTAGRAM = "https://www.instagram.com/"



class InstagramFollowBot:
"""
class for handling selenium, and instagram follow bot
"""
def __init__(self, chrome_driver_path:str) -> None:
# creating service for the chrome driver
self.chrome_driver_service = Service(chrome_driver_path)
# creating the webdriver
self.driver = webdriver.Chrome(service=self.chrome_driver_service)


def _wait_for_presence_of_element(self, wait_time_sec: int, locator_strategy: By,
selector: str, error_message: str) -> WebElement | bool:
"""
function to wait for an element presence, and then returns this element
if not found in a given time a Timeout exception will occure and the function will close the driver and
return False
"""
try:
results_element = WebDriverWait(self.driver, wait_time_sec).until(
EC.presence_of_element_located((locator_strategy, selector))
)
return results_element
except TimeoutException as timeout_ex:
print(type(timeout_ex))
print(error_message)
# quitting / closing the driver
self.driver.quit()
return False


def login(self) -> bool:
"""
login to your instagram account.
returns True if success and False if fails.
"""
self.driver.get(InstagramBotSettings.WEBSITE_URL_INSTAGRAM_LOGIN)

# wait for page to load
# time.sleep(3)

login_username_element = self._wait_for_presence_of_element(
wait_time_sec=10,
locator_strategy=By.CSS_SELECTOR,
selector='input[name="username"]',
error_message="Cant find the username input on the login page",
)
if not login_username_element: return False

login_username_element.send_keys(MY_INSTAGRAM_EMAIL)
login_password_element = self.driver.find_element(By.CSS_SELECTOR,'input[name="password"]')
login_password_element.send_keys(MY_INSTAGRAM_PASSWORD)

time.sleep(1)
# login !
login_password_element.send_keys(Keys.ENTER)

# wait for the homepage to load - waiting for the homepage login logo
logo_hompage_element = self._wait_for_presence_of_element(
wait_time_sec=10,
locator_strategy=By.CSS_SELECTOR,
selector="div[data-testid='instagram-homepage-logo']",
error_message="Cant find the homepage after login. Looking for login logo!",
)
if not logo_hompage_element: return False

# giving soem extra time to load
time.sleep(1)

return True


def find_followers(self, instagram_account: str) -> bool:
"""
finding followers for the given account name.
returns True if success and False if fails.
"""
# wait for the previous step to load the page
#time.sleep(10)
# changing the url to the user
# https://www.instagram.com/star._.wars._.memes/
self.driver.get(InstagramBotSettings.WEBSITE_URL_INSTAGRAM+f"{instagram_account}/")

# try to fidn the button with followers
followers_element = self._wait_for_presence_of_element(
wait_time_sec=10,
locator_strategy=By.CSS_SELECTOR,
# href=/star._.wars._.memes/followers/
selector=f"a[href='/{instagram_account}/followers/']",
error_message=f"Cant find the followers for the user {instagram_account}. Maybe the user:{instagram_account} doesn't exist!",
)
if not followers_element: return False

# if followers exists press it to open the modal with the deatl view of all followers
followers_element.click()

# wait for the modal to appear
# time.sleep(3)
return_element = self._wait_for_presence_of_element(
wait_time_sec=3,
locator_strategy=By.CSS_SELECTOR,
# href=/star._.wars._.memes/followers/
selector="div[id='scrollview']",
error_message=f"Cant find followers modal scrollview id that appears in the main view.",
)
if not return_element: return False

# extra time to load
time.sleep(1)

# select the modal
#modal_element = self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div/div/div[2]')
modal_element = self.driver.find_element(By.XPATH,"/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[2]")

# every time the loops goes by the modal will be scrolled down
# and more users will be loaded
for _ in range(2):
# scrolling down to load more followers
#In this case we're executing some Javascript, that's what the execute_script() method does.
#The method can accept the script as well as a HTML element.
#The modal in this case, becomes the arguments[0] in the script.
#Then we're using Javascript to say: "scroll the top of the modal (popup) element by the height of the modal (popup)"
self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", modal_element)
time.sleep(2)

return True


def follow(self) -> bool:
"""
function for following the followers of the target account.
returns True if success and False if fails.
"""
follow_buttons_elements = self.driver.find_elements(By.XPATH,"/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[2]/div[1]//button")


# going through each of the buttons and following the users
if len(follow_buttons_elements) > 0:
for follow_button in follow_buttons_elements:

try:
follow_button.click()
# wait time so instagram doesnt think it's a bot
time.sleep(1)
except ElementClickInterceptedException as intercept_exception:
# this occurse when we follow already the person and we get a popup window to unfollow
# in which case we have to press the cancle button
cancel_button_for_additonal_pop_up_window = self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div/div/div[2]/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[3]/button[2]')
cancel_button_for_additonal_pop_up_window.click()
time.sleep(1)

return True


def logout(self) -> None:
"""
function for log out from your instagram account.
"""
# logout
self.driver.get(InstagramBotSettings.WEBSITE_URL_INSTAGRAM_LOGOUT)
# time to say goodbye
time.sleep(5)
# and exit the driver
self.driver.quit()
29 changes: 29 additions & 0 deletions Instagram_follower_bot/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from instagram_bot import InstagramFollowBot


####################### INSTAGRAM SETTINGS #############################
# adjust this account name to your liking !
# instagram account you want to replicate followers for. Figure out which
# instagram account you would like to target. (Pick a large account that has
# a lot of followers).
# this user has to exist in instagram
SIMILAR_INSTAGRAM_ACCOUNT = "name_of_the_instagram_account" # adjust this !!
################################################################################


####################### SELENIUM SETTINGS ######################################
# adjust this path to wherever you have unpacked the chrome driver file !
# dont forget that the verion of chrome drive has to match the verison of
# chrome browser
# https://chromedriver.chromium.org/downloads
# relative path, you can use as well absolute path !
CHROME_DRIVER_PATH = "ChromeDriver_browser/chromedriver.exe"
################################################################################

# creating instagram bot object
instagram_bot = InstagramFollowBot(CHROME_DRIVER_PATH)

if instagram_bot.login():
if instagram_bot.find_followers(SIMILAR_INSTAGRAM_ACCOUNT):
if instagram_bot.follow():
instagram_bot.logout()
1 change: 1 addition & 0 deletions Instagram_follower_bot/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
1 change: 1 addition & 0 deletions Instagram_follower_bot/runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Python 3.10.6

0 comments on commit bbfaac6

Please sign in to comment.