usrxprd

mc_video_link_retriever

Oct 21st, 2024
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # This is a CGI script for retrieving video links from posts on Mainchan.com.
  4.  
  5. import os
  6. import cgi, cgitb   # will be removed in python 3.13
  7. import requests
  8. from bs4 import BeautifulSoup
  9. import re
  10.  
  11. mc_video_link_retriever_version = "1.1"
  12.  
  13. def validMCPostURL(post_url):
  14.  
  15.     # This function returns true if post_url is a valid mainchan post url and false otherwise.
  16.     # post_url: A url to a mainchan post.
  17.  
  18.     return bool(re.match("^(http|https)://mainchan\.com/s/[0-9a-zA-Z]+/[0-9]+/[0-9a-zA-Z\-]+$", post_url))
  19.  
  20. def getVideoURL(post_url, video_resolution):
  21.  
  22.     # This function returns the mp4 video url in the specified resolution from a mainchan post.
  23.     # post_url:     A url to a mainchan post containing the video.
  24.     # video_resolution: The desired video resolution.
  25.  
  26.     headers = {'User-Agent': f'MCVideoLinkRetriever/{mc_video_link_retriever_version} (admin contact: )'}
  27.  
  28.     # might change?
  29.     pullzone = "vz-28f5c635-ae7.b-cdn.net"
  30.  
  31.     valid_resolutions = ['240p', '360p', '480p', '720p', '1080p']
  32.  
  33.     if video_resolution not in valid_resolutions and video_resolution != 'highest':
  34.         return "invalid resolution"
  35.  
  36.     page = requests.get(post_url, headers=headers)
  37.     if page.status_code != 200:
  38.         return "couldn't download the post"
  39.  
  40.     soup = BeautifulSoup(page.content, "html.parser")
  41.     video_elem = soup.find("iframe")    # just grab the first iframe and hope it's the right one
  42.     if video_elem == None:
  43.         return "no embed found in post"
  44.     embed_src = video_elem['src']
  45.     if not bool(re.match("^(http|https)://iframe\.mediadelivery\.net.*$", embed_src)):
  46.         return "the embed is not valid"
  47.     video_id = embed_src.split('/')[5]
  48.  
  49.     if video_resolution == 'highest':
  50.         # try to download each resolution from the list beggining with the highest
  51.         # and check if the response is 200
  52.         for i in range(len(valid_resolutions)-1, -1, -1):
  53.             url = f"https://{pullzone}/{video_id}/play_{valid_resolutions[i]}.mp4"
  54.             response = requests.head(url, headers=headers)
  55.             if response.status_code == 200:
  56.                 return url
  57.         return "video unavailable in all tested resolutions"
  58.     else:
  59.         return f"https://{pullzone}/{video_id}/play_{video_resolution}.mp4"
  60.  
  61. print("Content-Type: text/html\r\n\r\n")
  62. print("<html>")
  63. print("  <head>")
  64. print("    <meta charset='utf-8'>")
  65. print("    <meta name='viewport' content='width=device-width, initial-scale=1'>")
  66. print("  </head>")
  67. print("  <body><center>")
  68. print("    <h1>Mainchan video link retriever</h1>")
  69. print("    <form action='mc_video_link_retriever.cgi' method='post'>")
  70. print("      <label for='mc_post_url'>Enter the link of the mainchan post:</label><br><br>")
  71. print("      <input type='text' id='mc_post_url' name='mc_post_url'/><br><br>")
  72. print("      <input type='submit' value='get video link'/>")
  73. print("    </form>")
  74.  
  75. if os.environ.get('REQUEST_METHOD') == "POST":
  76.     form = cgi.FieldStorage()
  77.     mc_post_url = form.getvalue('mc_post_url')
  78.     if mc_post_url != None:
  79.         if validMCPostURL(mc_post_url):
  80.             vdo_dl_url = getVideoURL(mc_post_url, "highest")
  81.             # if the below is not true, then getVideoURL returned an error message
  82.             if vdo_dl_url[0:8] == "https://":
  83.                 print(f"<a href='{vdo_dl_url}' rel='noreferrer'>{vdo_dl_url}</a>")
  84.             else:
  85.                 print(f"<p style='color:red;'>error: {vdo_dl_url}</p>")
  86.         else:
  87.             print("<p style='color:red;'>error: invalid mainchan post link</p>")
  88.     else:
  89.         print("<p style='color:red;'>error: no valid input name found</p>")
  90.  
  91. print("  </center></body>")
  92. print("</html>")
Advertisement
Add Comment
Please, Sign In to add comment