user12099710
user12099710

Reputation:

Problem with Interacting with comments on instagram

I was creating the following script using Instapy:

#!/usr/bin/env python3

from instapy import InstaPy

number = int(input("How many Number of people you want to follow: "))
user = []
for i in range(0,number):
    name = input("Enter Username to interact: ")
    user.append(name)
session = InstaPy(username=" ",password=" ")
session.login()
#Used to Follow the User
session.set_do_follow(enabled=True, percentage=100)
#Used to comment on a post
session.set_comments(["Cool", "Super!"])
session.set_do_comment(enabled=True, percentage=80)
#Used to Like the Post of the User
session.set_do_like(True, percentage=70)
#Used to Interact with the User
session.interact_by_users(user, amount=5,randomize=True, media='Photo')
#Used to insteract by Comments
session.set_use_meaningcloud(enabled=True, license_key=' ', polarity="P")
session.set_use_yandex(enabled=True, API_key=' ', match_language=True, language_code="en")
session.set_do_reply_to_comments(enabled=True, percentage=100)
session.set_comment_replies(replies=[u"😎😎😎", u"😁😁😁😁😁😁😁πŸ’ͺ🏼", u"πŸ˜‹πŸŽ‰", "πŸ˜€πŸ¬", u"πŸ˜‚πŸ˜‚πŸ˜‚πŸ‘ˆπŸΌπŸ‘πŸΌπŸ‘πŸΌ", u"πŸ™‚πŸ™‹πŸΌβ€β™‚οΈπŸš€πŸŽŠπŸŽŠπŸŽŠ", u"😁😁😁", u"πŸ˜‚",  u"πŸŽ‰",  u"😎", u"πŸ€“πŸ€“πŸ€“πŸ€“πŸ€“", u"πŸ‘πŸΌπŸ˜‰"],media="Photo")
session.set_do_like(enabled=True, percentage=94)
session.interact_by_comments(user, posts_amount=10, comments_per_post=5, reply=True, interact=True, randomize=False, media="Photo")
session.end()

Whenever I try to run the script following Error displays:

Oh no! Failed to get the list of supported languages by Yandex Translate :( ~text language won't be matched

And

link_elems error local variable 'post_href' referenced before assignment

The main problem is writing comments on the post.

Upvotes: 0

Views: 1213

Answers (1)

Nizamudheen At
Nizamudheen At

Reputation: 334

There are couple of issues I have found with Instapy comment. I can help you with those.

  • Comments will not work if any other accounts are mentioned like @nizamudheen_at
  • Comments will break if it is too long.

These can be fixed quickly by modifying comment_util.py (Python37_64\Lib\site-packages\instapy) little bit.

One disadvantage instapy have it uses 'Enter' button always to post comment. So we need to tweak it make it is commenting by clicking "Post". Other wise long comments will not work.

To do that: Add a function in comment_util.py like:

def get_comment_post(browser):
    comment_post = browser.find_elements_by_xpath(
        read_xpath(get_comment_post.__name__, "comment_post")
    )

    return comment_post

And in comment_image function, make sure you add first if statement in try. This will make sure if you are mentioning any account and also if comment is longer, it is handled properly :

def comment_image(browser, username, comments, blacklist, logger, logfolder):
    """Checks if it should comment on the image"""
    # check action availability
    if quota_supervisor("comments") == "jump":
        return False, "jumped"

    rand_comment = random.choice(comments).format(username)
    rand_comment = emoji.demojize(rand_comment)
    rand_comment = emoji.emojize(rand_comment, use_aliases=True)

    open_comment_section(browser, logger)
    # wait, to avoid crash
    sleep(3)
    comment_input = get_comment_input(browser)
    comment_post = get_comment_post(browser)
    logger.info("{} pooooost".format(comment_post))
    logger.info("{} pooooost".format(comment_post[0]))

    try:
        if len(comment_input) > 0:
            # wait, to avoid crash
            sleep(2)
            comment_input = get_comment_input(browser)
            logger.info("{} .....".format(comment_input))
            logger.info("{} input[0]".format(comment_input[0]))
            # below, an extra space is added to force
            # the input box to update the reactJS core
            comment_to_be_sent = rand_comment

            # wait, to avoid crash
            sleep(2)
            # click on textarea/comment box and enter comment
            
            # wait, to avoid crash
            sleep(1)
            if '@' in comment_to_be_sent:
                (
                    ActionChains(browser)
                    .move_to_element(comment_input[0])
                    .click()
                    .send_keys(comment_to_be_sent)
                    .pause(3)
                    .move_to_element(comment_post[0])
                    .click()
                    .perform()
                )
            else:            
                
                # post comment / <enter>
                (
                    ActionChains(browser)
                    .move_to_element(comment_input[0])
                    .send_keys(Keys.ENTER)
                    .perform()
                )

            update_activity(
                browser,
                action="comments",
                state=None,
                logfolder=logfolder,
                logger=logger,
            )

Last but not the least, add xpath function to handle "post" button: In xpath_compile.py

xpath["get_comment_post"] = {
    "comment_post": "//button[text()='Post']",
    
}

Upvotes: 1

Related Questions