Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #12 from boatrite/master
Browse files Browse the repository at this point in the history
Fixed a bug and stuff
  • Loading branch information
hjhart committed Dec 3, 2013
2 parents 3beaa02 + dc052b8 commit d0507d9
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 136 deletions.
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rotten_pirate
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.0.0
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

11 changes: 6 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
source :rubygems
source 'https://rubygems.org'

group :test do
gem 'rspec', '~>2.7'
gem 'awesome_print'
gem 'rspec', '~>2.7'
gem 'awesome_print'
end

gem 'torrent_api', '0.2.7'
gem 'sequel', '~>3.2'
gem 'sqlite3', '~>1.3'
gem 'whenever'
gem 'sqlite3'
gem 'i18n'
gem 'rake'
gem 'prowl'
gem 'pry'
12 changes: 10 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
activesupport (3.1.1)
multi_json (~> 1.0)
awesome_print (0.4.0)
chronic (0.6.5)
coderay (1.1.0)
diff-lcs (1.1.3)
hpricot (0.8.6)
i18n (0.6.0)
method_source (0.8.2)
multi_json (1.0.3)
nokogiri (1.5.2)
prowl (0.1.3)
pry (0.9.12.4)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
rake (0.9.2.2)
rspec (2.7.0)
rspec-core (~> 2.7.0)
Expand All @@ -21,6 +27,7 @@ GEM
diff-lcs (~> 1.1.2)
rspec-mocks (2.7.0)
sequel (3.29.0)
slop (3.4.7)
sqlite3 (1.3.4)
torrent_api (0.2.7)
hpricot
Expand All @@ -36,9 +43,10 @@ DEPENDENCIES
awesome_print
i18n
prowl
pry
rake
rspec (~> 2.7)
sequel (~> 3.2)
sqlite3 (~> 1.3)
sqlite3
torrent_api (= 0.2.7)
whenever
46 changes: 24 additions & 22 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ task :default => :console

desc "Loads up a console environment"
task :console do
exec "irb -I lib -r the_rotten_pirate"
exec "irb -I lib -r #{File.join(File.dirname(__FILE__), "lib/the_rotten_pirate")}"
end

desc "Runs download specs"
task :spec do
exec "rspec #{File.join(File.dirname(__FILE__), 'spec/download_spec.rb')}"
end

desc "Creates the initial database for storing movie downloads and missing config files"
task :initialize do

require "sequel"

FileUtils.mkdir_p('db')
DB = Sequel.sqlite('db/downloads.sqlite')

begin
DB.create_table :downloads do
primary_key :id
Expand All @@ -22,16 +27,16 @@ task :initialize do
rescue Exception
puts "Database already exists"
end
config_templpate = File.join(File.dirname(File.expand_path(__FILE__)), "config", "config.template.yml")
prowl_template = File.join(File.dirname(File.expand_path(__FILE__)), "config", "prowl.template.yml")
target_config_file = File.join(File.dirname(File.expand_path(__FILE__)), "config", "config.yml")
target_prowl_file = File.join(File.dirname(File.expand_path(__FILE__)), "config", "prowl.yml")

config_templpate = File.join(File.dirname(__FILE__), "config", "config.template.yml")
prowl_template = File.join(File.dirname(__FILE__), "config", "prowl.template.yml")
target_config_file = File.join(File.dirname(__FILE__), "config", "config.yml")
target_prowl_file = File.join(File.dirname(__FILE__), "config", "prowl.yml")
unless File.exists? target_prowl_file
FileUtils.copy(prowl_template, target_prowl_file)
puts "Creating working prowl config file..."
end

unless File.exists? target_config_file
FileUtils.copy(config_templpate, target_config_file)
puts "Creating working rotten pirate config file..."
Expand All @@ -40,40 +45,37 @@ end

desc "This task loads the config file, fetches the rotten tomatoes feed, and downloads torrent files."
task :execute do
$:.push 'lib'
require 'the_rotten_pirate'
require_relative 'lib/the_rotten_pirate'
TheRottenPirate.execute
end

desc "This task loads a file (newline delimited) and downloads each of the movies."
task :download_from_watch_file do
$:.push 'lib'
require 'the_rotten_pirate'
require_relative 'lib/the_rotten_pirate'
trp = TheRottenPirate.new
filename = trp.config['watch_file']
if filename.nil?
puts "There was no filename specified in the config file"
return
end

File.open(filename, 'r').each do |movie_title|
trp.initialize_download movie_title
end

File.open(filename, 'w') {} # truncate file
end


desc "This task will download a single movie"

task :download, :movie do |t, args|
movie = args[:movie]

abort "You must specify a movie to download: rake download[\"The Lion King\"]" if movie.nil?

$:.push 'lib'
require 'the_rotten_pirate'

require_relative 'lib/the_rotten_pirate.rb'
trp = TheRottenPirate.new
trp.initialize_download movie

end
34 changes: 20 additions & 14 deletions lib/download.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
require 'sequel'
require 'uri'

class Download
def self.connection
Sequel.sqlite('db/downloads.sqlite')
end

def self.exists? name
db = connection
!!(db[:downloads].filter(:name => name).first)
end

def self.insert name
db = connection
db[:downloads].insert(:name => name)
end

def self.delete name
connection[:downloads].filter(:name => name).delete
end

def self.clean_title movie_title
movie_title.gsub(/-/, '')
end

def self.download_directory
config = YAML.load(File.open('config/config.yml').read)
config = YAML.load(File.open(File.dirname(__FILE__) + '/../config/config.yml').read)
download_dir = config["download_directory"] || 'tmp/torrent'
dirs = download_dir.strip.split('/')
# This converts any relative home directories to one ruby likes better (osx only probably)
dirs.map { |dir| dir == "~" ? Dir.home : dir }
dirs.map { |dir| dir == "~" ? Dir.home : dir }
end

def self.torrent_from_url url
require 'net/http'



torrent_filename_match = url.match(/.*\/(.*)/)
torrent_name = torrent_filename_match.nil? ? "tmp.torrent" : torrent_filename_match[1]
torrent_domain, torrent_uri = url.gsub(/https?:\/\//, '').split('.se')
torrent_uri = URI.escape(torrent_uri)
torrent_domain += '.se'

FileUtils.mkdir_p File.join(self.download_directory)
filename = File.join(self.download_directory, torrent_name)
Net::HTTP.start(torrent_domain) do |http|
resp = http.get(torrent_uri)
open(filename, "wb") { |file| file.write(resp.body) }
Net::HTTP.start(torrent_domain) do |http|
resp = http.get(torrent_uri)
if resp.msg == 'OK'
open(filename, "wb") { |file| file.write(resp.body) }
else
puts "HTTP Response not OK; was #{resp.msg}: #{resp.code}"
nil
end
end
end
end
Loading

0 comments on commit d0507d9

Please sign in to comment.