Welcome folks today in this blog post we will be using the instagram
api in flask to download all images and video posts from insta
profile and export it as zip
file in flask using python. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new flask
project using the below command as shown below
pip install flask
pip install instaloader
And after that you will see the below directory
structure of the flask app as shown below
And now inside the app.py
file you need to copy paste the below code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import os import shutil import instaloader import zipfile import time import urllib.parse from flask import Flask, request, jsonify, send_file,render_template # Initialize the Flask app app = Flask(__name__) # Create an instance of Instaloader L = instaloader.Instaloader() # Define a function to download a user's profile def download_profile(username): # Download all the user's posts L.download_profile(username, profile_pic=True) # Create a folder with the username folder_name = username os.makedirs(folder_name, exist_ok=True) # Move all the downloaded media into the folder for filename in os.listdir('.'): if not filename.startswith(username): continue if filename.endswith('.jpg') or filename.endswith('.mp4'): shutil.move(filename, os.path.join(folder_name, filename)) # Create a zip file containing all the downloaded media zip_filename = folder_name + '.zip' with zipfile.ZipFile(zip_filename, 'w') as zip_file: for filename in os.listdir(folder_name): extension = os.path.splitext(filename)[1].lower() if extension in ['.mp4', '.jpg', '.jpeg', '.png']: zip_file.write(os.path.join(folder_name, filename), arcname=filename) # Delete the folder with the downloaded media shutil.rmtree(folder_name) return zip_filename # Define a route for the homepage @app.route('/') def home(): return render_template('index.html') def get_username(url): # Parse the URL and extract the username from it parsed = urllib.parse.urlparse(url) if parsed.netloc == 'www.instagram.com' and parsed.path != '': return parsed.path.split('/')[1] return None # Define a route for the download function @app.route('/download', methods=['POST']) def download(): # Get the username or profile URL from the form data username_or_url = request.form['username_or_url'] username = get_username(username_or_url) if username is None: username = username_or_url.strip() # Download the user's profile zip_filename = download_profile(username) # Return the zip file to the user return send_file(zip_filename, as_attachment=True) # Run the app if __name__ == '__main__': app.run(debug=True) |
As you can see we have started the flask
server at the port number 5000 and then we are displaying the index.html
template when the user goes to the /
route and then we have the post request to download the posts
from the instagram username that is submitted by the user and then export it as the zip
file.
And now we need to make the templates
folder and inside it we need to make the index.html
file and copy paste the following code
templates/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <head> <title>Download Instagram Media</title> </head> <body> <form id="download-form" action="/download" method="POST"> <label for="username_or_url">Username or Profile URL:</label> <input type="text" name="username_or_url" id="username_or_url" required> <br> <button type="submit">Download Media</button> </form> </body> </html> |