From: Michael Orlitzky Date: Sun, 7 Sep 2008 05:51:52 +0000 (-0400) Subject: Made the output filename the responsibility of the website subclass. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=83e06f83d8274cb32a406739839d56e759664b09;p=dead%2Fwhatever-dl.git Made the output filename the responsibility of the website subclass. 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. --- diff --git a/bin/whatever-dl b/bin/whatever-dl index 47253f1..a59e61b 100755 --- a/bin/whatever-dl +++ b/bin/whatever-dl @@ -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 diff --git a/doc/todo.txt b/doc/todo.txt index e476e3b..7e758a9 100644 --- a/doc/todo.txt +++ b/doc/todo.txt @@ -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. diff --git a/src/uri_utilities.rb b/src/uri_utilities.rb index 61d57af..df09619 100644 --- a/src/uri_utilities.rb +++ b/src/uri_utilities.rb @@ -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 . # 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) diff --git a/src/website.rb b/src/website.rb index 1239712..96290de 100644 --- a/src/website.rb +++ b/src/website.rb @@ -20,6 +20,12 @@ # 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 diff --git a/src/websites/howcast.rb b/src/websites/howcast.rb index c07d848..5cf490e 100644 --- a/src/websites/howcast.rb +++ b/src/websites/howcast.rb @@ -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 diff --git a/src/websites/infoq.rb b/src/websites/infoq.rb index fe05562..dca55fa 100644 --- a/src/websites/infoq.rb +++ b/src/websites/infoq.rb @@ -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 diff --git a/src/websites/redtube.rb b/src/websites/redtube.rb index 9ecdcec..4a34bbc 100644 --- a/src/websites/redtube.rb +++ b/src/websites/redtube.rb @@ -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) diff --git a/src/websites/veoh.rb b/src/websites/veoh.rb index e10d985..c961d68 100644 --- a/src/websites/veoh.rb +++ b/src/websites/veoh.rb @@ -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 diff --git a/src/websites/vimeo.rb b/src/websites/vimeo.rb index 3836df1..56243fa 100644 --- a/src/websites/vimeo.rb +++ b/src/websites/vimeo.rb @@ -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) diff --git a/src/websites/youporn.rb b/src/websites/youporn.rb index ef2fb28..07d7ca0 100644 --- a/src/websites/youporn.rb +++ b/src/websites/youporn.rb @@ -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 diff --git a/src/websites/youtube.rb b/src/websites/youtube.rb index 0d9bf39..b9d9aa6 100644 --- a/src/websites/youtube.rb +++ b/src/websites/youtube.rb @@ -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 index 0000000..9af1943 --- /dev/null +++ b/test/fixtures/veoh/details_data-v15795090Z6mZAbSq.xml @@ -0,0 +1,285 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/infoq_remote_test.rb b/test/infoq_remote_test.rb index 35ecc15..e66fad9 100644 --- a/test/infoq_remote_test.rb +++ b/test/infoq_remote_test.rb @@ -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 diff --git a/test/infoq_test.rb b/test/infoq_test.rb index e34f501..d6cdf44 100644 --- a/test/infoq_test.rb +++ b/test/infoq_test.rb @@ -30,7 +30,7 @@ class InfoqTest < Test::Unit::TestCase def test_parse_video_url - iq = Infoq.new() + iq = Infoq.new(nil) page_data = nil diff --git a/test/redtube_test.rb b/test/redtube_test.rb index b6cae0f..9d6c709 100644 --- a/test/redtube_test.rb +++ b/test/redtube_test.rb @@ -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 diff --git a/test/uri_utilities_test.rb b/test/uri_utilities_test.rb index ad4d4f3..60a266b 100644 --- a/test/uri_utilities_test.rb +++ b/test/uri_utilities_test.rb @@ -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() diff --git a/test/veoh_test.rb b/test/veoh_test.rb index 1a53e43..ad37267 100644 --- a/test/veoh_test.rb +++ b/test/veoh_test.rb @@ -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 diff --git a/test/vimeo_test.rb b/test/vimeo_test.rb index 6578b8c..203afae 100644 --- a/test/vimeo_test.rb +++ b/test/vimeo_test.rb @@ -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 diff --git a/test/website_test.rb b/test/website_test.rb index e140676..ff5f055 100644 --- a/test/website_test.rb +++ b/test/website_test.rb @@ -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 diff --git a/test/youporn_remote_test.rb b/test/youporn_remote_test.rb index 6dd26d7..79bcb37 100644 --- a/test/youporn_remote_test.rb +++ b/test/youporn_remote_test.rb @@ -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 diff --git a/test/youporn_test.rb b/test/youporn_test.rb index d06743e..24d8643 100644 --- a/test/youporn_test.rb +++ b/test/youporn_test.rb @@ -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 diff --git a/test/youtube_test.rb b/test/youtube_test.rb index b5de6a8..cbc0375 100644 --- a/test/youtube_test.rb +++ b/test/youtube_test.rb @@ -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