Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI-Instagram-Downloader #25

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions projects/GUI-Instagram-Downloader/GUI-Instagram-post-downloader.mdx
Original file line number Diff line number Diff line change
@@ -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
<p>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.<p>

## Instaloader
In this project we will be using <a href="https://instaloader.github.io">Instaloader</a>, 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 <a href="https://instaloader.github.io/installation.html#install"> Install Instaloader</a> 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
- <a href='https://docs.python.org/3/library/tkinter.html'>Tkinter Library</a>
- <a href='https://instaloader.github.io/'>Instaloader Module</a><span> ( Version 4.9.2 )</span>
- <a href='https://pillow.readthedocs.io/'>Pillow Library</a><span> ( Version 9.0.1 )</span>
- <a href='https://docs.python-requests.org/'>Request Module</a><span> ( Version 2.27.1 )</span>
- <a href='https://docs.python.org/3/library/os.html'>Os Module</a>
- <a href='https://docs.python.org/3/library/threading.html'>Threading Module</a>

## 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

<p align="center">
<img src="https://github.com/Arone-S-G-H/GUI-Insta-Downloader/blob/main/Result/Instagram%20Downloader%202.png">
</p>

## Explanation
<p> Import the required Libraries and Modules </P>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain each library and module (and lowercase "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
```

<p>function for downloading User profile picture </p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain what filedialog.askdirectory() is, and what the Instaloader() code does in more detail



```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()
```

<p> Function for downloading image by URL</P>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, talk about logging into Instagram and how that works with the instaloader



```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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a separate code block and explanation of messagebox

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()
```


<p> To create a tkinter GUI frame</p>
Copy link
Contributor

@Bobliuuu Bobliuuu Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand about Tinkter here. What exactly does it do? Why do we need it in this project?




```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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the new_window function? How does it fit into the project flow?



```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/)




Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added projects/GUI-Instagram-Downloader/img/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added projects/GUI-Instagram-Downloader/img/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions projects/GUI-Instagram-Downloader/main.py
Original file line number Diff line number Diff line change
@@ -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()
Loading