#
require 'src/website'
+require 'cgi'
+
+class BliptvMediaFormat
+ # This is just a convenience class for parsing two parameters out of
+ # an RSS feed: 'url' and 'blip:role'.
+ def initialize(line)
+ @url = nil
+ @role = nil
+
+ url_regex = /url=\"([^\"]+)\"/
+ role_regex = /blip:role=\"([^\"]+)\"/
+ url_matches = url_regex.match(line)
+ role_matches = role_regex.match(line)
+
+ if not url_matches.nil? and (url_matches.length) > 1
+ @url = url_matches[1]
+ end
+
+ if not role_matches.nil? and (role_matches.length > 1)
+ @role = role_matches[1]
+ end
+ end
+
+ def role
+ return @role
+ end
+
+ def url
+ return @url
+ end
+end
+
class Bliptv < Website
- VALID_BLIPTV_URL_REGEX = /^(http:\/\/)?([[:alnum:]\-]+\.)?blip\.tv\/file\/(\d+)(.*)?$/
+ VALID_BLIPTV_URL_REGEX = /^(http:\/\/)?blip\.tv\/.*?(\d+)$/
def self.owns_url?(url)
return url =~ VALID_BLIPTV_URL_REGEX
end
-
+
def get_video_url()
- page_data = self.get_page_data(@url)
- filepath = parse_video_url(page_data)
-
- return filepath
+ video_id = self.parse_video_id()
+ rss_page_url = "http://blip.tv/rss/flash/#{video_id}"
+ rss_data = get_page_data(rss_page_url)
+ video_url = parse_video_url(rss_data)
+
+ return video_url
end
-
+
protected;
-
- def parse_video_url(page_data)
- # First, try to find the MOV video. The source videos are usually
- # encoded with MOV.
- video_url_regex = /"Quicktime \(\.mov\)", "attribute" : "(.*?\.mov)/i
- matches = video_url_regex.match(page_data)
+ def parse_video_id()
+ video_id_regex = /(\d+)$/
+ matches = video_id_regex.match(@url)
- if not matches.nil?
+ if matches.nil? or (matches.length < 2)
+ raise StandardError.new("Couldn't parse the video id from the URL.")
+ else
return matches[1]
end
+ end
- # If that didn't work, try the WMV format, which is occasionally
- # used for the source as well.
- video_url_regex = /"Windows Media \(\.wmv\)", "attribute" : "(.*?\.wmv)/i
- matches = video_url_regex.match(page_data)
- if not matches.nil?
- return matches[1]
+ def choose_best_format(formats)
+ # 'formats' is assumed to be an array of BliptvMediaFormat. We
+ # return the best one (in terms of video quality).
+ formats.each do |f|
+ if f.url.nil? or f.role.nil?
+ formats.delete(f)
+ next
+ end
+
+ return f if f.role == "Source"
+ end
+
+ if formats.length == 0
+ raise StandardError.new("No valid formats in the RSS feed.")
+ else
+ # Return whatever's left if we don't have a 'Source' video.
+ return formats[0]
end
+ end
-
- # If neither of the source formats are present, just grab the
- # video URL from the Flash variable and be done with it.
- video_url_regex = /setPrimaryMediaUrl\("(.*?\.(flv|mov|wmv|mp4))/i
- matches = video_url_regex.match(page_data)
+ def parse_video_url(page_data)
+ # All of the elements containing video URLs begin like this.
+ media_regex = /^\s*<media:content/
- if matches.nil?
- raise StandardError.new("Couldn't parse any of the video format URLs.")
+ formats = []
+ # Create an array of BliptvMediaFormat from lines matching
+ # media_regex.
+ page_data.lines.each do |line|
+ if (line =~ media_regex)
+ bp = BliptvMediaFormat.new(line)
+ formats << bp
+ end
end
- return matches[1]
+ # And return the URL from the best one.
+ # choose_best_format will raise an error if need be.
+ return choose_best_format(formats).url
end
end