DEV Community

Cover image for ๐Ÿ“ขDownload Pictures and Videos from Pinterest Using Flask and python ๐Ÿ๐Ÿ“Œ๐Ÿ–ผ๏ธ๐ŸŽฅ

Posted on

๐Ÿ“ขDownload Pictures and Videos from Pinterest Using Flask and python ๐Ÿ๐Ÿ“Œ๐Ÿ–ผ๏ธ๐ŸŽฅ

Calling all Pinterest fans! We have some thrilling news for you. Get ready to elevate your Pinterest experience with our brand new Python code that enables seamless downloading of captivating pictures and videos. With just a few lines of code, you can bring your favorite Pinterest media right to your fingertips. Let's delve into the details and unlock a world of visual inspiration! ๐ŸŽ‰

Here's a brief overview of the code:

from flask import Flask, render_template, request
from Pinterest import Pinterest
from base64 import b64decode

app = Flask(__name__)

def home():
    return render_template("home.html")

@app.route("/download", methods=["POST", "GET"])
def download():
    if request.method == "GET":
        return render_template("Download.html", data={'type': request.args["type"], "url": b64decode(request.args["data"]).decode('ascii')})
    elif request.method == "POST":
        url = b64decode(request.form["url"]).decode('ascii')
        p = Pinterest(url)
        return p.get_media_link()

if __name__ == "__main__":

Enter fullscreen mode Exit fullscreen mode

This code utilizes Flask, a powerful web framework, to create a user-friendly interface. Let's break down the functionality:

  1. We import the necessary modules, including Flask, render_template, and request, to handle web requests and render HTML templates.

  2. We define our Flask app and set up the routes. The home route renders the home.html template, which serves as the main page for our app.

3.The /download route handles both GET and POST requests. For GET requests, we render the Download.html template, passing in the data required to display the media type and URL. The URL is base64 decoded for security purposes.

4 with the POST request in /download, we retrieve the URL and base64 decode it. We then create an instance of the Pinterest class and call the get_media_link method, which fetches and returns the media link.

Pinterest Class

import requests
import re
import json

class Pinterest:
    def __init__(self, url):
        self.url = url

    def is_url_valid(self):
        if re.match(r'(^http(s)?://)?(www.)?pinterest.\w+/pin/[\w\-?]+', self.url):
            return True
        return False

    def get_page_content(self):
        content = requests.get(self.url).text
        return content

    def is_a_video(self):
        if "video-snippet" in self.get_page_content():
            return True
        return False

    def get_media_link(self):
        if self.is_url_valid():
                if self.is_a_video():
                    match = re.findall(r'<script data-test-id="video-snippet".+?</script>', self.get_page_content())
                    j = json.loads(match[0].replace('<script data-test-id="video-snippet" type="application/ld+json">', "").replace("</script>", ""))
                    return {"type": "video", "link": j["contentUrl"], "success": True}
                    match = re.findall(r'<script data-test-id="leaf-snippet".+?</script>', self.get_page_content())
                    j = json.loads(match[0].replace('<script data-test-id="leaf-snippet" type="application/ld+json">', "").replace("</script>", ""))
                    return {"type": "image", "link": j["image"], "success": True}
                return {"type": "", "link": "", "success": False}
            return {"type": "", "link": "", "success": False}

Enter fullscreen mode Exit fullscreen mode

This Python code provides a class called Pinterest, which encapsulates the functionality for downloading pictures and videos from Pinterest. Let's explore the key features:

  1. The is_url_valid method checks if the provided URL follows the Pinterest pin URL pattern.

  2. The get_page_content method retrieves the HTML content of the Pinterest page.

  3. The is_a_video method checks if its a video or an image.

  4. The get_media_link method performs the main logic for extracting the media link. It first validates the URL, then determines if it's an image or video. Using regular expressions and JSON parsing, it extracts the media link accordingly.

  5. Finally, we run the Flask app in debug mode, allowing us to see any errors or debug information during development.

This Flask app provides a simple and intuitive way to download pictures and videos from Pinterest. You can access the app through the defined routes, enter the Pinterest URL, and retrieve your desired media content.

source code here

So, what are you waiting for? Give this Flask app a spin and start downloading your favorite Pinterest visuals with ease. Happy coding! ๐ŸŒŸโœจ

Top comments (0)