+ return second_form_matches[1] if not (second_form_matches.nil? ||
+ second_form_matches.length < 2)
+
+ # ...and the third.
+ third_form_video_id_regex = /\/([[:alnum:]]+)$/i
+ third_form_matches = third_form_video_id_regex.match(@url)
+ return third_form_matches[1] if not (third_form_matches.nil? ||
+ third_form_matches.length < 2)
+
+ # If we made it here, we couldn't figure out the video id. Yes,
+ # this is fatal, since we don't know where the video file is
+ # located.
+ raise StandardError.new("Could not parse the video id.")
+ end
+
+
+
+ def get_format_url_map(page_data)
+ # Youtube has implemented a new fmt_url_map that (perhaps
+ # unsurprisingly) maps formats to video URLs. This makes it
+ # easyish to parse the video URLs.
+ url_map = {}
+ url_map_regex = /fmt_url_map=([^&\"]+)/
+
+ matches = url_map_regex.match(page_data)
+
+ if (matches.nil? || matches.length < 1)
+ raise StandardError.new("Could not parse the fmt_url_map Flash variable.")
+ end
+
+ # The map is stored entirely in one Flash variable. The format is
+ # key|value,key|value,...
+ maptext = CGI::unescape(matches[1])
+ entries = maptext.split(',')
+ entries.each do |entry|
+ key = entry.split('|')[0].to_i
+ value = entry.split('|')[1]
+ url_map[key] = value
+ end
+
+ if (url_map.length < 1)
+ raise StandardError.new("Could not find any valid format URLs.")
+ end
+
+ return url_map