youtuberb 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f8d3b68ce9d206b19c96ccf31e5707379fcb98a989805f133df36a59c440e65
4
- data.tar.gz: 0f9ef583ce567c2dcc7da4474cd9575efd54ba967a59c1e1809a7bbc78f07d5c
3
+ metadata.gz: 9723022ed2efe8846dce8650e21077bc2be1e888bb034596b87620ba1cbda433
4
+ data.tar.gz: ed5ecf1b108bd4b9601e4ad93836dbe070dbc16fc60a1c1bca54d79c2d34f5df
5
5
  SHA512:
6
- metadata.gz: 6fda64e56e4ac55557fb661a3d35e952bf5f2d415848598598cae30b0a12cd5ce8ed46ee62df1107030c2102039e53544f5d58128b711191ec4556ba539a0b03
7
- data.tar.gz: 86e6f3c535e6d1e40947237c5dc89f0330edf64ea4fa438dd18075fc91be5ec1cb33b3c58956507f0084e3bae7c7967d0c0e551fbeea96db5f95dcde6b5d5e7f
6
+ metadata.gz: 719d5673d88e53d3fd4220a28c4eb4da599127d008a2d0223913eb1fbab10b1ac748f33d88c90c6a16069a908dfc4fa9f156eb063f0bb1e182bba4785bc09de4
7
+ data.tar.gz: 50c1bc722fcd3e0db8d008938aa96e0cd866818246fe94a040caa039a5d37238608771cd8271326a444a9f67ab642192cc5b660a8d8ca8da6dc93e964221c57a
data/Gemfile CHANGED
@@ -6,4 +6,5 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
- gem "dotenv"
9
+ gem "minitest", "~> 5.0"
10
+ gem "dotenv"
data/Gemfile.lock CHANGED
@@ -1,17 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- youtuberb (0.1.3)
4
+ youtuberb (0.2.0)
5
5
  faraday (~> 2.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
+ base64 (0.1.1)
10
11
  dotenv (2.7.6)
11
- faraday (2.2.0)
12
- faraday-net_http (~> 2.0)
12
+ faraday (2.7.11)
13
+ base64
14
+ faraday-net_http (>= 2.0, < 3.1)
13
15
  ruby2_keywords (>= 0.0.4)
14
- faraday-net_http (2.0.2)
16
+ faraday-net_http (3.0.2)
17
+ minitest (5.20.0)
15
18
  rake (13.0.6)
16
19
  ruby2_keywords (0.0.5)
17
20
 
@@ -20,6 +23,7 @@ PLATFORMS
20
23
 
21
24
  DEPENDENCIES
22
25
  dotenv
26
+ minitest (~> 5.0)
23
27
  rake (~> 13.0)
24
28
  youtuberb!
25
29
 
data/README.md CHANGED
@@ -16,10 +16,14 @@ gem 'youtuberb'
16
16
 
17
17
  ### Set Client Details
18
18
 
19
- Firstly you'll need to set an API Key and an Access Token.
20
- An Access Token will be an OAuth2 token generated after authentication.
19
+ Firstly you'll need to set an API Key. A user Access Token is required to access private data.
20
+ An Access Token will be an OAuth2 token generated after authentication.
21
21
 
22
22
  ```ruby
23
+ # To access public data, just an API Key is required
24
+ @client = YouTube::Client.new(api_key: "")
25
+
26
+ # Or to access data for a user, an Access Token is also required
23
27
  @client = YouTube::Client.new(api_key: "", access_token: "")
24
28
  ```
25
29
 
@@ -41,15 +45,32 @@ An Access Token will be an OAuth2 token generated after authentication.
41
45
  ```ruby
42
46
  # Get a single video
43
47
  @client.videos.list(id: "abc123")
48
+ # => #<YouTube::Video...
44
49
 
45
50
  # Get multiple videos
46
- @client.videos.list(id: "abc123,123abc")
51
+ @client.videos.list(ids: ["abc123", "123abc"])
52
+ # => #<YouTube::Collection...
47
53
 
48
54
  # Liked videos for the currently authenticated user
49
55
  @client.videos.liked
56
+ # => #<YouTube::Collection...
50
57
 
51
58
  # Get a video owned by the current user. This retrieves extra information so will only work on videos owned by the current user.
52
59
  @client.videos.retrieve(id: "abc123")
60
+ # => #<YouTube::Video...
61
+ ```
62
+
63
+ #### Getting a list of Videos for a Channel
64
+
65
+ ```ruby
66
+ # First, grab the Channel details
67
+ channel = @client.channels.retrieve(id: "channel_id")
68
+
69
+ # Then use the Playlist Items endpoint to get the Videos
70
+ @client.playlist_items.list playlist_id: channel.contentDetails.relatedPlaylists.uploads
71
+
72
+ # Or use the Search endpoint
73
+ @client.search.list channelId: channel.id
53
74
  ```
54
75
 
55
76
  ### Playlists
@@ -69,6 +90,7 @@ An Access Token will be an OAuth2 token generated after authentication.
69
90
  @client.playlists.update(id: "playlist_id", title: "My Playlist", privacy_status: "public")
70
91
  @client.playlists.delete(id: "playlist_id")
71
92
  ```
93
+
72
94
  ### Playlist Items
73
95
 
74
96
  ```ruby
@@ -85,6 +107,21 @@ An Access Token will be an OAuth2 token generated after authentication.
85
107
  @client.playlist_items.delete(id: "playlist_id")
86
108
  ```
87
109
 
110
+ ### Search
111
+
112
+ For a full list of parameters, see the [YouTube API Docs](https://developers.google.com/youtube/v3/docs/search/list).
113
+
114
+ ```ruby
115
+ # Search YouTube for a term
116
+ @client.search.list(q: "search term")
117
+
118
+ # Restrict the search to a channel
119
+ @client.search.list(channelId: "channel")
120
+
121
+ # Search a channel for videos only and ordered by date
122
+ @client.search.list(channelId: "channel", type: "video", order: "date")
123
+ ```
124
+
88
125
  ## Contributing
89
126
 
90
127
  Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/youtuberb.
data/Rakefile CHANGED
@@ -1,4 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- task default: %i[]
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
13
+
@@ -4,13 +4,10 @@ module YouTube
4
4
 
5
5
  attr_reader :api_key, :access_token, :adapter
6
6
 
7
- def initialize(api_key:, access_token:, adapter: Faraday.default_adapter, stubs: nil)
7
+ def initialize(api_key:, access_token: nil, adapter: Faraday.default_adapter)
8
8
  @api_key = api_key
9
9
  @access_token = access_token
10
10
  @adapter = adapter
11
-
12
- # Test stubs for requests
13
- @stubs = stubs
14
11
  end
15
12
 
16
13
  def channels
@@ -29,6 +26,10 @@ module YouTube
29
26
  PlaylistItemsResource.new(self)
30
27
  end
31
28
 
29
+ def search
30
+ SearchResource.new(self)
31
+ end
32
+
32
33
  def connection
33
34
  @connection ||= Faraday.new(BASE_URL) do |conn|
34
35
  conn.request :authorization, :Bearer, access_token
@@ -7,7 +7,7 @@ module YouTube
7
7
 
8
8
  new(
9
9
  data: body["items"].map { |attrs| type.new(attrs) },
10
- total: body["pageInfo"]["totalResults"],
10
+ total: body["items"].count,
11
11
  next_page_token: body["nextPageToken"],
12
12
  prev_page_token: body["prevPageToken"],
13
13
  # cursor: body.dig("pagination", "cursor")
@@ -0,0 +1,30 @@
1
+ module YouTube
2
+ class SearchResult < Object
3
+
4
+ def initialize(options = {})
5
+ super options
6
+
7
+ if options["id"]
8
+ self.video_id = options["id"]["videoId"] || nil
9
+ self.playlist_id = options["id"]["playlistId"] || nil
10
+ end
11
+
12
+ if options["snippet"]
13
+ self.title = options["snippet"]["title"]
14
+ self.description = options["snippet"]["description"]
15
+ self.published_at = options["snippet"]["publishedAt"]
16
+ self.channel_id = options["snippet"]["channelId"]
17
+
18
+ if options["snippet"]["thumbnails"]
19
+ thumb = options["snippet"]["thumbnails"]
20
+ self.thumbnail_default = thumb["default"]["url"] if thumb["default"]
21
+ self.thumbnail_medium = thumb["medium"]["url"] if thumb["medium"]
22
+ self.thumbnail_high = thumb["high"]["url"] if thumb["high"]
23
+ self.thumbnail_standard = thumb["standard"]["url"] if thumb["standard"]
24
+ self.thumbnail_maxres = thumb["maxres"]["url"] if thumb["maxres"]
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -9,7 +9,7 @@ module YouTube
9
9
  self.description = options["snippet"]["description"]
10
10
  self.published_at = options["snippet"]["publishedAt"]
11
11
  self.channel_id = options["snippet"]["channelId"]
12
-
12
+
13
13
  if options["snippet"]["thumbnails"]
14
14
  thumb = options["snippet"]["thumbnails"]
15
15
  self.thumbnail_default = thumb["default"]["url"] if thumb["default"]
@@ -25,6 +25,12 @@ module YouTube
25
25
  self.blocked = !options["contentDetails"]["regionRestriction"].nil?
26
26
  end
27
27
 
28
+ if options["liveStreamingDetails"]
29
+ self.live_stream = options["liveStreamingDetails"]["actualStartTime"].present?
30
+ else
31
+ self.live_stream = false
32
+ end
33
+
28
34
  if options["status"]
29
35
  self.upload_status = options["status"]["uploadStatus"]
30
36
  self.privacy_status = options["status"]["privacyStatus"]
@@ -1,7 +1,7 @@
1
1
  module YouTube
2
2
  class ChannelsResource < Resource
3
3
 
4
- PARTS = "id,snippet,status,statistics"
4
+ PARTS = "id,snippet,status,statistics,contentDetails"
5
5
 
6
6
  # Retrieve the channel of the currently authenticated user
7
7
  def mine
@@ -9,12 +9,13 @@ module YouTube
9
9
  return nil if response.body["pageInfo"]["totalResults"] == 0
10
10
  Channel.new(response.body["items"][0])
11
11
  end
12
-
12
+
13
13
  # Retrieve a Channel by its ID or Username
14
14
  def retrieve(id: nil, username: nil)
15
15
  attrs = {}
16
16
  attrs[:id] = id if id
17
17
  attrs[:forUsername] = username if username
18
+ raise ArgumentError, "Must provide either an ID or Username" if attrs.empty?
18
19
 
19
20
  response = get_request "channels", params: attrs.merge({part: PARTS})
20
21
  return nil if response.body["pageInfo"]["totalResults"] == 0
@@ -0,0 +1,10 @@
1
+ module YouTube
2
+ class SearchResource < Resource
3
+
4
+ def list(**params)
5
+ response = get_request "search", params: {part: "id,snippet"}.merge(params)
6
+ Collection.from_response(response, type: SearchResult)
7
+ end
8
+
9
+ end
10
+ end
@@ -1,13 +1,27 @@
1
1
  module YouTube
2
2
  class VideosResource < Resource
3
3
 
4
- PARTS = "id,contentDetails,snippet,statistics,status"
5
- AUTH_PARTS = "id,contentDetails,fileDetails,processingDetails,snippet,statistics,status,suggestions"
6
-
4
+ PARTS = "id,snippet,contentDetails,liveStreamingDetails,statistics,status"
5
+ AUTH_PARTS = "id,snippet,contentDetails,fileDetails,processingDetails,statistics,status,suggestions"
6
+
7
7
  # Retrieves Videos by ID. Can be comma separated to retrieve multiple.
8
- def list(id:)
9
- response = get_request "videos", params: {id: id, part: PARTS}
10
- Collection.from_response(response, type: Video)
8
+ def list(id: nil, ids: nil)
9
+ raise "Either id or ids is required" unless !id.nil? || !ids.nil?
10
+
11
+ if id
12
+ response = get_request("videos", params: {id: id, part: PARTS})
13
+ elsif ids
14
+ response = get_request("videos", params: {id: ids.join(","), part: PARTS})
15
+ end
16
+
17
+ body = response.body["items"]
18
+ if body.count == 1
19
+ Video.new body[0]
20
+ elsif body.count > 1
21
+ Collection.from_response(response, type: Video)
22
+ else
23
+ return nil
24
+ end
11
25
  end
12
26
 
13
27
  # Retrieves liked Videos for the currently authenticated user
@@ -1,3 +1,3 @@
1
1
  module YouTube
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/you_tube.rb CHANGED
@@ -15,10 +15,12 @@ module YouTube
15
15
  autoload :VideosResource, "you_tube/resources/videos"
16
16
  autoload :PlaylistsResource, "you_tube/resources/playlists"
17
17
  autoload :PlaylistItemsResource, "you_tube/resources/playlist_items"
18
+ autoload :SearchResource, "you_tube/resources/search"
18
19
 
19
20
  autoload :Channel, "you_tube/objects/channel"
20
21
  autoload :Video, "you_tube/objects/video"
21
22
  autoload :Playlist, "you_tube/objects/playlist"
22
23
  autoload :PlaylistItem, "you_tube/objects/playlist_item"
24
+ autoload :SearchResult, "you_tube/objects/search_result"
23
25
 
24
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youtuberb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Perry
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-10 00:00:00.000000000 Z
11
+ date: 2023-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
- description:
27
+ description:
28
28
  email:
29
29
  - dean@deanpcmad.com
30
30
  executables: []
@@ -47,11 +47,13 @@ files:
47
47
  - lib/you_tube/objects/channel.rb
48
48
  - lib/you_tube/objects/playlist.rb
49
49
  - lib/you_tube/objects/playlist_item.rb
50
+ - lib/you_tube/objects/search_result.rb
50
51
  - lib/you_tube/objects/video.rb
51
52
  - lib/you_tube/resource.rb
52
53
  - lib/you_tube/resources/channels.rb
53
54
  - lib/you_tube/resources/playlist_items.rb
54
55
  - lib/you_tube/resources/playlists.rb
56
+ - lib/you_tube/resources/search.rb
55
57
  - lib/you_tube/resources/videos.rb
56
58
  - lib/you_tube/version.rb
57
59
  - lib/youtuberb.rb
@@ -62,7 +64,7 @@ licenses:
62
64
  metadata:
63
65
  homepage_uri: https://deanpcmad.com
64
66
  source_code_uri: https://github.com/deanpcmad/youtuberb
65
- post_install_message:
67
+ post_install_message:
66
68
  rdoc_options: []
67
69
  require_paths:
68
70
  - lib
@@ -77,8 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
79
  - !ruby/object:Gem::Version
78
80
  version: '0'
79
81
  requirements: []
80
- rubygems_version: 3.2.22
81
- signing_key:
82
+ rubygems_version: 3.4.10
83
+ signing_key:
82
84
  specification_version: 4
83
85
  summary: A Ruby library for interacting with the YouTube API
84
86
  test_files: []