]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blobdiff - lib/whatever-dl/websites/bliptv.rb
Move all of the 'src' code under the more-standard 'lib'.
[dead/whatever-dl.git] / lib / whatever-dl / websites / bliptv.rb
diff --git a/lib/whatever-dl/websites/bliptv.rb b/lib/whatever-dl/websites/bliptv.rb
new file mode 100644 (file)
index 0000000..3320587
--- /dev/null
@@ -0,0 +1,125 @@
+#
+# Copyright Michael Orlitzky
+#
+# http://michael.orlitzky.com/
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# http://www.fsf.org/licensing/licenses/gpl.html
+#
+
+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:\/\/)?blip\.tv\/.*?(\d+)$/
+
+  def self.owns_url?(url)
+    return url =~ VALID_BLIPTV_URL_REGEX
+  end
+
+
+  def get_video_url()
+    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_id()
+    video_id_regex = /(\d+)$/
+    matches = video_id_regex.match(@url)
+
+    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
+
+
+  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
+
+  def parse_video_url(page_data)
+    # All of the elements containing video URLs begin like this.
+    media_regex = /^\s*<media:content/
+
+    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
+
+    # 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