|
| 1 | +import os, shutil, sys, instaloader |
| 2 | +from prettytable import PrettyTable |
| 3 | +instagramBot = instaloader.Instaloader(quiet = True) |
| 4 | + |
| 5 | +def auth(): |
| 6 | + # Inputting the Username of User |
| 7 | + userName = input("Enter Your Instagram Username: ") |
| 8 | + try: |
| 9 | + # Try getting auth credentials from pre-saved files |
| 10 | + instagramBot.load_session_from_file(userName) |
| 11 | + except: |
| 12 | + try: |
| 13 | + # Make the auth credentials |
| 14 | + os.system("instaloader -l {0}".format(userName)) |
| 15 | + instagramBot.load_session_from_file(userName) |
| 16 | + except: |
| 17 | + # Login in the account |
| 18 | + instagramBot.login(userName,input("Enter Password: ")) |
| 19 | + instagramBot.save_session_to_file() |
| 20 | + print("{0} Logged In.".format(userName)) |
| 21 | + |
| 22 | +def main(): |
| 23 | + # Login |
| 24 | + auth() |
| 25 | + # While Loop so Script does not exit |
| 26 | + while True: |
| 27 | + # Printing the Tasks |
| 28 | + print("Indexes and Tasks") |
| 29 | + myTable = PrettyTable(["Index","Task"]) |
| 30 | + myTable.add_row(["1", "Download Profile Image from Username"]) |
| 31 | + myTable.add_row(["2", "Download All Posts from Username"]) |
| 32 | + myTable.add_row(["3", "Download Images from Hashtags"]) |
| 33 | + print(myTable) |
| 34 | + # Inputting what to do |
| 35 | + query = input("Enter Index to Perform: ") |
| 36 | + # Download Profile Picture case |
| 37 | + if query == "1": |
| 38 | + currentPath = os.getcwd() |
| 39 | + username = input("Enter Username to Download Profile Image: ") |
| 40 | + instagramBot.download_profile(username, profile_pic_only = True) |
| 41 | + for file in os.listdir(username): |
| 42 | + if ".jpg" in file: |
| 43 | + shutil.move("{0}/{1}".format(username,file),currentPath) |
| 44 | + shutil.rmtree(username) |
| 45 | + print("'{0}' Profile Image Downloaded.".format(username)) |
| 46 | + # Download all Photos and Videos |
| 47 | + elif query == "2": |
| 48 | + currentPath = os.getcwd() |
| 49 | + username = input("Enter Username to Download: ") |
| 50 | + try : |
| 51 | + profile = instaloader.Profile.from_username(instagramBot.context, username) |
| 52 | + except: |
| 53 | + print("Username Not Found.") |
| 54 | + if os.path.exists(username) is False: |
| 55 | + os.mkdir(username) |
| 56 | + os.chdir(username) |
| 57 | + if os.path.exists("Videos") is False: |
| 58 | + os.mkdir("Videos") |
| 59 | + posts = profile.get_posts() |
| 60 | + print("Downloading... Ctrl + C to Stop in between. Don't Open Username Folder.") |
| 61 | + for index, post in enumerate(posts): |
| 62 | + try: |
| 63 | + instagramBot.download_post(post, target = index) |
| 64 | + except KeyboardInterrupt: |
| 65 | + print("Downloader Exited.") |
| 66 | + break |
| 67 | + # Organizing the Directory |
| 68 | + for folder in os.listdir(): |
| 69 | + if "." not in folder: |
| 70 | + for item in os.listdir(folder): |
| 71 | + if ".jpg" in item: |
| 72 | + shutil.move(folder+"/"+item, "{0}/{1}".format(currentPath, username)) |
| 73 | + elif ".mp4" in item: |
| 74 | + try: |
| 75 | + shutil.move(folder+"/"+item, "{0}/{1}/Videos".format(currentPath, username)) |
| 76 | + except: |
| 77 | + continue |
| 78 | + shutil.rmtree(folder) |
| 79 | + print("{} Folder Created.".format(username)) |
| 80 | + # Download Images using hashtag |
| 81 | + elif query == "3": |
| 82 | + try: |
| 83 | + instaloader.Instaloader(download_videos=False, save_metadata=False, post_metadata_txt_pattern='').download_hashtag(input("Enter Hashtag: "), max_count=20) |
| 84 | + except KeyboardInterrupt: |
| 85 | + print("Downloader Exited") |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments