+
+ def get_available_formats(page_data)
+ # 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_regexes = [ /\"fmt_list\"\:[[:space:]]\"([^\"]+?)\"/,
+ /fmt_list=([^&\"\\]+)/ ]
+
+ matches = fmt_list_regexes.map { |flr| flr.match(page_data) }
+
+ if matches.nitems == 0
+ raise StandardError.new("Could not find any valid formats.")
+ end
+
+ first_match = matches.compact[0]
+ fmts_string = CGI::unescape(first_match[1])
+
+ fmts_string.split(',').each do |fmt|
+ # Each "fmt" will look something like,
+ #
+ # 35/640000/9/0/115
+ #
+ # with the format identifier coming before the first slash.
+ first_slash_idx = fmt.index('/')
+ available_formats << fmt[0...first_slash_idx].to_i
+ end
+
+ return available_formats
+ end
+
+
+ def get_desired_format(available_formats)
+ # Check for the presence of formats, in order of preference
+ # (quality). That is, we check for the best formats first. As soon
+ # as a format is found to be available, we return it as the
+ # desired format, since the first format we find is going to be
+ # the best available format.
+ return 37 if available_formats.include?(37)
+ return 22 if available_formats.include?(22)
+ return 35 if available_formats.include?(35)
+ return 18 if available_formats.include?(18)
+ return 34 if available_formats.include?(34)
+ return 17 if available_formats.include?(17)
+ end