diff --git a/projects/GUI-Instagram-Downloader/GUI-Instagram-post-downloader.mdx b/projects/GUI-Instagram-Downloader/GUI-Instagram-post-downloader.mdx new file mode 100644 index 00000000..c8adaae3 --- /dev/null +++ b/projects/GUI-Instagram-Downloader/GUI-Instagram-post-downloader.mdx @@ -0,0 +1,221 @@ +--- +Title: GUI-Instagram-Downloader +author: Muhammad Abubakar +datePublished: 2022-29-10 +description: This allows you to download any Instagram post/profile picture. +header: To be filled +tags: + - Intermediate +--- +# GUI-Insta-Downloader + +**_Graphical User Interface Instagram Downloader_** + +## Introduction +

Have you ever wanted to download a post from Instagram? and realise that Instagram does not provide a download option. + Well in this tutorial we will create our own Instagram Post downloader.

+ +## Instaloader +In this project we will be using Instaloader, the Instaloader module is a Python package having great functionalities to scrap instagram, it's functions can be used as command-line utility.It is a tool to download pictures or videos along with their captions and other metadata from Instagram. +**How to Install:** +With Python already installed, do: +```py +$ pip3 install instaloader +$ instaloader profile [profile ...] +``` +See Install Instaloader for more options on how to install Instaloader. + +## Tkinter +Python has a lot of GUI frameworks, but Tkinter is the only framework that’s built into the Python standard library. Tkinter has several strengths. It’s cross-platform, so the same code works on Windows, macOS, and Linux. Visual elements are rendered using native operating system elements, so applications built with Tkinter look like they belong on the platform where they’re run. + +## Requirements +- Tkinter Library +- Instaloader Module ( Version 4.9.2 ) +- Pillow Library ( Version 9.0.1 ) +- Request Module ( Version 2.27.1 ) +- Os Module +- Threading Module + +## Features +- Download Profile Picture +- Download Posts Using the Link +- Ability to Specify where to Save Images and Videos +- Simple and beautiful User interface + +## How to Install +- clone this repository[to be filled] +- pip install pillow +- pip install instaloader +- pip install tk +- python3 main.py + +## Result + +

+ +

+ +## Explanation +

Import the required Libraries and Modules

+ +```py +import qrcode +from tkinter import ttk +from tkinter import * +from tkinter import messagebox +from tkinter import filedialog +from PIL import ImageTk, Image +from instaloader import Instaloader, Post +import threading +import os +``` + +

function for downloading User profile picture

+ + +```py +def download_profile(): + def download_image(): + try: + location = filedialog.askdirectory() + os.chdir(location) + #Profile Picture starts downloading here. + obj = Instaloader() + profile = profile_pic_input.get() + obj.download_profile(profile, profile_pic_only = True) + messagebox.showinfo('STATUS','Profile Image Downloaded Successfully') + except: + messagebox.showerror('ERROR','Username Is Incorrect or Does Not Exist') + # Thread is a separate flow of execution. This means that our program will have two things happening at once + threading.Thread(target = download_image).start() +``` + +

Function for downloading image by URL

+ + +```py +def download_post( ): + # Get url from user by GUI input (Entry) + link = post_input.get() + def media(): + if 'https://www.instagram.com/p/' in link : + location = filedialog.askdirectory() + os.chdir(location) + L = Instaloader() + try : + L.login(Username,Password) + except : + messagebox.showerror('ERROR','Username or Password is Wrong') + try : + short_link = link[28:39] + post = Post.from_shortcode(L.context , short_link) + L.download_post(post,target = short_link) + messagebox.showinfo('STATUS','Download Completed !') + except : + messagebox.showerror('ERROR','Link Not Found,please enter the link of the image') + else : + messagebox.showerror('ERROR','URL Is Incorrect') + # thread is a separate flow of execution. This means that our program will have two things happening at once + threading.Thread(target = media).start() +``` + + +

To create a tkinter GUI frame

+ + + +```py +root = Tk() +# Define the geometry +root.geometry('300x300') +root.resizable(False,False) +# Set the title of tkinter frame +root.title('Instagram Downloader') +# Set the background of tkinter frame +root.config(background = 'grey') +Icon = PhotoImage(file='img\\Icon.png') +root.iconphoto(False,Icon) +``` + + +This function takes the Username and Password and pass them to the new_window function + + +```py +def caller(): + Username = username_entry.get() + Password = password_entry.get() + if Username and Password : + new_window(Username,Password) + else : + messagebox.showerror('Error','You have to enter your Username and Password') +``` + + The new_window function + + +```py +def new_window(Username,Password): + # created a new tkinter gui window frame + new_window = Toplevel(root) + # Define the geometry + new_window.geometry('600x600') + new_window.resizable(False,False) + # Set the title of tkinter frame + new_window.title('Instagram Downloader') + # Set the background of tkinter frame + new_window.config(background = 'grey') + Icon = PhotoImage(file='img\\Icon.png') + new_window.iconphoto(False,Icon) + + # Load an image + load_img = Image.open('img\image.jpg') + # Resize the image using resize method + resize_img = load_img.resize((150,150),Image.Resampling.LANCZOS) + # Create an object of tkinter ImageTk and pass the resized image to it + img = ImageTk.PhotoImage(resize_img) + # Create a Label Widget to display the Image + label_img = ttk.Label(new_window,image = img) + label_img.place(x = 30,y = 20) + + # Create a Label Widget to display the text next to the img + label_img_text = Label(new_window , text = 'Instagram' , bg = 'grey' , fg = 'white' , font = ('Calibri' , 50 , 'bold')) + label_img_text.place(x = 230 , y = 45) + # Creat a Label Widget to display the hint text + hint_text=Label(new_window , text = '* Enter the Username of your desired account in below to download profile picture *' , bg = 'grey' , fg = 'yellow' , font = ('Calibri' , 10)) + hint_text.place(x = 60 , y = 220) + hint_text2=Label(new_window , text = '* Enter the Link of your desire Image from instagram in below to download it *' , bg = 'grey' , fg = 'yellow' , font = ('Calibri' , 10)) + hint_text2.place(x = 70 , y = 360) + + # Set the current value of the input with a StringVar object + Current_value = StringVar() + # Set input to receive username and download the profile picture + profile_pic_input = ttk.Entry(new_window , textvariable = Current_value , width = 35) + # the Entry widget has focus, it’s ready to accept the user input + profile_pic_input.focus() + profile_pic_input.place(x = 190 , y = 260) + + # Set the current value of the input with a StringVar object + Current_value2 = StringVar() + # Set input to recieve instagram image or video URL + post_input = ttk.Entry(new_window , textvariable = Current_value2 , width = 35) + # the Entry widget has focus, it’s ready to accept the user input + post_input.focus() + post_input.place(x = 190 , y = 400) + ... +``` + + + + + +## More Resources + +- [Solution on GitHub](To be filled) +- [Documentation: tkinter](https://docs.python.org/3/library/tkinter.html) +- [Documentation: Instaloader](https://instaloader.github.io) +- [Documentation: Pillow](https://pypi.org/project/Pillow/) + + + + diff --git a/projects/GUI-Instagram-Downloader/Result/Instagram Downloader 2.png b/projects/GUI-Instagram-Downloader/Result/Instagram Downloader 2.png new file mode 100644 index 00000000..d69d9882 Binary files /dev/null and b/projects/GUI-Instagram-Downloader/Result/Instagram Downloader 2.png differ diff --git a/projects/GUI-Instagram-Downloader/Result/Instagram Downloader.png b/projects/GUI-Instagram-Downloader/Result/Instagram Downloader.png new file mode 100644 index 00000000..aaf6ce3a Binary files /dev/null and b/projects/GUI-Instagram-Downloader/Result/Instagram Downloader.png differ diff --git a/projects/GUI-Instagram-Downloader/img/Icon.png b/projects/GUI-Instagram-Downloader/img/Icon.png new file mode 100644 index 00000000..ed9b72fa Binary files /dev/null and b/projects/GUI-Instagram-Downloader/img/Icon.png differ diff --git a/projects/GUI-Instagram-Downloader/img/image.jpg b/projects/GUI-Instagram-Downloader/img/image.jpg new file mode 100644 index 00000000..1082fa22 Binary files /dev/null and b/projects/GUI-Instagram-Downloader/img/image.jpg differ diff --git a/projects/GUI-Instagram-Downloader/main.py b/projects/GUI-Instagram-Downloader/main.py new file mode 100644 index 00000000..81f79d78 --- /dev/null +++ b/projects/GUI-Instagram-Downloader/main.py @@ -0,0 +1,167 @@ +# Programmer = Muhammad Abubakr + +# Import the required Libraries and Modules +from tkinter import ttk +from tkinter import * +from tkinter import messagebox +from tkinter import filedialog +from PIL import ImageTk, Image +from instaloader import Instaloader , Post +import threading +import os + +def new_window(Username , Password): + # created a new tkinter gui window frame + new_window = Toplevel(root) + # Define the geometry + new_window.geometry('600x600') + new_window.resizable(False , False) + # Set the title of tkinter frame + new_window.title('Instagram Downloader') + # Set the background of tkinter frame + new_window.config(background = 'grey') + Icon = PhotoImage(file='img\\Icon.png') + new_window.iconphoto(False, Icon) + + # Load an image + load_img = Image.open('img\image.jpg') + # Resize the image using resize method + resize_img = load_img.resize((150 , 150) , Image.Resampling.LANCZOS) + # Create an object of tkinter ImageTk and pass the resized image to it + img = ImageTk.PhotoImage(resize_img) + # Create a Label Widget to display the Image + label_img = ttk.Label(new_window , image = img) + label_img.place(x = 30 , y = 20) + + # Create a Label Widget to display the text next to the img + label_img_text = Label(new_window , text = 'Instagram' , bg = 'grey' , fg = 'white' , font = ('Calibri' , 50 , 'bold')) + label_img_text.place(x = 230 , y = 45) + # Creat a Label Widget to display the hint text + hint_text=Label(new_window , text = '* Enter the Username of your desired account in below to download profile picture *' , bg = 'grey' , fg = 'yellow' , font = ('Calibri' , 10)) + hint_text.place(x = 60 , y = 220) + hint_text2=Label(new_window , text = '* Enter the Link of your desire Image from instagram in below to download it *' , bg = 'grey' , fg = 'yellow' , font = ('Calibri' , 10)) + hint_text2.place(x = 70 , y = 360) + + # Set the current value of the input with a StringVar object + Current_value = StringVar() + # Set input to receive username and download the profile picture + profile_pic_input = ttk.Entry(new_window , textvariable = Current_value , width = 35) + # the Entry widget has focus, it’s ready to accept the user input + profile_pic_input.focus() + profile_pic_input.place(x = 190 , y = 260) + + # Set the current value of the input with a StringVar object + Current_value2 = StringVar() + # Set input to recieve instagram image or video URL + post_input = ttk.Entry(new_window , textvariable = Current_value2 , width = 35) + # the Entry widget has focus, it’s ready to accept the user input + post_input.focus() + post_input.place(x = 190 , y = 400) + + # function for downloading User profile picture + def download_profile(): + def download_image(): + try: + location = filedialog.askdirectory() + os.chdir(location) + # Start download Profile Picture + obj = Instaloader() + profile = profile_pic_input.get() + obj.download_profile(profile , profile_pic_only = True) + messagebox.showinfo('STATUS','Profile Image Downloaded Successfully') + except: + messagebox.showerror('ERROR','Username Is Incorrect or Does Not Exist') + # thread is a separate flow of execution. This means that our program will have two things happening at once + threading.Thread(target = download_image).start() + + # Function for downloadin image by URL + def download_post(): + # Get url from user by GUI input (Entry) + link = post_input.get() + def media(): + if 'https://www.instagram.com/p/' in link : + location = filedialog.askdirectory() + os.chdir(location) + L = Instaloader() + try : + L.login(Username , Password) + except : + messagebox.showerror('ERROR' , 'Username or Password is Wrong') + try : + short_link = link[28:39] + post = Post.from_shortcode(L.context , short_link) + L.download_post(post , target = short_link) + messagebox.showinfo('STATUS','Download Completed !') + except : + messagebox.showerror('ERROR' , 'Link Not Found , please enter the link of the image') + else : + messagebox.showerror('ERROR','URL Is Incorrect') + # thread is a separate flow of execution. This means that our program will have two things happening at once + threading.Thread(target = media).start() + + # Create style Object + style_button=ttk.Style() + # configure style, and naming that , style TButtton is used for ttk.button + style_button.configure('TButton' , font = ('calibri' , 10 , 'bold' , UNDERLINE) , foreground = 'red') + button1=ttk.Button(new_window , text = 'Download' , style = 'TButton' , command = download_profile) + button1.place(x = 260 , y = 300) + button1=ttk.Button(new_window , text = 'Download' , style = 'TButton' , command = download_post) + button1.place(x=260,y=440) + button2=ttk.Button(new_window , text = 'Exit' , style = 'TButton' , command = root.destroy) + button2.place(x = 260 , y = 500) + # Start the GUI fram of our app + new_window.mainloop() + +# created a tkinter gui window frame +root = Tk() +# Define the geometry +root.geometry('300x300') +root.resizable(False , False) +# Set the title of tkinter frame +root.title('Instagram Downloader') +# Set the background of tkinter frame +root.config(background = 'grey') +Icon = PhotoImage(file='img\\Icon.png') +root.iconphoto(False, Icon) + +# this function takes the Username and Password and pass them to the new_window function +def caller(): + Username = username_entry.get() + Password = password_entry.get() + if Username and Password : + new_window(Username , Password) + else : + messagebox.showerror('Error' , 'You have to enter your Username and Password') + +message = Label(root , text = 'Enter Username and Password of your Account' , +bg = 'grey' , fg = 'yellow' , font = ('Calibri',11)) +message.place(x = 0 , y = 10) + +# Set the current value of the input with a StringVar object +Current_value = StringVar() +username_entry = ttk.Entry(root , textvariable = Current_value , width = 30) +# # the Entry widget has focus, it’s ready to accept the user input +username_entry.focus() +username_entry.place(x = 80 , y = 60) +username_text = Label(root , text = 'Username :' , bg = 'grey' , fg = 'yellow' , font = ('Calibri' , 8)) +username_text.place(x=20,y=60) + +# Set the current value of the input with a StringVar object +Current_value2 = StringVar() +password_entry = ttk.Entry(root , textvariable = Current_value2 , width = 30) +# # the Entry widget has focus, it’s ready to accept the user input +password_entry.focus() +password_entry.place(x = 80 , y = 100) +password_text = Label(root , text = 'Password : ', bg = 'grey' , fg = 'yellow' , font = ('Calibri' , 8)) +password_text.place(x = 20 , y = 100) + +# # Create style Object +style_button=ttk.Style() +# # configure style, and naming that , style TButtton is used for ttk.button +style_button.configure('TButton' , font = ('calibri' , 10 , 'bold' , UNDERLINE) , foreground = 'red') +ok_button = ttk.Button(root , text = 'OK' , style = 'TButton' , command = caller) +ok_button.place(x = 110 , y = 160) +exit_button = ttk.Button(root , text = 'Exit' , style = 'TButton' , command = root.destroy) +exit_button.place(x = 110 , y = 220) + +root.mainloop() \ No newline at end of file diff --git a/projects/GUI-Instagram-Downloader/modules/connection.py b/projects/GUI-Instagram-Downloader/modules/connection.py new file mode 100644 index 00000000..d3da1df1 --- /dev/null +++ b/projects/GUI-Instagram-Downloader/modules/connection.py @@ -0,0 +1,18 @@ +import requests +from tkinter import messagebox + +# Function to check the internet connection +def connection(url = 'http://www.google.com/' , timeout = 5): + try: + req = requests.get(url , timeout = timeout) + req.raise_for_status() + return True + + except requests.HTTPError as error: + messagebox.showerror( + 'Error',f'Checking Internet Connection Failed, Status Code: {error.response.status_code}') + return False + + except requests.ConnectionError: + messagebox.showerror('Error','No Internet Connection Available.') + return False \ No newline at end of file