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__)
@app.route('/')
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__":
app.run(debug=True)
This code utilizes Flask, a powerful web framework, to create a user-friendly interface. Let's break down the functionality:
We import the necessary modules, including Flask, render_template, and request, to handle web requests and render HTML templates.
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():
try:
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}
else:
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}
except:
return {"type": "", "link": "", "success": False}
else:
return {"type": "", "link": "", "success": False}
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:
The is_url_valid method checks if the provided URL follows the Pinterest pin URL pattern.
The get_page_content method retrieves the HTML content of the Pinterest page.
The is_a_video method checks if its a video or an image.
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.
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)