talktalk_tv 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1139b80a3cf64b98bb8408549b291370e21797e5
4
+ data.tar.gz: 40005b17dd1c235139cb0c6906ce4b07126ef667
5
+ SHA512:
6
+ metadata.gz: 7c28bd4cb4f6e5434c7071225fd5eccb496d501a2c5f73cde338781e818fb6878ff8278def11dbe457cdf6318adc07eb6fdb1d0756063a87d07991b7c5b11a3b
7
+ data.tar.gz: 35276117d945d5f8eba8bc2592501d07b431b0597220f529b4ca1c3877291866c52dca495cbb16b8e11ab0447ad7cbf99ab88e3bbcec95a85f3b8f35c26ee427
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,26 @@
1
+ module TalkTalkTV
2
+ class Film
3
+ def initialize(
4
+ title:,
5
+ url: nil,
6
+ image_url: nil,
7
+ certificate: nil,
8
+ release_year: nil,
9
+ running_time_in_minutes: nil,
10
+ buy_price: nil,
11
+ rental_price: nil
12
+ )
13
+ @title = title
14
+ @url = url
15
+ @image_url = image_url
16
+ @certificate = certificate
17
+ @release_year = release_year
18
+ @running_time_in_minutes = running_time_in_minutes
19
+ @buy_price = buy_price
20
+ @rental_price = rental_price
21
+ end
22
+
23
+ attr_reader :title, :url, :image_url, :certificate, :release_year,
24
+ :running_time_in_minutes, :buy_price, :rental_price
25
+ end
26
+ end
@@ -0,0 +1,69 @@
1
+ require 'uri'
2
+ require 'nokogiri'
3
+ require 'httpclient'
4
+
5
+ module TalkTalkTV
6
+ class FilmPage
7
+ def initialize(body:, url: nil)
8
+ @body = body
9
+ @url = url
10
+ end
11
+
12
+ attr_reader :body, :url
13
+
14
+ def self.from_url(url)
15
+ response = HTTPClient.new.get(url)
16
+ FilmPage.new(body: Nokogiri::HTML(response.body), url: url)
17
+ end
18
+
19
+ def film
20
+ Film.new(
21
+ title: title,
22
+ url: url,
23
+ image_url: image_url,
24
+ release_year: release_year,
25
+ certificate: certificate,
26
+ running_time_in_minutes: running_time_in_minutes,
27
+ rental_price: rental_price,
28
+ buy_price: buy_price
29
+ )
30
+ end
31
+
32
+ def title
33
+ body.css('.g-assetTitle').first.content.strip
34
+ end
35
+
36
+ def image_url
37
+ body.css('.c-packShot noscript img').first.attributes['src'].value
38
+ end
39
+
40
+ def release_year
41
+ match = body.css('.g-assetInfo li').map { |n| %r{(\d{4})}.match(n.content) }.compact.first
42
+ match && match[1].to_i
43
+ end
44
+
45
+ def certificate
46
+ match = body.css('.g-assetInfo li').map { |n| %r{CERT (.*)}.match(n.content) }.compact.first
47
+ match && match[1]
48
+ end
49
+
50
+ def running_time_in_minutes
51
+ match = body.css('.g-assetInfo li').map { |n| %r{(\d+) HRS (\d+) MINS}.match(n.content) }.compact.first
52
+ match && (match[1].to_i * 60 + match[2].to_i)
53
+ end
54
+
55
+ def rental_price
56
+ select_price(body, '.c-retailButton--rental .c-retailButton__price > text()')
57
+ end
58
+
59
+ def buy_price
60
+ select_price(body, '.c-retailButton--purchase .c-retailButton__price > text()')
61
+ end
62
+
63
+ private
64
+ def select_price(body, selector)
65
+ price_tag = body.css(selector).first
66
+ price_tag && price_tag.text.strip
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,68 @@
1
+ require 'uri'
2
+ require 'nokogiri'
3
+ require 'httpclient'
4
+
5
+ module TalkTalkTV
6
+ class Search
7
+ def search(query)
8
+ r = response(query)
9
+ films = film_fragments(r.body).map { |f|
10
+ Film.new(
11
+ title: film_title(f),
12
+ url: film_url(f),
13
+ image_url: film_image_url(f),
14
+ certificate: film_certificate(f),
15
+ running_time_in_minutes: film_running_time_in_minutes(f)
16
+ )
17
+ }
18
+
19
+ if films.empty? & !no_results_page?(r.body)
20
+ raise TalkTalkTV::SearchResultsPageNotRecognised
21
+ else
22
+ films
23
+ end
24
+ end
25
+
26
+ private
27
+ def no_results_page?(page)
28
+ page.include?('no results found for')
29
+ end
30
+
31
+ def response(query)
32
+ HTTPClient.new.get('https://www.talktalktvstore.co.uk/search', { 'Search' => query })
33
+ end
34
+
35
+ def film_fragments(page)
36
+ Nokogiri::HTML(page).css('.p-searchResults li.b-assetCollection__item')
37
+ end
38
+
39
+ def film_title(fragment)
40
+ fragment.css('h3').first.content.strip
41
+ end
42
+
43
+ def film_url(fragment)
44
+ u = URI.parse(extract_film_path_or_url(fragment))
45
+ u.host ||= 'www.talktalktvstore.co.uk'
46
+ u.scheme ||= 'https'
47
+ u.to_s
48
+ end
49
+
50
+ def extract_film_path_or_url(fragment)
51
+ fragment.css('h3 a').first.attributes['href'].value
52
+ end
53
+
54
+ def film_image_url(fragment)
55
+ fragment.css('noscript img').first.attributes['src'].value
56
+ end
57
+
58
+ def film_certificate(fragment)
59
+ match = fragment.css('.c-assetCollectionItem__metaDataItem').map { |n| %r{CERT (\S+)}.match(n.content) }.compact.first
60
+ match && match[1]
61
+ end
62
+
63
+ def film_running_time_in_minutes(fragment)
64
+ match = fragment.css('.c-assetCollectionItem__metaDataItem').map { |n| %r{(\d+) HRS? (\d+) MINS?}.match(n.content) }.compact.first
65
+ match && (match[1].to_i * 60 + match[2].to_i)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,4 @@
1
+ module TalkTalkTV
2
+ class SearchResultsPageNotRecognised < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'talktalk_tv/film'
2
+ require 'talktalk_tv/film_page' || raise
3
+ require 'talktalk_tv/search_results_page_not_recognised'
4
+ require 'talktalk_tv/search'
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'talktalk_tv'
3
+
4
+ describe 'A film page', :vcr do
5
+ context 'A film' do
6
+ subject { TalkTalkTV::FilmPage.from_url('https://www.talktalktvstore.co.uk/movies/the-dark-knight-(28710)').film }
7
+
8
+ it { expect(subject.title).to eq('The Dark Knight') }
9
+ it { expect(subject.url).to eq('https://www.talktalktvstore.co.uk/movies/the-dark-knight-(28710)') }
10
+ it { expect(subject.image_url).to eq('https://fa-i-p1.ttcdn.uk/i/contentasset3/000/028/710/gl0lloqf/v=319/w=215;h=306;rm=Crop;q=85/image.jpg') }
11
+ it { expect(subject.release_year).to eq(2008) }
12
+ it { expect(subject.certificate).to eq('12') }
13
+ it { expect(subject.running_time_in_minutes).to eq(152) }
14
+ it { expect(subject.rental_price).to eq('£2.49') }
15
+ it { expect(subject.buy_price).to eq('£6.99') }
16
+ end
17
+
18
+ context 'A single TV series' do
19
+ subject { TalkTalkTV::FilmPage.from_url('https://www.talktalktvstore.co.uk/tv/12-monkeys-(2313)/s01-(3555)').film }
20
+
21
+ it { expect(subject.title).to eq('12 Monkeys') }
22
+ it { expect(subject.url).to eq('https://www.talktalktvstore.co.uk/tv/12-monkeys-(2313)/s01-(3555)') }
23
+ it { expect(subject.image_url).to eq('https://fa-i-p1.ttcdn.uk/i/tvseries/000/002/313/yzr3t1rw/v=319/w=215;h=306;rm=Crop;q=85/image.jpg') }
24
+ it { expect(subject.release_year).to eq(2015) }
25
+ it { expect(subject.certificate).to eq('15') }
26
+ it { expect(subject.running_time_in_minutes).to be_nil }
27
+ it { expect(subject.rental_price).to be_nil }
28
+ it { expect(subject.buy_price).to eq('£9.99') }
29
+ end
30
+ end
31
+
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'talktalk_tv'
3
+
4
+ describe 'A search' do
5
+ context 'with zero results', :vcr do
6
+ it { expect(TalkTalkTV::Search.new.search('qwerty')).to be_empty }
7
+ end
8
+
9
+ context 'with some results', :vcr do
10
+ subject { TalkTalkTV::Search.new.search('dark knight') }
11
+
12
+ it { expect(subject).to_not be_empty }
13
+ it { expect(subject.first.title).to eq('The Dark Knight') }
14
+ it { expect(subject.first.url).to eq('https://www.talktalktvstore.co.uk/movies/the-dark-knight-(28710)') }
15
+ it { expect(subject.first.image_url).to eq('https://fa-i-p1.ttcdn.uk/i/contentasset31/000/028/710/1gkiab4e/v=319/w=234;h=132;rm=Crop;q=85/image.jpg') }
16
+ it { expect(subject.first.certificate).to eq('12') }
17
+ it { expect(subject.first.running_time_in_minutes).to eq(152) }
18
+ end
19
+
20
+ context 'with unrecognised page format returned' do
21
+ before do
22
+ VCR.turn_off!
23
+
24
+ stub_request(:get, "https://www.talktalktvstore.co.uk/search?Search=dark%20knight").
25
+ to_return(:body => '<html><body><h1>Not what you expected</h1></body></html>')
26
+ end
27
+
28
+ after do
29
+ VCR.turn_on!
30
+ end
31
+
32
+ it do
33
+ expect {
34
+ TalkTalkTV::Search.new.search('dark knight')
35
+ }.to raise_error('TalkTalkTV::SearchResultsPageNotRecognised')
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe TalkTalkTV::FilmPage do
4
+ describe '#buy_price' do
5
+ subject { TalkTalkTV::FilmPage.new(body: body) }
6
+
7
+ context 'standard price' do
8
+ let(:body) {
9
+ Nokogiri::HTML('<button type="submit" class="c-retailButton c-retailButton--purchase">
10
+ BUY
11
+ <span class="c-retailButton__price">
12
+ £6.99
13
+ </span>
14
+ </button>')
15
+ }
16
+
17
+ it { expect(subject.buy_price).to eq('£6.99') }
18
+ end
19
+
20
+ context 'discount price' do
21
+ let(:body) {
22
+ Nokogiri::HTML('<button type="submit" class="c-retailButton c-retailButton--purchase">
23
+ BUY
24
+ <span class="c-retailButton__price">
25
+ £3.99
26
+ <span class="c-retailButton__saving c-retailButton__saving--purchase">
27
+ £6.99
28
+ </span>
29
+ </span>
30
+ </button>')
31
+ }
32
+
33
+ it { expect(subject.buy_price).to eq('£3.99') }
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,105 @@
1
+ require 'vcr'
2
+ require 'webmock/rspec'
3
+
4
+ VCR.configure do |c|
5
+ c.cassette_library_dir = 'spec/cassettes'
6
+ c.hook_into :webmock
7
+ c.configure_rspec_metadata!
8
+ end
9
+
10
+ # This file was generated by the `rspec --init` command. Conventionally, all
11
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
12
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
13
+ # this file to always be loaded, without a need to explicitly require it in any
14
+ # files.
15
+ #
16
+ # Given that it is always loaded, you are encouraged to keep this file as
17
+ # light-weight as possible. Requiring heavyweight dependencies from this file
18
+ # will add to the boot time of your test suite on EVERY test run, even for an
19
+ # individual file that may not need all of that loaded. Instead, consider making
20
+ # a separate helper file that requires the additional dependencies and performs
21
+ # the additional setup, and require it from the spec files that actually need
22
+ # it.
23
+ #
24
+ # The `.rspec` file also contains a few flags that are not defaults but that
25
+ # users commonly want.
26
+ #
27
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
28
+ RSpec.configure do |config|
29
+ # rspec-expectations config goes here. You can use an alternate
30
+ # assertion/expectation library such as wrong or the stdlib/minitest
31
+ # assertions if you prefer.
32
+ config.expect_with :rspec do |expectations|
33
+ # This option will default to `true` in RSpec 4. It makes the `description`
34
+ # and `failure_message` of custom matchers include text for helper methods
35
+ # defined using `chain`, e.g.:
36
+ # be_bigger_than(2).and_smaller_than(4).description
37
+ # # => "be bigger than 2 and smaller than 4"
38
+ # ...rather than:
39
+ # # => "be bigger than 2"
40
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
41
+ end
42
+
43
+ # rspec-mocks config goes here. You can use an alternate test double
44
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
45
+ config.mock_with :rspec do |mocks|
46
+ # Prevents you from mocking or stubbing a method that does not exist on
47
+ # a real object. This is generally recommended, and will default to
48
+ # `true` in RSpec 4.
49
+ mocks.verify_partial_doubles = true
50
+ end
51
+
52
+ # The settings below are suggested to provide a good initial experience
53
+ # with RSpec, but feel free to customize to your heart's content.
54
+ =begin
55
+ # These two settings work together to allow you to limit a spec run
56
+ # to individual examples or groups you care about by tagging them with
57
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
58
+ # get run.
59
+ config.filter_run :focus
60
+ config.run_all_when_everything_filtered = true
61
+
62
+ # Allows RSpec to persist some state between runs in order to support
63
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
64
+ # you configure your source control system to ignore this file.
65
+ config.example_status_persistence_file_path = "spec/examples.txt"
66
+
67
+ # Limits the available syntax to the non-monkey patched syntax that is
68
+ # recommended. For more details, see:
69
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
70
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
72
+ config.disable_monkey_patching!
73
+
74
+ # This setting enables warnings. It's recommended, but in some cases may
75
+ # be too noisy due to issues in dependencies.
76
+ config.warnings = true
77
+
78
+ # Many RSpec users commonly either run the entire suite or an individual
79
+ # file, and it's useful to allow more verbose output when running an
80
+ # individual spec file.
81
+ if config.files_to_run.one?
82
+ # Use the documentation formatter for detailed output,
83
+ # unless a formatter has already been configured
84
+ # (e.g. via a command-line flag).
85
+ config.default_formatter = 'doc'
86
+ end
87
+
88
+ # Print the 10 slowest examples and example groups at the
89
+ # end of the spec run, to help surface which specs are running
90
+ # particularly slow.
91
+ config.profile_examples = 10
92
+
93
+ # Run specs in random order to surface order dependencies. If you find an
94
+ # order dependency and want to debug it, you can fix the order by providing
95
+ # the seed, which is printed after each run.
96
+ # --seed 1234
97
+ config.order = :random
98
+
99
+ # Seed global randomization in this process using the `--seed` CLI option.
100
+ # Setting this allows you to use `--seed` to deterministically reproduce
101
+ # test failures related to randomization by passing the same `--seed` value
102
+ # as the one that triggered the failure.
103
+ Kernel.srand config.seed
104
+ =end
105
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: talktalk_tv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Joel Chippindale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-28 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/talktalk_tv.rb
106
+ - lib/talktalk_tv/film.rb
107
+ - lib/talktalk_tv/film_page.rb
108
+ - lib/talktalk_tv/search.rb
109
+ - lib/talktalk_tv/search_results_page_not_recognised.rb
110
+ - spec/feature/film_page_spec.rb
111
+ - spec/feature/search_spec.rb
112
+ - spec/lib/talktalk_tv/film_page_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: https://github.com/mocoso/talktalk_tv
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.5.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: A simple search API for https://www.talktalktvstore.co.uk
138
+ test_files: []