Post by Deleted on May 30, 2020 18:29:05 GMT
So I was given this bot to run by my cousin who got it from his friend. His friend didn't give any instructions however on how to use this bot, and I've been trying to run it.
So far with Elmon's help I managed to get functions to run, however the functions run in seperate chrome windows. For example, the login happens in one window but it opens another to start following people. (check the code to see what i mean).
What do I need to do to change it?
The code:
instaFuncs:
instaConsts
main
So far with Elmon's help I managed to get functions to run, however the functions run in seperate chrome windows. For example, the login happens in one window but it opens another to start following people. (check the code to see what i mean).
What do I need to do to change it?
The code:
instaFuncs:
from instaConsts import *
import time
from time import sleep
import random
from selenium import webdriver
'''
Topics to learn:
Variables + (scope),
List/Array,
For Loop, While loop, and if else,
Range,
functions syntax,
try catch syntax,
<-- Selenium -->
- driver.get
- find_element by path
XPath:
- XPaths <-- you want to learn how to do this
'''
def start_up(username, password):
username = username
password = password
mobile_emulation = {"deviceName": "Nexus 5"}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(desired_capabilities=chrome_options.to_capabilities())
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
time.sleep(1)
login(username, password, driver)
def follow(username, driver):
try:
if not is_following(username, driver):
driver.find_element_by_xpath("//button[text() = \"Follow\"]").click()
# update the table of usernames
return True
else:
return False
except:
print("following :" + username + " failed")
return False
def unfollow(username, driver):
if is_following(username, driver):
driver.find_element_by_xpath(
"//*[@id=\"react-root\"]/section/main/div/header/section/div[2]/div[2]/span/span[1]/button/div/span["
"contains(@aria-label, \"Following\")]").click()
sleep_random_decimals(small_lower, small_upper)
driver.find_element_by_xpath("// button[text() = \"Unfollow\"]").click()
return True
elif is_requested(username, driver):
driver.find_element_by_xpath("//button[text() = \"Requested\"]").click()
sleep_random_decimals(small_lower, small_upper)
driver.find_element_by_xpath("// button[text() = \"Unfollow\"]").click()
return True
else:
return False
def get_users_from_user(user, driver):
# returns an array
go_to_page(user, driver)
# driver.get("https://www.instagram.com/"+ user +"/followers/")
sleep_random_decimals(3, 7)
driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/main/div/ul/li[2]/a/span").click()
usernames = []
sleep_random_decimals(4, 7)
for i in range(1, 9):
try:
new_username = driver.find_element_by_xpath(
"//*[@id=\"react-root\"]/section/main/div[2]/ul/div/li[" + str(i) + "]/div/div[1]/div[2]/div[1]/a").text
time.sleep(1)
print(new_username)
time.sleep(2)
usernames.append(new_username)
except:
print("error for now...")
sleep_random_decimals(6,10)
return usernames
sleep_random_decimals(6, 10)
sleep_random_decimals(5, 10)
return usernames
def get_users_from_first_post(user, driver):
# returns array of 9 users who liked the post
go_to_page(user, driver)
sleep_random_decimals(2, 4)
first_post = driver.find_element_by_xpath(
"//*[@id=\"react-root\"]/section/main/div/div[4]/article/div[1]/div/div[1]/div[1]/a/div/div[2]")
first_post.location_once_scrolled_into_view
sleep_random_decimals(2, 4)
first_post.click()
sleep_random_decimals(2, 4)
try:
likes = driver.find_element_by_xpath(
"//*[@id=\"react-root\"]/section/main/div/div/article/div[2]/section[2]/div/div/a")
sleep_random_decimals(2, 4)
likes.location_once_scrolled_into_view
except:
print("probably a video")
return []
sleep_random_decimals(1, 2)
likes.click()
sleep_random_decimals(2, 4)
usernames = []
for i in range(1, 9):
try:
new_username = driver.find_element_by_xpath(
"//*[@id=\"react-root\"]/section/main/div[1]/div[1]/div[" + str(i) + "]/div[2]/div[1]").text
time.sleep(1)
print(new_username)
time.sleep(2)
usernames.append(new_username)
except:
print("error for now...")
sleep_random_decimals(4, 9)
return usernames
def get_my_followers(your_username, driver):
driver.get("https://www.instagram.com/" + your_username)
sleep_random_decimals(2, 4)
number_of_followers = int(
driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/main/div/ul/li[2]/a/span").text)
count = 0
followers = []
driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/main/div/ul/li[2]/a").click()
sleep_random_decimals(4, 5)
for i in range(1, number_of_followers + 1):
try:
new_username = driver.find_element_by_xpath(
"// *[ @ id = \"react-root\"] / section / main / div[2] / ul / div / li[" + str(
i) + "] / div / div[1] / div[2] / div")
new_username.location_once_scrolled_into_view
print(new_username.text)
followers.append(new_username.text)
sleep_random_decimals(0.5, 1)
except:
if (count < 11):
print("Error, trying again")
count += 1
print(followers)
return followers
def get_my_following(your_username, driver):
driver.get("https://www.instagram.com/" + your_username)
sleep_random_decimals(2, 4)
number_of_followers = int(
driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/main/div/ul/li[3]/a/span").text)
count = 0
following = []
driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/main/div/ul/li[3]/a").click()
sleep_random_decimals(4, 5)
for i in range(1,number_of_followers + 1):
try:
new_username = driver.find_element_by_xpath( "// *[ @ id = \"react-root\"] / section / main / div[2] / ul / div / li["+ str(i) +"] / div / div[1] / div[2] / div")
new_username.location_once_scrolled_into_view
print(new_username.text)
following.append(new_username.text)
sleep_random_decimals(0.5,1)
except:
if (count < 11):
print("Error, trying again")
count += 1
print(following)
return following
def unfollowing_my_following(your_username, driver):
driver.get("https://www.instagram.com/" + your_username)
followers = get_my_followers(your_username, driver)
following = get_my_following(your_username, driver)
for i in following:
if i not in followers:
print("unfollowing user: " + i)
unfollow(i, driver)
sleep_random_decimals(15, 50)
print("finished unfollowing dickhead users")
def follow_new_users(driver):
popular_accounts = ["travelandleisure", "bestvacations", "travel_a_little_luxe", "tourist2townie", "andyto",
"kristarossow"]
users_to_follow = []
count = 0
while count < 5:
for i in popular_accounts:
# collect the usernames that you need to follow
users_to_follow = users_to_follow + get_users_from_first_post(i, driver)
# sleep randomly inbetween getting users to reduce the suspicion of instagram
sleep_random_decimals(60, 120)
# sleep again
sleep(20)
# print all the users you're about the follow
print(users_to_follow)
print("sleeping for random between 2 mins and 5 mins")
sleep_random_decimals(150, 300)
# follow the users that you found with a random sleep
for j in users_to_follow:
follow(j, driver)
sleep_random_decimals(60, 120)
# reset the value of this array
users_to_follow = []
count += 1
print("Program Finished")
def action_blocked_checker(driver):
try:
driver.find_element_by_xpath("/html/body/div[3]/div/div/div[2]/button[2]")
print("Instagram Bot Detected... ")
sleep(1000000)
except:
i = 1
def send_message(username, message, driver):
# Message must only contain 4 bit Unicode.
go_to_page(username, driver)
sleep_random_decimals(2,5)
message_button = driver.find_element_by_xpath("//button[text()=\"Message\"]")
message_button.click()
sleep_random_decimals(2,5)
send_message_field = driver.find_element_by_xpath("//textarea[@placeholder=\"Message...\"]")
send_message_field.send_keys(message)
sleep_random_decimals(2,5)
send_button = driver.find_element_by_xpath("//button[text()=\"Send\"]")
send_button.click()
# helper methods
def login(username, password, driver):
driver.find_element_by_xpath("//Input[@name=\"username\"]") \
.send_keys(username)
driver.find_element_by_xpath("//Input[@name=\"password\"]").send_keys(password)
time.sleep(0.5)
driver.find_element_by_xpath("//button[@type =\"submit\"]").click()
sleep_random_decimals(2, 3)
try:
driver.find_element_by_xpath("//button[text()=\"Not Now\"]").click()
sleep_random_decimals(2, 3)
except:
print("Could not find \"Not Now\" button, carrying on with login authentication flow")
sleep_random_decimals(2, 3)
try:
driver.find_element_by_xpath("//button[text()=\"Cancel\"]").click()
sleep_random_decimals(2, 3)
except:
print("Could not fine \"Cancel\" button, carrying on with login authentication flow")
sleep_random_decimals(2, 3)
def get_username_from_url(driver):
# assumes you've gone to the correct URL
return driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/main/div/header/section/div[1]/h1").text
def is_following(username, driver):
go_to_page(username, driver)
sleep_random_decimals(0, 0.5)
try:
driver.find_element_by_xpath(
"//*[@id=\"react-root\"]/section/main/div/header/section/div[2]/div[2]/span/span[1]/button/div/span["
"contains(@aria-label, \"Following\")]")
print("you are following" + username)
return True
except:
print("you are not following" + username)
return False
def is_requested(username, driver):
go_to_page(username, driver)
sleep_random_decimals(0, 0.5)
try:
driver.find_element_by_xpath("//button[text()=\"Requested\"]")
print("you have requested" + username)
return True
except:
print("you have not requested" + username)
return False
def go_to_page(username, driver):
driver.get("https://www.instagram.com/" + username + "/")
def sleep_random_decimals(lower_bound, upper_bound):
time.sleep(float(random.randrange(lower_bound * 1000, upper_bound * 1000) / 1000))
def unfollow_users_not_following(following, followers, driver):
# params - following = list of perople following,
# followers = list of who im following
# collect my followers in an array,
# collect my following in an array,
# find users in following but not followers
# unfollow these users.
for i in following:
if i not in followers:
unfollow(i, driver)
print("unfollowed: " + i)
sleep_random_decimals(60, 120)
print("Done")
instaConsts
heart_emoji1 = "\u2764"
heart_emoji2 = "\u2763"
smile_emoji = "\uE414"
small_lower = 0.2
small_upper = 1.5
medium_lower = 5
medium_upper = 10
large_lower = 500
large_upper = 10000
account_login_user = "ENTER USERNAME 1"
account_login_user1 = "ENTER USERNAME 2"
account_login_pass = "ENTER PASSWORD 1"
account_login_pass1 = "ENTER PASSWORD 2"
TESTUSERNAME1 = "ryhanx"
TESTUSERNAME2 = "instagram"
TESTUSERNAME3 = "youtube"
TESTUSERNAME4 = "bbcnews"
# Username and pass removed for reasons.
main
import instaConsts
from instaFuncs import *
start_up(account_login_user, account_login_pass)
print('Setup complete. Now starting your chosen function')
follow_new_users(webdriver.Chrome())