- response = Net::HTTP.start(uri.host, uri.port) do |http|
- http.get(uri.request_uri)
+ 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