# 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,
#