tvdbjson 0.1.0

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: 3f31635d50ebac2a73984a6a67ff90ada68e1e64
4
+ data.tar.gz: 556dad6d367076be42d09893689777e9a2363938
5
+ SHA512:
6
+ metadata.gz: c278a1a3ad4341da93bb431bd87c328d61e4ead7fb5470f85ca417b32076b02e1a444289e55d5476158ae31cc173dd9c34c935d5faff2a61a50d4b5d7adaa509
7
+ data.tar.gz: e7b45149f67cc04141adc869c572bd81c78f382ce6c325861cc5df17433040e8f1507a36fba54de38983a5d319faff2bf77dd3da236cb4ba76d04f353703f2f0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2017 Jeremy Tennant
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Tvdbjson
2
+
3
+ ## Description
4
+ A super-unofficial Ruby Gem for thetvdb.com ['swagger' api](https://api.thetvdb.com/swagger), please feel free to contribute!
5
+
6
+ ### Install the gem
7
+
8
+ Visit [rubygems](https://rubygems.org/gems/thetvdbjson) for instructions on how to install this gem via the command line, or for how to include it in your gemfile.
9
+
10
+ ### Create some environment variables
11
+ [Create an account](https://www.thetvdb.com/?tab=register) and [register for your API Key](https://www.thetvdb.com/?tab=apiregister) on thetvdb.com
12
+ ```
13
+ export TVDB_API_KEY="Your API Key"
14
+ export TVDB_USER_KEY="Your User Key"
15
+ export TVDB_USER_NAME="Your User Name"
16
+ ```
17
+
18
+ ## Overview
19
+ You're going to need to read the source for full details, as this gem is a work in progress.
20
+
21
+ ```
22
+ # Create an authentication object.
23
+ auth = Tvdbjson::Authentication.new
24
+
25
+ # An auth token lasts 24 hours, so implement this as needed
26
+ auth.refresh_token if auth.token_expired?
27
+
28
+ # Search for a Television Series
29
+ search_results = Tvdbjson::Series.search_by_name("Game of Thrones", auth)
30
+
31
+ # Just to make the rest of this easy, we'll store the thetvdb's result for "Game of Thrones" in a variable
32
+ game_of_thrones = search_results.first
33
+
34
+ # By the way, this gem uses awesome_print so you can get output like this:
35
+
36
+ ap game_of_thrones
37
+ # => {
38
+ # "id" => 121361,
39
+ # "name" => "Game of Thrones",
40
+ # "overview" => "Seven noble families fight for control of the mythical land of Westeros. Friction between the houses leads to full-scale war. All while a very ancient evil awakens in the farthest north. Amidst the war, a neglected military order of misfits, the Night's Watch, is all that stands between the realms of men and the icy horrors beyond.",
41
+ # "network" => "HBO",
42
+ # "banner" => "graphical/121361-g19.jpg",
43
+ # "first_aired" => "2011-04-17",
44
+ # "status" => "Continuing",
45
+ # "aliases" => []
46
+ # }
47
+
48
+ # Search for images for that Television series
49
+
50
+ search_options = { :series_id => game_of_thrones.id, :type => "fanart" }
51
+ image_results = Tvdbjson::Image.search_by_series(search_options, auth)
52
+
53
+ search_options = { :series_id => game_of_thrones.id, :type => "poster" }
54
+ image_results = Tvdbjson::Image.search_by_series(search_options, auth)
55
+
56
+ search_options = { :series_id => game_of_thrones.id, :type => "season" }
57
+ image_results = Tvdbjson::Image.search_by_series(search_options, auth)
58
+
59
+ search_options = { :series_id => game_of_thrones.id, :type => "seasonwide" }
60
+ image_results = Tvdbjson::Image.search_by_series(search_options, auth)
61
+
62
+ search_options = { :series_id => game_of_thrones.id, :type => "series" }
63
+ image_results = Tvdbjson::Image.search_by_series(search_options, auth)
64
+
65
+ # Or perhaps just the images for Season 5 of a Television series
66
+ search_options = { :series_id => game_of_thrones.id, :type => "season", :subkey => 5 }
67
+ image_results = Tvdbjson::Image.search_by_series_and_season(search_options, auth)
68
+
69
+ search_options = { :series_id => game_of_thrones.id, :type => "fanart", :subkey => "graphical" }
70
+ image_results = Tvdbjson::Image.search_by_series_and_season(search_options, auth)
71
+
72
+ # Search for a specific episode
73
+ search_query = { :series_id => game_of_thrones.id, :season_number => 6, :episode_number => 9 }
74
+ episode_results = Tvdbjson::Episode.search_by_season_and_episode(search_query,auth)
75
+
76
+ ```
77
+
78
+ ## License
79
+ MIT - For terms refer to LICENSE.md
80
+
81
+ ## thetvdb
82
+ The creator of this gem is in no way affiliated with thetvdb
data/lib/tvdbjson.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "httparty"
2
+ require "awesome_print"
3
+ require 'tvdbjson/hashable.rb'
4
+ require 'tvdbjson/requestable.rb'
5
+
6
+ Dir[File.dirname(__FILE__) + '/tvdbjson/*.rb'].each do |file|
7
+ require file
8
+ end
9
+
10
+ module Tvdbjson
11
+
12
+ end
@@ -0,0 +1,37 @@
1
+ module Tvdbjson
2
+ class Authentication
3
+
4
+ attr_accessor :expiry, :token
5
+
6
+ TVDB_API_KEY = ENV['TVDB_API_KEY']
7
+ TVDB_USER_KEY = ENV['TVDB_USER_KEY']
8
+ TVDB_USER_NAME = ENV['TVDB_USER_NAME']
9
+
10
+ def initialize()
11
+ new_token
12
+ end
13
+
14
+ def token_expired?
15
+ Time.now >= @expiry
16
+ end
17
+
18
+ def new_token
19
+ @expiry = Time.now + 86400
20
+ @token = get_token
21
+ end
22
+
23
+ private
24
+
25
+ def get_token
26
+ options = {}
27
+ options[:uri] = "/login"
28
+ options[:body] = { "apikey" => TVDB_API_KEY, "userkey" => TVDB_USER_KEY, "username" => TVDB_USER_NAME }
29
+ options[:header] = {}
30
+
31
+ request = Request.new(options)
32
+ response = request.post
33
+ response.parsed_response["token"]
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,58 @@
1
+ module Tvdbjson
2
+ class Episode
3
+ include Tvdbjson::Hashable
4
+ extend Tvdbjson::Requestable
5
+
6
+ attr_accessor :id, :name, :series_id, :episode_number, :season_number, :first_aired, :overview
7
+
8
+ def initialize(options = {})
9
+ @id = options[:id]
10
+ @name = options[:name]
11
+ @series_id = options[:series_id]
12
+ @episode_number = options[:episode_number]
13
+ @season_number = options[:season_number]
14
+ @first_aired = options[:first_aired]
15
+ @overview = options[:overview]
16
+ end
17
+
18
+ def self.search_by_season_and_episode(options = {}, authentication)
19
+ raise Tvdbjson::RequiredSeriesIdMissingError if !options[:series_id]
20
+ raise Tvdbjson::RequiredSearchParamMissingError if !options[:season_number]
21
+ raise Tvdbjson::RequiredSearchParamMissingError if !options[:episode_number]
22
+ raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)
23
+
24
+ hash = {
25
+ :method => "GET",
26
+ :uri => "/series/#{options[:series_id]}/episodes/query",
27
+ :after_action => "Tvdbjson::Episode.from_response(response, #{options[:series_id]})",
28
+ :params => { "airedSeason" => options[:season_number], "airedEpisode" => options[:episode_number] }
29
+ }
30
+ send_authenticated_request(hash, authentication)
31
+ end
32
+
33
+ def self.from_response(response, series_id)
34
+ results_array = []
35
+
36
+ begin
37
+ response.parsed_response["data"].each do |record|
38
+ hash = {}
39
+ hash[:id] = record["id"]
40
+ hash[:name] = record["episodeName"]
41
+ hash[:series_id] = series_id
42
+ hash[:episode_number] = record["airedEpisodeNumber"]
43
+ hash[:season_number] = record["airedSeasonID"]
44
+ hash[:first_aired] = record["firstAired"]
45
+ hash[:overview] = record["overview"]
46
+
47
+ result = Episode.new(hash)
48
+ results_array << result
49
+ end
50
+ rescue NoMethodError
51
+ # Means an empty result set was found, don't do anything special
52
+ # here as this method will return an empty array.
53
+ end
54
+ results_array
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ module Tvdbjson
2
+ class Error < StandardError; end
3
+ class RequiredSearchParamMissingError < Error; end
4
+ class RequiredSeriesIdMissingError < Error; end
5
+ class InvalidImageTypeRequestedError < Error; end
6
+ class AuthenticationObjectMissingError < Error; end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Tvdbjson
2
+ module Hashable
3
+
4
+ def to_hash
5
+ hash = {}
6
+ instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var) }
7
+ hash
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,77 @@
1
+ module Tvdbjson
2
+ class Image
3
+ include Tvdbjson::Hashable
4
+ extend Tvdbjson::Requestable
5
+
6
+ attr_accessor :id, :series_id, :url, :type, :subkey, :resolution, :rating_average, :rating_count, :thumbnail_url
7
+
8
+ def initialize(options = {})
9
+ @id = options[:id]
10
+ @series_id = options[:series_id]
11
+ @url = options[:url]
12
+ @type = options[:type]
13
+ @subkey = options[:subkey]
14
+ @resolution = options[:resolution]
15
+ @rating_average = options[:rating_average]
16
+ @rating_count = options[:rating_count]
17
+ @thumbnail_url = options[:thumbnail_url]
18
+ end
19
+
20
+ def self.search_by_series(options = {}, authentication)
21
+ raise Tvdbjson::RequiredSeriesIdMissingError if !options[:series_id]
22
+ raise Tvdbjson::RequiredSearchParamMissingError if !options[:type]
23
+ raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)
24
+ raise Tvdbjson::InvalidImageTypeRequestedError unless %w(fanart poster season seasonwide series).include?(options[:type])
25
+
26
+ hash = {
27
+ :method => "GET",
28
+ :uri => "/series/#{options[:series_id]}/images/query",
29
+ :after_action => "Tvdbjson::Image.from_response(response, #{options[:series_id]})",
30
+ :params => { "keyType" => options[:type].downcase }
31
+ }
32
+ send_authenticated_request(hash, authentication)
33
+ end
34
+
35
+ def self.search_by_series_and_season(options = {}, authentication)
36
+ raise Tvdbjson::RequiredSeriesIdMissingError if !options[:series_id]
37
+ raise Tvdbjson::RequiredSearchParamMissingError if !options[:subkey]
38
+ raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)
39
+ raise Tvdbjson::InvalidImageTypeRequestedError unless %w(fanart poster season seasonwide series).include?(options[:type])
40
+
41
+ hash = {
42
+ :method => "GET",
43
+ :uri => "/series/#{options[:series_id]}/images/query",
44
+ :after_action => "Tvdbjson::Image.from_response(response, #{options[:series_id]})",
45
+ :params => { "keyType" => options[:type].downcase, "subKey" => options[:subkey] }
46
+ }
47
+ send_authenticated_request(hash, authentication)
48
+ end
49
+
50
+ def self.from_response(response, series_id)
51
+ results_array = []
52
+
53
+ begin
54
+ response.parsed_response["data"].each do |record|
55
+ hash = {}
56
+ hash[:id] = record["id"]
57
+ hash[:series_id] = series_id
58
+ hash[:url] = "https://thetvdb.com/banners/" + record["fileName"]
59
+ hash[:type] = record["keyType"]
60
+ hash[:subkey] = record["subKey"]
61
+ hash[:resolution] = record["resolution"]
62
+ hash[:rating_average] = record["ratingsInfo"]["average"] if record["ratingsInfo"]
63
+ hash[:rating_count] = record["ratingsInfo"]["count"] if record["ratingsInfo"]
64
+ hash[:thumbnail_url] = "https://thetvdb.com/banners/" + record["thumbnail"]
65
+
66
+ result = Image.new(hash)
67
+ results_array << result
68
+ end
69
+ rescue NoMethodError
70
+ # Means an empty result set was found, don't do anything special
71
+ # here as this method will return an empty array.
72
+ end
73
+ results_array
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,26 @@
1
+ module Tvdbjson
2
+ class Request
3
+ BASE_URI = "https://api.thetvdb.com"
4
+
5
+ attr_accessor :url, :body, :headers, :authentication
6
+
7
+ def initialize(options = {})
8
+ @url = "#{BASE_URI}#{options[:uri]}"
9
+ @body = options[:body]
10
+ @headers = {
11
+ 'Accept-Language' => 'en',
12
+ 'Content-Type' => 'application/json',
13
+ 'User-Agent' => "Tvdbjson Ruby Gem v#{Tvdbjson::VERSION}"
14
+ }.merge(options[:header])
15
+ end
16
+
17
+ def get
18
+ HTTParty.get(@url, :headers => @headers, :body => @body.to_json)
19
+ end
20
+
21
+ def post
22
+ HTTParty.post(@url, :headers => @headers, :body => @body.to_json)
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,35 @@
1
+ module Tvdbjson
2
+ module Requestable
3
+
4
+ def send_authenticated_request(hash, authentication)
5
+ options = {}
6
+ options[:uri] = build_uri(hash[:uri],hash[:params])
7
+ options[:body] = hash[:body]
8
+ options[:header] = { "Accept-Language" => "en", "Authorization" => "Bearer #{authentication.token}" }
9
+
10
+ request = Request.new(options)
11
+
12
+ response = if hash[:method] == "GET"
13
+ request.get
14
+ elsif hash[:method] == "POST"
15
+ request.post
16
+ end
17
+
18
+ if hash[:after_action]
19
+ eval(hash[:after_action])
20
+ else
21
+ response
22
+ end
23
+
24
+ end
25
+
26
+ def build_uri(uri, params)
27
+ query_string = "?"
28
+ params.each do |key, value|
29
+ query_string = "#{query_string}#{key}=#{value}&"
30
+ end
31
+ URI.escape(uri+query_string)
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,83 @@
1
+ module Tvdbjson
2
+ class Series
3
+ include Tvdbjson::Hashable
4
+ extend Tvdbjson::Requestable
5
+ attr_accessor :id, :name, :overview, :network, :banner_url, :first_aired, :status, :aliases
6
+
7
+ def initialize(options = {})
8
+ @id = options[:id]
9
+ @name = options[:name]
10
+ @overview = options[:overview]
11
+ @network = options[:network]
12
+ @banner_url = options[:banner_url]
13
+ @first_aired = options[:first_aired]
14
+ @status = options[:status]
15
+ @aliases = options[:aliases]
16
+ end
17
+
18
+ def self.search_by_name(str, authentication)
19
+ raise Tvdbjson::RequiredSearchParamMissingError if !str
20
+ raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)
21
+
22
+ hash = {
23
+ :method => "GET",
24
+ :uri => "/search/series",
25
+ :after_action => "Tvdbjson::Series.from_response(response)",
26
+ :params => { "name" => str }
27
+ }
28
+ send_authenticated_request(hash, authentication)
29
+ end
30
+
31
+ def self.search_by_imdb(str, authentication)
32
+ raise Tvdbjson::RequiredSearchParamMissingError if !str
33
+ raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)
34
+
35
+ hash = {
36
+ :method => "GET",
37
+ :uri => "/search/series",
38
+ :after_action => "Tvdbjson::Series.from_response(response)",
39
+ :params => { "imdbId" => str }
40
+ }
41
+ send_authenticated_request(hash, authentication)
42
+ end
43
+
44
+ def self.search_by_zap2it_id(str, authentication)
45
+ raise Tvdbjson::RequiredSearchParamMissingError if !str
46
+ raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)
47
+
48
+ hash = {
49
+ :method => "GET",
50
+ :uri => "/search/series",
51
+ :after_action => "Tvdbjson::Series.from_response(response)",
52
+ :params => { "zap2itId" => str }
53
+ }
54
+ send_authenticated_request(hash, authentication)
55
+ end
56
+
57
+ def self.from_response(response)
58
+ results_array = []
59
+
60
+ begin
61
+ response.parsed_response["data"].each do |record|
62
+ hash = {}
63
+ hash[:id] = record["id"]
64
+ hash[:name] = record["seriesName"]
65
+ hash[:overview] = record["overview"]
66
+ hash[:network] = record["network"]
67
+ hash[:banner_url] = "https://thetvdb.com/banners/" + record["banner"]
68
+ hash[:first_aired] = record["firstAired"]
69
+ hash[:status] = record["status"]
70
+ hash[:aliases] = record["aliases"]
71
+
72
+ result = Series.new(hash)
73
+ results_array << result
74
+ end
75
+ rescue NoMethodError
76
+ # Means an empty result set was found, don't do anything special
77
+ # here as this method will return an empty array.
78
+ end
79
+ results_array
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module Tvdbjson
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tvdbjson
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Tennant
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry-byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: awesome_print
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.13'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.13'
69
+ description: Unofficial Ruby Gem for thetvdb.com Swagger API (JSON API)
70
+ email: tennantje@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - Gemfile
76
+ - LICENSE.md
77
+ - README.md
78
+ - lib/tvdbjson.rb
79
+ - lib/tvdbjson/authentication.rb
80
+ - lib/tvdbjson/episode.rb
81
+ - lib/tvdbjson/error.rb
82
+ - lib/tvdbjson/hashable.rb
83
+ - lib/tvdbjson/image.rb
84
+ - lib/tvdbjson/request.rb
85
+ - lib/tvdbjson/requestable.rb
86
+ - lib/tvdbjson/series.rb
87
+ - lib/tvdbjson/version.rb
88
+ homepage: https://github.com/tennantje/tvdbjson
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Unofficial Ruby Gem for thetvdb.com Swagger API (JSON API)
112
+ test_files: []