IIC2233 / syllabus-2021-1

Repositorio oficial del curso IIC2233 Programación Avanzada 🚀✨
https://iic2233.github.io
38 stars 49 forks source link

Duda tema recreacional externo al curso para ganar concurso de instagram #638

Closed vcrb18 closed 3 years ago

vcrb18 commented 3 years ago

Prerrequisitos

(Marcar colocando una X entre los corchetes los ítems que ya hiciste, así: "[X]")

Contenido

Este issue tiene 0 apuro en ser contestado y no tiene nada que ver con la universidad. Estaba averiguando y pidiendo ayuda para ver como hacer un código que haga un comentario en una foto de instagram para ganar un concurso. Con la ayuda que me dieron, conseguí un código que comente una foto cada cierto tiempo, sin embargo, lo que yo quiero lograr es ser el primero en comentar una foto. Recurro aquí para pedir ayuda porque a lo más me dicen que no me pueden ayudar jajaja, pero me gusta mucho esto por el hecho de que es aplicar códigos a la vida real. No sé si me pueden ayudar a ver qué cambios se deberían hacer en el código para lograr hacer este cambio. Este código esta compuesto por dos archivos.py, en donde en el segundo solo se guarda la contraseña. Si es que hay algún ayudante que le interesa ayudarme puedo enviarle los códigos por discord, de cualquiera manera, muchas gracias si ya solo leyeron este issue jajaja.


from time import sleep
from secrets2 import pw
from selenium.webdriver.common.keys import Keys
from random import randint, sample, random, choice, choices

class Bot():

    links = []

    comments = [
        'quiero ganarr'
    ]

    tags = [
        '@account',
        '@account',
        '@account',
        '@account',
        '@account',
        '@account',
    ]

    letras = list("abcdefghijklmnopqrtsuvwxyz1234567890")

    accounts = [
        'leckabier'
    ]

    def __init__(self):

        # self.like_comment_by_hashtag('technology')
        # self.comment_on_account()
        op = webdriver.FirefoxOptions()
        op.add_argument('headless')

        counter = 3000
        while True:
            try:
                driver = "firefox"
                self.login('account', driver, op)
                for i in range(50000):
                    # comment = " ".join(sample(self.tags, k=3))
                    # # if random() <= 0.9:
                    # #     comment += " " + "!" * randint(0, 20)
                    # # else:
                    # #     comment += choice(self.comments)
                    # let = randint(1, 6)
                    # comment += " " + "".join(choices(self.letras, k=let))
                    comment = "Quiero ganarr!"
                    self.comment_mike(comment, driver)
                    counter += 1
                    print("Comentario nr", counter)
                    # if i % 40 == 0:
                    #     self.driver.close()
                    # if driver == "chrome":
                    #     driver = "firefox"
                    # else:
                    #     driver = "chrome"
                    # self.login('account', driver, op)
            except Exception:
                print("Error")
                self.driver.close()

    def login(self, username, driver, op):
        if driver == "chrome":
            self.driver = webdriver.Chrome()
        else:
            self.driver = webdriver.Firefox(options=op)
        self.driver.get('https://www.instagram.com/')
        sleep(5)
        username_input = self.driver.find_element_by_xpath(
            "//input[@name='username']")
        username_input.send_keys(username)
        password_input = self.driver.find_element_by_xpath(
            "//input[@name='password']")
        password_input.send_keys(pw)
        submit_btn = self.driver.find_element_by_xpath(
            "//button[@type='submit']")
        submit_btn.click()
        sleep(5)
        try:
            self.driver.find_element_by_xpath(
                '//*[@id="react-root"]/section/main/div/div/div/div/button').click()
        except:
            pass
        try:
            self.driver.find_element_by_xpath(
                '/html/body/div[4]/div/div/div[3]/button[2]').click()
        except:
            pass

    def comment_mike(self, comment, driver):
        link = "https://www.instagram.com/p/CCjyJMDD0dvo56IbzEsYZrYO9SfefF7yFgoYXs0/"
        self.driver.get(link)
        self.driver.find_element_by_class_name('RxpZH').click()
        if driver == "chrome":
            self.driver.find_element_by_xpath(
                "//textarea[@placeholder='Añade un comentario...']").send_keys(comment)
        else:
            self.driver.find_element_by_xpath(
                "//textarea[@placeholder='Add a comment…']").send_keys(comment)
        # sleep(2)
        self.driver.find_element_by_xpath(
            "//button[@type='submit']").click()

    def comment_on_account(self):
        for account in self.accounts:
            # get to profile page
            self.driver.get('https://www.instagram.com/{}/'.format(account))
            # get most recent photo
            links = self.driver.find_elements_by_tag_name('a')

            def condition(link):
                return '.com/p/' in link.get_attribute('href')
            valid_links = list(filter(condition, links))
            last_photo_url = valid_links[1].get_attribute('href')
            self.driver.get(last_photo_url)
            # comment on the photo

            # sleep(1)
            for _ in range(10):
                self.driver.find_element_by_class_name('RxpZH').click()
                self.driver.find_element_by_xpath(
                    "//textarea[@placeholder='Añade un comentario...']").send_keys(self.comments[0])
                # sleep(2)
                self.driver.find_element_by_xpath(
                    "//button[@type='submit']").click()
                # sleep(2)

    def like_comment_by_hashtag(self, hashtag):
        search_box = self.driver.find_element_by_xpath(
            "//input[@placeholder='Search']")
        search_box.send_keys('#'+hashtag)
        sleep(5)
        self.driver.find_element_by_xpath(
            '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a[1]').send_keys(Keys.ENTER)
        sleep(5)

        links = self.driver.find_elements_by_tag_name('a')

        def condition(link):
            return '.com/p/' in link.get_attribute('href')
        valid_links = list(filter(condition, links))

        for i in range(5):
            link = valid_links[i].get_attribute('href')
            if link not in self.links:
                self.links.append(link)

        for link in self.links:
            self.driver.get(link)

            # like
            self.driver.find_element_by_xpath(
                '//*[@id="react-root"]/section/main/div/div[1]/article/div[2]/section[1]/span[1]/button').click()
            sleep(3)

            # comment
            self.driver.find_element_by_class_name('RxpZH').click()
            sleep(1)
            self.driver.find_element_by_xpath(
                "//textarea[@placeholder='Add a comment…']").send_keys(self.comments[randint(0, 8)])
            sleep(2)
            self.driver.find_element_by_xpath(
                "//button[@type='submit']").click()
            sleep(2)

def main():
    my_bot = Bot()

if __name__ == '__main__':
    main()```
pascualferrer commented 3 years ago

Hola, no te puedo ayudar con lo que andai buscando, pero me interesa esto y hace un tiempo anduve indagando para hacer un programa que me comentara una foto cada cierto tiempo, pa aumentar mis posibilidades de ganar. No sé si me puedes hablar al discord para ayudarme con eso? Gracias, te lo dejo... Pascual Ferrer#7048

cgvalle commented 3 years ago

bot que creo que hace lo que quieres

vcrb18 commented 3 years ago

gracias!