|
| 1 | +################################################### Instagram Automation #################################################### |
| 2 | +################################################ Developed By Avdhesh Varshney ############################################## |
| 3 | +############################################ (https://github.com/Avdhesh-Varshney) ########################################## |
| 4 | + |
| 5 | +'''Install Python Packages as listed below using these commands in the terminal |
| 6 | +
|
| 7 | +Sr. No. Package Name Commands |
| 8 | +1. os pip install os |
| 9 | +2. random pip install random |
| 10 | +3. instaloader pip install instaloader |
| 11 | +4. instabot pip install instabot |
| 12 | +
|
| 13 | +After install all these packages then import all of them in this file.''' |
| 14 | + |
| 15 | +# Importing libraries |
| 16 | +import os |
| 17 | +import random |
| 18 | +import instaloader |
| 19 | +from instabot import Bot |
| 20 | +from time import sleep |
| 21 | + |
| 22 | +os.system("cls" if os.name == "nt" else "clear") |
| 23 | + |
| 24 | +# Global variables for username and password |
| 25 | +print("\nWelcome to Instagram Automation 🤖\n") |
| 26 | +username = input("Enter your Instagram username: ") |
| 27 | +password = input("\nEnter your Instagram password: ") |
| 28 | + |
| 29 | +# Function to tell about your profile id number |
| 30 | +def profileIDNumber(): |
| 31 | + loader = instaloader.Instaloader() |
| 32 | + id = instaloader.Profile.from_username(loader.context, username) |
| 33 | + |
| 34 | + print(f"Your profile ID is - {id}") |
| 35 | + |
| 36 | +# Function to download the profile picture of a user using instaloader |
| 37 | +def downloadProfilePicture(): |
| 38 | + loader = instaloader.Instaloader() |
| 39 | + # Download the profile picture |
| 40 | + loader.download_profile(username, profile_pic_only=True) |
| 41 | + |
| 42 | + print(f"Profile picture of {username} downloaded.") |
| 43 | + |
| 44 | +# Function to like posts from a user's profile using instabot |
| 45 | +def likeUserPosts(): |
| 46 | + bot = Bot() |
| 47 | + bot.login(username=username, password=password) |
| 48 | + |
| 49 | + # Get the user's recent posts |
| 50 | + userPosts = bot.get_user_medias(username=username, filtration=None) |
| 51 | + |
| 52 | + # Randomly like a few posts |
| 53 | + numPostsToLike = min(5, len(userPosts)) |
| 54 | + postsToLike = random.sample(userPosts, numPostsToLike) |
| 55 | + for post in postsToLike: |
| 56 | + bot.like(media_id=post.pk) |
| 57 | + print(f"Liked post with caption: {post.caption.text}") |
| 58 | + |
| 59 | + bot.logout() |
| 60 | + |
| 61 | +# Function to follow a user using instabot |
| 62 | +def followUser(): |
| 63 | + bot = Bot() |
| 64 | + bot.login(username=username, password=password) |
| 65 | + |
| 66 | + # Follow the user |
| 67 | + userToFollow = input("\nEnter the username to follow: ") |
| 68 | + sleep(60) |
| 69 | + bot.follow(userToFollow) |
| 70 | + print(f"Followed user: {userToFollow}") |
| 71 | + |
| 72 | + bot.logout() |
| 73 | + |
| 74 | +# Function to unfollow a user using instabot |
| 75 | +def unfollowUser(): |
| 76 | + bot = Bot() |
| 77 | + bot.login(username=username, password=password) |
| 78 | + |
| 79 | + # Unfollow the user |
| 80 | + userToUnfollow = input("\nEnter the username to unfollow: ") |
| 81 | + bot.unfollow(userToUnfollow) |
| 82 | + print(f"Unfollowed user: {userToUnfollow}") |
| 83 | + |
| 84 | + bot.logout() |
| 85 | + |
| 86 | +# Function to send a direct message using instabot |
| 87 | +def sendDirectMessage(): |
| 88 | + bot = Bot() |
| 89 | + bot.login(username=username, password=password) |
| 90 | + |
| 91 | + # Send the direct message |
| 92 | + userToMessage = input("\nEnter the username to send a direct message: ") |
| 93 | + message = input("Enter the message: ") |
| 94 | + bot.send_message(message, [bot.get_user_id_from_username(userToMessage)]) |
| 95 | + |
| 96 | + bot.logout() |
| 97 | + |
| 98 | +# Function to download a user's recent posts and stories using instaloader |
| 99 | +def downloadUserData(): |
| 100 | + loader = instaloader.Instaloader() |
| 101 | + profile = instaloader.Profile.from_username(loader.context, username) |
| 102 | + |
| 103 | + # Download recent posts |
| 104 | + loader.download_posts(profile, max_count=10) |
| 105 | + |
| 106 | + # Download stories |
| 107 | + stories = profile.get_stories() |
| 108 | + for story in stories: |
| 109 | + loader.download_storyitem(story, filename_prefix=f"{username}_story_") |
| 110 | + |
| 111 | + print(f"Downloaded recent posts and stories of {username}.") |
| 112 | + |
| 113 | +# Function to download multiple profiles using instaloader |
| 114 | +def downloadMultipleProfiles(): |
| 115 | + loader = instaloader.Instaloader() |
| 116 | + usernames = input("Enter multiple usernames separated by spaces: ").split() |
| 117 | + for user in usernames: |
| 118 | + profile = instaloader.Profile.from_username(loader.context, user) |
| 119 | + loader.download_profile(profile, download_stories=True) |
| 120 | + print("Downloaded profiles and their stories.") |
| 121 | + |
| 122 | +# Function to get hashtag information using instabot |
| 123 | +def getHashtagInfo(): |
| 124 | + bot = Bot() |
| 125 | + bot.login(username=username, password=password) |
| 126 | + |
| 127 | + # Get the top posts for the hashtag |
| 128 | + hashtag = input("\nEnter the hashtag to get information: ") |
| 129 | + topPosts = bot.get_hashtag_medias(hashtag) |
| 130 | + |
| 131 | + print(f"Top posts for the hashtag: {hashtag}") |
| 132 | + for post in topPosts: |
| 133 | + print(f"Post URL: {post.link}") |
| 134 | + |
| 135 | + bot.logout() |
| 136 | + |
| 137 | +# Main function |
| 138 | +def main(): |
| 139 | + |
| 140 | + while True: |
| 141 | + # Get user's choice |
| 142 | + os.system('cls') |
| 143 | + print('''\n*********************** Welcome to Instagram Automation 🤖 ***********************\n |
| 144 | + \tEnter 1️⃣ to download a user's profile picture |
| 145 | + \tEnter 2️⃣ to like posts from a user's profile |
| 146 | + \tEnter 3️⃣ to follow a user |
| 147 | + \tEnter 4️⃣ to unfollow a user |
| 148 | + \tEnter 5️⃣ to send a direct message to a user |
| 149 | + \tEnter 6️⃣ to download a user's recent posts and stories |
| 150 | + \tEnter 7️⃣ to download multiple profiles and their stories |
| 151 | + \tEnter 8️⃣ to get hashtag information |
| 152 | + \tEnter 9️⃣ to know you ID number |
| 153 | + \tEnter 0️⃣ to exit the program\n''') |
| 154 | + |
| 155 | + try: |
| 156 | + value = int(input("Enter your choice: ")) |
| 157 | + except ValueError: |
| 158 | + print("\nInvalid input! Please enter a number from the options.") |
| 159 | + continue |
| 160 | + |
| 161 | + os.system('cls') |
| 162 | + print("\n*********************** Welcome to Instagram Automation 🤖 ***********************\n") |
| 163 | + |
| 164 | + if value == 0: |
| 165 | + os.system('cls') |
| 166 | + print("\n******************* Thanks for using Instagram Automation 🤖 Program 👋 *******************\n\n") |
| 167 | + exit(0) |
| 168 | + |
| 169 | + elif value == 1: |
| 170 | + print("\nDownloading a user's profile picture\n") |
| 171 | + downloadProfilePicture() |
| 172 | + |
| 173 | + elif value == 2: |
| 174 | + print("\nLiking posts from a user's profile\n") |
| 175 | + likeUserPosts() |
| 176 | + |
| 177 | + elif value == 3: |
| 178 | + print("\nFollowing a user\n") |
| 179 | + followUser() |
| 180 | + |
| 181 | + elif value == 4: |
| 182 | + print("\nUnfollowing a user\n") |
| 183 | + unfollowUser() |
| 184 | + |
| 185 | + elif value == 5: |
| 186 | + print("\nSending a direct message to a user\n") |
| 187 | + sendDirectMessage() |
| 188 | + |
| 189 | + elif value == 6: |
| 190 | + print("\nDownloading a user's recent posts and stories\n") |
| 191 | + downloadUserData() |
| 192 | + |
| 193 | + elif value == 7: |
| 194 | + print("\nDownloading multiple profiles and their stories\n") |
| 195 | + downloadMultipleProfiles() |
| 196 | + |
| 197 | + elif value == 8: |
| 198 | + print("\nGetting hashtag information\n") |
| 199 | + getHashtagInfo() |
| 200 | + |
| 201 | + elif value == 9: |
| 202 | + print("\nFetching Data\n") |
| 203 | + profileIDNumber() |
| 204 | + |
| 205 | + else: |
| 206 | + print("\nInvalid choice. Please enter a valid number from the options.\n") |
| 207 | + |
| 208 | + input("\n\nPress 'Enter Key' to continue !\n\n") |
| 209 | + |
| 210 | +# Driver function |
| 211 | +if __name__ == "__main__": |
| 212 | + main() |
0 commit comments