Advertisement
TechManDylan

YoutubeDownloader

Feb 24th, 2023
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. local http = require("socket.http")
  2. local ltn12 = require("ltn12")
  3. local lfs = require("lfs")
  4. local lx = require("lxp.lom")
  5.  
  6. function sendHttpRequest(url)
  7.     local response_body = {}
  8.     local res, code, response_headers = http.request{
  9.         url = url,
  10.         method = "GET",
  11.         headers = {
  12.             ["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
  13.         },
  14.         sink = ltn12.sink.table(response_body)
  15.     }
  16.     local response = table.concat(response_body)
  17.     return response
  18. end
  19.  
  20. function parseVideoPage(videoPage)
  21.     local tree = lx.parse(videoPage)
  22.     local videoId = nil
  23.     for _, elem in ipairs(tree) do
  24.         if elem.tag == "div" and elem.attr.class == "watch-main-col" then
  25.             for _, child in ipairs(elem) do
  26.                 if child.tag == "div" and child.attr.id == "watch7-content" then
  27.                     for _, grandchild in ipairs(child) do
  28.                         if grandchild.tag == "div" and grandchild.attr.id == "watch7-player" then
  29.                             videoId = grandchild.attr["data-video-id"]
  30.                         end
  31.                     end
  32.                 end
  33.             end
  34.         end
  35.     end
  36.     local videoUrl = "https://www.youtube.com/watch?v=" .. videoId
  37.     return videoId, videoUrl
  38. end
  39.  
  40. function downloadVideo(videoUrl)
  41.     local response_body = {}
  42.     local res, code, response_headers = http.request{
  43.         url = videoUrl,
  44.         method = "GET",
  45.         headers = {
  46.             ["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
  47.         },
  48.         sink = ltn12.sink.table(response_body)
  49.     }
  50.     local response = table.concat(response_body)
  51.     return response
  52. end
  53.  
  54. function saveVideoToFile(videoData, filename)
  55.     local file, err = io.open(filename, "wb")
  56.     if not file then
  57.         print("Error opening file:", err)
  58.         return
  59.     end
  60.     file:write(videoData)
  61.     file:close()
  62. end
  63.  
  64. -- main program
  65. print("Enter the URL of the YouTube video you want to download:")
  66. local videoPageUrl = io.read("*line")
  67. local videoPage = sendHttpRequest(videoPageUrl)
  68. local videoId, videoUrl = parseVideoPage(videoPage)
  69. local videoData = downloadVideo(videoUrl)
  70. saveVideoToFile(videoData, "video.mp4")
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement