tvrage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tvrage.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Erik van der Wal
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1 @@
1
+ Simple library to access the TVRage XML feeds
@@ -0,0 +1,29 @@
1
+ # Tvrage
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tvrage'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tvrage
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ require "tvrage/version"
2
+
3
+ require 'net/http'
4
+ require 'open-uri'
5
+ require 'nokogiri'
6
+ require 'date'
7
+ require 'time'
8
+ require 'timeout'
9
+
10
+ directory = File.expand_path(File.dirname(__FILE__))
11
+ require File.join(directory, 'tvrage', 'client')
12
+ require File.join(directory, 'tvrage', 'tvshow')
13
+ require File.join(directory, 'tvrage', 'episode')
@@ -0,0 +1,63 @@
1
+ module Tvrage
2
+
3
+ class Client
4
+
5
+ def search(query)
6
+ response = get("/search.php?show=#{URI.escape(query)}")
7
+ response.xpath('//Results/show').collect { |r| {
8
+ 'id' => r.xpath('showid').text.to_i,
9
+ 'name' => r.xpath('name').text,
10
+ 'country' => r.xpath('country').text,
11
+ 'started' => r.xpath('started').text.to_i,
12
+ 'ended' => r.xpath('ended').text.to_i,
13
+ 'seasons' => r.xpath('seasons').text.to_i,
14
+ 'status' => r.xpath('status').text,
15
+ 'classification' => r.xpath('classification').text
16
+ } }
17
+ end
18
+
19
+ def tvshow_by_id(tvshow_id, full_info = false)
20
+ response = get("/full_show_info.php?sid=#{tvshow_id}")
21
+ Tvshow.new(response.xpath('//Show').first) unless response.xpath('//Show/name').text.empty?
22
+ end
23
+
24
+ def updates_since(time)
25
+ if time.is_a?(Date)
26
+ time = time.to_time.to_i
27
+ elsif time.is_a?(Time)
28
+ time = time.to_time.to_i
29
+ elsif !time.is_a?(Integer)
30
+ raise "Incorrect time supplied. This needs to be either a Date, Time or Integer"
31
+ end
32
+
33
+ url = "/last_updates.php?"
34
+ url << ((time < 1000) ? "hours=#{time}" : "since=#{time}")
35
+
36
+ response = get(url)
37
+
38
+ output = {}
39
+ output['time'] = response.xpath("//updates").attr("at").text.to_i
40
+ output['showing'] = response.xpath("//updates").attr("showing").text
41
+ output['tvshows'] = response.xpath("//show/id").collect { |s| s.text.to_i }
42
+ output
43
+ end
44
+
45
+ def base_url
46
+ # "http://www.tvrage.com/feeds"
47
+ "http://services.tvrage.com/feeds"
48
+ end
49
+
50
+ private
51
+ def get(uri)
52
+ uri = URI.parse("#{base_url}#{uri}")
53
+ response = Net::HTTP.get_response(uri)
54
+ if response.code.to_i == 404
55
+ raise RuntimeError, "The api returned status code #{response.code} for #{uri}"
56
+ end
57
+
58
+ Nokogiri::XML(response.body)
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,25 @@
1
+ module Tvrage
2
+
3
+ class Episode
4
+
5
+ attr_accessor :season, :number, :product_number, :link, :title, :air_date, :screencap
6
+
7
+ def initialize(data, season_number = 0)
8
+ @season = season_number.to_i
9
+ @number = data.xpath('seasonnum').text.to_i
10
+ @product_number = data.xpath('prodnum').text
11
+ @link = data.xpath('link').text
12
+ @title = data.xpath('title').text
13
+ @screencap = data.xpath('screencap').text if !data.xpath('screencap').text.empty?
14
+
15
+ begin
16
+ @air_date = Date.parse(data.xpath('airdate').text) if !data.xpath('airdate').text.empty?
17
+ rescue Exception => e
18
+ # air date is invalid
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,48 @@
1
+ module Tvrage
2
+
3
+ class Tvshow
4
+
5
+ attr_accessor :id, :name, :season_count, :seasons, :started, :ended, :link, :image, :country, :status, :classification, :genres, :runtime, :network, :air_time, :air_day, :timezone, :episodes
6
+
7
+ def initialize(data)
8
+ @id = data.xpath('showid').text.to_i
9
+ @name = data.xpath('name').text
10
+ @season_count = data.xpath('totalseasons').text.to_i
11
+ @image = data.xpath('image').text
12
+ @country = data.xpath('origin_country').text
13
+ @link = data.xpath('link').text
14
+ @status = data.xpath('status').text
15
+ @classification = data.xpath('classification').text
16
+ @runtime = data.xpath('runtime').text.to_i
17
+ @network = data.xpath('network').text
18
+ @air_day = data.xpath('air_day').text
19
+ @timezone = data.xpath('timezone').text
20
+ @link = data.xpath('showlink').text
21
+
22
+ begin
23
+ @air_time = Time.parse(data.xpath('airtime').text) if !data.xpath('airtime').text.empty?
24
+ rescue Exception => e
25
+ # invalid air time
26
+ end
27
+
28
+ @genres = data.xpath('genres/genre').map { |g| g.text } if data.xpath('genres/genre').length > 0
29
+ @genres.reject! { |g| g.empty? } unless @genres.nil?
30
+
31
+ begin
32
+ @started = Date.parse(data.xpath('started').text) if !data.xpath('started').text.empty?
33
+ rescue Exception => e
34
+ # invalid start date
35
+ end
36
+
37
+ begin
38
+ @ended = Date.parse(data.xpath('ended').text) if !data.xpath('ended').text.empty?
39
+ rescue Exception => e
40
+ # invalid end date
41
+ end
42
+
43
+ @episodes = data.xpath('Episodelist//Season/episode').map { |e| Episode.new(e, e.parent['no']) }
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ module Tvrage
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
12
+
13
+ require 'tvrage'
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tvrage do
4
+ before(:each) do
5
+ @client = Tvrage::Client.new
6
+ end
7
+
8
+ describe "search" do
9
+ it "should return results on valid search" do
10
+ results = @client.search('Dexter')
11
+ first = results.first
12
+ first['id'].should == 7926
13
+ first['name'].should == "Dexter"
14
+ first['country'].should == "US"
15
+ first['started'].should == 2006
16
+ end
17
+
18
+ it "should return no results on bogus search" do
19
+ results = @client.search('lfkdsjksd')
20
+ results.should be_empty
21
+ end
22
+ end
23
+
24
+ describe "tvshow" do
25
+ it "should return a valid tvshow" do
26
+ result = @client.tvshow_by_id(7926)
27
+ result.should be_a(Tvrage::Tvshow)
28
+ result.id.should == 7926
29
+ result.name.should == "Dexter"
30
+ result.country.should == "US"
31
+ result.timezone == "GMT-5 -DST"
32
+ end
33
+
34
+ it "should have a bunch of episodes" do
35
+ result = @client.tvshow_by_id(7926)
36
+ result.episodes.map { |e| e.should be_a(Tvrage::Episode) }
37
+ result.episodes.count.should > 0
38
+ result.season_count > 0
39
+ end
40
+
41
+ it "should not return a invalid tvshow" do
42
+ result = @client.tvshow_by_id(4238902)
43
+ result.should be_nil
44
+ end
45
+ end
46
+
47
+ describe "episode" do
48
+ it "should be a valid episode" do
49
+ result = @client.tvshow_by_id(7926)
50
+ episode = result.episodes.first
51
+ episode.should be_a(Tvrage::Episode)
52
+ episode.title.should == "Dexter"
53
+ episode.screencap == "http://images.tvrage.com/screencaps/40/7926/408409.jpg"
54
+ end
55
+ end
56
+
57
+ describe "updates" do
58
+ it "should return updates for the past 24 hours" do
59
+ result = @client.updates_since(24)
60
+ result['time'].should_not be_nil
61
+ result['showing'].should eq "Last 24H"
62
+ result['tvshows'].should_not be_empty
63
+ end
64
+
65
+ it "should return updates for the past 48 hours" do
66
+ result = @client.updates_since(48)
67
+ result['time'].should_not be_nil
68
+ result['showing'].should eq "Last 2D"
69
+ result['tvshows'].should_not be_empty
70
+ end
71
+
72
+ it "should return updates since last visit" do
73
+ result = @client.updates_since(Time.now.to_i-14400) # 4 hours ago
74
+ result['time'].should_not be_nil
75
+ result['showing'].should eq "Last 24H"
76
+ result['tvshows'].should_not be_empty
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/tvrage/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Erik van der Wal"]
6
+ gem.email = ["erikvdwal@gmail.com"]
7
+ gem.homepage = "http://www.erikvdwal.nl/"
8
+ gem.description = %q{Simple library to access the TVRage XML feeds}
9
+ gem.summary = %q{Simple library to access the TVRage XML feeds}
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "tvrage"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Tvrage::VERSION
17
+
18
+ gem.add_runtime_dependency "nokogiri"
19
+ gem.add_development_dependency "rspec"
20
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tvrage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erik van der Wal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple library to access the TVRage XML feeds
47
+ email:
48
+ - erikvdwal@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE
57
+ - README
58
+ - README.md
59
+ - Rakefile
60
+ - lib/tvrage.rb
61
+ - lib/tvrage/client.rb
62
+ - lib/tvrage/episode.rb
63
+ - lib/tvrage/tvshow.rb
64
+ - lib/tvrage/version.rb
65
+ - spec/spec_helper.rb
66
+ - spec/tvrage_spec.rb
67
+ - tvrage.gemspec
68
+ homepage: http://www.erikvdwal.nl/
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Simple library to access the TVRage XML feeds
92
+ test_files:
93
+ - spec/spec_helper.rb
94
+ - spec/tvrage_spec.rb