movies 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,5 @@
1
+ --color
2
+ -fs
3
+ -Ilib
4
+ -Ispec
5
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in movies.gemspec
4
+ gemspec
@@ -0,0 +1,102 @@
1
+ # Movies
2
+
3
+ *Movies* is the bridge between IMDb's unofficial API; [imdbapi.com](http://imdbapi.com/) and Ruby.
4
+
5
+ Follow me on [Twitter](http://twitter.com/linusoleander) or [Github](https://github.com/oleander/) for more info and updates.
6
+
7
+ ## How to use
8
+
9
+ ### Search for a title
10
+
11
+ ```` ruby
12
+ Movies.find_by_title("The dark night")
13
+ ````
14
+
15
+ ### Find movie based on an IMDb id
16
+
17
+ ```` ruby
18
+ Movies.find_by_id("tt0337978")
19
+ ````
20
+
21
+ ### Find by release name
22
+
23
+ This method will try to filter out as much irrelevant data as possible using [this exclude list](https://github.com/oleander/Movies/blob/master/lib/movies/exclude.yml), before doing a request to the server.
24
+ It will also try to find a release year in the title, which will be passed to [imdbapi.com](http://imdbapi.com/).
25
+
26
+ ```` ruby
27
+ Movies.find_by_release_name("Heartbreaker.2010.LIMITED.DVDRip.XviD-SUBMERGE")
28
+ ````
29
+
30
+ The *snippet* above will pass the following data to the server.
31
+
32
+ ```` ruby
33
+ Movies.find_by_title("Heartbreaker", {
34
+ y: "2010"
35
+ })
36
+ ````
37
+
38
+ ## Settings
39
+
40
+ You can pass some arguments if you for example want to search for a particular year.
41
+
42
+ ```` ruby
43
+ Movies.find_by_title("The dark night", {
44
+ y: "2010"
45
+ })
46
+ ````
47
+
48
+ These params are supported.
49
+
50
+ - **y** (*Any number*) Year of the movie.
51
+ - **plot** (*short, full*) Short or extended plot (short default).
52
+ - **tomatoes** (*Boolean*) Adds rotten tomatoes data.
53
+
54
+ ## Rottentomatoes
55
+
56
+ ```` ruby
57
+ movie = Movies.find_by_title("Die Hard 4.0", {
58
+ tomatoes: "true"
59
+ })
60
+
61
+ movie.tomato.meter
62
+ # => 82
63
+ movie.tomato.image
64
+ # => "certified"
65
+ movie.tomato.rating
66
+ # => 6.8
67
+ movie.tomato.reviews
68
+ # => 198
69
+ movie.tomato.fresh
70
+ # => 162
71
+ movie.tomato.rotten
72
+ # => 36
73
+ ````
74
+
75
+ ## Data to work with
76
+
77
+ These accessors are available for the object that is being returned from the `find_by_*` methods.
78
+
79
+ - **year** (*Fixnum*) Year of the movie.
80
+ - **released** (*Date*) Release date.
81
+ - **writers** (*Array < String >*) Writers.
82
+ - **actors** (*Array < String>*) Actors.
83
+ - **director** (*String*) Name of director.
84
+ - **rating** (*Float*) Rating from 1.0 to 10.0.
85
+ - **votes** (*Float*) Number of votes.
86
+ - **runtime** (*Fixnum*) Run time in seconds.
87
+ - **href** (*String*) IMDb url.
88
+ - **id** (*String*) IMDb id.
89
+ - **poster** (*String*) Url to poster.
90
+ - **found?** (*Boolean*) Where anything found?
91
+
92
+ ## How to install
93
+
94
+ [sudo] gem install movies
95
+
96
+ ## Requirements
97
+
98
+ *Movies* is tested in *OS X 10.6.7* using Ruby *1.9.2*.
99
+
100
+ ## License
101
+
102
+ *Movies* is released under the *MIT license*.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,124 @@
1
+ require "json"
2
+ require "rest-client"
3
+ require "date"
4
+ require "yaml"
5
+
6
+ class Movies
7
+ attr_reader :title, :year, :rated, :plot, :genres, :director, :writers, :actors, :poster, :runtime, :rating, :votes, :id
8
+
9
+ def initialize(url, params = {})
10
+ if params.keys.include?(:callback)
11
+ raise ArgumentError.new("Passing the callback option makes not sense.")
12
+ end
13
+ @url = "#{url}&#{(@params = params).map{|key, value| "#{key}=#{value}"}.join("&")}"
14
+ end
15
+
16
+ def self.find_by_id(id, params = {})
17
+ unless id.to_s.match(/tt\d{4,}/)
18
+ raise ArgumentError.new("The id is not valid.")
19
+ end
20
+ Movies.new("http://www.imdbapi.com/?i=#{id}", params).prepare
21
+ end
22
+
23
+ def self.find_by_title(title, params = {})
24
+ if title.nil? or title.empty?
25
+ raise ArgumentError.new("Title can not be blank.")
26
+ end
27
+ Movies.new("http://www.imdbapi.com/?t=#{URI.encode(title)}", params).prepare
28
+ end
29
+
30
+ def self.find_by_release_name(title, params = {})
31
+ if title.nil? or title.empty?
32
+ raise ArgumentError.new("Title can not be blank.")
33
+ end
34
+
35
+ params.merge!(y: $1) if title =~ /((19|20)\d{2})/
36
+ Movies.new("http://www.imdbapi.com/?t=#{URI.encode(Movies.cleaner(title))}", params).prepare
37
+ end
38
+
39
+ def prepare
40
+ tap do
41
+ return self unless found?
42
+
43
+ content.keys.each do |name|
44
+ instance_variable_set "@" + name.to_s.downcase, content[name]
45
+ end
46
+
47
+ @year = @year.to_i
48
+ @genres = @genre.split(",")
49
+ @writers = @writer.split(",")
50
+ @actors = @actors.split(", ")
51
+ @rating = @rating.to_f
52
+ @votes = @votes.to_i
53
+
54
+ if @runtime =~ /(\d+).+?(\d+)/
55
+ @runtime = $1.to_i * 60 + $1.to_i
56
+ elsif @runtime =~ /(\d+) hrs/
57
+ @runtime = $1.to_i * 60
58
+ else
59
+ @runtime = 0
60
+ end
61
+ end
62
+ end
63
+
64
+ def found?
65
+ content["Response"] == "True"
66
+ end
67
+
68
+ def released
69
+ Date.parse(@released)
70
+ rescue ArgumentError
71
+ return nil
72
+ end
73
+
74
+ def tomato
75
+ unless @params[:tomatoes]
76
+ raise ArgumentError.new("You have to set 'tomatoes' to true to get this data.")
77
+ end
78
+
79
+ @_tomato ||= Struct.new(
80
+ :meter,
81
+ :image,
82
+ :rating,
83
+ :reviews,
84
+ :fresh,
85
+ :rotten
86
+ ).new(
87
+ @tomatometer.to_i,
88
+ @tomatoimage,
89
+ @tomatorating.to_f,
90
+ @tomatoreviews.to_i,
91
+ @tomatofresh.to_i,
92
+ @tomatorotten.to_i
93
+ )
94
+ end
95
+
96
+ def href
97
+ "http://www.imdb.com/title/#{@id}/"
98
+ end
99
+
100
+ def self.excluded
101
+ @_excluded ||= YAML.load_file("#{File.dirname(__FILE__)}/movies/exclude.yml")["excluded"]
102
+ end
103
+
104
+ def self.cleaner(string)
105
+ excluded.each do |clean|
106
+ string = string.gsub(/#{clean}/i, ' ')
107
+ end
108
+
109
+ [/((19|20)\d{2})/, /\./, /\s*-\s*/, /\s{2,}/].each do |regex|
110
+ string = string.gsub(regex, ' ')
111
+ end
112
+
113
+ string.strip
114
+ end
115
+
116
+ private
117
+ def content
118
+ @_content ||= JSON.parse(download)
119
+ end
120
+
121
+ def download
122
+ @_download ||= RestClient.get(@url, timeout: 3)
123
+ end
124
+ end
@@ -0,0 +1,22 @@
1
+ excluded:
2
+ - EXTENDED
3
+ - "((xvid(-[a-z]+)?))"
4
+ - "(720|480|1080)p"
5
+ - B(R|D)Rip
6
+ - cam
7
+ - telesync|([^a-z])ts([^a-z]?)
8
+ - telecine|([^a-z])tc([^a-z]?)
9
+ - "([^a-z])rx([^a-z]?)"
10
+ - dvdscr
11
+ - screener
12
+ - hdtv
13
+ - dvdrip
14
+ - workprint
15
+ - brrip|bluray|blu(-?)ray
16
+ - dvd(-?)r
17
+ - x264
18
+ - UNRATED
19
+ - IMAGiNE
20
+ - SCR
21
+ - "((ac3(-[a-z]+)?))"
22
+ - LIMITED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "movies"
6
+ s.version = "0.1.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Linus Oleander"]
9
+ s.email = ["linus@oleander.nu"]
10
+ s.homepage = "https://github.com/oleander/Movies"
11
+ s.summary = %q{Movies is the bridge between IMDb's unofficial API; imdbapi.com and Ruby.}
12
+ s.description = %q{Movies is the bridge between IMDb's unofficial API; imdbapi.com and Ruby}
13
+
14
+ s.rubyforge_project = "movies"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency("rest-client")
22
+ s.add_dependency("nokogiri")
23
+
24
+ s.add_development_dependency("vcr")
25
+ s.add_development_dependency("rspec")
26
+ s.add_development_dependency("webmock")
27
+
28
+ s.required_ruby_version = "~> 1.9.0"
29
+ end
@@ -0,0 +1,54 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :get
5
+ uri: http://www.imdbapi.com:80/?t=Consinsual&y=2010
6
+ body:
7
+ headers:
8
+ accept:
9
+ - "*/*; q=0.5, application/xml"
10
+ accept-encoding:
11
+ - gzip, deflate
12
+ timeout:
13
+ - "10"
14
+ response: !ruby/struct:VCR::Response
15
+ status: !ruby/struct:VCR::ResponseStatus
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ cache-control:
20
+ - no-cache
21
+ pragma:
22
+ - no-cache
23
+ content-type:
24
+ - text/html; charset=utf-8
25
+ content-encoding:
26
+ - gzip
27
+ expires:
28
+ - "-1"
29
+ vary:
30
+ - Accept-Encoding
31
+ server:
32
+ - Microsoft-IIS/7.0
33
+ x-aspnet-version:
34
+ - 4.0.30319
35
+ x-powered-by:
36
+ - ASP.NET
37
+ date:
38
+ - Sat, 30 Apr 2011 03:37:31 GMT
39
+ content-length:
40
+ - "469"
41
+ body: !binary |
42
+ H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3m
43
+ kuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZk
44
+ AWz2zkrayZ4hgKrIHz9+fB8/In7xR2+Ktsw/evTRSbVsimWzzsqPRh/9PnlW
45
+ 02d7O7s79NerrM1n9Ocr/J6Xedbwny/uHtMHn+fLGu8/rbNFRn8/Lep82lZ4
46
+ /WW2LtOn4/Tb2XKZzem779ZFm0e/OcYrDX3ze+X5Mkuf5fXFuqmWo/R1kb3N
47
+ 0i+ypsl+0boqRunvlbXz+nqZvsmuy6oepcdl/q5o0p8qJlVZNATqZVm1BOg4
48
+ nVbrVZmnV/MqbfJ80aRtlc6zyzwt2jQry3S1buY5fTrPi5pavEvL4jxHI/ok
49
+ zWcX+Th9Q781hNl1epG3TVosCf32qljmMwKbL9NsmVblLD0vs0U+Sid5W2fX
50
+ WTlKy3VDfSxn6Syf5tRdjhcZ7qqYtus6HwPRqqFPCVUh5Kv1si0WIOVuOq/T
51
+ e/fSBc0HvsjaYnmBz8f79OdPVm0OSu3doz/OntJvbbv7YGf//oMD+uBV3qxo
52
+ JgHmTb3OP/ol/w80fcQQ4gEAAA==
53
+
54
+ http_version: "1.1"
@@ -0,0 +1,93 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :get
5
+ uri: http://www.imdbapi.com:80/?i=ttrandom&y=1970
6
+ body:
7
+ headers:
8
+ accept:
9
+ - "*/*; q=0.5, application/xml"
10
+ accept-encoding:
11
+ - gzip, deflate
12
+ timeout:
13
+ - "10"
14
+ response: !ruby/struct:VCR::Response
15
+ status: !ruby/struct:VCR::ResponseStatus
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ cache-control:
20
+ - no-cache
21
+ pragma:
22
+ - no-cache
23
+ content-type:
24
+ - text/html; charset=utf-8
25
+ content-encoding:
26
+ - gzip
27
+ expires:
28
+ - "-1"
29
+ vary:
30
+ - Accept-Encoding
31
+ server:
32
+ - Microsoft-IIS/7.0
33
+ x-aspnet-version:
34
+ - 4.0.30319
35
+ x-powered-by:
36
+ - ASP.NET
37
+ date:
38
+ - Sat, 30 Apr 2011 03:25:49 GMT
39
+ content-length:
40
+ - "144"
41
+ body: !binary |
42
+ H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3m
43
+ kuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZk
44
+ AWz2zkrayZ4hgKrIHz9+fB8/In7xR6/yZlUtm/yjRx+9zOomT0/ruqo/+iX/
45
+ D1RQWLAaAAAA
46
+
47
+ http_version: "1.1"
48
+ - !ruby/struct:VCR::HTTPInteraction
49
+ request: !ruby/struct:VCR::Request
50
+ method: :get
51
+ uri: http://www.imdbapi.com:80/?i=ttrandom
52
+ body:
53
+ headers:
54
+ accept:
55
+ - "*/*; q=0.5, application/xml"
56
+ accept-encoding:
57
+ - gzip, deflate
58
+ timeout:
59
+ - "10"
60
+ response: !ruby/struct:VCR::Response
61
+ status: !ruby/struct:VCR::ResponseStatus
62
+ code: 200
63
+ message: OK
64
+ headers:
65
+ cache-control:
66
+ - no-cache
67
+ pragma:
68
+ - no-cache
69
+ content-type:
70
+ - text/html; charset=utf-8
71
+ content-encoding:
72
+ - gzip
73
+ expires:
74
+ - "-1"
75
+ vary:
76
+ - Accept-Encoding
77
+ server:
78
+ - Microsoft-IIS/7.0
79
+ x-aspnet-version:
80
+ - 4.0.30319
81
+ x-powered-by:
82
+ - ASP.NET
83
+ date:
84
+ - Sat, 30 Apr 2011 03:37:30 GMT
85
+ content-length:
86
+ - "144"
87
+ body: !binary |
88
+ H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3m
89
+ kuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZk
90
+ AWz2zkrayZ4hgKrIHz9+fB8/In7xR6/yZlUtm/yjRx+9zOomT0/ruqo/+iX/
91
+ D1RQWLAaAAAA
92
+
93
+ http_version: "1.1"