myimdb 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Gaurav Sharma
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = myimdb
2
+
3
+ Utility gem for fetching movie details.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2009 Gaurav Sharma. See LICENSE for details.
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "myimdb"
8
+ gem.summary = %Q{Utility gem for fetching movie details.}
9
+ gem.email = "gaurav@vinsol.com"
10
+ gem.homepage = "http://github.com/gauravs/myimdb"
11
+ gem.authors = ["Gaurav"]
12
+ gem.add_dependency 'httparty', '>= 0.4.5'
13
+ gem.add_dependency 'nokogiri', '>= 1.3.3'
14
+ gem.rubyforge_project = 'myimdb'
15
+ gem.files = FileList["**/*"]
16
+ end
17
+ Jeweler::RubyforgeTasks.new do |rubyforge|
18
+ rubyforge.doc_task = "rdoc"
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # point this file to a folder and all the folder inside it will be renamed
4
+ require 'myimdb.rb'
5
+
6
+ unless ARGV[0]
7
+ p "Source directory required - exiting"
8
+ exit(0)
9
+ end
10
+
11
+ file_paths = Dir["#{ARGV[0]}/*"]
12
+ search_scope = "imdb.com"
13
+
14
+ file_paths.each do |path|
15
+ next if !File.directory?(path)
16
+ p "============================================"
17
+ name = File.basename(path)
18
+ if name.scan(/\[.*?\]/).size == 3
19
+ p "Skipping: #{name}"
20
+ else
21
+ p "Fetching details for: #{name}"
22
+ begin
23
+ search_result = Myimdb::Search::Google.search_text(name, search_scope)[0]
24
+ imdb = Myimdb::Scraper::Imdb.new(search_result["url"])
25
+ new_name = name.gsub(/\[\S+\]/, "").strip
26
+ new_name << " [#{imdb.year}] [#{imdb.rating},#{imdb.votes}] [#{imdb.directors.join(',')}]"
27
+ p "Renaming: #{name} to: #{new_name}"
28
+ new_path = File.join(File.dirname(path), new_name)
29
+ File.rename(path, new_path)
30
+ rescue
31
+ p "Unable to fetch details for: #{name}"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1 @@
1
+ @"ruby.exe" "%~dpn0" %*
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'nokogiri'
4
+ require 'open-uri'
5
+
6
+ require 'myimdb/search'
7
+ require 'myimdb/scraper'
@@ -0,0 +1,5 @@
1
+ require 'myimdb/scraper/string_extensions'
2
+ require 'myimdb/scraper/base'
3
+ require 'myimdb/scraper/imdb'
4
+ require 'myimdb/scraper/metacritic'
5
+ require 'myimdb/scraper/rotten_tomatoes'
@@ -0,0 +1,45 @@
1
+ class UnformattedHtml < Exception; end
2
+
3
+ module HandleExceptions
4
+ def self.included(base)
5
+ base.send(:include, InstanceMethods)
6
+ base.send(:extend, ClassMethods)
7
+ end
8
+
9
+ module InstanceMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def handle_exceptions_for(*method_names)
14
+ method_names.each do |method_name|
15
+ alias_method("_#{method_name}", method_name)
16
+ define_method(method_name) do
17
+ begin
18
+ send("_#{method_name}")
19
+ rescue
20
+ raise UnformattedHtml.new("Unable to find tag: #{method_name}, probably you are parsing the wrong page.")
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ module Myimdb
29
+ module Scraper
30
+ class Base
31
+ include HandleExceptions
32
+ def year
33
+ end
34
+
35
+ def tagline
36
+ end
37
+
38
+ def directors
39
+ end
40
+
41
+ def writers
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ module Myimdb
2
+ module Scraper
3
+ class Imdb < Scraper::Base
4
+ def initialize(url)
5
+ @url = url
6
+ end
7
+
8
+ def directors
9
+ document.css('.info h5:contains("Director") + .info-content a:not(.tn15more)').collect{ |a| a.text }
10
+ end
11
+
12
+ def writers
13
+ document.css('.info h5:contains("Writer") + .info-content a:not(.tn15more)').collect{ |a| a.text }
14
+ end
15
+
16
+ def rating
17
+ document.css(".general.rating b").inner_text.strip.split('/').first.to_f
18
+ end
19
+
20
+ def votes
21
+ document.css(".general.rating a").inner_text.strip.sub(',', '').to_i
22
+ end
23
+
24
+ def genres
25
+ document.css('.info h5:contains("Genre:") + .info-content a:not(.tn15more)').collect{ |a| a.text }
26
+ end
27
+
28
+ def tagline
29
+ document.css('.info h5:contains("Tagline:") + .info-content').children[0].text.strip
30
+ end
31
+
32
+ def plot
33
+ document.css('.info h5:contains("Plot:") + .info-content').children[0].text.strip
34
+ end
35
+
36
+ def year
37
+ document.css("div#tn15title a:first")[0].text.to_i
38
+ end
39
+
40
+ private
41
+ def document
42
+ @document ||= Nokogiri::HTML(open(@url))
43
+ end
44
+
45
+ handle_exceptions_for :directors, :writers, :rating, :votes, :genres, :tagline, :plot, :year
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,32 @@
1
+ module Myimdb
2
+ module Scraper
3
+ class Metacritic < Scraper::Base
4
+ def initialize(url)
5
+ @url = url
6
+ end
7
+
8
+ def rating
9
+ document.css("#metascore").inner_text.strip.to_f/10
10
+ end
11
+
12
+ def votes
13
+ document.at("a[@href='#critics']").inner_text.strip.to_i
14
+ end
15
+
16
+ def genres
17
+ document.css("#productinfo p:first").text.gsub(/^\S+:/, '').split("|").map(&:strip_useless_chars)
18
+ end
19
+
20
+ def plot
21
+ document.css("#productsummary .summarytext").inner_text.strip
22
+ end
23
+
24
+ private
25
+ def document
26
+ @document ||= Nokogiri::HTML(open(@url))
27
+ end
28
+
29
+ handle_exceptions_for :rating, :votes, :genres, :plot
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module Myimdb
2
+ module Scraper
3
+ class RottenTomatoes < Scraper::Base
4
+ def initialize(url)
5
+ @url = url
6
+ end
7
+
8
+ def rating
9
+ document.css("#tomatometer_data p:nth-child(4) span").inner_text.strip.to_i
10
+ end
11
+
12
+ def votes
13
+ document.css("#tomatometer_data p:nth-child(1) span").inner_text.strip.to_i
14
+ end
15
+
16
+ def genres
17
+ document.css("#movie_stats .fl:first p:last .content a").inner_text.scraper_unescape_html.to_a
18
+ end
19
+
20
+ def plot
21
+ document.css("#movie_synopsis_all").inner_text.strip
22
+ end
23
+
24
+ private
25
+ def document
26
+ @document ||= Nokogiri::HTML(open(@url))
27
+ end
28
+
29
+ handle_exceptions_for :rating, :votes, :genres, :plot
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ require 'cgi'
2
+ require 'iconv'
3
+
4
+ module Myimdb
5
+ module Scraper
6
+ module StringExtensions
7
+ def scraper_unescape_html
8
+ Iconv.conv("UTF-8", 'ISO-8859-1', CGI::unescapeHTML(self))
9
+ end
10
+
11
+ def scraper_strip_tags
12
+ gsub(/<\/?[^>]*>/, "")
13
+ end
14
+
15
+ def strip_useless_chars
16
+ gsub(/[^a-zA-z0-9\|\-_\(\)@$\/\\]/, '')
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ String.send :include, Myimdb::Scraper::StringExtensions
@@ -0,0 +1,2 @@
1
+ require 'httparty'
2
+ require 'myimdb/search/google'
@@ -0,0 +1,17 @@
1
+ module Myimdb
2
+ module Search
3
+ class Google
4
+ include HTTParty
5
+
6
+ format :json
7
+ headers 'Content-Type' => 'application/json'
8
+ base_uri 'ajax.googleapis.com'
9
+
10
+ def self.search_text( text, restrict_to=nil )
11
+ text = "site:#{restrict_to} #{text}" if restrict_to
12
+ response = get( '/ajax/services/search/web', :query=> {:v=> '1.0', :q=> text} )
13
+ response['responseData'] and response['responseData']['results']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{myimdb}
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Gaurav"]
12
+ s.date = %q{2009-12-28}
13
+ s.email = %q{gaurav@vinsol.com}
14
+ s.executables = ["myimdb", "myimdb.bat"]
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "bin/myimdb",
25
+ "bin/myimdb.bat",
26
+ "lib/myimdb.rb",
27
+ "lib/myimdb/scraper.rb",
28
+ "lib/myimdb/scraper/base.rb",
29
+ "lib/myimdb/scraper/imdb.rb",
30
+ "lib/myimdb/scraper/metacritic.rb",
31
+ "lib/myimdb/scraper/rotten_tomatoes.rb",
32
+ "lib/myimdb/scraper/string_extensions.rb",
33
+ "lib/myimdb/search.rb",
34
+ "lib/myimdb/search/google.rb",
35
+ "myimdb.gemspec",
36
+ "pkg/myimdb-0.0.0.gem",
37
+ "pkg/myimdb-0.1.0.gem",
38
+ "pkg/myimdb-0.2.0.gem",
39
+ "pkg/myimdb-0.3.0.gem",
40
+ "test/helper.rb",
41
+ "test/test_myimdb.rb"
42
+ ]
43
+ s.homepage = %q{http://github.com/gauravs/myimdb}
44
+ s.rdoc_options = ["--charset=UTF-8"]
45
+ s.require_paths = ["lib"]
46
+ s.rubyforge_project = %q{myimdb}
47
+ s.rubygems_version = %q{1.3.5}
48
+ s.summary = %q{Utility gem for fetching movie details.}
49
+ s.test_files = [
50
+ "test/helper.rb",
51
+ "test/test_myimdb.rb"
52
+ ]
53
+
54
+ if s.respond_to? :specification_version then
55
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
56
+ s.specification_version = 3
57
+
58
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
59
+ s.add_runtime_dependency(%q<httparty>, [">= 0.4.5"])
60
+ s.add_runtime_dependency(%q<nokogiri>, [">= 1.3.3"])
61
+ else
62
+ s.add_dependency(%q<httparty>, [">= 0.4.5"])
63
+ s.add_dependency(%q<nokogiri>, [">= 1.3.3"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<httparty>, [">= 0.4.5"])
67
+ s.add_dependency(%q<nokogiri>, [">= 1.3.3"])
68
+ end
69
+ end
70
+
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'myimdb'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestMyimdb < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: myimdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Gaurav
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-28 00:00:00 +05:30
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.4.5
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: nokogiri
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.3
34
+ version:
35
+ description:
36
+ email: gaurav@vinsol.com
37
+ executables:
38
+ - myimdb
39
+ - myimdb.bat
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - LICENSE
44
+ - README.rdoc
45
+ files:
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - bin/myimdb
51
+ - bin/myimdb.bat
52
+ - lib/myimdb.rb
53
+ - lib/myimdb/scraper.rb
54
+ - lib/myimdb/scraper/base.rb
55
+ - lib/myimdb/scraper/imdb.rb
56
+ - lib/myimdb/scraper/metacritic.rb
57
+ - lib/myimdb/scraper/rotten_tomatoes.rb
58
+ - lib/myimdb/scraper/string_extensions.rb
59
+ - lib/myimdb/search.rb
60
+ - lib/myimdb/search/google.rb
61
+ - myimdb.gemspec
62
+ - pkg/myimdb-0.0.0.gem
63
+ - pkg/myimdb-0.1.0.gem
64
+ - pkg/myimdb-0.2.0.gem
65
+ - pkg/myimdb-0.3.0.gem
66
+ - test/helper.rb
67
+ - test/test_myimdb.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/gauravs/myimdb
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project: myimdb
92
+ rubygems_version: 1.3.5
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Utility gem for fetching movie details.
96
+ test_files:
97
+ - test/helper.rb
98
+ - test/test_myimdb.rb