From dbde07faafd93f8a9503c1fedfe317723da7cac5 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 7 Apr 2010 11:58:55 -0400 Subject: [PATCH] Add Youtube support for the "fmt_list" and "t" variables stored as URL parameters. --- src/websites/youtube.rb | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/websites/youtube.rb b/src/websites/youtube.rb index 5f87754..ed5ab4b 100644 --- a/src/websites/youtube.rb +++ b/src/websites/youtube.rb @@ -124,31 +124,43 @@ class Youtube < Website # Parse out the "t" parameter from the video's page. I'm not sure - # what "t" stands for, but it's located in some JSON, and is required - # for the final video URL to work. + # what "t" stands for, but it's required for the final video URL to + # work. It can be stored in either JSON or URL parameters. def parse_t_parameter(page_data) t_parameter = nil - t_parameter_regex = /\"t\"\:[[:space:]]\"([^\"]+?)\"/ - matches = t_parameter_regex.match(page_data) - t_parameter = matches[1] if not (matches.nil? || matches.length < 2) + t_parameter_regexes = [ /\"t\"\:[[:space:]]\"([^\"]+?)\"/, + /&t=([^&\"\\]+)/ ] + matches = t_parameter_regexes.map { |tpr| tpr.match(page_data) } + + if matches.nitems == 0 + raise StandardError.new("Could not parse the 't' parameter.") + end + + first_match = matches.compact[0] + t_parameter = CGI::unescape(first_match[1]) return t_parameter end def get_available_formats(page_data) - # Parse the list of available formats from the "fmt_list" Flash - # variable. + # Parse the list of available formats from the "fmt_list" + # variable. It can be stored as either a Flash variable (JSON + # notation), or as URL parameter. available_formats = [] - fmt_list_regex = /\"fmt_list\"\:[[:space:]]\"([^\"]+?)\"/ - matches = fmt_list_regex.match(page_data) + fmt_list_regexes = [ /\"fmt_list\"\:[[:space:]]\"([^\"]+?)\"/, + /fmt_list=([^&\"\\]+)/ ] - if matches.nil? + matches = fmt_list_regexes.map { |flr| flr.match(page_data) } + + if matches.nitems == 0 raise StandardError.new("Could not find any valid formats.") end - fmts_string = CGI::unescape(matches[1]) + first_match = matches.compact[0] + fmts_string = CGI::unescape(first_match[1]) + fmts_string.split(',').each do |fmt| # Each "fmt" will look something like, # -- 2.43.2