- # Get the URL map from the page.
- fmt_url_map = get_format_url_map(page_data)
-
- # Figure out which formats are available, and if any are,
- # choose the best one.
- available_formats = fmt_url_map.keys()
- desired_format = get_desired_format(available_formats)
-
- # First we cache the format so that when we're asked for the
- # video filename later, we don't have to recompute the format.
- @format = desired_format
-
- # And then use whatever URL is available for the desired format.
- # We assume that all available formats will have an entry in the
- # fmt_url_map hash.
- video_url = fmt_url_map[desired_format]
+ begin
+ # Get the URL map from the page.
+ fmt_url_map = get_format_url_map(page_data)
+
+ # Figure out which formats are available, and if any are,
+ # choose the best one.
+ available_formats = fmt_url_map.keys()
+ desired_format = get_desired_format(available_formats)
+
+ # First we cache the format so that when we're asked for the
+ # video filename later, we don't have to recompute the format.
+ @format = desired_format
+
+ # And then use whatever URL is available for the desired format.
+ # We assume that all available formats will have an entry in the
+ # fmt_url_map hash.
+ video_url = fmt_url_map[desired_format]
+
+ return video_url
+ rescue StandardError => e
+ # If at first you do not succeed, maybe someone decided to
+ # change some shit. This alternate method parses
+ # url_encoded_fmt_stream_map.
+ fmt_streams = get_fmt_stream_list(page_data)
+ video_url = self.unicode_unescape(fmt_streams[0])
+ video_url = CGI::unescape(video_url)
+
+ # Strip off everything after the first space in the URL.
+ # I don't know why this works, but if we leave the space
+ # in (encoded, even), Youtube throws us 403 errors.
+ video_url.gsub!(/ .+$/, '')
+ end