diff --git a/lib/youtube_titles.rb b/lib/youtube_titles.rb index 1ebe240ec..7262da5fe 100644 --- a/lib/youtube_titles.rb +++ b/lib/youtube_titles.rb @@ -9,14 +9,16 @@ module YoutubeTitles end title || I18n.t('application.helper.video_title.unknown') end + def get_youtube_title text + youtube_match = text.enum_for(:scan, YOUTUBE_ID_REGEX).map { Regexp.last_match } + return if youtube_match.empty? + self.youtube_titles ||= {} - youtube_match = text.match(YOUTUBE_ID_REGEX) - return unless youtube_match - video_id = youtube_match[1] - unless self.youtube_titles[video_id] - self.youtube_titles[video_id] = CGI::escape(youtube_title_for(video_id)) + youtube_match.each do |match_data| + self.youtube_titles[match_data[1]] = CGI::escape(youtube_title_for(match_data[1])) end end - YOUTUBE_ID_REGEX = /youtube\.com.*?v=([A-Za-z0-9_\\\-]+)/ unless defined? YOUTUBE_ID_REGEX + + YOUTUBE_ID_REGEX = /(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})/im unless defined? YOUTUBE_ID_REGEX end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index e7433a9e9..4a2ac74ce 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -139,13 +139,13 @@ describe ApplicationHelper do end it "recognizes multiple links of different types" do - message = "http:// Hello World, this is for www.joindiaspora.com and not for http://www.google.com though their Youtube service is neat, take http://www.youtube.com/watch?v=foobar or www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related It is a good idea we finally have youtube, so enjoy this video http://www.youtube.com/watch?v=rickrolld" + message = "http:// Hello World, this is for www.joindiaspora.com and not for http://www.google.com though their Youtube service is neat, take http://www.youtube.com/watch?v=foobar----- or www.youtube.com/watch?foo=bar&v=BARFOO-----&whatever=related It is a good idea we finally have youtube, so enjoy this video http://www.youtube.com/watch?v=rickrolld--" res = markdownify(message) res.should =~ /a target=\"_blank\" href=\"http:\/\/www.joindiaspora.com\"/ res.should =~ /a target=\"_blank\" href=\"http:\/\/www.google.com\"/ - res.should =~ /data-video-id="foobar"/ - res.should =~ /data-video-id="BARFOO"/ - res.should =~ /data-video-id="rickrolld"/ + res.should =~ /data-video-id="foobar-----"/ + res.should =~ /data-video-id="BARFOO-----"/ + res.should =~ /data-video-id="rickrolld--"/ end it "should recognize basic ftp links" do diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index bf754590a..f844c2ab7 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -92,20 +92,22 @@ describe Comment do before do @message = alice.post :status_message, :text => "hi", :to => @alices_aspect.id end + it 'should process youtube titles on the way in' do - video_id = "ABYnqp-bxvg" - url="http://www.youtube.com/watch?v=#{video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" - expected_title = "UP & down & UP & down &" + first_video_id = "ABYnqp-1111" + second_video_id = "ABYnqp-2222" + url = "http://www.youtube.com/watch?v=#{first_video_id} http://www.youtube.com/watch?v=#{second_video_id}" + expected_title = "UP & down & UP & down &" mock_http = mock("http") - Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http) - mock_http.should_receive(:get).with('/feeds/api/videos/'+video_id+'?v=2', nil).and_return( + Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).twice.and_return(mock_http) + mock_http.should_receive(:get).with(/\/feeds\/api\/videos/, nil).twice.and_return( [nil, 'Foobar