diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index f44ffe42e..398b265fd 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -3,6 +3,8 @@ # the COPYRIGHT file. module StatusMessagesHelper + @@youtube_title_cache = Hash.new("no-title") + def my_latest_message unless @latest_status_message.nil? return @latest_status_message.message @@ -17,10 +19,31 @@ module StatusMessagesHelper # next line is important due to XSS! (h is rail's make_html_safe-function) message = h(message).html_safe message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/, '\1http://\2') - message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch.*v=([A-Za-z0-9_]+)[^ ]*/, '\1youtube::\2') + message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch.*v=([A-Za-z0-9_]+)[^ ]*/, '\1youtube.com::\2') message.gsub!(/(http|ftp):\/\/([^ ]+)/, '\2') - message.gsub!(/youtube::([A-Za-z0-9_]+)/, 'Youtube: \1') + youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/) + youtube.to_a.each do |videoid| + if videoid.match('::').nil? + message.gsub!('youtube.com::'+videoid, 'Youtube: ' + youtube_title(videoid) + '') + end + end return message end + def youtube_title(id) + #TODO Check if id is cached, and return cached value + + ret = 'Unknown Video Title' #TODO add translation + http = Net::HTTP.new('gdata.youtube.com', 80) + path = '/feeds/api/videos/'+id+'?v=2' + resp, data = http.get(path, nil) + title = data.match(/