bbc_iplayer_search 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d26340cd9ec4e64cfdda0cc46855ae4f0498184e
4
+ data.tar.gz: 4646bde41dfac8e19b4d9656fe0971454dd23aa5
5
+ SHA512:
6
+ metadata.gz: a22b35e994f56cd8b1e392322e9701836e552e5a93bf90bf42e56d1bb562d185926529548cb4d4f4c4eaf6e2ae547c3f0742869bd8cd6a1d8167e4b28e63e96e
7
+ data.tar.gz: 88b2c0a0fec149fbdd7b03553aa784e144a583e599005dd400c890e621794309bccfd2108086922ee6d5b8e1f21a85894b8f1ff35dab066109e3dea484ef459e
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Joel Chippindale
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
6
+ rescue LoadError
7
+ # no rspec available
8
+ end
@@ -0,0 +1,4 @@
1
+ require 'bbc_iplayer_search/search_results_page_not_recognised'
2
+ require 'bbc_iplayer_search/result_parser'
3
+ require 'bbc_iplayer_search/search'
4
+
@@ -0,0 +1,34 @@
1
+ module BBCIplayerSearch
2
+ class ResultParser
3
+ def initialize(fragment)
4
+ @fragment = fragment
5
+ end
6
+
7
+ def title
8
+ fragment.css('.title').first.content.strip
9
+ end
10
+
11
+ def url
12
+ u = URI.parse(extract_path_or_url)
13
+ u.host ||= 'www.bbc.co.uk'
14
+ u.scheme ||= 'http'
15
+ u.to_s
16
+ end
17
+
18
+ def image_url
19
+ fragment.css('.r-image').first.attributes['data-ip-src'].value
20
+ end
21
+
22
+ def available?
23
+ fragment.css('.unavailable').empty?
24
+ end
25
+
26
+ private
27
+ attr_reader :fragment
28
+
29
+ def extract_path_or_url
30
+ fragment.css('a').first.attributes['href'].value
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,39 @@
1
+ require 'uri'
2
+ require 'nokogiri'
3
+ require 'httpclient'
4
+
5
+ module BBCIplayerSearch
6
+ class Search
7
+ def search(query)
8
+ r = response(query)
9
+ programmes = programme_fragments(r.body).map do |f|
10
+ rp = ResultParser.new(f)
11
+ {
12
+ :title => rp.title,
13
+ :url => rp.url,
14
+ :image_url => rp.image_url,
15
+ :available => rp.available?
16
+ }
17
+ end
18
+
19
+ if programmes.empty? & !no_results_page?(r.body)
20
+ raise BBCIplayerSearch::SearchResultsPageNotRecognised
21
+ else
22
+ programmes
23
+ end
24
+ end
25
+
26
+ private
27
+ def no_results_page?(page)
28
+ page.include?('There are no results for')
29
+ end
30
+
31
+ def response(query)
32
+ HTTPClient.new.get('http://www.bbc.co.uk/iplayer/search', { 'q' => query })
33
+ end
34
+
35
+ def programme_fragments(page)
36
+ Nokogiri::HTML(page).css('.iplayer-list li.list-item')
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,4 @@
1
+ module BBCIplayerSearch
2
+ class SearchResultsPageNotRecognised < StandardError
3
+ end
4
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'A search' do
4
+ context 'with zero results', :vcr do
5
+ it { expect(BBCIplayerSearch::Search.new.search('qqqqqqqqqqqqqqqqq')).to be_empty }
6
+ end
7
+
8
+ context 'with some results', :vcr do
9
+ subject { BBCIplayerSearch::Search.new.search('eastenders') }
10
+
11
+ it { expect(subject).to_not be_empty }
12
+ it { expect(subject.first[:title]).to eq('EastEnders') }
13
+ it { expect(subject.first[:url]).to match(%r{^http://www.bbc.co.uk/iplayer/episode/.*}) }
14
+ it { expect(subject.first[:image_url]).to match(%r{http://ichef.bbci.co.uk/images/ic/.*\.jpg}) }
15
+ end
16
+
17
+ context 'with unrecognised page format returned' do
18
+ before do
19
+ VCR.turn_off!
20
+
21
+ stub_request(:get, 'http://www.bbc.co.uk/iplayer/search?q=eastenders').
22
+ to_return(:body => '<html><body><h1>Not what you expected</h1></body></html>')
23
+ end
24
+
25
+ after do
26
+ VCR.turn_on!
27
+ end
28
+
29
+ it do
30
+ expect {
31
+ BBCIplayerSearch::Search.new.search('eastenders')
32
+ }.to raise_error(BBCIplayerSearch::SearchResultsPageNotRecognised)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe BBCIplayerSearch::ResultParser do
4
+ let(:title) { 'Citizen Kane' }
5
+ let(:path) { '/iplayer/episode/b0074n82/citizen-kane' }
6
+ let(:image_url) { 'http://ichef.bbci.co.uk/images/ic/336x189/p01gct3l.jpg' }
7
+ let(:fragment) {
8
+ Nokogiri::HTML::DocumentFragment.parse("<li class='list-item'>
9
+ <a href='/iplayer/episode/b0074n82/citizen-kane'>
10
+ <div class='title'>Citizen Kane</div>
11
+ <div>
12
+ <div class='r-image' data-ip-src='#{image_url}'>
13
+ </div>
14
+ </div>
15
+ </a>
16
+ </li>")
17
+ }
18
+
19
+ subject { BBCIplayerSearch::ResultParser.new(fragment) }
20
+
21
+ describe '#title' do
22
+ it { expect(subject.title).to eq(title) }
23
+ end
24
+
25
+ describe '#url' do
26
+ it { expect(subject.url).to eq('http://www.bbc.co.uk/iplayer/episode/b0074n82/citizen-kane') }
27
+ end
28
+
29
+ describe '#image_url' do
30
+ it { expect(subject.image_url).to eq(image_url) }
31
+ end
32
+
33
+ describe '#available' do
34
+ it { expect(subject.available?).to eq(true) }
35
+
36
+ context 'when not available' do
37
+ let(:fragment) {
38
+ Nokogiri::HTML::DocumentFragment.parse("<li class='list-item unavailable'>
39
+ </li>")
40
+ }
41
+
42
+ it { expect(subject.available?).to eq(false) }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,106 @@
1
+ require 'vcr'
2
+ require 'webmock/rspec'
3
+ require 'bbc_iplayer_search'
4
+
5
+ VCR.configure do |c|
6
+ c.cassette_library_dir = 'spec/cassettes'
7
+ c.hook_into :webmock
8
+ c.configure_rspec_metadata!
9
+ end
10
+
11
+ # This file was generated by the `rspec --init` command. Conventionally, all
12
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
13
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
14
+ # this file to always be loaded, without a need to explicitly require it in any
15
+ # files.
16
+ #
17
+ # Given that it is always loaded, you are encouraged to keep this file as
18
+ # light-weight as possible. Requiring heavyweight dependencies from this file
19
+ # will add to the boot time of your test suite on EVERY test run, even for an
20
+ # individual file that may not need all of that loaded. Instead, consider making
21
+ # a separate helper file that requires the additional dependencies and performs
22
+ # the additional setup, and require it from the spec files that actually need
23
+ # it.
24
+ #
25
+ # The `.rspec` file also contains a few flags that are not defaults but that
26
+ # users commonly want.
27
+ #
28
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
29
+ RSpec.configure do |config|
30
+ # rspec-expectations config goes here. You can use an alternate
31
+ # assertion/expectation library such as wrong or the stdlib/minitest
32
+ # assertions if you prefer.
33
+ config.expect_with :rspec do |expectations|
34
+ # This option will default to `true` in RSpec 4. It makes the `description`
35
+ # and `failure_message` of custom matchers include text for helper methods
36
+ # defined using `chain`, e.g.:
37
+ # be_bigger_than(2).and_smaller_than(4).description
38
+ # # => "be bigger than 2 and smaller than 4"
39
+ # ...rather than:
40
+ # # => "be bigger than 2"
41
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
42
+ end
43
+
44
+ # rspec-mocks config goes here. You can use an alternate test double
45
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
46
+ config.mock_with :rspec do |mocks|
47
+ # Prevents you from mocking or stubbing a method that does not exist on
48
+ # a real object. This is generally recommended, and will default to
49
+ # `true` in RSpec 4.
50
+ mocks.verify_partial_doubles = true
51
+ end
52
+
53
+ # The settings below are suggested to provide a good initial experience
54
+ # with RSpec, but feel free to customize to your heart's content.
55
+ =begin
56
+ # These two settings work together to allow you to limit a spec run
57
+ # to individual examples or groups you care about by tagging them with
58
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
59
+ # get run.
60
+ config.filter_run :focus
61
+ config.run_all_when_everything_filtered = true
62
+
63
+ # Allows RSpec to persist some state between runs in order to support
64
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
65
+ # you configure your source control system to ignore this file.
66
+ config.example_status_persistence_file_path = "spec/examples.txt"
67
+
68
+ # Limits the available syntax to the non-monkey patched syntax that is
69
+ # recommended. For more details, see:
70
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
71
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
72
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
73
+ config.disable_monkey_patching!
74
+
75
+ # This setting enables warnings. It's recommended, but in some cases may
76
+ # be too noisy due to issues in dependencies.
77
+ config.warnings = true
78
+
79
+ # Many RSpec users commonly either run the entire suite or an individual
80
+ # file, and it's useful to allow more verbose output when running an
81
+ # individual spec file.
82
+ if config.files_to_run.one?
83
+ # Use the documentation formatter for detailed output,
84
+ # unless a formatter has already been configured
85
+ # (e.g. via a command-line flag).
86
+ config.default_formatter = 'doc'
87
+ end
88
+
89
+ # Print the 10 slowest examples and example groups at the
90
+ # end of the spec run, to help surface which specs are running
91
+ # particularly slow.
92
+ config.profile_examples = 10
93
+
94
+ # Run specs in random order to surface order dependencies. If you find an
95
+ # order dependency and want to debug it, you can fix the order by providing
96
+ # the seed, which is printed after each run.
97
+ # --seed 1234
98
+ config.order = :random
99
+
100
+ # Seed global randomization in this process using the `--seed` CLI option.
101
+ # Setting this allows you to use `--seed` to deterministically reproduce
102
+ # test failures related to randomization by passing the same `--seed` value
103
+ # as the one that triggered the failure.
104
+ Kernel.srand config.seed
105
+ =end
106
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bbc_iplayer_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joel Chippindale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpclient
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.22'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.22'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Built with some wonky page scraping
98
+ email: joel@joelchippindale.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - LICENSE.txt
104
+ - Rakefile
105
+ - lib/bbc_iplayer_search.rb
106
+ - lib/bbc_iplayer_search/result_parser.rb
107
+ - lib/bbc_iplayer_search/search.rb
108
+ - lib/bbc_iplayer_search/search_results_page_not_recognised.rb
109
+ - spec/feature/search_spec.rb
110
+ - spec/lib/bbc_iplayer_search/result_parser_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/mocoso/bbc_iplayer_search
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.8
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: A simple search API for BBC iplayer
136
+ test_files: []