Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # This is a CGI script for retrieving video links from posts on Mainchan.com.
- import os
- import cgi, cgitb # will be removed in python 3.13
- import requests
- from bs4 import BeautifulSoup
- import re
- mc_video_link_retriever_version = "1.1"
- def validMCPostURL(post_url):
- # This function returns true if post_url is a valid mainchan post url and false otherwise.
- # post_url: A url to a mainchan post.
- return bool(re.match("^(http|https)://mainchan\.com/s/[0-9a-zA-Z]+/[0-9]+/[0-9a-zA-Z\-]+$", post_url))
- def getVideoURL(post_url, video_resolution):
- # This function returns the mp4 video url in the specified resolution from a mainchan post.
- # post_url: A url to a mainchan post containing the video.
- # video_resolution: The desired video resolution.
- headers = {'User-Agent': f'MCVideoLinkRetriever/{mc_video_link_retriever_version} (admin contact: )'}
- # might change?
- pullzone = "vz-28f5c635-ae7.b-cdn.net"
- valid_resolutions = ['240p', '360p', '480p', '720p', '1080p']
- if video_resolution not in valid_resolutions and video_resolution != 'highest':
- return "invalid resolution"
- page = requests.get(post_url, headers=headers)
- if page.status_code != 200:
- return "couldn't download the post"
- soup = BeautifulSoup(page.content, "html.parser")
- video_elem = soup.find("iframe") # just grab the first iframe and hope it's the right one
- if video_elem == None:
- return "no embed found in post"
- embed_src = video_elem['src']
- if not bool(re.match("^(http|https)://iframe\.mediadelivery\.net.*$", embed_src)):
- return "the embed is not valid"
- video_id = embed_src.split('/')[5]
- if video_resolution == 'highest':
- # try to download each resolution from the list beggining with the highest
- # and check if the response is 200
- for i in range(len(valid_resolutions)-1, -1, -1):
- url = f"https://{pullzone}/{video_id}/play_{valid_resolutions[i]}.mp4"
- response = requests.head(url, headers=headers)
- if response.status_code == 200:
- return url
- return "video unavailable in all tested resolutions"
- else:
- return f"https://{pullzone}/{video_id}/play_{video_resolution}.mp4"
- print("Content-Type: text/html\r\n\r\n")
- print("<html>")
- print(" <head>")
- print(" <meta charset='utf-8'>")
- print(" <meta name='viewport' content='width=device-width, initial-scale=1'>")
- print(" </head>")
- print(" <body><center>")
- print(" <h1>Mainchan video link retriever</h1>")
- print(" <form action='mc_video_link_retriever.cgi' method='post'>")
- print(" <label for='mc_post_url'>Enter the link of the mainchan post:</label><br><br>")
- print(" <input type='text' id='mc_post_url' name='mc_post_url'/><br><br>")
- print(" <input type='submit' value='get video link'/>")
- print(" </form>")
- if os.environ.get('REQUEST_METHOD') == "POST":
- form = cgi.FieldStorage()
- mc_post_url = form.getvalue('mc_post_url')
- if mc_post_url != None:
- if validMCPostURL(mc_post_url):
- vdo_dl_url = getVideoURL(mc_post_url, "highest")
- # if the below is not true, then getVideoURL returned an error message
- if vdo_dl_url[0:8] == "https://":
- print(f"<a href='{vdo_dl_url}' rel='noreferrer'>{vdo_dl_url}</a>")
- else:
- print(f"<p style='color:red;'>error: {vdo_dl_url}</p>")
- else:
- print("<p style='color:red;'>error: invalid mainchan post link</p>")
- else:
- print("<p style='color:red;'>error: no valid input name found</p>")
- print(" </center></body>")
- print("</html>")
Advertisement
Add Comment
Please, Sign In to add comment