Instapy: Cycle through tags

Created on 30 May 2018  路  18Comments  路  Source: timgrossmann/InstaPy

I'm not quite understanding how like_by_tags is working.
When I add a few tags, I want it to run indefinitely , but cycle through the tags that I use.

For example, here is my command:
session.like_by_tags(['travel', 'nature', 'design'], amount=3)

It runs until it cycles through 3 pictures from each tag.
I want it to run until it successfully likes 3 pictures from a tag, but continues forever.

Is this possible?

wontfix

Most helpful comment

Worked! @uluQulu Thank you ! Really!
And by the way yes I was thinking of doing randomization after this as an improvement. I will research and experiment and will see what i can do. Again, Thank you very much.

All 18 comments

from instapy import InstaPy
import time
import os

insta_username = 'username'
insta_password = 'password'

session = InstaPy(username=insta_username, password=insta_password, headless_browser=True)
session.login()

session.set_user_interact(amount=1, randomize=True, percentage=100, media='Photo')
session.set_do_follow(enabled=True, percentage=100)
session.set_do_like(enabled=True, percentage=100)
session.set_comments(["Cool", "Nice!"])
session.set_do_comment(enabled=True, percentage=80)

for cycle in range(7):
print("\n\nLOOP {}\n".format(cycle+1))
session.interact_user_followers(['tech_insider', 'pcgaming', 'pcmagofficial'], amount=9999999, randomize=True)
time.sleep(50) #take a 50 seconds of break after finishing the feature above
session.unfollow_users(amount=500, onlyInstapyFollowed = False, onlyInstapyMethod = 'FIFO', sleep_delay=60 )
time.sleep(30) #take some more break after finishing unfollows

session.end()

the range determines the cycles

Looks like I may have fixed it by adding one line:

while True:
session.like_by_tags(['travel', 'nature', 'design'], amount=3)

Will let it run for a while and verify

Confirmed, while True: will run script indefinitely

Would be great if you share the example structure. Thanks.

import os
import time

from tempfile import gettempdir

from selenium.common.exceptions import NoSuchElementException

from instapy import InstaPy

insta_username = '###'
insta_password = '###'

session = InstaPy(username=insta_username,
password=insta_password,
headless_browser=True)
#multi_logs=True)

try:
session.login()

# settings
session.set_relationship_bounds(enabled=True,
             potency_ratio=-1.10,
              delimit_by_numbers=True,
               max_followers=5000,
                max_following=10000,
                 min_followers=10,
                  min_following=100)

session.set_delimit_liking(enabled=True, max=150, min=0)

# actions
while True:
    session.like_by_tags(['travel', 'nature', 'design'], amount=3)

except Exception as exc:
# if changes to IG layout, upload the file to help us locate the change
if isinstance(exc, NoSuchElementException):
file_path = os.path.join(gettempdir(), '{}.html'.format(time.strftime('%Y%m%d-%H%M%S')))
with open(file_path, 'wb') as fp:
fp.write(session.browser.page_source.encode('utf8'))
print('{0}\nIf raising an issue, please also upload the file located at:\n{1}\n{0}'.format(
'*' * 70, file_path))
# full stacktrace when raising Github issue
raise

finally:
# end the bot session
session.end()

Need to be sure that there are limitations set up, or you may get blocked from liking too many. My restrictions will only like pictures that have under 150 likes, and only by people with a -1.1 followed/followers ratio. This keeps it going at a slow pace, as there are many photos that are skipped.

Thank you.

Hey your code works great! Simple and effective!

I have an idea and wanted to share with you and with @uluQulu. I saw that he reached to your comment and maybe he would be interested.

I don't know if that's possible but i was thinking of a cycle that's starts to cycle during between certain hours by checking the system time.

For example:
START LOOP
white True:
systime= check current system time
if (systime<=14:00 && systime=>9:00) {
do action with limited number for example 100
}

else if (systime<19:00 && systime=>14:00) {
do another different action with limited number
}
else
do nothing for X minutes and start cycle again

Because while True is an infinite loop, is it possible to have if else statements in in this Boolean loop? Also when do we need to introduce the system time? before the loop or after the loop?

This in my mind is just a messy idea and maybe you guys could help to make it work if that's even possible.

What will be even better is to have random values of times and random actions buts that's not necessary.

I am looking forward to hear what are your thoughts on this.

Hey @imbarismustafa
In python all you need is indentation, those curly braces, parenthesis around conditions, etc. are not needed :D
_wonder in which language you have programmed before.._

Just hit the edge of your imagination and write down the ultimate activity flow, I will help you achieve it, but be concrete. :/

Hello @uluQulu. I normally use c++ for programming.
I tried to use python to achieve my goal. My goal is to make a loop like @levfo made with while True.
I want to turn this infinite loop into a scheduler with the use of a library called "datetime".
I wrote this code so that I can understand and the others to understand step by step:

import time
import datetime

#Set start and stop time
start1=datetime.time(hour=8, minute=00).strftime("%H:%M")
stop1=datetime.time(hour=13, minute=00).strftime("%H:%M")
start2=datetime.time(hour=15, minute=00).strftime("%H:%M")
stop2=datetime.time(hour=21, minute=00).strftime("%H:%M")

#Checks for current system time
current_time=datetime.datetime.now().strftime("%H:%M")

#First if statement checks is the time is between 8:00 to 13:00
#Seconds elif checks if the time is between 15:00 to 21:00
#Third is if the time is anything else to just pause and do nothing.
if current_time<=stop1 and current_time>=start1:
    print('Do work from 8am to 1pm')
elif current_time<=stop2 and current_time>=start2:
    print('Do work from 3pm to 9pm')
 else: 
    print('Do nothing for X seconds')

Then I tried to combine this logic with quickstart.py:

import os
import time
import datetime #CHANGE IN THIS LINE: datetime imported
from tempfile import gettempdir
from selenium.common.exceptions import NoSuchElementException
from instapy import InstaPy

insta_username = username''
insta_password = 'pass'

session = InstaPy(username=insta_username,
                  password=insta_password,
                  headless_browser=False,
                  multi_logs=True)
try:  
    session.login()   
    #SET VALUES TO START AND STOP 1 AND 2
    start1=datetime.time(hour=8, minute=00).strftime("%H:%M")
    stop1=datetime.time(hour=10, minute=00).strftime("%H:%M")
    start2=datetime.time(hour=19, minute=00).strftime("%H:%M")
    stop2=datetime.time(hour=20, minute=55).strftime("%H:%M")

    #CHECK FOR CURRENT TIME
    current_time=datetime.datetime.now().strftime("%H:%M")

    # SETTINGS
    session.set_relationship_bounds(enabled=True,
                 potency_ratio=0.67,
                  delimit_by_numbers=True,
                   max_followers=4590,
                    max_following=5555,
                     min_followers=45,
                      min_following=77

    # ACTIONS
    while True:          #I USED THE WHILE TRUE TO MAKE THE INFINITE LOOP
        if current_time<=stop1 and current_time>=start1: 
            session.like_by_tags(['natgeo'], amount=1)
        elif current_time<=stop2 and current_time>=start2:
            session.like_by_tags(['instagram'], amount=1)
        else:
            time.sleep(20)

except Exception as exc:
THE REST IS SAME

WHAT I MANAGED TO DO AS AN AMATEUR:

  1. The code works and is able to differentiate the time and selects action based on the time. For example when I run the code I think the code knows what time it is and selects the appropriate if elif and else statement.
  2. The code keeps running because the time is in the specified window.

WHAT IS THE ISSUE:

  1. Once a code runs it never changes the statement it just keeps repeating itself even when the current time changes. For example start 1 is 10:00 and stop 1 is 10:05. The function keeps repeating itself even after 10:06 and it just does the same thing. I have the feeling that current time in the loop is not updated.

WHAT DO I NEED HELP WITH:

  1. I need help with making the code run in a loop, check the current time, go to the needed function, perform that, go back to the begging, check the time, and if the time has changed to another condition go and perform that condition.
  2. For else statement i need the code to do nothing for X seconds and go back to the begging, check the time, and if the time is in the start and stop, perform the function.

Please feel free to experiment with the files attached.
quickstart.txt
timepy.txt

Amazing @imbarismustafa
I'm also a C++ programmer and have not used python outside InstaPy (_literally_) :P

You already got a great programming knowledge to do the job, but what I would suggest is checking time interval inside the [_inifinite_] loop so that it behaves as expected.

while True:
    current_time=datetime.datetime.now().strftime("%H:%M")

    if current_time<=stop1 and current_time>=start1: 
        #work work work
    elif current_time<=stop2 and current_time>=start2:
        #work work work
    else:
        ##ZZ sleep

_Else than that, I couldn't find a problem, but if there is sth else, just ask right away xD_

Thank you for the quick answer @uluQulu ! Really appreciate your time!
I used the settings you mentioned:

try:
    session.login()
    # settings
    session.set_relationship_bounds(enabled=True,
                 potency_ratio=0.62,
                  delimit_by_numbers=True,
                   max_followers=4590,
                    max_following=5555,
                     min_followers=45,
                      min_following=77)
    #set start and end time
    start1=datetime.time(hour=00, minute=15).strftime("%H:%M")
    stop1=datetime.time(hour=00, minute=20).strftime("%H:%M")
    start2=datetime.time(hour=00, minute=25).strftime("%H:%M")
    stop2=datetime.time(hour=00, minute=30).strftime("%H:%M")
    # actions
    while True:
        #checks for current time
        current_time=datetime.datetime.now().strftime("%H:%M")
        if current_time>=start1 and current_time<=stop1:
            session.like_by_tags(['minimal'], amount=5)
        elif current_time>=stop2 and current_time<=start2:
            session.like_by_tags(['minimalism'], amount=5)
        else:
            time.sleep(20)

I tested in in whis way:

python3 quickstart.py
INFO [2018-05-31 00:14:08] [XXXX]  Session started - 2018-05-31 00:14:08
INFO [2018-05-31 00:14:38] [XXXX]  Logged in successfully!
INFO [2018-05-31 00:15:19] [XXXX]  Tag [1/1]
INFO [2018-05-31 00:15:19] [XXXX]  --> b'minimal'
#AFTER SOME TIME
INFO [2018-05-31 00:21:10] [XXXX]  Not valid users: 0
#AFTER THIS POINT THERE NO ACTIVITY AT ALL

For some reason after the list line at 00:21:10(Hour:MInutes:Seconds) nothing happens. I waited until 00:30 my computer time and canceled it manually because there was no activity after 00:21:10.
PLEASE PAY ATTENTION TO THE TIMES I HAVE SET.
I am trying to find out why the loop doesn't happen but for now I don't know. I want to know what is your opinion on that.
Thank you!

My quickstart.py file:
quickstart.txt

Glad to help @imbarismustafa
It's a logical issue

elif current_time>=stop2 and current_time<=start2:
````
cos of the `start2` and `stop2` here
```python
start2=datetime.time(hour=00, minute=25).strftime("%H:%M")
stop2=datetime.time(hour=00, minute=30).strftime("%H:%M"

from these expessions, and also considering the first conditional statement which ends at 00:20, it will not do any thing before 00:30

_maybe you wanted to write_
```python
elif current_time>=start2 and current_time<=stop2:
````

But if I were you I would re-write this idea using timestamps with reset variables, which could bring realtime randomization to time intervals and free you from defining every specific time interval by hand.

Worked! @uluQulu Thank you ! Really!
And by the way yes I was thinking of doing randomization after this as an improvement. I will research and experiment and will see what i can do. Again, Thank you very much.

@uluQulu Hey. For randomization i did the following:

start1=datetime.time(hour=3, minute=random.randint(34, 35)).strftime("%H:%M")
stop1=datetime.time(hour=3, minute=random.randint(37, 38)).strftime("%H:%M")

If you have any suggestion on how to improve the code I will be more than happy. Never tried but I will maybe try also to randomize the function inside the if and elif so that in each cycle theres a different function. Maybe I can achieve that again with random.randint but as I mention never tried and will keep you updated once I try.
Thank you, and looking forward to hear your professional opinion.

Welcome @imbarismustafa
If I could get the scope- edges of the idea, I can help you more.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this problem still occurs, please open a new issue

Was this page helpful?
0 / 5 - 0 ratings