]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blobdiff - src/website.rb
Add a generic parser that will hopefully supplant some site-specific subclasses.
[dead/whatever-dl.git] / src / website.rb
index 1239712a696a3b855ec3ad79472865a608e7f937..e9e65ca1909f7add3c8b2c6ecc800a3856145528 100644 (file)
 # http://www.fsf.org/licensing/licenses/gpl.html
 #
 
+# Needed for the default implementation of get_page_data.
+require 'net/http'
+
+# Necessary in a lot of subclasses; plus, we need it
+# to parse the server name out of our URL.
+require 'uri'
+
+# Needed to download.. things.
+require 'net/http'
+
 # This class keeps track of all its subclasses
 # We use this to loop through every "website" in an
 # attempt to determine to which site a URL belongs.
 class Website
+
+  protected;
+
+  @url = nil
+
+
   def self.inherited(subclass)
     if superclass.respond_to? :inherited
       superclass.inherited(subclass)
@@ -31,18 +47,101 @@ class Website
     @subclasses << subclass
   end
 
-  def self.subclasses
-    @subclasses
+
+  def server
+    # Get the HTTP server portion of our URI
+    uri = URI.parse(@url)
+    return uri.host
   end
 
-  # This should be overridden in any class that wants
-  # to claim ownership of a URL.
+
+
+  def get_page_data(url)
+    # A naive implementation that just grabs the
+    # data from a page.
+    uri = URI.parse(url)
+
+    response = Net::HTTP.start(uri.host, uri.port) do |http|
+      http.get(uri.request_uri, self.headers)
+    end
+
+    # Set the referer in case it is needed for some later request.
+    self.headers['Referer'] = uri.request_uri
+
+    return response.body
+  end
+
+
+
+  public;
+
+  # Additional headers used when requesting data from the website.
+  # These aren't passed as a parameter because the (final)
+  # downloaders need them as well.
+  attr_accessor :headers
+
+  def initialize(url)
+    @url = url
+    self.headers = { 'User-Agent' => Configuration::USER_AGENT }
+  end
+
+
+  def self.create(url)
+    # Factory method returning an instance of
+    # the appropriate subclass.
+
+    # While we're looping through the list of subclasses,
+    # we'll set this to the Generic class.
+    generic = nil
+
+    # Check the URL against each website's class.
+    # The class will know whether or not the URL
+    # "belongs" to its website.
+    @subclasses.each do |w|
+      if w.owns_url?(url)
+        if w.to_s == 'Generic'
+          generic = w
+        else
+          # We don't want to return Generic here because some
+          # other subclasses further down the list might match
+          # the URL.
+          return w.new(url)
+        end
+      end
+    end
+
+    # If nothing matched, try the generic parser.
+    return generic.new(url)
+  end
+
+
+  # Abstract definition. Each subclass of Website
+  # should support it on its own.
   def self.owns_url?(url)
-    return false
+    raise NotImplementedError
   end
 
-  # Same here. We want to default to nil unless overridden.
-  def get_video_url(url)
-    return nil
+
+  # Same here. Abstract.
+  def get_video_url()
+    raise NotImplementedError
   end
+
+
+  # The website class should be responsible for determining the
+  # video's filename. By default, we can take the last component
+  # of the video URL, but in some cases, subclasses will want
+  # to override this behavior.
+  def get_video_filename()
+    # Use whatever comes after the final front slash.
+    file_and_params = get_video_url().split('/').pop()
+
+    # Unless it contains URL parameters. We don't want those.
+    return file_and_params unless file_and_params.include?('?')
+
+    # There must be some parameters. Strip them off.
+    param_start_idx = file_and_params.index('?')
+    return file_and_params[0...(param_start_idx)]
+  end
+
 end