From 2a5f7ac542e7f613b3fa686496ea3ab32e959ae9 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 24 Jan 2011 20:49:17 -0500 Subject: [PATCH] Make headers a property of the website class rather than passing them to get_page_data. Define a USER_AGENT in the configuration file. --- bin/configuration.rb | 2 ++ src/website.rb | 12 +++++++++--- src/websites/dailymotion.rb | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/bin/configuration.rb b/bin/configuration.rb index ad942c7..60066cc 100644 --- a/bin/configuration.rb +++ b/bin/configuration.rb @@ -9,4 +9,6 @@ module Configuration # example. DOWNLOAD_METHOD = :openuri #DOWNLOAD_METHOD = :wget + + USER_AGENT = 'whatever-dl' end diff --git a/src/website.rb b/src/website.rb index 2f03e6c..4e20466 100644 --- a/src/website.rb +++ b/src/website.rb @@ -56,13 +56,13 @@ class Website - def get_page_data(url, headers = {}) + 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, headers) + http.get(uri.request_uri, self.headers) end return response.body @@ -72,11 +72,17 @@ class Website 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. diff --git a/src/websites/dailymotion.rb b/src/websites/dailymotion.rb index f988ce6..26b3d4f 100644 --- a/src/websites/dailymotion.rb +++ b/src/websites/dailymotion.rb @@ -29,8 +29,8 @@ class Dailymotion < Website def get_video_url() # Disable the family filter if necessary. - headers = { 'Cookie' => 'family_filter=off' } - page_data = self.get_page_data(@url, headers) + self.headers['Cookie'] = 'family_filter=off' + page_data = self.get_page_data(@url) video_url = self.parse_video_url(page_data) return video_url -- 2.43.2