Putting youtube titles back into status messages

This commit is contained in:
Raphael 2010-11-27 11:43:12 -05:00
parent 702abddce3
commit 7030ed0fed
10 changed files with 94 additions and 28 deletions

View file

@ -12,6 +12,8 @@ end
class Comment
require File.join(Rails.root, 'lib/diaspora/websocket')
require File.join(Rails.root, 'lib/youtube_titles')
include YoutubeTitles
include MongoMapper::Document
include ROXML
include Diaspora::Webhooks
@ -34,7 +36,9 @@ class Comment
validates_presence_of :text, :diaspora_handle
validates_with HandleValidator
before_save :get_youtube_title
before_save do
get_youtube_title text
end
timestamps!
@ -68,23 +72,5 @@ class Comment
verify_signature(creator_signature, person)
end
def get_youtube_title
self[:url_maps] ||= {}
youtube_match = text.match(/youtube\.com.*?v=([A-Za-z0-9_\\\-]+)/)
return unless youtube_match
video_id = youtube_match[1]
unless self[:url_maps][video_id]
ret = I18n.t 'application.helper.youtube_title.unknown'
http = Net::HTTP.new('gdata.youtube.com', 80)
path = "/feeds/api/videos/#{video_id}?v=2"
resp, data = http.get(path, nil)
title = data.match(/<title>(.*)<\/title>/)
unless title.nil?
title = title.to_s[7..-9]
end
self[:url_maps][video_id] = title
end
end
end

View file

@ -4,6 +4,8 @@
class StatusMessage < Post
include Diaspora::Socketable
include YoutubeTitles
require File.join(Rails.root, 'lib/youtube_titles')
validates_length_of :message, :maximum => 1000, :message => "please make your status messages less than 1000 characters"
xml_name :status_message
@ -15,6 +17,9 @@ class StatusMessage < Post
attr_accessible :message
before_save do
get_youtube_title message
end
def to_activity
<<-XML
<entry>

View file

@ -7,6 +7,6 @@
.content
.from
= link_to person.real_name, person
= markdownify(comment.text, :youtube_maps => comment[:url_maps])
= markdownify(comment.text, :youtube_maps => comment[:youtube_titles])
%div.time
= "#{time_ago_in_words(comment.updated_at)} #{t('ago')}"

View file

@ -3,7 +3,7 @@
-# the COPYRIGHT file.
%p
= markdownify(post.message)
= markdownify(post.message, :youtube_maps => post[:youtube_titles])
- if post.photos.count > 0
.photo_attachments

View file

@ -8,7 +8,7 @@
.span-14.append-1.last
#show_text
%p
= markdownify(@status_message.message)
= markdownify(@status_message.message, :youtube_maps => @status_message[:youtube_titles])
- for photo in @status_message.photos
= link_to (image_tag photo.url(:thumb_small)), object_path(photo)

22
lib/youtube_titles.rb Normal file
View file

@ -0,0 +1,22 @@
module YoutubeTitles
def youtube_title_for video_id
http = Net::HTTP.new('gdata.youtube.com', 80)
path = "/feeds/api/videos/#{video_id}?v=2"
resp, data = http.get(path, nil)
title = data.match(/<title>(.*)<\/title>/)
unless title.nil?
title = title.to_s[7..-9]
end
title || I18n.t('application.helper.youtube_title.unknown')
end
def get_youtube_title text
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] = youtube_title_for(video_id)
end
end
YOUTUBE_ID_REGEX = /youtube\.com.*?v=([A-Za-z0-9_\\\-]+)/
end

View file

@ -24,17 +24,22 @@ describe StatusMessagesController do
before do
@video_id = "ABYnqp-bxvg"
@url="http://www.youtube.com/watch?v=#{@video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
@message = user.post :status_message, :message => @url, :to => aspect.id
end
it 'renders posts with youtube urls' do
get :show, :id => @message.id
message = user.build_post :status_message, :message => @url, :to => aspect.id
message[:youtube_titles]= {@video_id => "title"}
message.save!
user.dispatch_post message, :to => aspect.id
get :show, :id => message.id
response.body.should match /Youtube: title/
end
it 'renders posts with comments with youtube urls' do
@comment = user.comment "none", :on => @message
message = user.post :status_message, :message => "Respond to this with a video!", :to => aspect.id
@comment = user.comment "none", :on => message
@comment.text = @url
@comment[:url_maps][@video_id] = "title"
@comment[:youtube_titles][@video_id] = "title"
@comment.save!
get :show, :id => @message.id
get :show, :id => message.id
response.body.should match /Youtube: title/
end
end

View file

@ -0,0 +1,29 @@
require 'spec_helper'
require 'youtube_titles'
describe YoutubeTitles do
include YoutubeTitles
describe '#youtube_title_for' do
before do
@video_id = "ABYnqp-bxvg"
@url="http://www.youtube.com/watch?v=#{@video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
@api_path = "/feeds/api/videos/#{@video_id}?v=2"
@expected_title = "UP & down & UP & down &amp;"
end
it 'gets a youtube title corresponding to an id' do
mock_http = mock("http")
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
mock_http.should_receive(:get).with(@api_path, nil).and_return(
[nil, "Foobar <title>#{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"])
youtube_title_for(@video_id).should == @expected_title
end
it 'returns a fallback for videos with no title' do
mock_http = mock("http")
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
mock_http.should_receive(:get).with(@api_path, nil).and_return(
[nil, "Foobar #{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"])
youtube_title_for(@video_id).should == I18n.t('application.helper.youtube_title.unknown')
end
end
end

View file

@ -192,7 +192,7 @@ describe Comment do
comment = user.build_comment url, :on => @message
comment.save!
comment[:url_maps].should == {video_id => expected_title}
comment[:youtube_titles].should == {video_id => expected_title}
end
end
end

View file

@ -56,5 +56,24 @@ describe StatusMessage do
end
end
describe 'youtube' do
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 &amp;"
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(
[nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
post = @user.build_post :status_message, :message => url, :to => @aspect.id
post.save!
post[:youtube_titles].should == {video_id => expected_title}
end
end
end