Initial Commit

master
Amir Hossein Moghiseh 2023-04-19 02:03:20 +04:30
commit 9ddb4c8bd1
6 changed files with 709 additions and 0 deletions

5
.gitignore vendored 100644
View File

@ -0,0 +1,5 @@
*.json
*.pkl
*.txt
*.log
*.html

97
app.py 100644
View File

@ -0,0 +1,97 @@
import requests
import socket
import socks
import json
import pymongo
import time
import urllib.parse
from datetime import datetime
import signal
import sys
def signal_handling(signum, frame):
print("you chose to end the program")
sys.exit()
signal.signal(signal.SIGINT, signal_handling)
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 1080)
socket.socket = socks.socksocket
def get_database():
# Provide the mongodb atlas url to connect python to mongodb using pymongo
CONNECTION_STRING = "mongodb://amir:1234@localhost:27017/insta"
# Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
client = pymongo.MongoClient(CONNECTION_STRING)
# Create the database for our example (we will use the same database throughout the tutorial
return client
def get_comments(min_id: str, post_pk):
url = f"https://www.instagram.com/api/v1/media/{post_pk}/comments/?can_support_threading=true&min_id={urllib.parse.quote(str(min_id).encode())}"
if (min_id.startswith("permalink")):
url = f"https://www.instagram.com/api/v1/media/{post_pk}/comments/?can_support_threading=true&permalink_enabled=false"
payload = {}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'X-CSRFToken': 'SyRjkhE11IZJJzEjFYNFOCAkbHObhpp6',
'X-IG-App-ID': '936619743392459',
'X-ASBD-ID': '198387',
'X-IG-WWW-Claim': 'hmac.AR3dSNyXs6hL8Kd5CQUR--JX03KHuY0enjoxwEaUbJ60jFfO',
'X-Requested-With': 'XMLHttpRequest',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://www.instagram.com/p/Cp7R84moFix/',
'Cookie': 'dpr=1.3636363636363635; ig_did=C7B7A9D0-EAFA-4B62-8AFF-8217A91EA3B8; datr=xKsVZPIxlYi2YshNs71hgDiE; csrftoken=SyRjkhE11IZJJzEjFYNFOCAkbHObhpp6; mid=ZBWrxgALAAEC4bnKm9IP1TZAOBVU; ig_nrcb=1; rur="ASH\\05458490635989\\0541710690982:01f7d99273b41cbcfaffa5a5edd22f59b954c960e0e8a5576dd95f63fe741570bc97a123"; ds_user_id=58490635989; sessionid=58490635989%3Asm9z3FQSns1adH%3A5%3AAYcZBsVcUAY3bASwyC9goX-5mMD89Zxnpbjt29E0ug',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'TE': 'trailers'
}
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
return data
# client = get_database()
# db = client['insta']
# cls = db.list_collection_names()
# return nextcursor
target_account_name = "bartar.academy"
target_classification = "seo-web"
if __name__ == "__main__":
try:
i = 0
postsfile = open(f"{target_account_name}.json", 'r', encoding="utf8")
posts = json.loads(postsfile.read())
for post in posts['items']:
nextcursor = 'permalink_enabled=false'
comments = []
i += 1
while True:
data = get_comments(nextcursor, post['pk'])
nextcursor = data['next_min_id'] if 'next_min_id' in data else None
comments += data['comments']
if nextcursor is None or 'cached_comments_cursor' not in nextcursor:
current = datetime.now()
f = open(
f"{i}-{target_classification}-{target_account_name}-{current.strftime('%Y-%m-%d-T-%H-%M-%S')}-{post['pk']}-scrapped.json", "w")
f.write(json.dumps(comments))
f.close()
print(len(comments))
break
time.sleep(3)
except:
pass

102
crawler.py 100644
View File

@ -0,0 +1,102 @@
import json
import time
import requests
import socket
import socks
import urllib.parse
from datetime import datetime
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 1080)
socket.socket = socks.socksocket
tag_name = 'برنامه_نویسی'
def getTopPosts():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'X-CSRFToken': '3rSOOhL1DSUQT2czImiCbMevgLgGx6kZ',
'X-IG-App-ID': '936619743392459',
'X-ASBD-ID': '198387',
'X-IG-WWW-Claim': 'hmac.AR0OphPNsUYX-i9oXNzfh6JF3hDx_3eDAjxFFjWI0DYyKsO6',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
'Referer': 'https://www.instagram.com/explore/tags/%D8%A8%D8%B1%D9%86%D8%A7%D9%85%D9%87_%D9%86%D9%88%DB%8C%D8%B3%DB%8C/',
'Cookie': 'ig_did=ACEC5413-54FC-4EFA-B0AF-C5F43D60BB8A; datr=usoZZDUz2YHk2UAtkauI2jWW; mid=ZBnKwwALAAEW_HOHO2zCuHUsHbG8; ig_nrcb=1; sessionid=58527153666%3AszpE7BNZUXL1Z9%3A7%3AAYelK_Wp6wCVw9U89yijo3VXXbDpn4zdD9Q-rOXy-Q; ds_user_id=58527153666; csrftoken=3rSOOhL1DSUQT2czImiCbMevgLgGx6kZ; dpr=1.25; shbid="16293\\05458527153666\\0541712933531:01f7f3123fe1f37549d037b1a1411e119e8d36295768b16f0d4189989400a3f6c4719399"; shbts="1681397531\\05458527153666\\0541712933531:01f78bd56e3232a7c8e15ca38b2d311e7d086901d832c9e272814290edb76152436a31dd"; rur="LDC\\05458527153666\\0541712933636:01f75496bea7dd870ec16fa6f07ff96b9d518c7d09d2d1b3c6b84cdcbd06689e2ae5bc1e"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
}
params = {
'tag_name': tag_name,
}
response = requests.get(
'https://www.instagram.com/api/v1/tags/web_info/', params=params, headers=headers)
return response.json()
def getComments(min_id: str, post_pk):
try:
url = f"https://www.instagram.com/api/v1/media/{post_pk}/comments/?can_support_threading=true&min_id={urllib.parse.quote(str(min_id).encode())}"
if (min_id.startswith("permalink")):
url = f"https://www.instagram.com/api/v1/media/{post_pk}/comments/?can_support_threading=true&permalink_enabled=false"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'X-CSRFToken': '3rSOOhL1DSUQT2czImiCbMevgLgGx6kZ',
'X-IG-App-ID': '936619743392459',
'X-ASBD-ID': '198387',
'X-IG-WWW-Claim': 'hmac.AR0OphPNsUYX-i9oXNzfh6JF3hDx_3eDAjxFFjWI0DYyKsO6',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
'Referer': 'https://www.instagram.com/p/CqkUyB6ICbR/',
'Cookie': 'ig_did=ACEC5413-54FC-4EFA-B0AF-C5F43D60BB8A; datr=usoZZDUz2YHk2UAtkauI2jWW; mid=ZBnKwwALAAEW_HOHO2zCuHUsHbG8; ig_nrcb=1; sessionid=58527153666%3AszpE7BNZUXL1Z9%3A7%3AAYelK_Wp6wCVw9U89yijo3VXXbDpn4zdD9Q-rOXy-Q; ds_user_id=58527153666; csrftoken=3rSOOhL1DSUQT2czImiCbMevgLgGx6kZ; dpr=1.25; shbid="16293\\05458527153666\\0541712933531:01f7f3123fe1f37549d037b1a1411e119e8d36295768b16f0d4189989400a3f6c4719399"; shbts="1681397531\\05458527153666\\0541712933531:01f78bd56e3232a7c8e15ca38b2d311e7d086901d832c9e272814290edb76152436a31dd"; rur="LDC\\05458527153666\\0541712933653:01f7eaf265c5347c6a98150aed096c93c7f28416075157f5491744f72be82f92a96b225b"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
}
response = requests.request("GET", url, headers=headers)
data = response.json()
return data
except:
return False
data = getTopPosts()
grids = data['data']['top']['sections']
comments = []
mediacounter = 0
for grid in grids:
medias = grid['layout_content']['medias']
for media in medias:
next_cursor = 'permalink_enabled=false'
while True:
data = getComments(min_id=next_cursor,
post_pk=media['media']['pk'])
if data is not False:
next_cursor = data['next_min_id'] if 'next_min_id' in data else None
comments += data['comments']
time.sleep(5)
if next_cursor is None or 'cached_comments_cursor' not in next_cursor:
mediacounter += 1
print(mediacounter)
break
print(f"comments counts: {len(comments)}")
current = datetime.now()
f = open(
f"{tag_name}-{current.strftime('%Y-%m-%d-T-%H-%M-%S')}-{media['media']['pk']}-scrapped.json", "w")
f.write(json.dumps(comments))
f.close()

145
keepDm.py 100644
View File

@ -0,0 +1,145 @@
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

65
listuser.py 100644
View File

@ -0,0 +1,65 @@
import json
import os
import math
i=0
target_classes = ['seo-web','برنامه_نویسی']
onlineShops=set()
users=set()
for file in os.listdir('.'):
for target_class in target_classes:
if target_class in file and 'users' not in file:
try:
f = open(file,'r')
jsondata = json.loads(f.read())
for cm in jsondata:
# print(cm['text'])
is_online_shop=False
username =username=cm['user']['username']
target_words = ['seo','web','programming']
# target_words =["sh0p","keratin","collection","beeauty","makeup","hairsalon","design","lift","nail","galerry","accessory","skin","skiin","skincaare","shoop","style","market","beauti","vitrin","art","badalijat",'store','shop',"arayesh","beauty","cosmetic","perfume","zibae","zibai","skincare","gallery"]
# target_words =["brauty","arayesh","shop"]
for target_word in target_words:
if target_word in username:
i+=1
onlineShops.add(username)
is_online_shop=True
if is_online_shop == False:
users.add(username)
except:
pass # print(f"https://instagram.com/{username}")
print(len(users))
print(len(onlineShops))
f2 = open("seo-web-focused.txt","a")
f3 = open("users-seo-web.txt","a")
f3.truncate(0)
f2.truncate(0)
for user in sorted(onlineShops):
f2.write(f"@{user}\n")
for user in users:
f3.write(f"@{user}\n")
# b=0
# filenumber = 1
# fl = open(f"users-list-seo-web-{str(filenumber)}.txt","a")
# for user in sorted(users):
# fl.write(f"https://instagram.com/{user}\n")
# b+=1
# if (b == 5):
# filenumber+=1
# b=0
# fl = open(f"users-list-seo-web-{str(filenumber)}.txt","a")
# fl = open(f"users-list-{target_class}.txt","a",encoding="utf-8")
# fl.truncate(0)
# for user in users:
# fl.write(f"@{user}\n")
# b+=1
# if b % 5 ==0:
# fl.write(f"نگاهتون رو به این محتوا دعوت میکنم")
# fl.write(f"\n------------------------------------------------\n")

295
sendDm.py 100644
View File

@ -0,0 +1,295 @@
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 = 1
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/Cp-00sZKpQr/"
target_story = "https://www.instagram.com/stories/highlights/17842412630962504/"
is_story = True
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)
browser = webdriver.Firefox(firefox_profile=profile)
def addMember(index, wantsToaddMember):
f = open(f"users-list-seo-web-{str(index)}.txt", "r")
for line in f:
username = line.split("://instagram.com/")[1]
if not wantsToaddMember:
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)
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]/div/div/div[1]/span"):
print("fetching ... ")
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]/div/div/div[1]/span")
targetelement.click()
else:
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)
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]/div/div/div[1]/span"):
print("fetching ... ")
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]/div/div/div[1]/span")
targetelement.click()
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()
save_coockie()
time.sleep(5)
composeElement = 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[1]/div/div[3]/button/div")
composeElement.click()
groupcreated = False
cursor = 250
countUser = 0
addmemeberscount = 0
_range = cursor+100
f = open(f"users-list-seo-web.txt", "r")
fc = open(f"cursor-seo-web.txt", "w")
lines = f.readlines()
allgpmembers = 0
while allgpmembers < 25:
for line in lines[cursor:]:
countUser += 1
fc.flush()
fc.write(str(cursor) + str(countUser))
username = line.split("://instagram.com/")[1]
if not groupcreated:
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)
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]/div/div/div[1]/span"):
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]/div/div/div[1]/span")
targetelement.click()
else:
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]/div/div/div[1]/span"):
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]/div/div/div[1]/span")
targetelement.click()
addmemeberscount += 1
if addmemeberscount > group_initial_size:
addmemeberscount = 0
print("chunk added", addmemeberscount)
break
nextelement = 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[1]/div/div[3]/div/button/div")
nextelement.click()
time.sleep(1)
if not groupcreated:
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()
gpName = f"میدونم چی دوس داری {str(random.randint(13, 500))}"
groupname = 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[1]/div[1]/input")
groupname.clear()
groupname.send_keys(gpName)
gpName = groupname.get_attribute('value')
donebuttonelement = 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[1]/div[1]/div/button")
donebuttonelement.click()
if is_story:
browser.get(target_story)
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[1]/div/div/div/div[3]/button"):
print("waitning for story page to load")
browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[1]/div/div/div/div[3]/button").click()
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/header/div[2]/div[2]/button[1]/div") or not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[3]/div/div/div[3]/button/div"):
print("waitning for story page to load")
browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/header/div[2]/div[2]/button[1]/div").click()
time.sleep(10)
browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[3]/div/div/div[3]/button/div").click()
searchinputelement = 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")
searchinputelement.send_keys(gpName)
time.sleep(10)
while not check_exists_by_xpath(browser.find_element(By.XPATH,f"/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[2]/div[1]/div/div/div[2]/div/div/div[1]/span[text()='{gpName}']")):
browser.find_element(
By.TAG_NAME, "body").send_keys(Keys.ESCAPE)
time.sleep(5)
browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[3]/div/div/div[3]/button/div").click()
gprow = 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]/div/div/div[1]/span")
gprow.click()
sendButton = 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[4]/button")
sendButton.click()
else:
browser.get(target_post)
time.sleep(10)
while not check_exists_by_xpath("/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/main/div[1]/div/article/div/div[2]/div/div[2]/section[1]/span[3]/button"):
print("waitning for page to load")
time.sleep(1)
sharebutton = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/main/div[1]/div/article/div/div[2]/div/div[2]/section[1]/span[3]/button")
sharebutton.click()
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[1]/div/div[2]/input"):
print("waitning for input")
time.sleep(1)
searchinputelement = 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")
searchinputelement.send_keys(gpName)
time.sleep(10)
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]/div/div/div[1]/span"):
browser.find_element(
By.TAG_NAME, "body").send_keys(Keys.ESCAPE)
sharebutton = browser.find_element(
By.XPATH, "/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/main/div[1]/div/article/div/div[2]/div/div[2]/section[1]/span[3]/button")
sharebutton.click()
time.sleep(5)
gprow = 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]/div/div/div[1]/span")
gprow.click()
sendButton = 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[4]/button")
sendButton.click()
browser.get("https://www.instagram.com/direct/inbox/")
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[3]/div/div/div/div/div[1]/a/div[1]/div/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[3]/div/div/div/div/div[1]/a/div[1]/div/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[2]/div/div[2]/div/div/div[2]/textarea"):
print("waitning for textarea")
time.sleep(1)
catName = " #تکنولوژی " + " #سئو "
inputdirect = 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[2]/div/div[2]/div/div/div[2]/textarea")
inputdirect.send_keys(f"من میدونم تو به {catName} علاقه داری")
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[2]/div/div[2]/div/div/div[3]/button"):
print("waitning for send button")
time.sleep(1)
sendButtonElement = 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[2]/div/div[2]/div/div/div[3]/button")
sendButtonElement.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
print("Done")