Skip to content

Commit

Permalink
add lesson6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pythontoday committed Aug 31, 2020
1 parent 8f8f995 commit b8905fa
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
venvInstagam_bot/
venvInstBot/
*.pyc
auth_data.py

Expand Down
Binary file modified chromedriver/chromedriver
Binary file not shown.
1 change: 1 addition & 0 deletions direct_users_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
direct_users_list = ['user1', 'user2', 'user3', '...']
Binary file added lesson_6/img1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 34 additions & 17 deletions lesson_6/lesson6.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from auth_data import username, password
from direct_users_list import direct_users_list
import time
import random
from selenium.common.exceptions import NoSuchElementException
Expand Down Expand Up @@ -362,7 +363,7 @@ def get_all_followers(self, userpage):
self.close_browser()

# метод для отправки сообщений в директ
def send_direct_message(self, username="", message=""):
def send_direct_message(self, usernames="", message="", img_path=''):

browser = self.browser
time.sleep(random.randrange(2, 4))
Expand All @@ -382,34 +383,50 @@ def send_direct_message(self, username="", message=""):
browser.find_element_by_xpath("/html/body/div[4]/div/div/div/div[3]/button[2]").click()
time.sleep(random.randrange(2, 4))

send_message_button = browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[2]/div/button").click()
send_message_button = browser.find_element_by_xpath(
"/html/body/div[1]/section/div/div[2]/div/div/div[2]/div/button").click()
time.sleep(random.randrange(2, 4))

# вводим получателя
to_input = browser.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div[1]/div/div[2]/input")
to_input.send_keys(username)
time.sleep(random.randrange(2, 4))
# отправка сообщения нескольким пользователям
for user in usernames:
# вводим получателя
to_input = browser.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div[1]/div/div[2]/input")
to_input.send_keys(user)
time.sleep(random.randrange(2, 4))

# выбираем получателя из списка
users_list = browser.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/div[2]").find_element_by_tag_name("button").click()
time.sleep(random.randrange(2, 4))
# выбираем получателя из списка
users_list = browser.find_element_by_xpath(
"/html/body/div[4]/div/div/div[2]/div[2]").find_element_by_tag_name("button").click()
time.sleep(random.randrange(2, 4))

next_button = browser.find_element_by_xpath("/html/body/div[4]/div/div/div[1]/div/div[2]/div/button").click()
next_button = browser.find_element_by_xpath(
"/html/body/div[4]/div/div/div[1]/div/div[2]/div/button").click()
time.sleep(random.randrange(2, 4))

text_message_area = browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea")
text_message_area.clear()
text_message_area.send_keys(message)
time.sleep(random.randrange(2, 4))
text_message_area.send_keys(Keys.ENTER)
print(f"Сообщение для {username} успешно отправлено!")
# отправка текстового сообщения
if message:
text_message_area = browser.find_element_by_xpath(
"/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea")
text_message_area.clear()
text_message_area.send_keys(message)
time.sleep(random.randrange(2, 4))
text_message_area.send_keys(Keys.ENTER)
print(f"Сообщение для {usernames} успешно отправлено!")
time.sleep(random.randrange(2, 4))

# отправка изображения
if img_path:
send_img_input = browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/form/input")
send_img_input.send_keys(img_path)
print(f"Изображение для {usernames} успешно отправлено!")
time.sleep(random.randrange(2, 4))

self.close_browser()


my_bot = InstagramBot(username, password)
my_bot.login()
my_bot.send_direct_message("username", "Hey! Are u here?")
my_bot.send_direct_message(direct_users_list, "Hey! How's it going?", "/home/cain/PycharmProjects/instagram_bot/lesson_6/img1.jpg")
# my_bot.get_all_followers('https://www.instagram.com/username/')
# my_bot.download_userpage_content("https://www.instagram.com/username/")

7 changes: 7 additions & 0 deletions req.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
certifi==2020.6.20
chardet==3.0.4
idna==2.10
pkg-resources==0.0.0
requests==2.24.0
selenium==3.141.0
urllib3==1.25.10

0 comments on commit b8905fa

Please sign in to comment.