Skip to content

Instantly share code, notes, and snippets.

@res0nat0r
Last active March 21, 2017 23:33
Show Gist options
  • Save res0nat0r/203719 to your computer and use it in GitHub Desktop.
Save res0nat0r/203719 to your computer and use it in GitHub Desktop.
Grab the first imdb result from google and save it to an .nfo file

Dependencies:

Mechanize

Example:

$ ./imdb.rb "pee wee's big adventure" /tmp

$ cat /tmp/movie.nfo 
http://www.imdb.com/title/tt0089791/
#!/usr/bin/env ruby
require 'rubygems'
require 'mechanize'
require 'open-uri'
class Nfo
attr_reader :title, :dir , :doc
def initialize(title, dir)
@title = title
@dir = dir
end
def search
agent = Mechanize.new { |a| a.user_agent_alias = 'Mac Safari' }
doc = agent.get("http://www.google.com/search?q=#{title}" + " imdb")
url = doc.link_with(:href => /imdb.com/).href + "\n"
file = File.open("#{dir}/movie.nfo", "w") unless File.exists?("#{dir}/movie.nfo")
if(file)
file.write(url)
file.close
else
puts "#{dir}/movie.nfo exists. Exiting..."
end
end
end
if $0 == __FILE__
item = Nfo.new(ARGV[0], ARGV[1])
item.search
end
#!/usr/bin/env ruby
require 'mechanize'
*args = ARGV
agent = Mechanize.new
page = agent.get('http://google.com/')
google_form = page.form('f')
google_form.q = "imdb #{args}"
page = agent.submit(google_form, google_form.buttons.first)
puts page.links_with(:text => /IMDb$/, :href => /imdb.com/).first.click.uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment