From 2c835ed7a247ed5639277bc9674b848722ad998d Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 5 Sep 2008 23:44:59 -0400 Subject: [PATCH] Fixed a bug in the Youtube class' parsing of the "t" parameter. Modified the "t" parsing regex to catch non-alphanumeric characters, which from experience I now know are possible. Added a test/fixture to expose the bug. The whatever-dl executable now outputs "Fetching #{video_url}" before actually fetching the file. --- bin/whatever-dl | 1 + src/websites/youtube.rb | 4 +- test/fixtures/youtube/K9iDMcmm0tE.html | 2007 ++++++++++++++++++++++++ test/youtube_test.rb | 29 + 4 files changed, 2039 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/youtube/K9iDMcmm0tE.html diff --git a/bin/whatever-dl b/bin/whatever-dl index 970e0e0..47253f1 100755 --- a/bin/whatever-dl +++ b/bin/whatever-dl @@ -102,6 +102,7 @@ if (__FILE__ == $0) then # Attempt to download the file, and rescue and report # any (predictable) exceptions. begin + puts "Fetching #{video_url}" uu.download_with_progress_bar(video_uri, outfile_name) rescue Errno::ECONNREFUSED => e puts 'The connection to the server (to download the video file) was refused. Check your connection, and try again later.' diff --git a/src/websites/youtube.rb b/src/websites/youtube.rb index 6766af2..0d9bf39 100644 --- a/src/websites/youtube.rb +++ b/src/websites/youtube.rb @@ -26,7 +26,7 @@ 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\/))[[:alnum:]]+(\&.*)?\#?$/ def self.owns_url?(url) return url =~ VALID_YOUTUBE_URL_REGEX @@ -86,7 +86,7 @@ class Youtube < Website def parse_t_parameter(page_data) t_parameter = nil - t_parameter_regex = /\"t\"\:[[:space:]]\"([[:alnum:]]+)\"/ + t_parameter_regex = /\"t\"\:[[:space:]]\"([^\"]+?)\"/ matches = t_parameter_regex.match(page_data) t_parameter = matches[1] if not (matches.nil? || matches.length < 2) diff --git a/test/fixtures/youtube/K9iDMcmm0tE.html b/test/fixtures/youtube/K9iDMcmm0tE.html new file mode 100644 index 0000000..d30d71d --- /dev/null +++ b/test/fixtures/youtube/K9iDMcmm0tE.html @@ -0,0 +1,2007 @@ + + + + + + + + + + YouTube - Icon The Mic King - Blind Folded Freestyle + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ + +
+ +
+ + + +
+ +
+ +
+
Loading...
+
+ +
+
+
+ + + + + + + + +
+
Icon The Mic King - Blind Folded Freestyle
+
+ +
+
+
+ + +
+
+ + + + + +
+
+
Rate:
+ + + + +
4.0
8 ratings
Sign in to rate
+ +
+ +
+
+ Views: + 1,773 +
+ + + +
+
+ +
+
+ +
+ + + + + + + +
+ + Share + Share + + + + Favorite + Favorite + + + + Add to Playlists + Playlists + + + + Report video as inappropriate + Flag + +
+
+
+
+ +
This video will appear on your blog shortly.
+
Loading...
+
Loading...
+
Loading...
+
Loading...
+
Thank you for sharing this video!
+ +
+
+
+
+ + + + +
+
+
Saving...
+
+
+ + + + +
+
+
+
+ +
 
+ This video has been added to your favorites.

+
+
+ +
 
+ This video has been removed from your favorites.

+
+
+ +
+
+
+ Want to add to Favorites?
+ Sign in to YouTube now! +
+ + Sign in with your Google Account! + + +
+ +
+
+
+
Loading...
+
The video has been added to your playlist.
+
+
+
+ Want to add to Playlists?
+ Sign in to YouTube now! +
+ + Sign in with your Google Account! + + +
+ +
+
+
+
Loading...
+
+
+
+
+ Want to flag a video?
+ Sign in to YouTube now! +
+ + Sign in with your Google Account! + + +
+ +
+
+
+
+ + + + + +
+ + + + + +
+ Commentary + + Statistics & Data +
+
+
+
+ Video Responses: 0 + Text Comments: 11 +
+ +
+ + Video Responses (0) +
+ This video has no Responses. Be the first to Post a Video Response. +
+
+ +
+
+ Sign in to post a Comment +
+ Text Comments (11) + +
+
+
+
+
+ Show: + + + Help + + Change this to see only comments above a certain value.
Change the value of a comment by clicking on a thumb. +
+
+
+
+ +
+ +
+ +
+ + + + + + + +
+ + +
+
+ MainOffenderKZ + (1 month ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ but i dont know that mc, he's kickin the shit"! +
+
+
+ + +
+ + + +
+ + +
+
+ MainOffenderKZ + (1 month ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ this shit is FIRE! +
+
+
+ + +
+ + + +
+ + +
+
+ Backawp + (3 months ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ haha i was there, it was completly insane haha, ''i dont know what that is, but thats a fucking 40!'' ''im a black guy, now im stealing your fuckign rolex!'' +
+
+
+ + +
+ + + +
+ + +
+
+ wino8 + (5 months ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ wow that mediocre shitty emcee had the balls to diss supernatural. lmao +
+
+
+ + +
+ + + +
+ +
+ +
+
+ bacon03 + (3 days ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ iCON is a rap gawd. +
+
+
+ +
+ +
+ + + +
+ + +
+
+ fraicheur0013 + (6 months ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ Was is in Montreal? +
+
+
+ + +
+ + + +
+ +
+ +
+
+ beekeeper514 + (6 months ago) + Show + Hide +
+ + Marked as spam +
+ Reply + + + + + +
+ +
+
+
+
+ yes +
+
+
+ +
+ +
+ + + +
+ +
+ +
+
+ fraicheur0013 + (6 months ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ I was there :)
Thanx for posting it coz this was ill +
+
+
+ +
+ +
+ + + +
+ + +
+
+ BringDaRuckusCZ + (6 months ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ iCON on the Hip Hop Kemp '07 = Best show i ever seen!! +
+
+
+ + +
+ + + +
+ + +
+
+ bigbodythugg + (7 months ago) + Show + Hide +
+
+ + 0 + + Poor comment + Good comment + +
+ + + Marked as spam +
+ Reply + + + + + + +
+ +
+
+
+
+ If i c u, ill put yo head in the toilet, scrapp yo brushes breaking your teeth
And make u eat yo stupid nerds glasses with your comic books +
+
+
+ + +
+ + + +
+
+ +Next +
+ +
+ Pages: + 1 + 2 +  ...  + +
+
+
+ + +
+ + + +
+

Would you like to comment?

+
+ Join YouTube for a free account, or + sign in if you are already a member. +
+
+ +
+
+
+ + +
+
+ + + + + + + + + + + +
Views: 1,773Ratings: 8
Responses: 0Comments: 11 + Favorited: 15 times +
+ + + + + + +
+
+ Honors for this video +
+
+ + Loading... +
+
+
+
+ Sites linking to this video +
+
+ + Loading... +
+
+
+
+ + + +
+
+
+ +
+ +
+ + +
+
+
+ Channel Icon +
+ + +
+ From: beekeeper514
+ Joined: 2 years ago
+ Videos: 21 +
+
+
+
+ + + +
+
+ +
+
+ Added: + + (More info) +
+ +
+ Icon The Mic King blind folds him self and gets... +
+
+ +
+
+ Added: + + (Less info) +
+ +
+ Icon The Mic King blind folds him self and gets a girl to pass him objects from the crowed and tells you what they are and raps about them +
+ +
+ Category:  + Music +
+ +
+
+ Tags:  +
+
+ Icon  + The  + Mic  + King  + Blind  + Folded  + Freestyle  + Hip  + Hop  + Rap  + Montreal  +
+
+
+ + +
+
+ + +
+
+
+ + +
+
+ +
+
+   +
+
+
+
+ +
+
+
+
+ +
Loading...
+ +
+ + + +
+ + + +
+ + + +
+ + + + + +
+
+ More From: beekeeper514 +
+
Loading...
+
+
+ +
+ + +
+
+
+ + Play All + + + Stop Autoplaying + + | + + Play Next + +
+ + + QuickList (0) + + +
+
+ + + + + + + + + + +
+
+ +
+ + Clear | + Save + +
+ +
+ +
+ + + + +
+ Promoted Videos +
+
+ + + +
+ +
Life Slices - Haunted Bedroom
+ + + + + 00:49 + + +
+ +
+
+ + + +
+ +
Malibu U Promo Reel (High Res)
+ + + + + 01:06 + +
+ EgoTV
+
+
+ +
+
+ + + +
+ +
Diane Von Furstenberg & Frederic Fekkai - Fashion Week 2008
+ + + + + 02:09 + + +
+ +
+
+ + + +
+ +
Cock-Eating Fish | Wicked Pissers Ep. 4
+ + + + + 03:45 + + +
+ +
+
+
+
+ + + +
+ + + + +
+ + + + +
+ + + + + + + + \ No newline at end of file diff --git a/test/youtube_test.rb b/test/youtube_test.rb index 08e8030..b5de6a8 100644 --- a/test/youtube_test.rb +++ b/test/youtube_test.rb @@ -28,6 +28,9 @@ class YoutubeTest < Test::Unit::TestCase assert(Youtube.owns_url?('http://www.youtube.com/v/SudixyugiX4')) assert(Youtube.owns_url?('http://www.youtube.com/v/SudixyugiX4&hl=en')) assert(Youtube.owns_url?('http://youtube.com/v/SudixyugiX4&hl=en')) + 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')) end @@ -46,6 +49,14 @@ class YoutubeTest < Test::Unit::TestCase assert_equal(expected_result, actual_result) end + + def test_parse_video_id_again + yt = Youtube.new() + expected_result = 'K9iDMcmm0tE' + actual_result = yt.send('parse_video_id', 'http://www.youtube.com/watch?v=K9iDMcmm0tE') + assert_equal(expected_result, actual_result) + end + def test_parse_t_parameter yt = Youtube.new() @@ -60,5 +71,23 @@ class YoutubeTest < Test::Unit::TestCase actual_result = yt.send('parse_t_parameter', page_data) assert_equal(expected_result, actual_result) end + + + def test_parse_t_parameter_again + # 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() + page_data = nil + + File.open('test/fixtures/youtube/K9iDMcmm0tE.html') do |f| + page_data = f.read + end + + expected_result = 'O_EgsToPDskJsXVvAXpAct1zug-lBJBz' + actual_result = yt.send('parse_t_parameter', page_data) + assert_equal(expected_result, actual_result) + end + end -- 2.44.2