Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.08 KB | None | 0 0
  1. --[[
  2. $Id$
  3.  
  4. Copyright © 2007-2018 the VideoLAN team
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  19. --]]
  20.  
  21. -- Helper function to get a parameter's value in a URL
  22. function get_url_param( url, name )
  23. local _, _, res = string.find( url, "[&?]"..name.."=([^&]*)" )
  24. return res
  25. end
  26.  
  27. -- Helper function to copy a parameter when building a new URL
  28. function copy_url_param( url, name )
  29. local value = get_url_param( url, name )
  30. return ( value and "&"..name.."="..value or "" ) -- Ternary operator
  31. end
  32.  
  33. function get_arturl()
  34. local iurl = get_url_param( vlc.path, "iurl" )
  35. if iurl then
  36. return iurl
  37. end
  38. local video_id = get_url_param( vlc.path, "v" )
  39. if not video_id then
  40. return nil
  41. end
  42. return vlc.access.."://img.youtube.com/vi/"..video_id.."/default.jpg"
  43. end
  44.  
  45. -- Pick the most suited format available
  46. function get_fmt( fmt_list )
  47. local prefres = vlc.var.inherit(nil, "preferred-resolution")
  48. if prefres < 0 then
  49. return nil
  50. end
  51.  
  52. local fmt = nil
  53. for itag,height in string.gmatch( fmt_list, "(%d+)/%d+x(%d+)[^,]*" ) do
  54. -- Apparently formats are listed in quality
  55. -- order, so we take the first one that works,
  56. -- or fallback to the lowest quality
  57. fmt = itag
  58. if tonumber(height) <= prefres then
  59. break
  60. end
  61. end
  62. return fmt
  63. end
  64.  
  65. -- Buffering iterator to parse through the HTTP stream several times
  66. -- without making several HTTP requests
  67. function buf_iter( s )
  68. s.i = s.i + 1
  69. local line = s.lines[s.i]
  70. if not line then
  71. -- Put back together statements split across several lines,
  72. -- otherwise we won't be able to parse them
  73. repeat
  74. local l = s.stream:readline()
  75. if not l then break end
  76. line = line and line..l or l -- Ternary operator
  77. until string.match( line, "};$" )
  78.  
  79. if line then
  80. s.lines[s.i] = line
  81. end
  82. end
  83. return line
  84. end
  85.  
  86. -- Helper to search and extract code from javascript stream
  87. function js_extract( js, pattern )
  88. js.i = 0 -- Reset to beginning
  89. for line in buf_iter, js do
  90. local ex = string.match( line, pattern )
  91. if ex then
  92. return ex
  93. end
  94. end
  95. vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
  96. return nil
  97. end
  98.  
  99. -- Descramble the URL signature using the javascript code that does that
  100. -- in the web page
  101. function js_descramble( sig, js_url )
  102. -- Fetch javascript code
  103. local js = { stream = vlc.stream( js_url ), lines = {}, i = 0 }
  104. if not js.stream then
  105. vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
  106. return sig
  107. end
  108.  
  109. -- Look for the descrambler function's name
  110. -- k.s&&f.set(k.sp,(0,window.encodeURIComponent)(DK((0,window.decodeURIComponent)(k.s))));
  111. -- k.s (from stream map field "s") holds the input scrambled signature
  112. -- k.sp (from stream map field "sp") holds a parameter name (normally
  113. -- "signature") to set with the output, descrambled signature
  114. local descrambler = js_extract( js, "%.set%([^,]-%.sp,[^;]-%((..)%(" )
  115. if not descrambler then
  116. vlc.msg.dbg( "Couldn't extract youtube video URL signature descrambling function name" )
  117. return sig
  118. end
  119.  
  120. -- Fetch the code of the descrambler function
  121. -- Go=function(a){a=a.split("");Fo.sH(a,2);Fo.TU(a,28);Fo.TU(a,44);Fo.TU(a,26);Fo.TU(a,40);Fo.TU(a,64);Fo.TR(a,26);Fo.sH(a,1);return a.join("")};
  122. local rules = js_extract( js, "^"..descrambler.."=function%([^)]*%){(.-)};" )
  123. if not rules then
  124. vlc.msg.dbg( "Couldn't extract youtube video URL signature descrambling rules" )
  125. return sig
  126. end
  127.  
  128. -- Get the name of the helper object providing transformation definitions
  129. local helper = string.match( rules, ";(..)%...%(" )
  130. if not helper then
  131. vlc.msg.dbg( "Couldn't extract youtube video URL signature transformation helper name" )
  132. vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
  133. return sig
  134. end
  135.  
  136. -- Fetch the helper object code
  137. -- var Fo={TR:function(a){a.reverse()},TU:function(a,b){var c=a[0];a[0]=a[b%a.length];a[b]=c},sH:function(a,b){a.splice(0,b)}};
  138. local transformations = js_extract( js, "[ ,]"..helper.."={(.-)};" )
  139. if not transformations then
  140. vlc.msg.dbg( "Couldn't extract youtube video URL signature transformation code" )
  141. return sig
  142. end
  143.  
  144. -- Parse the helper object to map available transformations
  145. local trans = {}
  146. for meth,code in string.gmatch( transformations, "(..):function%([^)]*%){([^}]*)}" ) do
  147. -- a=a.reverse()
  148. if string.match( code, "%.reverse%(" ) then
  149. trans[meth] = "reverse"
  150.  
  151. -- a.splice(0,b)
  152. elseif string.match( code, "%.splice%(") then
  153. trans[meth] = "slice"
  154.  
  155. -- var c=a[0];a[0]=a[b%a.length];a[b]=c
  156. elseif string.match( code, "var c=" ) then
  157. trans[meth] = "swap"
  158. else
  159. vlc.msg.warn("Couldn't parse unknown youtube video URL signature transformation")
  160. end
  161. end
  162.  
  163. -- Parse descrambling rules, map them to known transformations
  164. -- and apply them on the signature
  165. local missing = false
  166. for meth,idx in string.gmatch( rules, "..%.(..)%([^,]+,(%d+)%)" ) do
  167. idx = tonumber( idx )
  168.  
  169. if trans[meth] == "reverse" then
  170. sig = string.reverse( sig )
  171.  
  172. elseif trans[meth] == "slice" then
  173. sig = string.sub( sig, idx + 1 )
  174.  
  175. elseif trans[meth] == "swap" then
  176. if idx > 1 then
  177. sig = string.gsub( sig, "^(.)("..string.rep( ".", idx - 1 )..")(.)(.*)$", "%3%2%1%4" )
  178. elseif idx == 1 then
  179. sig = string.gsub( sig, "^(.)(.)", "%2%1" )
  180. end
  181. else
  182. vlc.msg.dbg("Couldn't apply unknown youtube video URL signature transformation")
  183. missing = true
  184. end
  185. end
  186. if missing then
  187. vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
  188. end
  189. return sig
  190. end
  191.  
  192. -- Parse and pick our video URL
  193. function pick_url( url_map, fmt, js_url )
  194. local path = nil
  195. for stream in string.gmatch( url_map, "[^,]+" ) do
  196. -- Apparently formats are listed in quality order,
  197. -- so we can afford to simply take the first one
  198. local itag = string.match( stream, "itag=(%d+)" )
  199. if not fmt or not itag or tonumber( itag ) == tonumber( fmt ) then
  200. local url = string.match( stream, "url=([^&,]+)" )
  201. if url then
  202. url = vlc.strings.decode_uri( url )
  203.  
  204. local sig = string.match( stream, "sig=([^&,]+)" )
  205. if not sig then
  206. -- Scrambled signature
  207. sig = string.match( stream, "s=([^&,]+)" )
  208. if sig then
  209. vlc.msg.dbg( "Found "..string.len( sig ).."-character scrambled signature for youtube video URL, attempting to descramble... " )
  210. if js_url then
  211. sig = js_descramble( sig, js_url )
  212. else
  213. vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
  214. end
  215. end
  216. end
  217. local signature = ""
  218. if sig then
  219. signature = "&signature="..sig
  220. end
  221.  
  222. path = url..signature
  223. break
  224. end
  225. end
  226. end
  227. return path
  228. end
  229.  
  230. -- Probe function.
  231. function probe()
  232. return ( ( vlc.access == "http" or vlc.access == "https" )
  233. and (
  234. string.match( vlc.path, "^www%.youtube%.com/" )
  235. or string.match( vlc.path, "^gaming%.youtube%.com/" )
  236. ) and (
  237. string.match( vlc.path, "/watch%?" ) -- the html page
  238. or string.match( vlc.path, "/live$" ) -- user live stream html page
  239. or string.match( vlc.path, "/live%?" ) -- user live stream html page
  240. or string.match( vlc.path, "/get_video_info%?" ) -- info API
  241. or string.match( vlc.path, "/v/" ) -- video in swf player
  242. or string.match( vlc.path, "/embed/" ) -- embedded player iframe
  243. ) )
  244. end
  245.  
  246. -- Parse function.
  247. function parse()
  248. if string.match( vlc.path, "^gaming%.youtube%.com/" ) then
  249. url = string.gsub( vlc.path, "^gaming%.youtube%.com", "www.youtube.com" )
  250. return { { path = vlc.access.."://"..url } }
  251. end
  252. if string.match( vlc.path, "/watch%?" )
  253. or string.match( vlc.path, "/live$" )
  254. or string.match( vlc.path, "/live%?" )
  255. then -- This is the HTML page's URL
  256. -- fmt is the format of the video
  257. -- (cf. http://en.wikipedia.org/wiki/YouTube#Quality_and_formats)
  258. fmt = get_url_param( vlc.path, "fmt" )
  259. while true do
  260. -- Try to find the video's title
  261. line = vlc.readline()
  262. if not line then break end
  263. if string.match( line, "<meta property=\"og:title\"" ) then
  264. _,_,name = string.find( line, "content=\"(.-)\"" )
  265. name = vlc.strings.resolve_xml_special_chars( name )
  266. name = vlc.strings.resolve_xml_special_chars( name )
  267. end
  268.  
  269. if not description then
  270. description = string.match( line, "<p id=\"eow%-description\"[^>]*>(.-)</p>" )
  271. if description then
  272. description = vlc.strings.resolve_xml_special_chars( description )
  273. end
  274. end
  275.  
  276.  
  277. if string.match( line, "<meta property=\"og:image\"" ) then
  278. _,_,arturl = string.find( line, "content=\"(.-)\"" )
  279. arturl = vlc.strings.resolve_xml_special_chars( arturl )
  280. end
  281.  
  282. if string.match(line, "\"author\": *\"(.-)\"") then
  283. _,_,artist = string.find(line, "\"author\": *\"(.-)\"")
  284. end
  285.  
  286. -- JSON parameters, also formerly known as "swfConfig",
  287. -- "SWF_ARGS", "swfArgs", "PLAYER_CONFIG", "playerConfig" ...
  288. if string.match( line, "ytplayer%.config" ) then
  289.  
  290. local js_url = string.match( line, "\"js\": *\"(.-)\"" )
  291. if js_url then
  292. js_url = string.gsub( js_url, "\\/", "/" )
  293. -- Resolve URL
  294. if string.match( js_url, "^/[^/]" ) then
  295. local authority = string.match( vlc.path, "^([^/]*)/" )
  296. js_url = "//"..authority..js_url
  297. end
  298. js_url = string.gsub( js_url, "^//", vlc.access.."://" )
  299. end
  300.  
  301. if not fmt then
  302. fmt_list = string.match( line, "\"fmt_list\": *\"(.-)\"" )
  303. if fmt_list then
  304. fmt_list = string.gsub( fmt_list, "\\/", "/" )
  305. fmt = get_fmt( fmt_list )
  306. end
  307. end
  308.  
  309. url_map = string.match( line, "\"url_encoded_fmt_stream_map\": *\"(.-)\"" )
  310. if url_map then
  311. -- FIXME: do this properly
  312. url_map = string.gsub( url_map, "\\u0026", "&" )
  313. path = pick_url( url_map, fmt, js_url )
  314. end
  315.  
  316. if not path then
  317. -- If this is a live stream, the URL map will be empty
  318. -- and we get the URL from this field instead
  319. local hlsvp = string.match( line, '\\"hlsManifestUrl\\": *\\"(.-)\\"' )
  320. if hlsvp then
  321. hlsvp = string.gsub( hlsvp, "\\/", "/" )
  322. path = hlsvp
  323. end
  324. end
  325. -- There is also another version of the parameters, encoded
  326. -- differently, as an HTML attribute of an <object> or <embed>
  327. -- tag; but we don't need it now
  328. end
  329. end
  330.  
  331. if not path then
  332. local video_id = get_url_param( vlc.path, "v" )
  333. if video_id then
  334. -- Passing no "el" parameter to /get_video_info seems to
  335. -- let it default to "embedded", and both known values
  336. -- of "embedded" and "detailpage" are wrong and fail for
  337. -- various restricted videos, so we pass a different value
  338. path = vlc.access.."://www.youtube.com/get_video_info?video_id="..video_id.."&el=detail"..copy_url_param( vlc.path, "fmt" )
  339. vlc.msg.warn( "Couldn't extract video URL, falling back to alternate youtube API" )
  340. end
  341. end
  342.  
  343. if not path then
  344. vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
  345. return { }
  346. end
  347.  
  348. if not arturl then
  349. arturl = get_arturl()
  350. end
  351.  
  352. return { { path = path; name = name; description = description; artist = artist; arturl = arturl } }
  353.  
  354. elseif string.match( vlc.path, "/get_video_info%?" ) then -- video info API
  355. local line = vlc.readline() -- data is on one line only
  356.  
  357. local fmt = get_url_param( vlc.path, "fmt" )
  358. if not fmt then
  359. local fmt_list = string.match( line, "&fmt_list=([^&]*)" )
  360. if fmt_list then
  361. fmt_list = vlc.strings.decode_uri( fmt_list )
  362. fmt = get_fmt( fmt_list )
  363. end
  364. end
  365.  
  366. local url_map = string.match( line, "&url_encoded_fmt_stream_map=([^&]*)" )
  367. if url_map then
  368. url_map = vlc.strings.decode_uri( url_map )
  369. path = pick_url( url_map, fmt )
  370. end
  371.  
  372. if not path then
  373. -- If this is a live stream, the URL map will be empty
  374. -- and we get the URL from this field instead
  375. local hlsvp = string.match( line, "%%22hlsManifestUrl%%22%%3A%%22(.-)%%22" )
  376. if hlsvp then
  377. hlsvp = vlc.strings.decode_uri( hlsvp )
  378. path = hlsvp
  379. end
  380. end
  381.  
  382. if not path then
  383. vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
  384. return { }
  385. end
  386.  
  387. local title = string.match( line, "&title=([^&]*)" )
  388. if title then
  389. title = string.gsub( title, "+", " " )
  390. title = vlc.strings.decode_uri( title )
  391. end
  392. local artist = string.match( line, "&author=([^&]*)" )
  393. if artist then
  394. artist = string.gsub( artist, "+", " " )
  395. artist = vlc.strings.decode_uri( artist )
  396. end
  397. local arturl = string.match( line, "&thumbnail_url=([^&]*)" )
  398. if arturl then
  399. arturl = vlc.strings.decode_uri( arturl )
  400. end
  401.  
  402. return { { path = path, title = title, artist = artist, arturl = arturl } }
  403.  
  404. else -- Other supported URL formats
  405. local video_id = string.match( vlc.path, "/[^/]+/([^?]*)" )
  406. if not video_id then
  407. vlc.msg.err( "Couldn't extract youtube video URL" )
  408. return { }
  409. end
  410. return { { path = vlc.access.."://www.youtube.com/watch?v="..video_id..copy_url_param( vlc.path, "fmt" ) } }
  411. end
  412. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement