[GH-ISSUE #1] Open and Close Instagram #461

Closed
opened 2026-03-13 21:29:20 +03:00 by kerem · 11 comments
Owner

Originally created by @GabrieleAnsaldo on GitHub (Jun 1, 2020).
Original GitHub issue: https://github.com/alexal1/Insomniac/issues/1

I guess you could open and close Instagram with the following code:

import os

# Open Instagram
os.system("adb shell monkey -p com.instagram.android 1 -")

# Close Instagram
os.system("adb shell am force-stop com.instagram.android")

Also, you could add a function that goes through the Recent page of a certain hashtag and starts liking a certain number of pictures.

You could make various functions of possible actions that you can do:

  • liking by hashtags (you could also comment)
  • liking by someone's followers (as you have already) (you could also comment)

I was thinking about how the following part could be implemented. Basically Instagram lets you go to your following page and see your followers ordered from the person you followed the latest. So basically if you follow 5 ppl you could just unfollow the latest 5 people. That's an idea. Otherwise, a slower process would be the one of going to each user's page and click the unfollow button.

Let me know if I could help somehow.

Originally created by @GabrieleAnsaldo on GitHub (Jun 1, 2020). Original GitHub issue: https://github.com/alexal1/Insomniac/issues/1 I guess you could open and close Instagram with the following code: ``` import os # Open Instagram os.system("adb shell monkey -p com.instagram.android 1 -") # Close Instagram os.system("adb shell am force-stop com.instagram.android") ``` Also, you could add a function that goes through the Recent page of a certain hashtag and starts liking a certain number of pictures. You could make various functions of possible actions that you can do: - liking by hashtags (you could also comment) - liking by someone's followers (as you have already) (you could also comment) I was thinking about how the following part could be implemented. Basically Instagram lets you go to your following page and see your followers ordered from the person you followed the latest. So basically if you follow 5 ppl you could just unfollow the latest 5 people. That's an idea. Otherwise, a slower process would be the one of going to each user's page and click the unfollow button. Let me know if I could help somehow.
kerem closed this issue 2026-03-13 21:29:25 +03:00
Author
Owner

@GabrieleAnsaldo commented on GitHub (Jun 1, 2020):

Other things that could be added:

  • Telegram connection so that you get notifications of how the bot is behaving when it is running ( in the case you are using an old android phone as a permanent Instagram bot)
  • The package schedule could be used to schedule the various actions at specific times during the day
<!-- gh-comment-id:637145217 --> @GabrieleAnsaldo commented on GitHub (Jun 1, 2020): Other things that could be added: - Telegram connection so that you get notifications of how the bot is behaving when it is running ( in the case you are using an old android phone as a permanent Instagram bot) - The package schedule could be used to schedule the various actions at specific times during the day
Author
Owner

@alexal1 commented on GitHub (Jun 2, 2020):

@GabrieleAnsaldo thanks for ideas, they are really great!
Definitely all of them should be implemented. I just don't have much time, so will have to prioritize them. Following is the most important now, isn't it?

So basically if you follow 5 ppl you could just unfollow the latest 5 people. That's an idea. Otherwise, a slower process would be the one of going to each user's page and click the unfollow button.

It's better to divide two processes: following and unfollowing. It gives more flexibility. If you want to do one immediately after another, you can run shell script like

# Unfollow all users previously followed by the script
pyhton insomniac.py --unfollow
# Follow 50% of interacted users
python --bloggers username1 --follow-percentage 50

Why going to each user's page? We can unfollow by buttons on the followings page.

P.S. I will VERY appreciate if you make pull requests. I see that you have great ideas and this script was originally your idea, I just applied UI Automator tool.

<!-- gh-comment-id:637403899 --> @alexal1 commented on GitHub (Jun 2, 2020): @GabrieleAnsaldo thanks for ideas, they are really great! Definitely all of them should be implemented. I just don't have much time, so will have to prioritize them. Following is the most important now, isn't it? > So basically if you follow 5 ppl you could just unfollow the latest 5 people. That's an idea. Otherwise, a slower process would be the one of going to each user's page and click the unfollow button. It's better to divide two processes: following and unfollowing. It gives more flexibility. If you want to do one immediately after another, you can run shell script like ``` # Unfollow all users previously followed by the script pyhton insomniac.py --unfollow # Follow 50% of interacted users python --bloggers username1 --follow-percentage 50 ``` Why going to each user's page? We can unfollow by buttons on the followings page. P.S. I will VERY appreciate if you make pull requests. I see that you have great ideas and this script was originally your idea, I just applied UI Automator tool.
Author
Owner

@gvmturl commented on GitHub (Jun 2, 2020):

Thank you guys! Any idea to unfollow non followers?

<!-- gh-comment-id:637445269 --> @gvmturl commented on GitHub (Jun 2, 2020): Thank you guys! Any idea to unfollow non followers?
Author
Owner

@alexal1 commented on GitHub (Jun 2, 2020):

@gvmturl a bit more tricky, but algorithm can be the following:
iterate over followings -> open each following -> go to his/her followings -> if you are followed back, you are on the first row -> return back and unfollow if not followed back -> return back to followings

So we can create such flags:

# Unfollow all users followed by the script
python insomniac.py --unfollow

# Unfollow all users followed by the script, 100 at most during the session
python insomniac.py --unfollow 100

# Unfollow all non-followers followed by the script
python insomniac.py --unfollow-non-followers

# Unfollow all non-followers followed by the script, 100 at most during the session
python insomniac.py --unfollow-non-followers 100
<!-- gh-comment-id:637449077 --> @alexal1 commented on GitHub (Jun 2, 2020): @gvmturl a bit more tricky, but algorithm can be the following: iterate over followings -> open each following -> go to his/her followings -> if you are followed back, you are on the first row -> return back and unfollow if not followed back -> return back to followings So we can create such flags: ``` # Unfollow all users followed by the script python insomniac.py --unfollow # Unfollow all users followed by the script, 100 at most during the session python insomniac.py --unfollow 100 # Unfollow all non-followers followed by the script python insomniac.py --unfollow-non-followers # Unfollow all non-followers followed by the script, 100 at most during the session python insomniac.py --unfollow-non-followers 100 ```
Author
Owner

@GabrieleAnsaldo commented on GitHub (Jun 2, 2020):

Okay, thanks for the answer @alexal1! Tonight I will try to work on it a bit and send a pull request so that you can review it! I will try and make as many changes as possible.

Also, I am kinda new to UI Automator, I had a hard time looking for documentation. Do you have any suggestions on where I can find some examples and or documentation on how to use it properly?

<!-- gh-comment-id:637449427 --> @GabrieleAnsaldo commented on GitHub (Jun 2, 2020): Okay, thanks for the answer @alexal1! Tonight I will try to work on it a bit and send a pull request so that you can review it! I will try and make as many changes as possible. Also, I am kinda new to UI Automator, I had a hard time looking for documentation. Do you have any suggestions on where I can find some examples and or documentation on how to use it properly?
Author
Owner

@alexal1 commented on GitHub (Jun 2, 2020):

@GabrieleAnsaldo of course, all you need is

  1. uiautomatortool – it's a tool to get layout hierarchy from the device. I find resourceId and className of UI elements there.

The uiautomatorviewer tool provides a convenient GUI to scan and analyze the UI components currently displayed on an Android device. You can use this tool to inspect the layout hierarchy and view the properties of UI components that are visible on the foreground of the device. This information lets you create more fine-grained tests using UI Automator, for example by creating a UI selector that matches a specific visible property.

The uiautomatorviewer tool is located in the /tools/bin directory.

https://developer.android.com/training/testing/ui-automator

  1. uiautomator Python library https://github.com/xiaocong/uiautomator. It has pretty well documentation on the main page, I mostly use it like
some_view = device(resourceId='...', className='...')
some_view.click.wait()
<!-- gh-comment-id:637453834 --> @alexal1 commented on GitHub (Jun 2, 2020): @GabrieleAnsaldo of course, all you need is 1. **uiautomatortool** – it's a tool to get layout hierarchy from the device. I find `resourceId` and `className` of UI elements there. > The uiautomatorviewer tool provides a convenient GUI to scan and analyze the UI components currently displayed on an Android device. You can use this tool to inspect the layout hierarchy and view the properties of UI components that are visible on the foreground of the device. This information lets you create more fine-grained tests using UI Automator, for example by creating a UI selector that matches a specific visible property. > > The uiautomatorviewer tool is located in the <android-sdk>/tools/bin directory. https://developer.android.com/training/testing/ui-automator 2. **uiautomator** Python library https://github.com/xiaocong/uiautomator. It has pretty well documentation on the main page, I mostly use it like ``` some_view = device(resourceId='...', className='...') some_view.click.wait() ```
Author
Owner

@gvmturl commented on GitHub (Jun 3, 2020):

@gvmturl a bit more tricky, but algorithm can be the following:
iterate over followings -> open each following -> go to his/her followings -> if you are followed back, you are on the first row -> return back and unfollow if not followed back -> return back to followings

So we can create such flags:

# Unfollow all users followed by the script
python insomniac.py --unfollow

# Unfollow all users followed by the script, 100 at most during the session
python insomniac.py --unfollow 100

# Unfollow all non-followers followed by the script
python insomniac.py --unfollow-non-followers

# Unfollow all non-followers followed by the script, 100 at most during the session
python insomniac.py --unfollow-non-followers 100

@alexal1 Thank you for replying, i will be waiting for this update...any solution for skipping private users?

Other things that could be added:

  • Telegram connection so that you get notifications of how the bot is behaving when it is running ( in the case you are using an old android phone as a permanent Instagram bot)
  • The package schedule could be used to schedule the various actions at specific times during the day

@GabrieleAnsaldo please let us know when telegram integration is complete.

Could you add the command list to telegramBot as variables define as such flags defined on that script or something to autoadd IG functions to telegram? Also could be defined a variable as comment on IG Function that completes ¨HELP¨ function on telegram.

Is there anyway to storage all errors into a log? Like IndexError? I got this error here but script still running...

Is everything working with IGTV Videos? I Just seem it entered on a long video here... i will keep checking it... isn't better open a Bug reports as a issue or is there any better way to report that bugs here?

Thank you

<!-- gh-comment-id:637956140 --> @gvmturl commented on GitHub (Jun 3, 2020): > @gvmturl a bit more tricky, but algorithm can be the following: > iterate over followings -> open each following -> go to his/her followings -> if you are followed back, you are on the first row -> return back and unfollow if not followed back -> return back to followings > > So we can create such flags: > > ``` > # Unfollow all users followed by the script > python insomniac.py --unfollow > > # Unfollow all users followed by the script, 100 at most during the session > python insomniac.py --unfollow 100 > > # Unfollow all non-followers followed by the script > python insomniac.py --unfollow-non-followers > > # Unfollow all non-followers followed by the script, 100 at most during the session > python insomniac.py --unfollow-non-followers 100 > ``` @alexal1 Thank you for replying, i will be waiting for this update...any solution for skipping private users? > Other things that could be added: > > * Telegram connection so that you get notifications of how the bot is behaving when it is running ( in the case you are using an old android phone as a permanent Instagram bot) > * The package schedule could be used to schedule the various actions at specific times during the day @GabrieleAnsaldo please let us know when telegram integration is complete. Could you add the command list to telegramBot as variables define as such flags defined on that script or something to autoadd IG functions to telegram? Also could be defined a variable as comment on IG Function that completes ¨HELP¨ function on telegram. Is there anyway to storage all errors into a log? Like IndexError? I got this error here but script still running... Is everything working with IGTV Videos? I Just seem it entered on a long video here... i will keep checking it... isn't better open a Bug reports as a issue or is there any better way to report that bugs here? Thank you
Author
Owner

@alexal1 commented on GitHub (Jun 3, 2020):

@gvmturl actually skipping private users works already (in not a very optimal way though). Algorithm tries to open a photo to like it.
If it can't – it scrolls (on some devices screen's height is too small and photos are not visible).
If it can't again – it skips user.

I don't know any way to detect private users without opening their profiles.

<!-- gh-comment-id:638190630 --> @alexal1 commented on GitHub (Jun 3, 2020): @gvmturl actually skipping private users works already (in not a very optimal way though). Algorithm tries to open a photo to like it. If it can't – it scrolls (on some devices screen's height is too small and photos are not visible). If it can't again – it skips user. I don't know any way to detect private users without opening their profiles.
Author
Owner

@alexal1 commented on GitHub (Jun 3, 2020):

Is there anyway to storage all errors into a log? Like IndexError? I got this error here but script still running...

@gvmturl this error occurs when opened screen is not the one the script expects. This happens sometimes, I fix these bugs as I can. Don't forget to git pull sometimes. Script continues working because the other way such sudden stops would be frustrating. It starts current "blogger" from the beginning, so it's kind of "autofix" :)
If you have some time to implement logging errors – it would be fantastic! Feel free to make PRs. I personally gonna do some more prioritized features first.

isn't better open a Bug reports as a issue or is there any better way to report that bugs here?

Yeah, creating separate bug issues would be great!

<!-- gh-comment-id:638202286 --> @alexal1 commented on GitHub (Jun 3, 2020): > Is there anyway to storage all errors into a log? Like IndexError? I got this error here but script still running... @gvmturl this error occurs when opened screen is not the one the script expects. This happens sometimes, I fix these bugs as I can. Don't forget to `git pull` sometimes. Script continues working because the other way such sudden stops would be frustrating. It starts current "blogger" from the beginning, so it's kind of "autofix" :) If you have some time to implement logging errors – it would be fantastic! Feel free to make PRs. I personally gonna do some more prioritized features first. > isn't better open a Bug reports as a issue or is there any better way to report that bugs here? Yeah, creating separate bug issues would be great!
Author
Owner

@alexal1 commented on GitHub (Jun 4, 2020):

Good news folks! @GabrieleAnsaldo @gvmturl

I've implemented Open and Close Instagram in 6eaa739971, so I'm closing this issue now.
I also added --repeat 180 argument which makes the script to restart the session with exactly same settings in 180 minutes after completion. It continues indefinitely, until you stop the script by control+C. Instagram app is automatically being closed between sessions. This feature is disabled by default, until you add --repeat N argument.

So don't forget to git pull and enjoy. Thanks for this discussion, if you have more ideas to add, please create separate Issues. It will help me to work on the script more efficient.

<!-- gh-comment-id:639142049 --> @alexal1 commented on GitHub (Jun 4, 2020): Good news folks! @GabrieleAnsaldo @gvmturl I've implemented **Open and Close Instagram** in 6eaa73997171db46827ddf04f84796e638004512, so I'm closing this issue now. I also added `--repeat 180` argument which makes the script to restart the session with exactly same settings in 180 minutes after completion. It continues indefinitely, until you stop the script by control+C. Instagram app is automatically being closed between sessions. This feature is disabled by default, until you add `--repeat N` argument. So don't forget to `git pull` and enjoy. Thanks for this discussion, if you have more ideas to add, please create separate Issues. It will help me to work on the script more efficient.
Author
Owner

@gvmturl commented on GitHub (Jun 8, 2020):

Hello @alexal1 i m still trying how everything works on this project to start helping on it...

could you explain me please where did you dump to get to get com.instagram.android/com.instagram.mainactivity.MainActivity

i see that originaly suggested @GabrieleAnsaldo using
os.system("adb shell monkey -p com.instagram.android 1 -")

but you you used
os.popen("adb shell am start -n com.instagram.android/com.instagram.mainactivity.MainActivity").close()

Could you explain me why you made it and what does it means...

I will be attentive to your comments.

Thank you.

<!-- gh-comment-id:640757383 --> @gvmturl commented on GitHub (Jun 8, 2020): Hello @alexal1 i m still trying how everything works on this project to start helping on it... could you explain me please where did you dump to get to get `com.instagram.android/com.instagram.mainactivity.MainActivity` i see that originaly suggested @GabrieleAnsaldo using `os.system("adb shell monkey -p com.instagram.android 1 -")` but you you used `os.popen("adb shell am start -n com.instagram.android/com.instagram.mainactivity.MainActivity").close()` Could you explain me why you made it and what does it means... I will be attentive to your comments. Thank you.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Insomniac#461
No description provided.