from flask import Flask, request, send_file, render_template_string
import instaloader
import os
app = Flask(__name__)
# HTML template for the form
HTML_TEMPLATE = ”’
Instagram Video Downloader
{% if error %}
{{ error }}
{% endif %}
{% if video_path %}
Click here to download the video
{% endif %}
”’
@app.route(‘/’, methods=[‘GET’, ‘POST’])
def index():
video_path = None
error = None
if request.method == ‘POST’:
url = request.form[‘url’]
loader = instaloader.Instaloader()
# Extract the shortcode from the URL
try:
shortcode = url.split(‘/’)[-2]
post = instaloader.Post.from_shortcode(loader.context, shortcode)
# Download the video
video_file = f”{shortcode}.mp4″
loader.download_post(post, target=shortcode)
# Set the video path for download
video_path = video_file
except Exception as e:
error = str(e)
return render_template_string(HTML_TEMPLATE, video_path=video_path, error=error)
if __name__ == ‘__main__’:
app.run(debug=True)