From 17272db143efa1f1fe61053c87da06ea9568f0ac Mon Sep 17 00:00:00 2001 From: Nyctef Date: Thu, 25 Dec 2014 22:09:34 +0000 Subject: [PATCH] try adding youtube download links doesn't work for some videos, should probably delegate to youtube-dl see https://github.com/jeckman/YouTube-Downloader/issues/9 --- config/application.rb | 2 ++ lib/services/html_title.rb | 5 ++-- lib/services/youtube_video.rb | 40 +++++++++++++++++++++++++++++ spec/services/youtube_video_spec.rb | 35 +++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 lib/services/youtube_video.rb create mode 100644 spec/services/youtube_video_spec.rb diff --git a/config/application.rb b/config/application.rb index a624bb8..2014c86 100644 --- a/config/application.rb +++ b/config/application.rb @@ -10,6 +10,7 @@ require "sprockets/railtie" require "rails/test_unit/railtie" require_relative "../lib/services/html_title" +require_relative "../lib/services/youtube_video" # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. @@ -36,6 +37,7 @@ class Application < Rails::Application config.after_initialize do Flavours.add HtmlTitle + Flavours.add YoutubeVideo end end diff --git a/lib/services/html_title.rb b/lib/services/html_title.rb index 4e71e77..eacfddd 100644 --- a/lib/services/html_title.rb +++ b/lib/services/html_title.rb @@ -7,13 +7,12 @@ class HtmlTitle @queue = :flavours def self.get_attrs(url) - puts "loading #{url} ..." + #puts "loading #{url} ..." page = Nokogiri::HTML(open(url)) - puts "got title #{page.css("title")}" + #puts "got title #{page.css("title")}" {html_title: page.css("title").text} rescue Exception => e puts e - puts e.message raise end end diff --git a/lib/services/youtube_video.rb b/lib/services/youtube_video.rb new file mode 100644 index 0000000..d2c69b0 --- /dev/null +++ b/lib/services/youtube_video.rb @@ -0,0 +1,40 @@ +require_relative "flavours" +require "rack" + +class YoutubeVideo + extend FlavourWorker + @queue = :flavours + + def self.get_attrs(url) + uri = URI.parse url + return {} if not is_youtube(uri) + params = decode_qs(uri.query) + video_id = params["v"] + #puts video_id + return {} if not video_id + video_info_url = "http://www.youtube.com/get_video_info?video_id="+video_id + #puts video_info_url + video_info_str = Net::HTTP.get_response(URI.parse(video_info_url)).body + video_info = decode_qs(video_info_str) + fmt_stream_map = video_info["url_encoded_fmt_stream_map"].split(",").map {|s| decode_qs(s)} + #puts fmt_stream_map + + #puts video_info + { + youtube_video_title: video_info["title"], + youtube_video_author: video_info["author"], + youtube_video_dl: fmt_stream_map.first["url"], + } + end + + private + def self.is_youtube(uri) + uri.host == "youtube.com" || + uri.host == "www.youtube.com" || + uri.host == "youtu.be" + end + + def self.decode_qs(qs) + Rack::Utils.parse_nested_query(qs) + end +end diff --git a/spec/services/youtube_video_spec.rb b/spec/services/youtube_video_spec.rb new file mode 100644 index 0000000..4860003 --- /dev/null +++ b/spec/services/youtube_video_spec.rb @@ -0,0 +1,35 @@ +require_relative "../../lib/services/youtube_video" +require_relative "../../app/models/drop" + +describe YoutubeVideo do + let (:url) { "a-url" } + let (:drop) { Drop.new(url: url) } + + before do + allow(Drop).to receive(:find).with(:id).and_return(drop) + end + + it "should accept drop id in perform method" do + allow(YoutubeVideo).to receive(:get_attrs).and_return({}) + expect(YoutubeVideo.perform(:id)).not_to be_nil + end + + it "should ignore non-youtube urls" do + expect(YoutubeVideo.get_attrs("http://google.com")).to eq({}) + end + + it "should add video title to attrs", :vcr, record: :new_episodes do + expect(YoutubeVideo.get_attrs("https://www.youtube.com/watch?v=9bZkp7q19f0")).to \ + include(youtube_video_title: "PSY - GANGNAM STYLE (강남스타일) M/V") + end + + it "should add video author to attrs", :vcr, record: :new_episodes do + expect(YoutubeVideo.get_attrs("https://www.youtube.com/watch?v=9bZkp7q19f0")).to \ + include(youtube_video_author: "officialpsy") + end + + it "should add video download link to attrs", :vcr, record: :new_episodes do + expect(YoutubeVideo.get_attrs("https://www.youtube.com/watch?v=9bZkp7q19f0")).to \ + include(:youtube_video_dl) + end +end