video_info 2.4.2 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +1 -3
- data/README.md +33 -2
- data/lib/video_info.rb +44 -2
- data/lib/video_info/provider.rb +21 -4
- data/lib/video_info/providers/vimeo.rb +81 -9
- data/lib/video_info/providers/vimeoplaylist.rb +14 -13
- data/lib/video_info/providers/vkontakte.rb +1 -1
- data/lib/video_info/providers/youtube.rb +20 -51
- data/lib/video_info/providers/youtube_api.rb +72 -0
- data/lib/video_info/providers/youtube_scraper.rb +99 -0
- data/lib/video_info/providers/youtubeplaylist.rb +28 -29
- data/lib/video_info/providers/youtubeplaylist_api.rb +57 -0
- data/lib/video_info/providers/youtubeplaylist_scraper.rb +47 -0
- data/lib/video_info/version.rb +1 -1
- data/spec/lib/video_info/providers/vimeo_playlist_spec.rb +19 -18
- data/spec/lib/video_info/providers/vimeo_spec.rb +45 -21
- data/spec/lib/video_info/providers/vkontakte_spec.rb +2 -2
- data/spec/lib/video_info/providers/youtube_api_spec.rb +392 -0
- data/spec/lib/video_info/providers/youtube_playlist_api_spec.rb +173 -0
- data/spec/lib/video_info/providers/youtube_playlist_spec.rb +6 -2
- data/spec/lib/video_info/providers/youtube_spec.rb +83 -6
- data/spec/lib/video_info_spec.rb +30 -0
- data/spec/spec_helper.rb +2 -0
- data/video_info.gemspec +4 -1
- metadata +55 -196
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/_available_/with_This_video_does_not_exist_or_has_been_deleted_/_available_/.yml +0 -60
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/_available_/with_valid_video/_available_/.yml +0 -59
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_date/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_description/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_duration/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_keywords/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_thumbnail_large/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_thumbnail_medium/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_thumbnail_small/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_title/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Dailymotion/with_video_x7lni3/_view_count/.yml +0 -62
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/_available_/with_password_required_video/_available_/.yml +0 -119
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/_available_/with_this_video_does_not_exist_video/_available_/.yml +0 -72
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/_available_/with_valid_video/_available_/.yml +0 -76
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_date/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_description/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_duration/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_height/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_keywords/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_thumbnail_large/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_thumbnail_medium/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_thumbnail_small/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_title/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_view_count/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vimeo/with_video_898029/_width/.yml +0 -83
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_VimeoPlaylist/_available_/with_invalid_playlist/_available_/.yml +0 -72
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_VimeoPlaylist/_available_/with_valid_playlist/_available_/.yml +0 -76
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_VimeoPlaylist/with_playlist_1921098/_description/.yml +0 -81
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_VimeoPlaylist/with_playlist_1921098/_thumbnail_large/.yml +0 -81
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_VimeoPlaylist/with_playlist_1921098/_thumbnail_medium/.yml +0 -81
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_VimeoPlaylist/with_playlist_1921098/_thumbnail_small/.yml +0 -81
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_VimeoPlaylist/with_playlist_1921098/_title/.yml +0 -81
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_VimeoPlaylist/with_playlist_1921098/_videos/.yml +0 -114
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/_available_/with_hashes/available_/.yml +0 -2255
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/_available_/with_invalid_video/available_/.yml +0 -46
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/_available_/with_private_video/available_/.yml +0 -382
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/_available_/with_redirect/available_/.yml +0 -210
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/_available_/with_redirect_to_main_page_for_auth/available_/.yml +0 -46
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/_available_/with_valid_video/available_/.yml +0 -1657
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/description/.yml +0 -730
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/duration/.yml +0 -731
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/embed_code/.yml +0 -732
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/embed_url/.yml +0 -732
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/height/.yml +0 -732
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/keywords/.yml +0 -732
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/title/.yml +0 -731
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/view_count/.yml +0 -731
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_kirill_lyanoi_z_video2152699_168591741_2F56fd229a9dfe2dcdbe/width/.yml +0 -728
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video-54799401_165822734/title/.yml +0 -1226
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/description/.yml +0 -997
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/duration/.yml +0 -997
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/embed_code/.yml +0 -997
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/embed_url/.yml +0 -998
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/height/.yml +0 -998
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/keywords/.yml +0 -997
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/title/.yml +0 -997
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/view_count/.yml +0 -997
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_video39576223_108370515/width/.yml +0 -998
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Vkontakte/with_video_videos43640822_/video43640822_168790809/title/.yml +0 -597
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/_available_/with_invalid_video/_available_/.yml +0 -69
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/_available_/with_valid_video/_available_/.yml +0 -71
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_pxonqr42is/_duration/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_pxonqr42is/_height/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_pxonqr42is/_thumbnail_large/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_pxonqr42is/_thumbnail_medium/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_pxonqr42is/_thumbnail_small/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_pxonqr42is/_title/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_pxonqr42is/_width/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_rs1me54mpw/_duration/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_rs1me54mpw/_height/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_rs1me54mpw/_thumbnail_large/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_rs1me54mpw/_thumbnail_medium/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_rs1me54mpw/_thumbnail_small/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_rs1me54mpw/_title/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Wistia/with_video_rs1me54mpw/_width/.yml +0 -78
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/_available_/with_valid_video/_available_/.yml +0 -56
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/_available_/with_video_is_unavailable_video/_available_/.yml +0 -46
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/_available_/with_video_no_longer_available_due_to_a_copyright_claim_video/_available_/.yml +0 -46
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_date/.yml +0 -61
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_description/.yml +0 -63
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_duration/.yml +0 -63
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_keywords/.yml +0 -63
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_thumbnail_large/.yml +0 -61
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_thumbnail_medium/.yml +0 -61
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_thumbnail_small/.yml +0 -61
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_title/.yml +0 -63
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_Youtube/with_video_mZqGqE0D0n4/_view_count/.yml +0 -61
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/_available_/with_invalid_playlist/_available_/.yml +0 -46
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/_available_/with_valid_playlist/_available_/.yml +0 -56
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/with_playlist_PL0E8117603D70E10A_in_embed_path/_videos/.yml +0 -61
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/with_playlist_PL9hW1uS6HUftLdHI6RIsaf/_description/.yml +0 -96
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/with_playlist_PL9hW1uS6HUftLdHI6RIsaf/_thumbnail_large/.yml +0 -96
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/with_playlist_PL9hW1uS6HUftLdHI6RIsaf/_thumbnail_medium/.yml +0 -96
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/with_playlist_PL9hW1uS6HUftLdHI6RIsaf/_thumbnail_small/.yml +0 -96
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/with_playlist_PL9hW1uS6HUftLdHI6RIsaf/_title/.yml +0 -98
- data/spec/fixtures/vcr_cassettes/VideoInfo_Providers_YoutubePlaylist/with_playlist_PL9hW1uS6HUftLdHI6RIsaf/_videos/.yml +0 -96
@@ -0,0 +1,72 @@
|
|
1
|
+
class VideoInfo
|
2
|
+
module Providers
|
3
|
+
module YoutubeAPI
|
4
|
+
def api_key
|
5
|
+
VideoInfo.provider_api_keys[:youtube]
|
6
|
+
end
|
7
|
+
|
8
|
+
def title
|
9
|
+
_video_snippet['title']
|
10
|
+
end
|
11
|
+
|
12
|
+
def description
|
13
|
+
_video_snippet['description']
|
14
|
+
end
|
15
|
+
|
16
|
+
def keywords
|
17
|
+
_video_snippet['tags']
|
18
|
+
end
|
19
|
+
|
20
|
+
def duration
|
21
|
+
video_duration = _video_content_details['duration'] || 0
|
22
|
+
ISO8601::Duration.new(video_duration).to_seconds.to_i
|
23
|
+
end
|
24
|
+
|
25
|
+
def date
|
26
|
+
return unless published_at = _video_snippet['publishedAt']
|
27
|
+
Time.parse(published_at, Time.now.utc)
|
28
|
+
end
|
29
|
+
|
30
|
+
def view_count
|
31
|
+
_video_statistics['viewCount'].to_i rescue 0
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def available?
|
37
|
+
data['items'].size > 0 rescue false
|
38
|
+
end
|
39
|
+
|
40
|
+
def _api_base
|
41
|
+
'www.googleapis.com'
|
42
|
+
end
|
43
|
+
|
44
|
+
def _api_path
|
45
|
+
"/youtube/v3/videos?id=#{video_id}&part=snippet,statistics,contentDetails&fields=items(id,snippet,statistics,contentDetails)&key=#{api_key}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def _api_url
|
49
|
+
"https://#{_api_base}#{_api_path}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def _video_snippet
|
53
|
+
return {} unless available?
|
54
|
+
data['items'][0]['snippet']
|
55
|
+
end
|
56
|
+
|
57
|
+
def _video_content_details
|
58
|
+
return {} unless available?
|
59
|
+
data['items'][0]['contentDetails']
|
60
|
+
end
|
61
|
+
|
62
|
+
def _video_statistics
|
63
|
+
return {} unless available?
|
64
|
+
data['items'][0]['statistics']
|
65
|
+
end
|
66
|
+
|
67
|
+
def _video_thumbnail(id)
|
68
|
+
_video_entry['media$group']['media$thumbnail'][id]['url']
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'oga'
|
2
|
+
require 'open-uri'
|
3
|
+
|
4
|
+
class VideoInfo
|
5
|
+
module Providers
|
6
|
+
module YoutubeScraper
|
7
|
+
def date
|
8
|
+
date = itemprop_node_value('datePublished')
|
9
|
+
|
10
|
+
Time.parse(date) unless !date
|
11
|
+
end
|
12
|
+
|
13
|
+
def description
|
14
|
+
meta_node_value('description')
|
15
|
+
end
|
16
|
+
|
17
|
+
def duration
|
18
|
+
duration = itemprop_node_value('duration')
|
19
|
+
|
20
|
+
if duration
|
21
|
+
ISO8601::Duration.new(duration).to_seconds.to_i
|
22
|
+
else
|
23
|
+
0
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def keywords
|
28
|
+
value = meta_node_value('keywords')
|
29
|
+
|
30
|
+
value.split(', ') unless !value
|
31
|
+
end
|
32
|
+
|
33
|
+
def title
|
34
|
+
meta_node_value('title')
|
35
|
+
end
|
36
|
+
|
37
|
+
def thumbnail_small
|
38
|
+
"https://i.ytimg.com/vi/#{video_id}/default.jpg"
|
39
|
+
end
|
40
|
+
|
41
|
+
def thumbnail_medium
|
42
|
+
"https://i.ytimg.com/vi/#{video_id}/mqdefault.jpg"
|
43
|
+
end
|
44
|
+
|
45
|
+
def thumbnail_large
|
46
|
+
"https://i.ytimg.com/vi/#{video_id}/hqdefault.jpg"
|
47
|
+
end
|
48
|
+
|
49
|
+
def view_count
|
50
|
+
data.css('div.watch-view-count').text.gsub(',', '').to_i
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def meta_nodes
|
56
|
+
@meta_nodes ||= data.css('meta')
|
57
|
+
end
|
58
|
+
|
59
|
+
def meta_node_value(name)
|
60
|
+
if available?
|
61
|
+
node = meta_nodes.detect { |n| n.attr('name').value == name }
|
62
|
+
|
63
|
+
node.attr('content').value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def itemprop_node_value(name)
|
68
|
+
if available?
|
69
|
+
node = meta_nodes.detect do |m|
|
70
|
+
itemprop_attr = m.attr('itemprop')
|
71
|
+
|
72
|
+
itemprop_attr.value == name unless !itemprop_attr
|
73
|
+
end
|
74
|
+
|
75
|
+
node.attr('content').value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def available?
|
80
|
+
data.css('div#unavailable-submessage').text.strip.empty?
|
81
|
+
end
|
82
|
+
|
83
|
+
def _set_data_from_api(api_url = _api_url)
|
84
|
+
uri = URI(api_url)
|
85
|
+
|
86
|
+
unless uri.scheme
|
87
|
+
uri.path = uri.path.prepend('//')
|
88
|
+
uri.scheme = 'http'
|
89
|
+
end
|
90
|
+
|
91
|
+
Oga.parse_html(open(uri.to_s, allow_redirections: :safe).read)
|
92
|
+
end
|
93
|
+
|
94
|
+
def _api_url
|
95
|
+
@url
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -1,56 +1,55 @@
|
|
1
|
+
require_relative 'youtubeplaylist_api'
|
2
|
+
require_relative 'youtubeplaylist_scraper'
|
3
|
+
|
1
4
|
class VideoInfo
|
2
5
|
module Providers
|
3
6
|
class YoutubePlaylist < Youtube
|
4
|
-
|
5
7
|
alias_method :playlist_id, :video_id
|
8
|
+
attr_accessor :playlist_items_data
|
6
9
|
|
7
|
-
def
|
8
|
-
url
|
9
|
-
end
|
10
|
+
def initialize(url, options = {})
|
11
|
+
super(url, options)
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
if VideoInfo.provider_api_keys[:youtube].nil?
|
14
|
+
extend YoutubePlaylistScraper
|
15
|
+
else
|
16
|
+
extend YoutubePlaylistAPI
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
17
|
-
def
|
18
|
-
|
20
|
+
def self.usable?(url)
|
21
|
+
url =~ /((youtube\.com)\/playlist)|((youtube\.com)\/embed\/videoseries)/
|
19
22
|
end
|
20
23
|
|
21
|
-
def
|
22
|
-
|
24
|
+
def date
|
25
|
+
nil
|
23
26
|
end
|
24
27
|
|
25
|
-
|
26
|
-
|
28
|
+
def duration
|
29
|
+
nil
|
27
30
|
end
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
def _playlist_entry
|
32
|
-
data['feed']
|
32
|
+
def keywords
|
33
|
+
nil
|
33
34
|
end
|
34
35
|
|
35
|
-
def
|
36
|
-
|
36
|
+
def view_count
|
37
|
+
nil
|
37
38
|
end
|
38
39
|
|
39
|
-
def
|
40
|
-
|
40
|
+
def embed_url
|
41
|
+
"//www.youtube.com/embed/videoseries?list=#{playlist_id}"
|
41
42
|
end
|
42
43
|
|
43
|
-
|
44
|
-
|
44
|
+
%w[date keywords duration view_count].each do |method|
|
45
|
+
define_method(method) { nil }
|
45
46
|
end
|
46
47
|
|
47
|
-
|
48
|
-
return [] unless _playlist_entry['entry']
|
49
|
-
_playlist_entry['entry'].map do |entry|
|
50
|
-
entry['media$group']['yt$videoid']['$t']
|
51
|
-
end
|
52
|
-
end
|
48
|
+
private
|
53
49
|
|
50
|
+
def _url_regex
|
51
|
+
/youtube.com\/playlist\?p=(\S*)|youtube.com\/embed\/videoseries\?list=([a-zA-Z0-9-]*)/
|
52
|
+
end
|
54
53
|
end
|
55
54
|
end
|
56
55
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class VideoInfo
|
2
|
+
module YoutubePlaylistAPI
|
3
|
+
def description
|
4
|
+
data['items'][0]['snippet']['description']
|
5
|
+
end
|
6
|
+
|
7
|
+
def date
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def duration
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def videos
|
16
|
+
_playlist_video_ids.map do |entry_id|
|
17
|
+
VideoInfo.new("http://www.youtube.com/watch?v=#{entry_id}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def view_count
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def _playlist_entry
|
28
|
+
data['items']
|
29
|
+
end
|
30
|
+
|
31
|
+
def _playlist_items
|
32
|
+
data['items']
|
33
|
+
end
|
34
|
+
|
35
|
+
def _api_path
|
36
|
+
"/youtube/v3/playlists?part=snippet&id=#{playlist_id}&key=#{api_key}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def _playlist_items_api_path
|
40
|
+
"/youtube/v3/playlistItems?part=snippet&playlistId=#{playlist_id}&fields=items&key=#{api_key}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def _playlist_items_api_url
|
44
|
+
"https://#{_api_base}#{_playlist_items_api_path}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def _playlist_items_data
|
48
|
+
@playlist_items_data ||= _set_data_from_api(_playlist_items_api_url)
|
49
|
+
end
|
50
|
+
|
51
|
+
def _playlist_video_ids
|
52
|
+
_playlist_items_data['items'].map do |item|
|
53
|
+
item['snippet']['resourceId']['videoId']
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'oga'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'open_uri_redirections'
|
4
|
+
|
5
|
+
class VideoInfo
|
6
|
+
module Providers
|
7
|
+
module YoutubePlaylistScraper
|
8
|
+
def date
|
9
|
+
nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def duration
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def keywords
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def videos
|
21
|
+
raise(NotImplementedError, 'To access videos, you must provide an API key to VideoInfo.provider_api_keys')
|
22
|
+
end
|
23
|
+
|
24
|
+
def view_count
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def thumbnail_small
|
29
|
+
thumbnail_medium.sub('mqdefault.jpg', 'default.jpg')
|
30
|
+
end
|
31
|
+
|
32
|
+
def thumbnail_medium
|
33
|
+
'https:' + data.css('div.pl-header-thumb img').attr('src')[0].value
|
34
|
+
end
|
35
|
+
|
36
|
+
def thumbnail_large
|
37
|
+
thumbnail_medium.sub('mqdefault.jpg', 'hqdefault.jpg')
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def available?
|
43
|
+
!data.css('div#page').attr('class')[0].value.include?('oops-content')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/video_info/version.rb
CHANGED
@@ -1,33 +1,36 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe VideoInfo::Providers::VimeoPlaylist do
|
4
|
+
before(:all) do
|
5
|
+
VideoInfo.provider_api_keys = { vimeo: '6b66b015a3504793b4f541d878f46ff6' }
|
6
|
+
end
|
4
7
|
|
5
|
-
describe
|
8
|
+
describe '.usable?' do
|
6
9
|
subject { VideoInfo::Providers::VimeoPlaylist.usable?(url) }
|
7
10
|
|
8
|
-
context
|
11
|
+
context 'with vimeo album url' do
|
9
12
|
let(:url) { 'http://vimeo.com/album/2755718' }
|
10
13
|
it { is_expected.to be_truthy }
|
11
14
|
end
|
12
15
|
|
13
|
-
context
|
16
|
+
context 'with vimeo hubnub embed url' do
|
14
17
|
let(:url) { 'http://player.vimeo.com/hubnut/album/2755718' }
|
15
18
|
it { is_expected.to be_truthy }
|
16
19
|
end
|
17
20
|
|
18
|
-
context
|
21
|
+
context 'with vimeo url' do
|
19
22
|
let(:url) { 'http://www.vimeo.com/898029' }
|
20
23
|
it { is_expected.to be_falsey }
|
21
24
|
end
|
22
25
|
|
23
|
-
context
|
26
|
+
context 'with other url' do
|
24
27
|
let(:url) { 'http://www.youtube.com/898029' }
|
25
28
|
it { is_expected.to be_falsey }
|
26
29
|
end
|
27
30
|
end
|
28
31
|
|
29
|
-
describe
|
30
|
-
context
|
32
|
+
describe '#available?' do
|
33
|
+
context 'with valid playlist', :vcr do
|
31
34
|
subject { VideoInfo.new('http://vimeo.com/album/2755718') }
|
32
35
|
|
33
36
|
describe '#available?' do
|
@@ -36,7 +39,7 @@ describe VideoInfo::Providers::VimeoPlaylist do
|
|
36
39
|
end
|
37
40
|
end
|
38
41
|
|
39
|
-
context
|
42
|
+
context 'with invalid playlist', :vcr do
|
40
43
|
subject { VideoInfo.new('http://vimeo.com/album/2') }
|
41
44
|
|
42
45
|
describe '#available?' do
|
@@ -46,14 +49,13 @@ describe VideoInfo::Providers::VimeoPlaylist do
|
|
46
49
|
end
|
47
50
|
end
|
48
51
|
|
49
|
-
|
50
|
-
context "with playlist 1921098", :vcr do
|
52
|
+
context 'with playlist 1921098', :vcr do
|
51
53
|
let(:videos) {
|
52
54
|
[
|
53
55
|
39837353, 40170418, 38809325, 38445453,
|
54
56
|
38445381, 38445243, 38394965, 42647970
|
55
57
|
].map do |video_id|
|
56
|
-
VideoInfo.new("
|
58
|
+
VideoInfo.new("https://vimeo.com/#{video_id}")
|
57
59
|
end
|
58
60
|
}
|
59
61
|
subject { VideoInfo.new('http://vimeo.com/album/1921098') }
|
@@ -100,7 +102,7 @@ describe VideoInfo::Providers::VimeoPlaylist do
|
|
100
102
|
|
101
103
|
describe '#duration' do
|
102
104
|
subject { super().duration }
|
103
|
-
it { is_expected.to
|
105
|
+
it { is_expected.to eq 1254 }
|
104
106
|
end
|
105
107
|
|
106
108
|
describe '#width' do
|
@@ -115,22 +117,22 @@ describe VideoInfo::Providers::VimeoPlaylist do
|
|
115
117
|
|
116
118
|
describe '#date' do
|
117
119
|
subject { super().date }
|
118
|
-
it { is_expected.to
|
120
|
+
it { is_expected.to eq Time.parse('2012-04-30T15:00:53+00:00', Time.now.utc).utc }
|
119
121
|
end
|
120
122
|
|
121
123
|
describe '#thumbnail_small' do
|
122
124
|
subject { super().thumbnail_small }
|
123
|
-
it { is_expected.to eq '
|
125
|
+
it { is_expected.to eq 'https://i.vimeocdn.com/video/299773432_100x75.jpg' }
|
124
126
|
end
|
125
127
|
|
126
128
|
describe '#thumbnail_medium' do
|
127
129
|
subject { super().thumbnail_medium }
|
128
|
-
it { is_expected.to eq '
|
130
|
+
it { is_expected.to eq 'https://i.vimeocdn.com/video/299773432_200x150.jpg' }
|
129
131
|
end
|
130
132
|
|
131
133
|
describe '#thumbnail_large' do
|
132
134
|
subject { super().thumbnail_large }
|
133
|
-
it { is_expected.to eq '
|
135
|
+
it { is_expected.to eq 'https://i.vimeocdn.com/video/299773432_640.jpg' }
|
134
136
|
end
|
135
137
|
|
136
138
|
describe '#videos' do
|
@@ -144,7 +146,7 @@ describe VideoInfo::Providers::VimeoPlaylist do
|
|
144
146
|
end
|
145
147
|
end
|
146
148
|
|
147
|
-
context
|
149
|
+
context 'with playlist 2755718 in embed path', :vcr do
|
148
150
|
subject { VideoInfo.new('http://player.vimeo.com/hubnut/album/2755718') }
|
149
151
|
|
150
152
|
describe '#playlist_id' do
|
@@ -152,5 +154,4 @@ describe VideoInfo::Providers::VimeoPlaylist do
|
|
152
154
|
it { is_expected.to eq '2755718' }
|
153
155
|
end
|
154
156
|
end
|
155
|
-
|
156
157
|
end
|