This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doesn't work for some videos, should probably delegate to youtube-dl see jeckman/YouTube-Downloader#9
- Loading branch information
Showing
4 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |