instagram-scrap-2/keepDm.py

146 lines
6.3 KiB
Python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time
import pickle
from os.path import exists
import random
profile = webdriver.FirefoxProfile()
# Socks5 Host SetUp:-
group_initial_size = 2
user = "midoonamchidoosdariv2"
myProxy = "127.0.0.1:1080"
ip, port = myProxy.split(':')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', ip)
profile.set_preference('network.proxy.socks_port', int(port))
target_post = "https://www.instagram.com/p/Cp8c_yjq9JU/"
browser = webdriver.Firefox(firefox_profile=profile)
def check_exists_by_xpath(xpath):
try:
browser.find_element(By.XPATH, xpath)
except NoSuchElementException:
return False
return True
def save_coockie():
pickle.dump(browser.get_cookies(), open(f"cookies-{user}.pkl", "wb"))
def load_coockie():
cookies = pickle.load(open(f"cookies-{user}.pkl", "rb"))
for cookie in cookies:
browser.add_cookie(cookie)
def login():
if (exists(f"cookies-{user}.pkl")):
browser.get('http://instagram.com')
load_coockie()
browser.refresh()
return True
else:
browser.get('http://www.instagram.com')
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div [1]/ div/label/input"):
print("waiting ...")
time.sleep(1)
usernameinput = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[1]/div/label/input")
passwordinput = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[2]/div/label/input")
usernameinput.send_keys(user)
passwordinput.send_keys('22102210aA@')
passwordinput.submit()
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div[1]/div/div/a/ div/div [2]/div/div"):
print("loading ...")
time.sleep(5)
save_coockie()
return True
if __name__ == "__main__":
if login():
browser.get("https://www.instagram.com/direct/inbox/")
retry = 0
while not check_exists_by_xpath("/html/body/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]"):
if retry == 5:
break
retry += 1
time.sleep(1)
if check_exists_by_xpath("/html/body/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]"):
browser.find_element(
By.XPATH, "/html/body/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]").click()
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div/div[2]/div/section/div/div/div/div/div[1]/div[2]/div/div/div/div/div[1]/a/div[1]/div"):
print("waitning for first row")
time.sleep(1)
firstRow = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div/div[2]/div/section/div/div/div/div/div[1]/div[2]/div/div/div/div/div[1]/a/div[1]/div")
firstRow.click()
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div/div[2]/div/section/div/div/div/div/div[2]/div[1]/div/div/div[3]/button[3]/div"):
print("creating group ...")
time.sleep(1)
infoicon = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div/div[2]/div/section/div/div/div/div/div[2]/div[1]/div/div/div[3]/button[3]/div")
infoicon.click()
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div/div[2]/div/section/div/div/div/div/div[2]/div/div[2]/div[2]/div/div[1]/button"):
print("waiting for addpeople button")
time.sleep(1)
time.sleep(20)
try:
addPeoplebutton = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div/div[2]/div/section/div/div/div/div/div[2]/div/div[2]/div[2]/div/div[1]/button")
addPeoplebutton.click()
except:
pass
groupcreated = True
cursor = 170
countUser = 0
addmemeberscount = 0
allgpmembers=0
_range = cursor+100
f = open(f"users-list-seo-web.txt", "r")
fc = open(f"cursor-seo-web.txt", "w")
lines = f.readlines()
while allgpmembers <25:
for line in lines[cursor+countUser:]:
countUser += 1
f.truncate(0)
fc.write(str(cursor) + str(countUser))
username = line.split("://instagram.com/")[1]
searchinput = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div[2]/div[1]/div/div[2]/input")
searchinput.send_keys(username)
retry = 0
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div[2]/div[2]/div[1]/div[1]/div/div/div[2]"):
time.sleep(1)
print("fetching ... ")
retry += 1
if retry == 5:
break
if retry == 5:
searchinput.clear()
continue
time.sleep(1)
targetelement = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div[2]/div[2]/div[1]/div[1]/div/div/div[2]")
targetelement.click()
addmemeberscount += 1
allgpmembers+=1
if addmemeberscount > group_initial_size :
addmemeberscount = 0
print("chunk added", addmemeberscount)
break