Skip to content

Commit

Permalink
Provider zamunda ready with auth option
Browse files Browse the repository at this point in the history
  • Loading branch information
murat committed Sep 3, 2017
2 parents 70f65e3 + 0f1bd9e commit ffcf535
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ Yep, there is a few provider option.
| extratorrent || Slow Connection |
| 1337x || Connection N+1 |
| zooqle || Skipping some torrents |
| torrentfunk | ☑️ | 🙈 |
| limetorrents | ☑️ | 🙈 |
| zamunda || Require authentication |
| torrentfunk | ☑️ | 🙈 |
| limetorrents | ☑️ | 🙈 |

Use `-p PROVIDER` flag for scrape another providers.

Expand All @@ -76,11 +77,12 @@ And you can list all active providers and usage instructions with `-h` or `--hel
-s, --search=s Search term [SEARCH]
-d, --directory=d Destination path for downloaded torrent [DIRECTORY]
-p, --provider=p Provider name [PROVIDER]
-u, --username=u Username for authentication
-w, --password=p Password for authentication
-l, --list-providers List providers
-a, --auto-download Auto download best choice
-o, --open Open torrent after downloading


[![See it in action](https://asciinema.org/a/lxRQTTiTSUXVhD3dpzIwCcIs2.png)](https://asciinema.org/a/lxRQTTiTSUXVhD3dpzIwCcIs2)

## Development
Expand Down
18 changes: 15 additions & 3 deletions lib/tors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ module TorS
opts.on('-p=p', '--provider=p', 'Provider name [PROVIDER]') do |p|
options[:provider] = p
end
opts.on('-u=u', '--username=u', 'Username for authentication') do |u|
options[:username] = u
end
opts.on('-w=p', '--password=p', 'Password for authentication') do |p|
options[:password] = p
end
opts.on('-l', '--list-providers', 'List providers') do |_l|
Dir[File.expand_path('providers/*.yml')].each do |f|
puts '- ' + File.basename(f).split('.').first
end
puts '- 1337x'
puts '- extratorrent'
puts '- katcr'
puts '- rarbg'
puts '- thepiratebay'
puts '- zamunda'
puts '- zooqle'
abort
end
opts.on('-a', '--auto-download', 'Auto download best choice') do
Expand All @@ -37,6 +47,8 @@ module TorS
end.parse!

tors = TorS::Search.new(options[:provider] || 'katcr') do |ts|
ts.username = options[:username]
ts.password = options[:password]
ts.query = options[:search]
ts.auto = options[:auto] || false
ts.directory = options[:directory] || Dir.pwd
Expand Down
47 changes: 40 additions & 7 deletions lib/tors/search.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require 'yaml'
require 'net/http'
require 'nokogiri'
require 'mechanize'
require 'open-uri'
require 'tty-table'
require 'tty-prompt'

module TorS
class Search
attr_accessor :query, :from, :auto, :directory, :open_torrent
attr_accessor :query, :from, :username, :password, :directory, :auto, :open_torrent

def initialize(from = 'katcr')
@from = from
Expand All @@ -32,12 +33,16 @@ def run
end

def scrape
@url = @provider['url'].gsub(/%{(\w+)}/, @query ? @query.tr(' ', '+') : '')
@url = URI.encode(@provider['url'].gsub(/%{(\w+)}/, @query ? @query : ''))
@page = Nokogiri::HTML(open(@url))

if @provider['authenticate']
authenticate
end

if @page.css(@provider['scrape']['selector']).empty?
if threat_defence @page
puts "😰 Sorry, I think you are banned from #{from}. There is a threat defense redirection."
puts "😰 Sorry, I think you are banned from #{from}. There is a threat defense redirection.".red
end

puts 'Please check this url is works : ' + @url
Expand All @@ -47,7 +52,7 @@ def scrape
@rows = []
@downloads = []

puts 'Scraping...'
puts 'Scraping...'.blue

key = 0
@page.css(@provider['scrape']['selector']).each do |row|
Expand Down Expand Up @@ -121,10 +126,15 @@ def download(choice)
target_file_name = choice[:name].tr("\n", ' ').squeeze(' ').strip + '.torrent'
puts 'Downloading ' + target_file_name

source = Net::HTTP.get(URI.parse(choice[:url]))
target_file = File.join(@directory, target_file_name)
target_file = File.join(@directory, target_file_name)

File.write(target_file, source)
if @provider['authenticate']
source = @mechanize.get URI.parse(choice[:url])
source.save_as(target_file)
else
source = Net::HTTP.get(URI.parse(choice[:url]))
File.write(target_file, source)
end
rescue IOError => e
# FIXME: what about HTTP errors? Net::HTTP throws a number of
# exceptions. It would be wise to use another HTTP library for this
Expand All @@ -140,6 +150,29 @@ def download(choice)
end
end

# FIXME: This must be refactored
# Nokogiri dependency is not necessary with mechanize
# (Mechanize already dependent to nokogiri)
# And mechanize has all features of nokogiri.
def authenticate
@mechanize = Mechanize.new
puts '⚠ Trying authentication'.cyan

@page = @mechanize.get @url
login_form = @page.form_with(name: @provider['authenticate']['form_name']) do |login|
login[@provider['authenticate']['username_input']] = @username
login[@provider['authenticate']['password_input']] = @password
end
@page = login_form.submit

if @page.css(@provider['scrape']['selector']).empty?
puts 'Login failed with your credentials!'.red
abort
end

puts '✔ Authentication successfull'.green
end

def threat_defence(page)
return false unless page.text =~ /threat_defence.php/
true
Expand Down
2 changes: 1 addition & 1 deletion lib/tors/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module TorS
VERSION = '0.3.2'.freeze
VERSION = '0.4.0'.freeze
end
16 changes: 16 additions & 0 deletions providers/zamunda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: 'zamunda'
url: "http://zamunda.net/bananas?c42=1&c25=1&c35=1&c46=1&c20=1&c19=1&c5=1&c24=1&c31=1&c28=1&c7=1&c33=1&c39=1&c4=1&c21=1&c17=1&c40=1&c12=1&c54=1&c6=1&c30=1&c29=1&c51=1&c34=1&c38=1&c1=1&c22=1&c43=1&c41=1&c36=1&c52=1&c53=1&c26=1&c23=1&c32=1&c37=1&search=%{query}&incldead=&field=name&sort=9&type=desc"
download_prefix: "http://zamunda.net"
authenticate:
form_name: 'login'
username_input: 'username'
password_input: 'password'
scrape:
selector: '.video-list > .item'
data:
name: '.title_genre h2 a'
category: '.video-meta > div:nth-child(5) a'
download: '.video-meta > div:nth-child(1) > a'
size: '.video-meta div:nth-child(1) a font:last-child'
seed: '.video-meta > div:nth-child(8) a:nth-child(9)'
leech: '.video-meta > div:nth-child(8) a:nth-child(10)'
1 change: 1 addition & 0 deletions tors.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_dependency 'nokogiri', '~> 1.8', '>= 1.8.0'
spec.add_dependency 'mechanize', '~> 2.7.5'
spec.add_dependency 'colorize', '~> 0.8.1'
spec.add_dependency 'tty-table', '~> 0.8.0'
spec.add_dependency 'tty-prompt', '~> 0.13.1'
Expand Down

0 comments on commit ffcf535

Please sign in to comment.