Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from vheuken/youtubeplaylist-fix
fixed YoutubePlaylist spec
- Loading branch information
Showing
16 changed files
with
2,311 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,39 @@ | ||
require_relative 'youtubeplaylist_api' | ||
require_relative 'youtubeplaylist_scraper' | ||
|
||
class VideoInfo | ||
module Providers | ||
class YoutubePlaylist < Youtube | ||
|
||
alias_method :playlist_id, :video_id | ||
attr_accessor :playlist_items_data | ||
|
||
def self.usable?(url) | ||
url =~ /((youtube\.com)\/playlist)|((youtube\.com)\/embed\/videoseries)/ | ||
def initialize(url, options = {}) | ||
if VideoInfo.provider_api_keys[:youtube].nil? | ||
extend YoutubePlaylistScraper | ||
else | ||
extend YoutubePlaylistAPI | ||
end | ||
|
||
super(url, options) | ||
end | ||
|
||
def videos | ||
_playlist_video_ids.map do |entry_id| | ||
VideoInfo.new("http://www.youtube.com/watch?v=#{entry_id}") | ||
end | ||
def self.usable?(url) | ||
url =~ /((youtube\.com)\/playlist)|((youtube\.com)\/embed\/videoseries)/ | ||
end | ||
|
||
def embed_url | ||
"//www.youtube.com/embed/videoseries?list=#{playlist_id}" | ||
end | ||
|
||
def description | ||
_playlist_entry['subtitle']['$t'] | ||
end | ||
|
||
%w[date keywords duration view_count].each do |method| | ||
define_method(method) { nil } | ||
end | ||
|
||
private | ||
|
||
def _playlist_entry | ||
data['feed'] | ||
end | ||
|
||
def _video_entry | ||
_playlist_entry | ||
end | ||
|
||
def _url_regex | ||
/youtube.com\/playlist\?p=(\S*)|youtube.com\/embed\/videoseries\?list=([a-zA-Z0-9-]*)/ | ||
end | ||
|
||
def _api_path | ||
"/feeds/api/playlists/#{playlist_id}?v=2&alt=json" | ||
end | ||
|
||
def _playlist_video_ids | ||
return [] unless _playlist_entry['entry'] | ||
_playlist_entry['entry'].map do |entry| | ||
entry['media$group']['yt$videoid']['$t'] | ||
end | ||
end | ||
|
||
end | ||
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,45 @@ | ||
class VideoInfo | ||
module YoutubePlaylistAPI | ||
def description | ||
data['items'][0]['snippet']['description'] | ||
end | ||
|
||
def videos | ||
_playlist_video_ids.map do |entry_id| | ||
VideoInfo.new("http://www.youtube.com/watch?v=#{entry_id}") | ||
end | ||
end | ||
|
||
private | ||
|
||
def _playlist_entry | ||
data['items'] | ||
end | ||
|
||
def _playlist_items | ||
data['items'] | ||
end | ||
|
||
def _api_path | ||
"/youtube/v3/playlists?part=snippet&id=#{playlist_id}&key=#{api_key}" | ||
end | ||
|
||
def _playlist_items_api_path | ||
"/youtube/v3/playlistItems?part=snippet&playlistId=#{playlist_id}&fields=items&key=#{api_key}" | ||
end | ||
|
||
def _playlist_items_api_url | ||
"https://#{_api_base}#{_playlist_items_api_path}" | ||
end | ||
|
||
def _playlist_items_data | ||
@playlist_items_data ||= _set_data_from_api(_playlist_items_api_url) | ||
end | ||
|
||
def _playlist_video_ids | ||
_playlist_items_data['items'].map do |item| | ||
item['snippet']['resourceId']['videoId'] | ||
end | ||
end | ||
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,59 @@ | ||
require 'oga' | ||
require 'open-uri' | ||
require 'open_uri_redirections' | ||
|
||
class VideoInfo | ||
module Providers | ||
module YoutubePlaylistScraper | ||
def description | ||
meta_nodes = data.css('meta') | ||
|
||
description_node = meta_nodes.detect do |m| | ||
m.attr('name').value == 'description' | ||
end | ||
|
||
description_node.attr('content').value | ||
end | ||
|
||
def title | ||
meta_nodes = data.css('meta') | ||
|
||
title_node = meta_nodes.detect do |m| | ||
m.attr('name').value == 'title' | ||
end | ||
|
||
title_node.attr('content').value | ||
end | ||
|
||
def videos | ||
raise(NotImplementedError, 'To access videos, you must provide an API key to VideoInfo.provider_api_keys') | ||
end | ||
|
||
def thumbnail_small | ||
thumbnail_medium.sub('mqdefault.jpg', 'default.jpg') | ||
end | ||
|
||
def thumbnail_medium | ||
'https:' + data.css('div.pl-header-thumb img').attr('src')[0].value | ||
end | ||
|
||
def thumbnail_large | ||
thumbnail_medium.sub('mqdefault.jpg', 'hqdefault.jpg') | ||
end | ||
|
||
private | ||
|
||
def available? | ||
!data.css('div#page').attr('class')[0].value.include?('oops-content') | ||
end | ||
|
||
def _set_data_from_api(api_url = _api_url) | ||
Oga.parse_html(open(api_url, :allow_redirections => :safe)) | ||
end | ||
|
||
def _api_url | ||
@url | ||
end | ||
end | ||
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
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
Oops, something went wrong.