]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/commitdiff
Made the output filename the responsibility of the website subclass.
authorMichael Orlitzky <michael@orlitzky.com>
Sun, 7 Sep 2008 05:51:52 +0000 (01:51 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 7 Sep 2008 05:51:52 +0000 (01:51 -0400)
Added a factory method to the website class which creates the appropriate subclass based on the URL given to it.
Added a get_video_filename method to the website class.
Made two Website methods abstract via NotImplementedError, get_video_url and owns_url?.
Created a default implementation of get_video_filename in the Website class.
Made the site URL member data of the Website class.
Fixed a bug in the URL handling for Youtube.
Added output stating the filename for the download.
Replaced all code in the classes/tests to accommodate the other changes.

22 files changed:
bin/whatever-dl
doc/todo.txt
src/uri_utilities.rb
src/website.rb
src/websites/howcast.rb
src/websites/infoq.rb
src/websites/redtube.rb
src/websites/veoh.rb
src/websites/vimeo.rb
src/websites/youporn.rb
src/websites/youtube.rb
test/fixtures/veoh/details_data-v15795090Z6mZAbSq.xml [new file with mode: 0644]
test/infoq_remote_test.rb
test/infoq_test.rb
test/redtube_test.rb
test/uri_utilities_test.rb
test/veoh_test.rb
test/vimeo_test.rb
test/website_test.rb
test/youporn_remote_test.rb
test/youporn_test.rb
test/youtube_test.rb

index 47253f13d8bd00eace1ccca43e69592de5948411..a59e61b2d56a0c92ef5187315f1192456aa4e6fe 100755 (executable)
@@ -51,25 +51,15 @@ if (__FILE__ == $0) then
     Kernel.exit(EXIT_NO_URL)
   end
 
-  # Check the URL against each website's class.
-  # The class will know whether or not the URL
-  # "belongs" to its website.
-
-  site = nil
-  
-  Website.subclasses.each do |w|
-    if w.owns_url?(ARGV[0])
-      site = w.new()
-      break
-    end
-  end
+  # Factory method.
+  site = Website.create(ARGV[0])
 
   if site.nil?
     puts 'Invalid URL.'
     exit(EXIT_INVALID_URL)
   end
   
-  video_url = site.get_video_url(ARGV[0])
+  video_url = site.get_video_url()
 
   if video_url.nil?
     puts 'Error retrieving video URL.'
@@ -79,22 +69,8 @@ if (__FILE__ == $0) then
   video_uri = URI.parse(video_url)
   uu = UriUtilities.new()
   
-
-  # Here, we start out with a default file name and
-  # extension. If UriUtilities can parse a sane filename
-  # out of the URL, we'll use that. Otherwise, we fall
-  # back to the default.
-  outfile_name = 'default.ext'
-  
-  if not uu.get_filename(video_uri).nil?
-    outfile_name = uu.get_filename(video_uri)
-  else
-    puts "We couldn't determine the video's filename. Falling back to the default, #{outfile_name}."
-  end
-
-  
-  if File.exists?(outfile_name)
-    puts "Error: output file already exists. Please remove #{outfile_name}, and try again."
+  if File.exists?(site.get_video_filename())
+    puts "Error: output file already exists. Please remove #{site.get_video_filename()}, and try again."
     Kernel.exit(EXIT_OUTPUT_FILE_ALREADY_EXISTS)
   end
 
@@ -103,7 +79,9 @@ if (__FILE__ == $0) then
   # any (predictable) exceptions.
   begin
     puts "Fetching #{video_url}"
-    uu.download_with_progress_bar(video_uri, outfile_name)
+    puts "Saving as #{site.get_video_filename()}."
+    puts ""
+    uu.download_with_progress_bar(video_uri, site.get_video_filename())
   rescue Errno::ECONNREFUSED => e
     puts 'The connection to the server (to download the video file) was refused. Check your connection, and try again later.'
     Kernel.exit(EXIT_CONNECTION_REFUSED)
@@ -114,5 +92,8 @@ if (__FILE__ == $0) then
     Kernel.exit(EXIT_HTTP_ERROR)
   end
 
+  # Write an empty line at the end for aesthetic reasons.
+  puts ""
+  
   Kernel.exit(EXIT_SUCCESS)
 end
index e476e3b90811e942f6616709132c5490ee0238b2..7e758a97ccefa10aa36e9e55f38d9dae1196c104 100644 (file)
@@ -1,5 +1,2 @@
-1) Refactor the download file naming procedure so that it's the
-   responsibility of the website (sub)class.
-
-2) Possibly generalize the http retrieval process (get_page_data et
+1) Possibly generalize the http retrieval process (get_page_data et
    al) into a Website method.
index 61d57af63ea29d84b03ef70b65dfebcbf34e6e32..df09619fce99cf8adaaaaed4f3322eb9cba8a151 100644 (file)
@@ -25,13 +25,6 @@ require 'vendor/ruby-progressbar/progressbar'
 # for trouble.
 class UriUtilities
 
-  # Get the filename portion of a given URI.
-  # Return nil if there is no filename portion.
-  def get_filename(uri)
-    return uri.path.split('/').last
-  end
-
-  
   # Download the given URI object to <outfile_name>.
   # Should use the progress_proc parameter to show
   # a progress bar using the Ruby/ProgressBar library.
@@ -39,7 +32,7 @@ class UriUtilities
     # We wrap the whole thing in a begin/rescue so that
     # we can clean up afterwards in case of an error.
     begin
-      open(outfile_name, 'wb') do |outfile|
+      File.open(outfile_name, 'wb') do |outfile|
         pbar = nil
         uri.open(:content_length_proc => lambda {|content_length|
                    if content_length && (0 < content_length)                   
index 1239712a696a3b855ec3ad79472865a608e7f937..96290dee3de99ac7d00f34e14298110be82455a0 100644 (file)
 # 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 +37,53 @@ class Website
     @subclasses << subclass
   end
 
-  def self.subclasses
-    @subclasses
+  
+  public;
+
+  def initialize(url)
+    @url = url
   end
 
-  # This should be overridden in any class that wants
-  # to claim ownership of a URL.
+  
+  def self.create(url)
+    # Factory method returning an instance of
+    # the appropriate subclass.
+    
+    # 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)
+        return w.new(url)
+      end
+    end
+
+    # If nothing matched, we don't return an instance
+    # of anything.
+    return nil
+  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.    
+    return get_video_url().split('/').pop()
   end
+  
 end
index c07d8489f31efea5e3ad98bdffb021db11cf12b7..5cf490e2d1dbaf77f1d428e627251392b02152b8 100644 (file)
@@ -26,18 +26,24 @@ class Howcast < Website
     return url =~ VALID_HOWCAST_URL_REGEX
   end
 
-  
-  def get_video_url(url)
+
+  def parse_video_id()
     # This regex just pulls out the video id
     id_regex = /\/(\d+)-/
-    matches = id_regex.match(url)
+    matches = id_regex.match(@url)
 
     if matches.nil?
       raise StandardError.new('The URL is a valid Howcast URL, but does not match on the digit portion of the regex. Since the digit portion is a subset of the "valid" regex, this should never occur.')
     end
+    
+    return matches[1]
+  end
 
-    video_id = matches[1]
+  
+  def get_video_url()
+    video_id = parse_video_id()
+    
     return "http://media.howcast.com/system/videos/#{video_id}/#{video_id}.flv"
   end
-
+  
 end
index fe05562508e61ca7e73ec31d793376016c4ff267..dca55fa5527dbd2dd5ffbbd932c409c89a2e2e84 100644 (file)
@@ -28,8 +28,8 @@ class Infoq < Website
   end
 
   
-  def get_video_url(url)
-    page_data = self.get_page_data(url)
+  def get_video_url()
+    page_data = self.get_page_data(@url)
     video_url = self.parse_video_url(page_data)
     return video_url
   end
index 9ecdceccd7d1575a0167d3d84738a048b10fba3a..4a34bbc2dc98ddb58f1f694f50adbb5b1e7ddd49 100644 (file)
@@ -45,9 +45,9 @@ class Redtube < Website
   # The only public method. This calls the other parts
   # of the algorithm and, with any luck, we wind up with
   # the URL to the video.
-  def get_video_url(url)
+  def get_video_url()
     # First, parse the video ID out of the URL.
-    video_id = /\d+/.match(url)[0]
+    video_id = parse_video_id()
     
     padded_id = video_id.to_s.pad_left('0', 7)
 
@@ -64,6 +64,11 @@ class Redtube < Website
   protected
 
   VIDEO_FILE_EXTENSION = '.flv'
+
+  def parse_video_id()
+    return /\d+/.match(@url)[0]
+  end
+
   
   # Not sure what they're thinking with this one.
   def get_video_dir(video_id)
index e10d9853d7ccaf45f4ffb823d6e5f13cd2362cec..c961d688e213b9500db2ab20726e966b2d896894 100644 (file)
@@ -33,7 +33,7 @@ class Veoh < Website
   end
 
   
-  def get_video_url(url)
+  def get_video_url()
     # First, figure out the video id from the URL.
     # Then, use the video id to construct the video details URL.
     # Get the video details page, and parse the redirect
@@ -41,23 +41,25 @@ class Veoh < Website
     # id from the redirect, but for now we're going to rely
     # on our HTTP library to follow the redirect for us and
     # save us a step.
-    video_id = parse_video_id(url)
+    video_id = self.parse_video_id()
     details_url = "http://www.veoh.com/rest/video/#{video_id}/details"
     details_data = get_page_data(details_url)
     redirect_url = parse_redirect_url(details_data)
 
-    # Being slightly explicit about what we're doing here...
-    video_url = redirect_url
-    
-    return video_url
+    # We trust our HTTP library to do the right thing here.
+    return redirect_url
   end
 
+
+  def get_video_filename()
+    return (self.parse_video_id() + '.flv')
+  end
   
   protected;
 
-  def parse_video_id(url)
+  def parse_video_id()
     video_id_regex = /[[:alnum:]]+$/
-    matches = video_id_regex.match(url)
+    matches = video_id_regex.match(@url)
     video_id = matches[0] if not matches.nil?
     
     return video_id
@@ -77,7 +79,7 @@ class Veoh < Website
   
   def get_page_data(url)
     uri = URI.parse(url)
-    
+
     response = Net::HTTP.start(uri.host, uri.port) do |http|
       http.get(uri.path)
     end
index 3836df1ae0edff133dc70a599a2a1951353c68bd..56243fad3ecd5d0616db0d5fdd5aa605bf01d2b2 100644 (file)
@@ -34,13 +34,13 @@ class Vimeo < Website
   end
 
   
-  def get_video_url(url)
+  def get_video_url()
     # First, figure out the video id from the URL.
     # Then, use the video id to construct the video details URL.
     # Get the video details page, and parse the resulting XML for
     # the junk we need to construct the video URL. Note that the file
     # URL given in the XML is *not* valid.
-    video_id = parse_video_id(url)
+    video_id = self.parse_video_id()
     details_url = "http://www.vimeo.com/moogaloop/load/clip:#{video_id}/local"
     details_data = get_page_data(details_url)
 
@@ -55,20 +55,27 @@ class Vimeo < Website
     return video_url
   end
 
+
+  def get_video_filename()
+    # The default behavior is no good here, use the video
+    # id with an extension tacked onto it.
+    return (self.parse_video_id() + '.flv')
+  end
+
   
   protected;
 
-  def parse_video_id(url)
+  def parse_video_id()
     video_id = nil
     
     # First, try to get the video id from the end of the URL.
     video_id_regex = /\/(\d+)$/
-    matches = video_id_regex.match(url)
+    matches = video_id_regex.match(@url)
 
     if matches.nil?
       # If that didn't work, the URL must be of the clip_id= form.
       video_id_regex = /clip_id\=(\d+)/
-      matches = video_id_regex.match(url)
+      matches = video_id_regex.match(@url)
       video_id = matches[1] if not (matches.nil? || matches.length < 1)
     else
       video_id = matches[1] if not (matches.nil? || matches.length < 1)
index ef2fb28242d97a733bb50df4c11fc04b6142a9a4..07d7ca022cadeda6f5c5403cfbea96801c2e453e 100644 (file)
@@ -26,15 +26,15 @@ require 'uri'
 
 class Youporn < Website
 
-  VALID_YOUPORN_URL_REGEX = /^(http:\/\/)?(www\.)?youporn\.com\/watch\/(\d+)$/
+  VALID_YOUPORN_URL_REGEX = /^(http:\/\/)?(www\.)?youporn\.com\/watch\/(\d+)(\/.*)?$/
   
   def self.owns_url?(url)
     return url =~ VALID_YOUPORN_URL_REGEX
   end
 
   
-  def get_video_url(url)
-    page_data = self.get_page_data(url)
+  def get_video_url()
+    page_data = self.get_page_data(@url)
     video_url = self.parse_video_url(page_data)
     return video_url
   end
@@ -61,15 +61,16 @@ class Youporn < Website
     response = Net::HTTP.start(uri.host, uri.port) do |http|
       # Bypass the stupid age verification.
       form_data = 'user_choice=Enter'
-      http.post(uri.path, form_data, self.get_headers(url))
+      http.post(uri.path, form_data, self.get_headers())
     end
     
     return response.body
   end
 
   # Build the header hash from the URL we're requesting.
-  def get_headers(url)
-    headers = { 'Referer' => url }
+  def get_headers()
+    headers = { 'Referer' => @url,
+                'Content-Type' => 'application/x-www-form-urlencoded' }
   end
 
   
index 0d9bf398338538a3dfd8e128e851a98680ca46ae..b9d9aa6c5e7ec586bb69b4d851428d975a006156 100644 (file)
@@ -26,15 +26,15 @@ require 'uri'
 
 class Youtube < Website
 
-  VALID_YOUTUBE_URL_REGEX = /^(http:\/\/)?(www\.)?youtube\.com\/((watch\?v=)|(v\/))[[:alnum:]]+(\&.*)?\#?$/
+  VALID_YOUTUBE_URL_REGEX = /^(http:\/\/)?(www\.)?youtube\.com\/((watch\?v=)|(v\/))[a-z0-9_\-]+(\&.*)?\#?$/i
   
   def self.owns_url?(url)
     return url =~ VALID_YOUTUBE_URL_REGEX
   end
 
   
-  def get_video_url(url)
-    video_id = self.parse_video_id(url)
+  def get_video_url()
+    video_id = self.parse_video_id()
 
     # The video's URL (the "page data" URL) may be different from the
     # URL that was passed to the program. We support the /v/video_id
@@ -52,12 +52,17 @@ class Youtube < Website
     return video_url
   end
 
+
+  def get_video_filename()
+    return (self.parse_video_id() + '.flv')
+  end
+
   
   protected;
 
   # Get the video id from the URL. Should be relatively easy,
   # unless Youtube supports some URL formats of which I'm unaware.
-  def parse_video_id(url)
+  def parse_video_id()
     # Return nil if we get no matches below.
     video_id = nil
     
@@ -65,14 +70,14 @@ class Youtube < Website
     # them one at a time. The only tricky situation is when
     # parameters like "&hl=en" are tacked on to the end.
     # We'll call /watch?v=video_id the "first form."
-    first_form_video_id_regex = /v=([[:alnum:]]+)$/
-    first_form_matches = first_form_video_id_regex.match(url)
+    first_form_video_id_regex = /v=([0-9a-z_\-]+)/i
+    first_form_matches = first_form_video_id_regex.match(@url)
     return first_form_matches[1] if not (first_form_matches.nil? ||
                                          first_form_matches.length < 2)
 
     # First form didn't work? Try the second.
-    second_form_video_id_regex = /\/v\/([[:alnum:]]+)/
-    second_form_matches = second_form_video_id_regex.match(url)
+    second_form_video_id_regex = /\/v\/([0-9a-z_\-]+)/i
+    second_form_matches = second_form_video_id_regex.match(@url)
     video_id = second_form_matches[1] if not (second_form_matches.nil? ||
                                               second_form_matches.length < 2)
     
diff --git a/test/fixtures/veoh/details_data-v15795090Z6mZAbSq.xml b/test/fixtures/veoh/details_data-v15795090Z6mZAbSq.xml
new file mode 100644 (file)
index 0000000..9af1943
--- /dev/null
@@ -0,0 +1,285 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+       \r
+               \r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+       \r
+\r
+<videos offset="0" items="1" numItems="1" title="" guid="6520470f-6730-403a-8f80-a800fb4239f8"  >\r
+\r
+       \r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+       \r
+               \r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
\r
+       \r
+               \r
+               \r
+       \r
+       \r
+\r
+\r
+       \r
+               \r
+               \r
+       \r
+       \r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+       \r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+       \r
+       \r
+\r
+       \r
+       \r
+\r
+       \r
+       \r
+\r
+       \r
+       \r
+\r
+       \r
+       \r
+\r
+       \r
+       \r
+\r
+\r
+\r
+\r
+       \r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+<video videoId="15795090"\r
+       uploadToken=""\r
+       permalinkId="v15795090Z6mZAbSq"\r
+       extension=".mov"\r
+       length="1 min 33 sec"\r
+       size="18605143"\r
+       releaseHash=""\r
+       tags="health aetna magic johnson aging life@50 basketball"\r
+       tagsCommaSeparated=" health,  aetna,  magic johnson,  aging,  life@50,  basketball"\r
+       previewHash="3f8044ce2538155c55af5c036cd96ba82a83ddb5"\r
+       fullPreviewHashPath="http://content.veoh.com/flash/f/2/v15795090Z6mZAbSq/3f8044ce2538155c55af5c036cd96ba82a83ddb5.flv?ct=ff4914d2e699e8ad04a69102faaab6ce82b7704998890f69" \r
+       fullPreviewToken=""\r
+       previewHashLow="52d2992ab57d6a53044cd2e0675d386dcd477003"\r
+       fullPreviewHashLowPath="http://content.veoh.com/flash/p/2/v15795090Z6mZAbSq/3f8044ce2538155c55af5c036cd96ba82a83ddb5.flv?ct=2de922866d56e92db1797cecff28f7c13f192be0a1e1e014" \r
+       fullPreviewTokenLow=""\r
+       highResImage="media-v15795090Z6mZAbSq1220545508.jpeg"\r
+       fullHighResImagePath="http://ll-images.veoh.com/image.out?imageId=media-v15795090Z6mZAbSq1220545508.jpeg"\r
+       medResImage="media-v15795090Z6mZAbSq1220545508.jpeg"\r
+       fullMedResImagePath="http://ll-images.veoh.com/image.out?imageId=media-v15795090Z6mZAbSq1220545508.jpeg"\r
+       username="MJenterprises"\r
+       views="546"\r
+       rating="5.0"\r
+       numRatingVotes="1"\r
+       description="Magic Johnson discusses turning 50 years old and living a healthy lifestyle."\r
+       title="Magic Johnson On Healthy Living"\r
+       dateAdded="2008-08-29 06:44:00"\r
+       age="1 week ago"\r
+       primaryCollectionPermalink=""\r
+       primaryCollectionTitle=""\r
+       primaryCollectionThumb=""\r
+       allowEmbedding="true"\r
+       premium="false"\r
+       paidContent="false" \r
+       \r
+       \r
+       portable="false" \r
+       ipodLink="" \r
+       showStreamingAd="1"\r
+       showCompanionAd="false"\r
+       \r
+       \r
+       houseAdDetailsUrl=""\r
+       houseAdPlacement="-1"\r
+       geoRestrictions=""\r
+       \r
+       \r
+       \r
+       \r
+       \r
+                                               \r
+       flashHash=""\r
+       flashExtension=""\r
+       flashPieceHashFile="http://p-cache.veoh.com/cache/veoh/.veoh"\r
+       flashUrlRoot="http://p-cache.veoh.com/cache"\r
+                                               \r
+       originalHash="d569b9d5efdd2d5efa541b2f7d36d2c6d693aaa4"\r
+       origExtension=".mov"    \r
+       originalPieceHashFile="http://p-cache.veoh.com/cache/veoh/d569b9d5efdd2d5efa541b2f7d36d2c6d693aaa4.veoh"\r
+       originalUrlRoot="http://p-cache.veoh.com/cache"\r
+       \r
+       \r
+               contentRating="1"\r
+       \r
+\r
+       \r
+       \r
+       \r
+       licenseId=""\r
+       licenseName=""\r
+       licenseType=""\r
+       licenseExpires=""\r
+       licensePrice=""\r
+       licenseDuration=""\r
+       licenseTimeUnits=""\r
+       \r
+       \r
+       \r
+       \r
+               aowPublisherName=""\r
+               aowPermalink=""\r
+               aowEmbedCode=""\r
+               aowGuideVideoId=""\r
+       \r
+       \r
+       isExternalMedia="false"\r
+       isDotComPlayable="true"\r
+       isTvPlayable="true"\r
+>\r
+       \r
+       \r
+       \r
+       \r
+       \r
+       \r
+               <categories>\r
+                       \r
+                               <category></category>\r
+                       \r
+                               <category></category>\r
+                       \r
+                               <category></category>\r
+                       \r
+               </categories>\r
+       \r
+\r
+       \r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+</video>\r
+\r
+       \r
+</videos>\r
+       \r
+       \r
+       \r
+       \r
+\r
index 35ecc15af3e5952ad059041a1531e3b93c49ac27..e66fad9e754a59f3ad16a5b8ab5f177d42c3c933 100644 (file)
@@ -25,15 +25,18 @@ require 'src/websites/infoq'
 class InfoqRemoteTest < Test::Unit::TestCase
   
   def test_get_page_data
-    iq = Infoq.new()
+    iq = Infoq.new('http://www.infoq.com/interviews/jim-weirich-discusses-rake')
+    expected_result = 'http://flv.thruhere.net/interviews/JimWeirich.flv'
+    actual_result = iq.get_video_url()
+    assert_equal(expected_result, actual_result)
+  end
 
-    # We can't rely on the fixture here, because Infoq might
-    # change their page layout. Instead, check that we can actually
-    # find the base64 regex (containing the FLV URL).
-    page_data = iq.send('get_page_data', 'http://www.infoq.com/interviews/jim-weirich-discusses-rake')
-    
-    test_result = iq.get_video_url(page_data)
-    assert_equal('http://flv.thruhere.net/interviews/JimWeirich.flv', test_result)
+  
+  def test_superclass_get_filename_works
+    iq = Infoq.new('http://www.infoq.com/interviews/jim-weirich-discusses-rake')
+    expected_result = 'JimWeirich.flv'
+    actual_result = iq.get_video_filename()
+    assert_equal(expected_result, actual_result)
   end
-    
+  
 end
index e34f501c285d0af0f0a54645d36012a5297d3763..d6cdf44054bf0c655f8e5d83c8d2374b1ba483f0 100644 (file)
@@ -30,7 +30,7 @@ class InfoqTest < Test::Unit::TestCase
 
   
   def test_parse_video_url
-    iq = Infoq.new()
+    iq = Infoq.new(nil)
     
     page_data = nil
     
index b6cae0f5f3fa6153b3b9361b01a4f0e8d9d4e2cd..9d6c709120fc0a3f46b5a948741629dffa2d1ee7 100644 (file)
@@ -47,10 +47,18 @@ class RedtubeTest < Test::Unit::TestCase
 
   
   def test_get_video_url
-    rt = Redtube.new()
-
-    test_result = rt.get_video_url('http://www.redtube.com/6807')
-    assert_equal("http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/0000006/X57OBH08G.flv", test_result)
+    rt = Redtube.new('http://www.redtube.com/6807')
+    expected_result = 'http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/0000006/X57OBH08G.flv'
+    actual_result = rt.get_video_url()
+    assert_equal(expected_result, actual_result)
   end
 
+  
+  def test_get_video_filename
+    rt = Redtube.new('http://www.redtube.com/6807')
+    # I don't know where they get these filenames from, but whatever.
+    expected_result = 'X57OBH08G.flv'
+    actual_result = rt.get_video_filename()
+    assert_equal(expected_result, actual_result)
+  end
 end
index ad4d4f3cbc641a099c830b7f035d26647ad1aee3..60a266b6bc8ac4894520de171cf744c3c977435c 100644 (file)
@@ -21,30 +21,6 @@ require 'src/uri_utilities.rb'
 
 class UriUtilitiesTest < Test::Unit::TestCase
 
-  def test_filename
-    uu = UriUtilities.new()
-    example_uri = 'http://www.example.com/whatever.avi'
-    uri = URI.parse(example_uri)
-    assert_equal('whatever.avi', uu.get_filename(uri))
-  end
-
-  
-  def test_no_filename_results_in_nil
-    uu = UriUtilities.new()
-    example_uri = 'http://www.example.com'
-    uri = URI.parse(example_uri)
-    assert(uu.get_filename(uri).nil?)    
-  end
-
-  
-  def test_no_filename_with_trailing_slash_results_in_nil
-    uu = UriUtilities.new()
-    example_uri = 'http://www.example.com/'
-    uri = URI.parse(example_uri)
-    assert(uu.get_filename(uri).nil?)    
-  end
-
-
   def test_system_call_exception_on_connection_refused
     uu = UriUtilities.new()
 
index 1a53e431b1481d555e7151041189c17f03935613..ad37267fef03f595275e0844f212554027d40be1 100644 (file)
@@ -22,14 +22,21 @@ require 'src/websites/veoh'
 class VeohTest < Test::Unit::TestCase
 
   def test_parse_video_id
-    v = Veoh.new
-    video_id = v.send('parse_video_id', 'http://www.veoh.com/videos/v801204yMZ8BWcC')
+    v = Veoh.new('http://www.veoh.com/videos/v801204yMZ8BWcC')
+    video_id = v.send('parse_video_id')
     assert_equal('v801204yMZ8BWcC', video_id)
   end
 
+
+  def test_parse_second_video_id
+    v = Veoh.new('http://www.veoh.com/videos/v15795090Z6mZAbSq')
+    video_id = v.send('parse_video_id')
+    assert_equal('v15795090Z6mZAbSq', video_id)
+  end
+
   
   def test_parse_redirect_url
-    v = Veoh.new
+    v = Veoh.new(nil)
 
     details_data = nil
 
@@ -42,10 +49,28 @@ class VeohTest < Test::Unit::TestCase
     
     assert_equal(expected_result, actual_result)
   end
+
+
+  def test_second_parse_redirect_url
+    v = Veoh.new(nil)
+
+    details_data = nil
+
+    File.open('test/fixtures/veoh/details_data-v15795090Z6mZAbSq.xml') do |f|
+      details_data = f.read
+    end
+
+    actual_result = v.send('parse_redirect_url', details_data)
+    expected_result = 'http://content.veoh.com/flash/f/2/v15795090Z6mZAbSq/3f8044ce2538155c55af5c036cd96ba82a83ddb5.flv?ct=ff4914d2e699e8ad04a69102faaab6ce82b7704998890f69'
+    
+    assert_equal(expected_result, actual_result)
+  end
+
   
   
   def test_owns_veoh_urls
     assert(Veoh.owns_url?('http://www.veoh.com/videos/v801204yMZ8BWcC'))
+    assert(Veoh.owns_url?('http://www.veoh.com/videos/v15795090Z6mZAbSq'))
   end
     
 end
index 6578b8c10dc951e6740d0c1ae8bbfe25b966ceab..203afae47a74ef91a33c6a04e66d39e37fe06834 100644 (file)
@@ -26,25 +26,25 @@ require 'src/websites/vimeo'
 class VimeoTest < Test::Unit::TestCase
 
   def test_parse_standard_video_id
-    v = Vimeo.new
+    v = Vimeo.new('http://www.vimeo.com/1561578')
     
     # First form, with the id at the end.
-    video_id = v.send('parse_video_id', 'http://www.vimeo.com/1561578')
+    video_id = v.send('parse_video_id')
     assert_equal('1561578', video_id)
   end
 
 
   def test_parse_swf_video_id
-    v = Vimeo.new
+    v = Vimeo.new('http://www.vimeo.com/moogaloop.swf?clip_id=1561578&server=www.vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1')
     
     # Second, clip_id= form.
-    video_id = v.send('parse_video_id', 'http://www.vimeo.com/moogaloop.swf?clip_id=1561578&server=www.vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1')
+    video_id = v.send('parse_video_id')
     assert_equal('1561578', video_id)
   end
 
   
   def test_parse_request_signature
-    v = Vimeo.new
+    v = Vimeo.new(nil)
 
     details_data = nil
 
@@ -60,7 +60,7 @@ class VimeoTest < Test::Unit::TestCase
   
 
   def test_parse_request_signature_expires
-    v = Vimeo.new
+    v = Vimeo.new(nil)
 
     details_data = nil
 
@@ -76,7 +76,7 @@ class VimeoTest < Test::Unit::TestCase
 
 
   def test_parse_quality
-    v = Vimeo.new
+    v = Vimeo.new(nil)
 
     details_data = nil
 
index e140676683502e3484117e1e6e06ff6707858d63..ff5f0551dd8b2e7a78cf4694e5b4deff6943ef8d 100644 (file)
@@ -28,12 +28,31 @@ end
 
 class WebsiteTest < Test::Unit::TestCase
 
-  def test_doesnt_own_misc_urls
-    Website.subclasses.each do |w|
-      assert(!w.owns_url?('6807'))
-      assert(!w.owns_url?('www'))
-      assert(!w.owns_url?('http'))
+  def test_nobody_owns_misc_urls
+    assert_nil(Website.create('6807'))
+    assert_nil(Website.create('www'))
+    assert_nil(Website.create('http'))
+  end
+
+
+  def test_owns_url_must_be_implemented
+    assert_raise NotImplementedError do
+      Website.owns_url?('http://www.example.com/')
+    end
+  end
+
+  
+  def test_get_video_url_must_be_implemented
+    w = Website.new(nil)
+    assert_raise NotImplementedError do
+      w.get_video_url()
     end
   end
 
+
+  def test_youtube_url_returns_youtube_instance
+    yt = Website.create('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir')
+    assert_instance_of(Youtube, yt)
+  end
+  
 end
index 6dd26d789f6a1f72d9051188bb8bc6d0a1b263eb..79bcb3726ce496a3b55d4429059782f05ad43362 100644 (file)
@@ -25,15 +25,10 @@ require 'src/websites/youporn'
 class YoupornRemoteTest < Test::Unit::TestCase
   
   def test_get_page_data
-    yp = Youporn.new()
-
-    # We can't rely on the fixture here, because Youporn might
-    # change their page layout. Instead, check that we can actually
-    # find the FLV URL on this page.
-    page_data = yp.send('get_page_data', 'http://www.youporn.com/watch/65778')
-    
-    test_result = yp.get_video_url(page_data)
-    assert_equal('http://download.youporn.com/download/112911/flv/65778_moaning_mom_fucked_at_night.flv', test_result)
+    yp = Youporn.new('http://www.youporn.com/watch/65778')
+    expected_result = 'http://download.youporn.com/download/112911/flv/65778_moaning_mom_fucked_at_night.flv'
+    actual_result = yp.get_video_url()
+    assert_equal(expected_result, actual_result)
   end
     
 end
index d06743e4e569fe4f5ee913962d99e36daa48806b..24d86431124755cb54085294adbada1396d9222b 100644 (file)
@@ -24,8 +24,14 @@ require 'src/websites/youporn'
 
 class YoupornTest < Test::Unit::TestCase
 
+  def test_owns_youporn_urls
+    assert(Youporn.owns_url?('http://www.youporn.com/watch/65778'))
+    assert(Youporn.owns_url?('http://www.youporn.com/watch/61637/amateursoffie-fuckingagain-andtakinglo-ad/'))
+  end
+
+  
   def test_parse_video_url
-    yp = Youporn.new()
+    yp = Youporn.new(nil)
     
     page_data = nil
     
index b5de6a8ca348babc7bc1a1850bed09f7d3b58089..cbc03755d8ca2bd40849de05b14fd7ee3dbaf119 100644 (file)
@@ -31,6 +31,8 @@ class YoutubeTest < Test::Unit::TestCase
     assert(Youtube.owns_url?('http://www.youtube.com/watch?v=K9iDMcmm0tE'))
     assert(Youtube.owns_url?('http://www.youtube.com/watch?v=K9iDMcmm0tE#'))
     assert(Youtube.owns_url?('http://www.youtube.com/v/K9iDMcmm0tE'))
+    assert(Youtube.owns_url?('http://www.youtube.com/watch?v=83-hlYMH1XE'))
+    assert(Youtube.owns_url?('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir'))
   end
 
 
@@ -43,23 +45,23 @@ class YoutubeTest < Test::Unit::TestCase
 
 
   def test_parse_video_id
-    yt = Youtube.new()
+    yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4')
     expected_result = 'SudixyugiX4'
-    actual_result = yt.send('parse_video_id', 'http://www.youtube.com/watch?v=SudixyugiX4')
+    actual_result = yt.send('parse_video_id')
     assert_equal(expected_result, actual_result)
   end
 
 
   def test_parse_video_id_again
-    yt = Youtube.new()
+    yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE')
     expected_result = 'K9iDMcmm0tE'
-    actual_result = yt.send('parse_video_id', 'http://www.youtube.com/watch?v=K9iDMcmm0tE')
+    actual_result = yt.send('parse_video_id')
     assert_equal(expected_result, actual_result)
   end
 
-  
+
   def test_parse_t_parameter
-    yt = Youtube.new()
+    yt = Youtube.new(nil)
     
     page_data = nil
     
@@ -77,7 +79,7 @@ class YoutubeTest < Test::Unit::TestCase
     # This was failing once because my regex missed a hyphen.
     # I modified the regex to match anything between the pair
     # of quotes, so it should now catch the hyphen and underscore.
-    yt = Youtube.new()
+    yt = Youtube.new(nil)
     
     page_data = nil
     
@@ -89,5 +91,39 @@ class YoutubeTest < Test::Unit::TestCase
     actual_result = yt.send('parse_t_parameter', page_data)
     assert_equal(expected_result, actual_result)
   end
+
+
+  def test_get_video_filename
+    yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4')
+    expected_result = 'SudixyugiX4.flv'
+    actual_result = yt.get_video_filename()
+    assert_equal(expected_result, actual_result)
+  end
+
+  
+  def test_get_video_filename_again
+    yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE')
+    expected_result = 'K9iDMcmm0tE.flv'
+    actual_result = yt.get_video_filename()
+    assert_equal(expected_result, actual_result)
+  end
+
+  
+  def test_get_troublesome_video_filename_first_form
+    # This non-alphanumeric video id was causing
+    # get_video_filename to barf.
+    yt = Youtube.new('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir')
+    expected_result = '83-hlYMH1XE.flv'
+    actual_result = yt.get_video_filename()
+    assert_equal(expected_result, actual_result)
+  end
+
+
+  def test_get_troublesome_video_filename_second_form
+    yt = Youtube.new('http://www.youtube.com/v/83-hlYMH1XE&feature=dir')
+    expected_result = '83-hlYMH1XE.flv'
+    actual_result = yt.get_video_filename()
+    assert_equal(expected_result, actual_result)
+  end
   
 end