From 68c113954112f6fdb701ac5738f4d89e4477b8b8 Mon Sep 17 00:00:00 2001 From: pikesley Date: Fri, 22 Jan 2016 16:43:17 +0000 Subject: [PATCH 1/3] Write SoundCloud URLs mostly for JSON API --- features/soundcloud-set.feature | 4 ++++ features/soundcloud.feature | 3 +++ lib/purdie/services/soundcloud.rb | 3 ++- lib/purdie/version.rb | 2 +- spec/purdie/services/soundcloud_spec.rb | 2 ++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/features/soundcloud-set.feature b/features/soundcloud-set.feature index f431be3..1ab97e7 100644 --- a/features/soundcloud-set.feature +++ b/features/soundcloud-set.feature @@ -13,24 +13,28 @@ Feature: Soundcloud set --- - title: Hexaflexagon id: 193008299 + url: http://soundcloud.com/rawfunkmaharishi/hexaflexagon-1 location: Islington Academy date: '2015-02-18' license: Attribution-NonCommercial-ShareAlike license_url: http://creativecommons.org/licenses/by-nc-sa/4.0/ - title: Beer, Of Course, But Why id: 193006525 + url: http://soundcloud.com/rawfunkmaharishi/beer-of-course-but-why location: Islington Academy date: '2015-02-18' license: Attribution-NonCommercial-ShareAlike license_url: http://creativecommons.org/licenses/by-nc-sa/4.0/ - title: Junalbandi id: 193005357 + url: http://soundcloud.com/rawfunkmaharishi/junalbandi-3 location: Islington Academy date: '2015-02-18' license: Attribution-NonCommercial-ShareAlike license_url: http://creativecommons.org/licenses/by-nc-sa/4.0/ - title: Bernard id: 192841052 + url: http://soundcloud.com/rawfunkmaharishi/bernard location: Islington Academy date: '2015-02-18' license: Attribution-NonCommercial-ShareAlike diff --git a/features/soundcloud.feature b/features/soundcloud.feature index b05db80..e89c4b3 100644 --- a/features/soundcloud.feature +++ b/features/soundcloud.feature @@ -12,6 +12,7 @@ Feature: Soundcloud """ - title: Hexaflexagon id: 193008299 + url: http://soundcloud.com/rawfunkmaharishi/hexaflexagon-1 location: Islington Academy date: '2015-02-18' license: Attribution-NonCommercial-ShareAlike @@ -30,12 +31,14 @@ Feature: Soundcloud """ - title: Hexaflexagon id: 193008299 + url: http://soundcloud.com/rawfunkmaharishi/hexaflexagon-1 location: Islington Academy date: '2015-02-18' license: Attribution-NonCommercial-ShareAlike license_url: http://creativecommons.org/licenses/by-nc-sa/4.0/ - title: Junalbandi id: 193005357 + url: http://soundcloud.com/rawfunkmaharishi/junalbandi-3 location: Islington Academy date: '2015-02-18' license: Attribution-NonCommercial-ShareAlike diff --git a/lib/purdie/services/soundcloud.rb b/lib/purdie/services/soundcloud.rb index 10572d0..4831bf5 100644 --- a/lib/purdie/services/soundcloud.rb +++ b/lib/purdie/services/soundcloud.rb @@ -21,7 +21,8 @@ def distill url results = {} results['title'] = track['title'] results['id'] = track['id'] - + results['url'] = track['permalink_url'] + description = YAML.load track['description'] if description.class == Hash description.keys.each do |k| diff --git a/lib/purdie/version.rb b/lib/purdie/version.rb index c620751..04042c0 100644 --- a/lib/purdie/version.rb +++ b/lib/purdie/version.rb @@ -1,3 +1,3 @@ module Purdie - VERSION = "0.1.1al" + VERSION = "0.1.2" end diff --git a/spec/purdie/services/soundcloud_spec.rb b/spec/purdie/services/soundcloud_spec.rb index 23ec4da..2a615e4 100644 --- a/spec/purdie/services/soundcloud_spec.rb +++ b/spec/purdie/services/soundcloud_spec.rb @@ -17,6 +17,7 @@ module Services "--- title: Hexaflexagon id: 193008299 +url: http://soundcloud.com/rawfunkmaharishi/hexaflexagon-1 location: Islington Academy date: '2015-02-18' license: Attribution-NonCommercial-ShareAlike @@ -31,6 +32,7 @@ module Services "--- title: Don The Crown id: 234830006 +url: http://soundcloud.com/rawfunkmaharishi/don-the-crown-1 location: Rogue Studios engineer: Alessio date: '2015-11-25' From 514c16cc8ac763b783243db7db2ac3925bb6dbf9 Mon Sep 17 00:00:00 2001 From: pikesley Date: Fri, 22 Jan 2016 17:24:53 +0000 Subject: [PATCH 2/3] WTF travis? --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2fc2edc..369b025 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: ruby rvm: -- 2.2.0 +- 2.3.0 env: global: - secure: Pp7JaGVsZyPJPaZHgpWEibRDtpg4aWWTC9yb7SITdXTJ7AN5gZPLIyNuYfY937Ypq2JoJAI2yhJzfMPTQJ/FwF8OV1d9dUZyDv3R6aMsE41xJoL6X2bLtyh3TduULSIeiemoOEk4Y5JqOJb8JEyFMd0n+0AD+hn+HBPysIfwgHM= From 9272273415ece33fe76c95d5249ef1357ba766a3 Mon Sep 17 00:00:00 2001 From: pikesley Date: Fri, 22 Jan 2016 20:51:18 +0000 Subject: [PATCH 3/3] bollocks to YouTube --- README.md | 7 +- features/youtube-playlist.feature | 52 -------------- features/youtube.feature | 20 ------ lib/purdie.rb | 1 - lib/purdie/services/youtube.rb | 104 --------------------------- spec/purdie/duff_keys_spec.rb | 26 ------- spec/purdie/resolver_spec.rb | 6 -- spec/purdie/services/youtube_spec.rb | 40 ----------- 8 files changed, 2 insertions(+), 254 deletions(-) delete mode 100644 features/youtube-playlist.feature delete mode 100644 features/youtube.feature delete mode 100644 lib/purdie/services/youtube.rb delete mode 100644 spec/purdie/services/youtube_spec.rb diff --git a/README.md b/README.md index 13de1fd..c7f20ac 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ [![Code Climate](http://img.shields.io/codeclimate/github/rawfunkmaharishi/purdie.svg?style=flat-square)](https://codeclimate.com/github/rawfunkmaharishi/purdie) [![Gem Version](http://img.shields.io/gem/v/purdie.svg?style=flat-square)](https://rubygems.org/gems/purdie) [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://rawfunkmaharishi.mit-license.org) -[![Badges](http://img.shields.io/:badges-7/7-ff6799.svg?style=flat-square)](https://github.com/badges/badgerbadgerbadger) #Purdie @@ -67,9 +66,7 @@ You also need a *.env* file with the relevant credentials in it: VIMEO_BEARER_TOKEN: this_is_bearer_token - YOUTUBE_API_KEY: this_is_key_for_youtube - -(get those things from [Flickr](https://www.flickr.com/services/apps/create/apply), [SoundCloud](http://soundcloud.com/you/apps/new), [Vimeo](https://developer.vimeo.com/apps/new) and [YouTube](https://console.developers.google.com/project)) +(get those things from [Flickr](https://www.flickr.com/services/apps/create/apply), [SoundCloud](http://soundcloud.com/you/apps/new) and [Vimeo](https://developer.vimeo.com/apps/new) And then you can run @@ -108,7 +105,7 @@ Tread carefully for now, because my metadata hacks aren't fully documented, and ##What next? -There's no reason I couldn't support other services - I've already added [YouTube](https://github.com/rawfunkmaharishi/purdie/blob/master/spec/services/youtube_spec.rb) [support](https://github.com/rawfunkmaharishi/purdie/blob/master/lib/purdie/services/youtube.rb) and others should be fairly simple. There's some introspection magic at the heart of all of this which means that as long as each service is represented by a class that: +There's no reason I couldn't support other services. There's some introspection magic at the heart of all of this which means that as long as each service is represented by a class that: * includes the `Purdie::Service` module, and * sports a `::matcher` class method which returns a string which will pick a URL out of an input file, and diff --git a/features/youtube-playlist.feature b/features/youtube-playlist.feature deleted file mode 100644 index 0cffbde..0000000 --- a/features/youtube-playlist.feature +++ /dev/null @@ -1,52 +0,0 @@ -@vcr -Feature: YouTube playlists - - Scenario: Generate YAML for a YouTube playlist - Given a file named "_sources/youtube" with: - """ - https://www.youtube.com/playlist?list=PLuPLM2FI60-OIgFTc9YCrGgH5XWGT6znV - """ - When I successfully run `purdie fetch` - Then a file named "_data/youtube.yaml" should exist - And the file "_data/youtube.yaml" should contain: - """ - --- - - title: Chuck Rainey - Bernard Purdie Project "Love the One Your With...Drink Muddy - Water" - id: U23Ezi6q30E - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - - title: Godfathers of Groove Bernard "Pretty" Purdie,Grant Green Jr,Ruben Wilson - March 30-31 - id: _ZbWTg8G6eM - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - - title: Pretty Purdie - Good Livin' (Good Lovin') HQ - id: zJnnNZk9o0Q - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - - title: Hot Popcorn Bernard Purdie - id: baQe6MoSAHw - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - - title: Red Beans and Rice Bernard Purdie - id: NLFP1T1e2BA - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - - title: Bernard Purdie - Ad Lib - id: E9E0WxLbqVA - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - - title: Bernard Purdie - Black Purd's Theme - id: wa-K4LouFVk - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - - title: Bernard "Pretty" Purdie - Heavy Soul Slinger - id: LatmKZQd7-s - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - - title: 'Bernard "Pretty" Purdie: Funky Grooves in Japan' - id: P842kq0bnOc - license: YouTube - license_url: https://www.youtube.com/static?gl=GB&template=terms - """ diff --git a/features/youtube.feature b/features/youtube.feature deleted file mode 100644 index 28f13e3..0000000 --- a/features/youtube.feature +++ /dev/null @@ -1,20 +0,0 @@ -@vcr -Feature: YouTube - - Scenario: Generate YouTube YAML - Given a file named "_sources/youtube.movies" with: - """ - https://www.youtube.com/watch?v=Qt_J0jNqtZg&list=PLuPLM2FI60-M0-aWejF9WgB-Dkt1TuQXv&index=2 - """ - When I successfully run `purdie fetch` - Then a file named "_data/youtube.yaml" should exist - And the file "_data/youtube.yaml" should contain: - """ - - title: Sam Pikesley – Vandalising your Github commit history for fun and profit - id: Qt_J0jNqtZg - license: Attribution - license_url: https://creativecommons.org/licenses/by/3.0/ - """ - And a file named "_data/soundcloud.yaml" should not exist - And a file named "_data/vimeo.yaml" should not exist - And a file named "_data/flickr.yaml" should not exist diff --git a/lib/purdie.rb b/lib/purdie.rb index 94fc9b9..6daec9a 100644 --- a/lib/purdie.rb +++ b/lib/purdie.rb @@ -20,4 +20,3 @@ require 'purdie/services/soundcloud' require 'purdie/services/flickr' require 'purdie/services/vimeo' -require 'purdie/services/youtube' diff --git a/lib/purdie/services/youtube.rb b/lib/purdie/services/youtube.rb deleted file mode 100644 index 8697ff1..0000000 --- a/lib/purdie/services/youtube.rb +++ /dev/null @@ -1,104 +0,0 @@ -require 'google/api_client' - -module Purdie - module Services - class YouTube - include Service - - API_SERVICE_NAME = 'youtube' - API_VERSION = 'v3' - - def configure - @api_service_name = 'youtube' - @api_version = 'v3' - super - end - - 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 - - def yt_service - @yt_service ||= client.discovered_api( - @api_service_name, - @api_version - ) - end - - def get_data id, part - client.execute!( - api_method: yt_service.videos.list, - parameters: { - id: id, - part: part - } - ) - end - - 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.message.match /Daily Limit for Unauthenticated Use Exceeded/ - raise Purdie::CredentialsException.new self, 'duff' if ce.message.match /Bad Request/ - end - end - - 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 - - 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 - - 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 - - def self.yt_service - client.discovered_api( - API_SERVICE_NAME, - API_VERSION - ) - end - - def self.matcher - 'youtube.com' - end - end - end -end diff --git a/spec/purdie/duff_keys_spec.rb b/spec/purdie/duff_keys_spec.rb index df3611c..6d2aabf 100644 --- a/spec/purdie/duff_keys_spec.rb +++ b/spec/purdie/duff_keys_spec.rb @@ -85,31 +85,5 @@ module Services } end end - - describe YouTube do - after :each do - reset_env - end - - it 'responds usefully in the face of no credentials' do - unset_env - y = YouTube.new - expect { y.distill 'https://www.youtube.com/watch?v=JCix1XW329g' }.to raise_exception { |e| - expect(e).to be_a Purdie::CredentialsException - expect(e.service.class).to eq Purdie::Services::YouTube - expect(e.message).to eq 'missing' - } - end - - it 'responds usefully in the face of duff credentials' do - randomise_env - y = YouTube.new - expect { y.distill 'https://www.youtube.com/watch?v=JCix1XW329g' }.to raise_exception { |e| - expect(e).to be_a Purdie::CredentialsException - expect(e.service.class).to eq Purdie::Services::YouTube - expect(e.message).to eq 'duff' - } - end - end end end diff --git a/spec/purdie/resolver_spec.rb b/spec/purdie/resolver_spec.rb index 5753384..387e7d7 100644 --- a/spec/purdie/resolver_spec.rb +++ b/spec/purdie/resolver_spec.rb @@ -30,11 +30,5 @@ module Purdie expect(Purdie.strip_scheme resolved[0]).to eq '//soundcloud.com/rawfunkmaharishi/beer-of-course-but-why' expect(Purdie.strip_scheme resolved[2]).to eq '//soundcloud.com/rawfunkmaharishi/junalbandi-3' end - - it 'resolves a YouTube playlist', :vcr do - resolved = Resolver.resolve 'https://www.youtube.com/playlist?list=PLuPLM2FI60-OIgFTc9YCrGgH5XWGT6znV' - expect(resolved.count).to eq 9 - expect(Purdie.strip_scheme resolved.first).to eq '//youtube.com/watch?v=U23Ezi6q30E' - end end end diff --git a/spec/purdie/services/youtube_spec.rb b/spec/purdie/services/youtube_spec.rb deleted file mode 100644 index 1caf697..0000000 --- a/spec/purdie/services/youtube_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'spec_helper' - -module Purdie - module Services - describe YouTube do - before :each do - @yt = YouTube.new - end - - it 'gets a video', :vcr do - expect(@yt.get 'https://www.youtube.com/watch?v=JCix1XW329g').to be_a Hash - end - - it 'has yaml for a video', :vcr do - dist = @yt.distill 'https://www.youtube.com/watch?v=JCix1XW329g' - expect(dist.to_yaml).to eq( -"--- -title: EMFCamp 2014 Day 3 Lightning talk 6 -id: JCix1XW329g -license: Attribution -license_url: https://creativecommons.org/licenses/by/3.0/ -" - ) - end - - it 'gets the correct license', :vcr do - vid = @yt.distill 'https://www.youtube.com/watch?v=baQe6MoSAHw' - expect(vid['license']).to eq 'YouTube' - end - - context 'resolve a playlist', :vcr do - it 'resolves a playlist from a shitty YouTube URL', :vcr do - list = YouTube.resolve 'https://www.youtube.com/playlist?list=PLuPLM2FI60-OIgFTc9YCrGgH5XWGT6znV' - expect(list.count).to eq 9 - expect(list[8]).to eq 'https://youtube.com/watch?v=P842kq0bnOc' - end - end - end - end -end