Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Commit

Permalink
try adding youtube download links
Browse files Browse the repository at this point in the history
doesn't work for some videos, should probably delegate to youtube-dl

see jeckman/YouTube-Downloader#9
  • Loading branch information
nyctef committed Dec 25, 2014
1 parent fabcc42 commit 17272db
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -36,6 +37,7 @@ class Application < Rails::Application

config.after_initialize do
Flavours.add HtmlTitle
Flavours.add YoutubeVideo
end

end
Expand Down
5 changes: 2 additions & 3 deletions lib/services/html_title.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions lib/services/youtube_video.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions spec/services/youtube_video_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 17272db

Please sign in to comment.