active_public_resources 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: dbbec7a8766492daa8e9975dfa8033cf22936348
4
- data.tar.gz: bc647bf66fc5e299c87f94d843f8f046d9da1c69
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDA3ZjczNDA1MmRjMDhiMzdkNzMwNDkyY2I4NjZmZmMwNGFmN2YxYQ==
5
+ data.tar.gz: !binary |-
6
+ NDliYWYzMjQ3NmM0ZDIxOTA4NThlOTJmNTE3NjEwYTY4MDMyYzM5ZQ==
5
7
  SHA512:
6
- metadata.gz: baa11d8f3a694013b11db6a572e83fcd6522427dc8191416aef414eee2852ad6bb998e0fb33bb8f4e5c04fec8f55bd0642f514d60e4832e3e66bb52715e0824c
7
- data.tar.gz: dc8cf7025d196f1c58377f5b65637a91ae6d285761deb8c2f205cf34c84f88fbdd2802eb27014d912581a45d321123f211d9bec0f742f5106a658bc692afbb6a
8
+ metadata.gz: !binary |-
9
+ Yzk1Njk5MmY3NDhlY2U3MDFkMmNmZjQxZmI2Njk3ZWZhNzM1NjY0ZDA1NGNl
10
+ MWY3ODM3Y2JiY2Q5OTFmODY2MWJkMTRiYjk0OWRiZTUxYmU4ZWRiMjExNjkw
11
+ NTNhYmYxMGU2MTg5YmVhZmRmNzE5NTExNzYxNGZiMzExNWQ2YjM=
12
+ data.tar.gz: !binary |-
13
+ Yjk3MjFmMDY2MWI4YTcyYWYzMWJiNDZkNWVjZmUyN2YxNzgyY2Q0NzZhYzQw
14
+ YjJjMzAzZmFmYWU5NDk2ZmU3ODQ1YzU2ZDc2ZjViMjdjMjY0ZWI2OWIzODNk
15
+ NGE1ZTFmNTRmYWQyOWQ5YWRiZjNmY2IyODQ1NjIyYmM5M2M1ODI=
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "activesupport", ">= 4.0.0"
22
22
  spec.add_dependency "activemodel", ">= 4.0.0"
23
+ spec.add_dependency "iso8601", "~> 0.8.6"
23
24
 
24
25
  # Drivers
25
26
  spec.add_dependency "vimeo", "~> 1.5.3"
@@ -1,41 +1,53 @@
1
1
  require 'net/https'
2
+ require 'iso8601'
2
3
 
3
4
  module ActivePublicResources
4
5
  module Drivers
5
6
  class Youtube < Driver
6
7
 
7
8
  DRIVER_NAME = "youtube"
9
+ API_KEY = ENV['YOUTUBE_API_KEY']
8
10
 
9
11
  def perform_request(request_criteria)
10
12
  request_criteria.validate_presence!([:query])
11
-
12
- uri = URI('https://gdata.youtube.com/feeds/api/videos')
13
+ uri = URI('https://www.googleapis.com/youtube/v3/search')
13
14
  params = {
14
- 'v' => 2,
15
15
  'q' => request_criteria.query,
16
- 'orderby' => sort(request_criteria.sort),
17
- 'alt' => 'json',
16
+ 'part' => 'snippet',
17
+ 'type' => 'video',
18
+ 'order' => sort(request_criteria.sort),
18
19
  'safeSearch' => content_filter(request_criteria.content_filter),
19
- 'start-index' => request_criteria.page || 1,
20
- 'max-results' => request_criteria.per_page || 25
20
+ 'maxResults' => request_criteria.per_page || 25,
21
+ 'key' => API_KEY
21
22
  }
23
+ params['pageToken'] = request_criteria.page if request_criteria.is_a? String
24
+ params['userIp'] = request_criteria.remote_ip if request_criteria.remote_ip
22
25
 
23
26
  uri.query = URI.encode_www_form(params)
24
27
 
25
28
  res = Net::HTTP.get_response(uri)
26
29
  results = JSON.parse(res.body)
27
-
28
- return parse_results(request_criteria, results)
30
+ return video_details(request_criteria, results)
29
31
  end
30
32
 
31
33
  private
32
34
 
35
+ def video_details(request_criteria, results)
36
+ video_ids = results['items'].map { |item| item['id']['videoId']}
37
+ uri = URI('https://www.googleapis.com/youtube/v3/videos')
38
+ params = { 'part' => 'snippet,contentDetails,statistics', 'id' => video_ids.join(','), 'key' => API_KEY }
39
+ uri.query = URI.encode_www_form(params)
40
+ res = Net::HTTP.get_response(uri)
41
+ videos = JSON.parse(res.body)['items']
42
+ return parse_results(request_criteria, results, videos)
43
+ end
44
+
33
45
  def sort(val)
34
46
  case val
35
47
  when APR::RequestCriteria::SORT_RELEVANCE
36
48
  'relevance'
37
49
  when APR::RequestCriteria::SORT_RECENT
38
- 'published'
50
+ 'date'
39
51
  when APR::RequestCriteria::SORT_POPULAR
40
52
  'viewCount'
41
53
  else
@@ -54,45 +66,47 @@ module ActivePublicResources
54
66
  end
55
67
  end
56
68
 
57
- def parse_results(request_criteria, results)
69
+ def parse_results(request_criteria, results, videos)
58
70
  @driver_response = DriverResponse.new(
59
71
  :criteria => request_criteria,
60
72
  :next_criteria => next_criteria(request_criteria, results),
61
- :total_items => results['feed']['openSearch$totalResults']['$t'].to_i,
62
- :items => results['feed']['entry'].map { |data| parse_video(data) }
73
+ :total_items => results['pageInfo']['totalResults'].to_i,
74
+ :items => videos.map { |video| parse_video(video) }
63
75
  )
64
76
  end
65
77
 
66
78
  def next_criteria(request_criteria, results)
67
- start_index = results['feed']['openSearch$startIndex']['$t'].to_i
68
- per_page = results['feed']['openSearch$itemsPerPage']['$t'].to_i
69
- total = results['feed']['openSearch$totalResults']['$t'].to_i
70
- if (start_index + per_page) < total
79
+ if results['nextPageToken']
71
80
  return RequestCriteria.new({
72
81
  :query => request_criteria.query,
73
- :page => start_index + per_page,
74
- :per_page => per_page
82
+ :page => results['nextPageToken'],
83
+ :per_page => results['pageInfo']['resultsPerPage'].to_i
75
84
  })
76
85
  end
77
86
  end
78
87
 
79
- def parse_video(data)
80
- video_id = data['id']['$t'].split(':').last
88
+
89
+
90
+ def parse_video(item)
91
+ video_id = item['id']
92
+ snippet = item['snippet']
93
+ statistics = item['statistics']
94
+ details = item['contentDetails']
81
95
  video = APR::ResponseTypes::Video.new
82
- video.id = video_id
83
- video.title = data['title']['$t']
84
- video.description = data['media$group']['media$description']['$t']
85
- video.thumbnail_url = data['media$group']['media$thumbnail'][0]['url']
86
- video.url = data['media$group']['media$player']['url']
87
- video.embed_url = "https://www.youtube.com/embed/#{video_id}?feature=oembed"
88
- video.duration = data['media$group']['yt$duration']['seconds'].to_i
89
- video.num_views = data['yt$statistics'] ? data['yt$statistics']['viewCount'].to_i : 0
90
- video.num_likes = data['yt$rating'] ? data['yt$rating']['numLikes'].to_i : 0
91
- video.num_comments = data['gd$comments'] ? data['gd$comments']['gd$feedLink']['countHint'] : 0
92
- video.created_date = Date.parse(data['published']['$t'])
93
- video.username = data['author'][0]['name']['$t']
94
- video.width = 640
95
- video.height = 360
96
+ video.id = video_id
97
+ video.title = snippet['title']
98
+ video.description = snippet['description']
99
+ video.thumbnail_url = snippet['thumbnails']['default']['url']
100
+ video.url = "https://www.youtube.com/watch?v=#{video_id}&feature=youtube_gdata_player"
101
+ video.embed_url = "https://www.youtube.com/embed/#{video_id}?feature=oembed"
102
+ video.duration = ISO8601::Duration.new(details['duration']).to_seconds
103
+ video.num_views = statistics['viewCount'] ? statistics['viewCount'].to_i : 0
104
+ video.num_likes = statistics['likeCount'] ? statistics['likeCount'].to_i : 0
105
+ video.num_comments = statistics['commentCount'] ? statistics['commentCount'] : 0
106
+ video.created_date = Date.parse(snippet['publishedAt'])
107
+ video.username = snippet['channelTitle']
108
+ video.width = 640
109
+ video.height = 360
96
110
 
97
111
  # Return Types
98
112
  video.return_types << APR::ReturnTypes::Url.new(
@@ -105,7 +119,7 @@ module ActivePublicResources
105
119
  video.return_types << APR::ReturnTypes::Iframe.new(
106
120
  :driver => DRIVER_NAME,
107
121
  :remote_id => video.id,
108
- :url => "https://www.youtube.com/embed/#{video.id}?feature=oembed",
122
+ :url => video.embed_url,
109
123
  :text => video.title,
110
124
  :title => video.title,
111
125
  :width => 640,
@@ -13,7 +13,7 @@ module ActivePublicResources
13
13
  CONTENT_FILTER_STRICT = 'strict'
14
14
  ]
15
15
 
16
- attr_accessor :query, :page, :per_page, :content_filter, :sort, :folder
16
+ attr_accessor :query, :page, :per_page, :content_filter, :sort, :folder, :remote_ip
17
17
 
18
18
  def initialize(args={})
19
19
  args.each do |k,v|
@@ -1,3 +1,3 @@
1
1
  module ActivePublicResources
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_public_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Berry
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-24 00:00:00.000000000 Z
11
+ date: 2015-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: 4.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: iso8601
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.6
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: vimeo
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +80,7 @@ dependencies:
66
80
  - - ~>
67
81
  - !ruby/object:Gem::Version
68
82
  version: '1.3'
69
- description: ' Normalized searching and browsing of public resources '
83
+ description: ! ' Normalized searching and browsing of public resources '
70
84
  email:
71
85
  - cavneb@gmail.com
72
86
  executables: []
@@ -148,17 +162,17 @@ require_paths:
148
162
  - lib
149
163
  required_ruby_version: !ruby/object:Gem::Requirement
150
164
  requirements:
151
- - - '>='
165
+ - - ! '>='
152
166
  - !ruby/object:Gem::Version
153
167
  version: '0'
154
168
  required_rubygems_version: !ruby/object:Gem::Requirement
155
169
  requirements:
156
- - - '>='
170
+ - - ! '>='
157
171
  - !ruby/object:Gem::Version
158
172
  version: '0'
159
173
  requirements: []
160
174
  rubyforge_project:
161
- rubygems_version: 2.2.1
175
+ rubygems_version: 2.2.2
162
176
  signing_key:
163
177
  specification_version: 4
164
178
  summary: Normalized searching and browsing of public resources