Class: Purdie::Services::YouTube
- Inherits:
-
Object
- Object
- Purdie::Services::YouTube
- Includes:
- Purdie::Service
- Defined in:
- lib/purdie/services/youtube.rb
Constant Summary collapse
- API_SERVICE_NAME =
'youtube'- API_VERSION =
'v3'
Class Method Summary collapse
Instance Method Summary collapse
- #client ⇒ Object
- #configure ⇒ Object
- #distill(url) ⇒ Object
- #get(url) ⇒ Object
- #get_data(id, part) ⇒ Object
- #yt_service ⇒ Object
Methods included from Purdie::Service
Class Method Details
.client ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/purdie/services/youtube.rb', line 83 def self.client Google::APIClient.new( key: ENV['YOUTUBE_API_KEY'], authorization: nil, application_name: self.class.name.split('::').first, application_version: Purdie::VERSION ) end |
.matcher ⇒ Object
99 100 101 |
# File 'lib/purdie/services/youtube.rb', line 99 def self.matcher 'youtube.com' end |
.resolve(url) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/purdie/services/youtube.rb', line 65 def self.resolve url query = CGI.parse(URI.parse(url).query).keys return [url] unless (query.include?('list') and not query.include?('index')) set = YouTube.client.execute!( api_method: YouTube.yt_service.playlist_items.list, parameters: { playlistId: 'PLuPLM2FI60-OIgFTc9YCrGgH5XWGT6znV', part: 'contentDetails', maxResults: 50 } ).body ids = JSON.parse(set)['items'].map { |v| v['contentDetails']['videoId'] } ids.map { |id| "https://youtube.com/watch?v=#{id}"} end |
.yt_service ⇒ Object
92 93 94 95 96 97 |
# File 'lib/purdie/services/youtube.rb', line 92 def self.yt_service client.discovered_api( API_SERVICE_NAME, API_VERSION ) end |
Instance Method Details
#client ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/purdie/services/youtube.rb', line 17 def client @client ||= Google::APIClient.new( key: ENV['YOUTUBE_API_KEY'], authorization: nil, application_name: self.class.name.split('::').first, application_version: Purdie::VERSION ) end |
#configure ⇒ Object
11 12 13 14 15 |
# File 'lib/purdie/services/youtube.rb', line 11 def configure @api_service_name = 'youtube' @api_version = 'v3' super end |
#distill(url) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/purdie/services/youtube.rb', line 53 def distill url video = get url results = {} results['title'] = video['items'][0]['snippet']['localized']['title'] results['id'] = Purdie.get_id(url) results.attach_license self, video['items'][0]['status']['license'] results end |
#get(url) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/purdie/services/youtube.rb', line 43 def get url begin data = get_data Purdie.get_id(url), 'status,snippet' JSON.parse data.body rescue Google::APIClient::ClientError => ce raise Purdie::CredentialsException.new self, 'missing' if ce..match /Daily Limit for Unauthenticated Use Exceeded/ raise Purdie::CredentialsException.new self, 'duff' if ce..match /Bad Request/ end end |
#get_data(id, part) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/purdie/services/youtube.rb', line 33 def get_data id, part client.execute!( api_method: yt_service.videos.list, parameters: { id: id, part: part } ) end |
#yt_service ⇒ Object
26 27 28 29 30 31 |
# File 'lib/purdie/services/youtube.rb', line 26 def yt_service @yt_service ||= client.discovered_api( @api_service_name, @api_version ) end |