peerflixrb 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c6bfdb5bcbdb1527015606cc4773e711ef704e85
4
- data.tar.gz: b99f8dfec68f877917e4856099f087e785bf41f9
3
+ metadata.gz: ecd842be46cd8c610100394084b572f3a2b30e7a
4
+ data.tar.gz: 9323654ec6e8fa53c1145f674484e3665486650b
5
5
  SHA512:
6
- metadata.gz: 24482a1282f2d8907cac9b86f2bc9240298e01584ee81021b44796176ba9bf06b3305ccc97705302fdee93c16f1264fb6cf8c0a05f12ed6565419de2afad998c
7
- data.tar.gz: b987b081bc33bdb1ba487226c2473a9fce2e5f946315c1c631f39c4609637bcc2dca3b0f1b67d6491b1183c07623832fc56a5177121c8944f1ad3fe9b391076a
6
+ metadata.gz: 6fc3279083411f97554e670daaf94c2b9e2a3891decc6a909ab47e70d8ecc351f99a76ac4a6000c1add86c33f774196a4ca9314bcf0d1040749ef3e506de0eb8
7
+ data.tar.gz: 6d3e85cabcb11806c7e6f23d7034227a7fa61a176f81d00b1fa4a8e403fb2b0e7b3815c145f06c5a5970b4d7b1f07afadd8a2718fe521099ecb0a2959dedb3bb
data/README.md CHANGED
@@ -20,9 +20,13 @@ Install the gem:
20
20
 
21
21
  ## Usage
22
22
 
23
- Pass a string with what you want to watch and it will try to fetch the file and play it through peerflix:
23
+ Pass a string with what you want to watch and it will search KAT with your query and present you with the first 5 results for you to choose. The file selected will then be sent to **peerflix**.
24
24
 
25
- $ peerflixrb suits s05e12
25
+ $ peerflixrb Suits s05e12
26
+
27
+ If you prefer to autoplay the first matched result, use the ```-y``` flag:
28
+
29
+ $ peerflixrb -y Archer s07e02
26
30
 
27
31
  You can play the video with subtitles with the ```-s``` option (Default: English). It will try and find the best match in [Addic7ed](http://www.addic7ed.com/).
28
32
 
@@ -34,11 +38,11 @@ Choose the language with ```-l LANGUAGE``` ([Available Languages](https://github
34
38
 
35
39
  If you prefer to use your own subtitles file, you can do that with the ```-t SUBTITLE_FILE``` option.
36
40
 
37
- $ peerflixrb better call saul s02e04 -t subtitle_file.srt
41
+ $ peerflixrb Better Call Saul s02e04 -t subtitle_file.srt
38
42
 
39
43
  You can autoplay in VLC or MPlayer with the corresponding option (Default: VLC).
40
44
 
41
- $ peerflixrb --mplayer the big bang theory s09e16 hdtv
45
+ $ peerflixrb --mplayer The Big Bang Theory s09e16 HDTV
42
46
 
43
47
  Or you can just let it download without autoplaying with ```-n``` or ```--no-player```.
44
48
 
data/bin/peerflixrb CHANGED
@@ -3,6 +3,7 @@ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w( .. lib ))
3
3
 
4
4
  require 'optparse'
5
5
  require 'peerflixrb'
6
+ require 'peerflixrb/commands'
6
7
 
7
8
  options = {
8
9
  language: 'en',
@@ -12,14 +13,18 @@ options = {
12
13
  optparse = OptionParser.new do |opts|
13
14
  opts.banner = 'Usage: peerflixrb [options] <search>'
14
15
 
15
- opts.on('-s', '--find-subtitles', 'Find subtitles on Addic7ed') do |s|
16
- options[:find_subtitles] = s
16
+ opts.on('-s', '--find-subtitles', 'Find subtitles on Addic7ed') do
17
+ options[:find_subtitles] = true
17
18
  end
18
19
 
19
20
  opts.on('-n', '--no-player', "Don't autoplay") do
20
21
  options[:video_player] = nil
21
22
  end
22
23
 
24
+ opts.on('-y', '--auto-select', 'Select first match') do
25
+ options[:auto_select] = true
26
+ end
27
+
23
28
  opts.on('-m', '--mplayer', 'Autoplay in mplayer*') do
24
29
  options[:video_player] = 'mplayer'
25
30
  end
@@ -56,40 +61,42 @@ if options[:search].empty?
56
61
  exit
57
62
  end
58
63
 
59
- # main
60
- # TODO: handle failing and invalid searches
61
- kat = Peerflixrb::KAT.new(options[:search])
62
- cache_dir = "/tmp/torrent-stream/#{kat.info_hash}"
63
-
64
- # Subtitle search
65
- sub_file = if options[:find_subtitles]
66
- Peerflixrb.find_subtitles(kat.filename, options[:language])
67
- elsif options[:subtitles]
68
- options[:subtitles]
69
- end
70
-
71
- # Was there a problem with the subtitle?
72
- if options[:find_subtitles] && sub_file.nil?
73
- puts 'Do you want to continue? [Y/n]'
74
- response = $stdin.gets.chomp.downcase
75
- exit if response == 'n' || response == 'no'
76
- end
64
+ # main program
65
+ begin
66
+ system 'clear'
67
+ cli = Commands::cli
77
68
 
78
- # Peerflix command build
79
- command = "peerflix '#{kat.magnet}'"
80
- command << " --subtitles '#{sub_file}'" if sub_file
81
- command << " --#{options[:video_player]}" if options[:video_player]
82
- command << ' -- --fullscreen'
69
+ # KAT Search
70
+ cli.say "Searching KAT for #{options[:search].blue}".yellow
71
+ kat_search = KAT.new(options[:search])
72
+
73
+ # Did it find results?
74
+ if not kat_search.results_found
75
+ cli.say "No results found for #{options[:search].blue}".red
76
+ exit
77
+ end
78
+
79
+ # Choose link and subtitles
80
+ link, sub_file = Commands.choose_video_and_subtitles(options, kat_search)
81
+
82
+ # Peerflix command build
83
+ command = "peerflix '#{link.magnet}'"
84
+ command << " --subtitles '#{sub_file}'" if sub_file
85
+ command << " --#{options[:video_player]}" if options[:video_player]
86
+ command << ' -- --fullscreen'
83
87
 
84
- begin
85
88
  # Execute peerflix
89
+ cli.say "Executing peerflix with #{link.filename.blue} ".yellow
90
+ cli.say "and #{sub_file.blue}".yellow if sub_file
86
91
  system command
87
- rescue Interrupt
88
- puts
89
- puts 'Interrupted!'
92
+ rescue Interrupt, EOFError
93
+ cli.say 'Interrupted!'.red
90
94
  ensure
91
95
  # Cleaning up
92
- video_file = Dir["#{cache_dir}/**/*.{mkv,mp4,avi,wmv,mpg,flv,mov}"].first
93
- FileUtils.mv(sub_file, File.dirname(video_file)) if sub_file
94
- puts "Your downloads will be in #{cache_dir} for 3 days :)"
96
+ if link
97
+ cache_dir = "/tmp/torrent-stream/#{link.info_hash}"
98
+ video_file = Dir["#{cache_dir}/**/*.{mkv,mp4,avi,wmv,mpg,flv,mov}"].first
99
+ FileUtils.mv(sub_file, File.dirname(video_file)) if sub_file
100
+ cli.say "Your downloads will be in #{cache_dir.blue} #{'for 3 days :)'.yellow}".yellow
101
+ end
95
102
  end
data/lib/peerflixrb.rb CHANGED
@@ -1,61 +1,3 @@
1
- require 'addic7ed'
2
- require 'cgi'
3
- require 'erb'
4
- require 'nokogiri'
5
- require 'open-uri'
6
1
  require 'peerflixrb/version'
7
- require 'yaml'
8
-
9
- module Peerflixrb
10
- def self.find_subtitles(video_file, language)
11
- episode = Addic7ed::Episode.new(video_file)
12
- return File.basename episode.download_best_subtitle!(language)
13
- rescue Addic7ed::EpisodeNotFound
14
- puts "Episode not found on Addic7ed : #{episode.video_file.filename}."
15
- rescue Addic7ed::ShowNotFound
16
- puts "Show not found on Addic7ed : #{episode.video_file.filename}."
17
- rescue Addic7ed::NoSubtitleFound
18
- puts "No (acceptable) subtitle has been found on Addic7ed for #{episode.video_file.filename}."
19
- rescue Addic7ed::InvalidFilename
20
- puts "Addic7ed gem doesn't like the format passed. Skipping subtitles."
21
- end
22
-
23
- ##
24
- # Extract file info and magnet link from the first match of your search
25
- # KAT.new("Suits s05e16 1080p")
26
- class KAT
27
- attr_accessor :url
28
-
29
- def initialize(search)
30
- @url = "https://kat.cr/usearch/#{ERB::Util.url_encode(search)}/"
31
- end
32
-
33
- def page
34
- @page ||= Nokogiri::HTML(open(@url))
35
- end
36
-
37
- def filename
38
- @filename ||= "#{CGI.unescape(params['name'])}.#{params['extension']}"
39
- end
40
-
41
- def magnet
42
- @magnet ||= params['magnet']
43
- end
44
-
45
- def info_hash
46
- @info_hash ||= extract_hash
47
- end
48
-
49
- private
50
-
51
- def params
52
- @params ||= YAML.load page.at_css('.iaconbox > div')['data-sc-params']
53
- end
54
-
55
- def extract_hash
56
- # Extract magnet properties to a Hash
57
- magnet_params = CGI.parse(URI.parse(magnet).query)
58
- magnet_params['xt'].first.match(/[0-9A-F]+/).to_s.downcase
59
- end
60
- end
61
- end
2
+ require 'peerflixrb/kat'
3
+ require 'peerflixrb/link'
@@ -0,0 +1,72 @@
1
+ require 'addic7ed'
2
+ require 'highline'
3
+
4
+ module Commands
5
+ class << self
6
+ attr_accessor :cli
7
+ end
8
+
9
+ self.cli = HighLine.new
10
+ HighLine.colorize_strings
11
+
12
+ def self.select_link(kat_search)
13
+ cli.choose(*kat_search.links) do |menu|
14
+ menu.header = 'Choose which file'.yellow
15
+ menu.default = '1'
16
+ menu.select_by = :index
17
+ end
18
+ end
19
+
20
+ def self.find_subtitles(video_file, language)
21
+ episode = Addic7ed::Episode.new(video_file)
22
+ return File.basename episode.download_best_subtitle!(language)
23
+ rescue Addic7ed::EpisodeNotFound
24
+ cli.say 'Episode not found on Addic7ed'.red
25
+ rescue Addic7ed::ShowNotFound
26
+ cli.say 'Show not found on Addic7ed'.red
27
+ rescue Addic7ed::NoSubtitleFound
28
+ cli.say 'No (acceptable) subtitle has been found on Addic7ed'.red
29
+ rescue Addic7ed::InvalidFilename
30
+ cli.say 'Addic7ed gem could not parse the filename correctly'.red
31
+ end
32
+
33
+ def self.choose_video_and_subtitles(options, kat_search)
34
+ # Proactively declare them because they would be lost in the block scope
35
+ link = nil
36
+ sub_file = nil
37
+
38
+ loop do
39
+ # Choose file
40
+ link = (options[:auto_select]) ? kat_search.links.first : select_link(kat_search)
41
+
42
+ # Subtitle search
43
+ sub_file = if options[:find_subtitles]
44
+ cli.say "Searching Addic7ed for subtitles for #{link.filename.blue}".yellow
45
+ find_subtitles(link.filename, options[:language])
46
+ elsif options[:subtitles]
47
+ options[:subtitles]
48
+ end
49
+
50
+ # Was there a problem with the subtitle?
51
+ if options[:find_subtitles] && sub_file.nil?
52
+ continue = cli.agree "Do you want to continue? #{'[y/n]'.yellow}".green
53
+
54
+ unless continue
55
+ # If :auto_select, exit program
56
+ # If video chosen, choose video again
57
+ if options[:auto_select]
58
+ exit
59
+ else
60
+ system 'clear'
61
+ next
62
+ end
63
+ end
64
+ end
65
+
66
+ # If the loop got here then it found subtitles
67
+ break
68
+ end
69
+
70
+ [link, sub_file]
71
+ end
72
+ end
@@ -0,0 +1,40 @@
1
+ require 'erb'
2
+ require 'nokogiri'
3
+ require 'yaml'
4
+
5
+ ##
6
+ # Extract a list of results from your search
7
+ # KAT.new("Suits s05e16 1080p")
8
+ class KAT
9
+ NUMBER_OF_LINKS = 5
10
+
11
+ attr_accessor :url
12
+
13
+ def initialize(search)
14
+ @url = "https://kat.cr/usearch/#{ERB::Util.url_encode(search)}/"
15
+ end
16
+
17
+ def page
18
+ @page ||= Nokogiri::HTML(open(@url))
19
+ end
20
+
21
+ def results_found
22
+ @results_found ||= page.at('p:contains("did not match any documents")').nil?
23
+ end
24
+
25
+ def links
26
+ @links ||= generate_links
27
+ end
28
+
29
+ private
30
+
31
+ def generate_links
32
+ links = []
33
+ crawled_links = page.css('.iaconbox > div')
34
+ crawled_links[0..NUMBER_OF_LINKS - 1].each do |link|
35
+ links << Link.new(YAML.load(link['data-sc-params']))
36
+ end
37
+
38
+ return links
39
+ end
40
+ end
@@ -0,0 +1,34 @@
1
+ require 'cgi'
2
+ require 'open-uri'
3
+
4
+ ##
5
+ # Object that contains the info for a torrent file
6
+ class Link
7
+ def initialize(params)
8
+ @params = params
9
+ end
10
+
11
+ def to_s
12
+ filename
13
+ end
14
+
15
+ def filename
16
+ @filename ||= "#{CGI.unescape(@params['name'])}.#{@params['extension']}"
17
+ end
18
+
19
+ def magnet
20
+ @magnet ||= @params['magnet']
21
+ end
22
+
23
+ def info_hash
24
+ @info_hash ||= extract_hash
25
+ end
26
+
27
+ private
28
+
29
+ def extract_hash
30
+ # Extract magnet properties to a Hash
31
+ magnet_params = CGI.parse(URI.parse(magnet).query)
32
+ magnet_params['xt'].first.match(/[0-9A-F]+/).to_s.downcase
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module Peerflixrb
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
data/peerflixrb.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_runtime_dependency 'nokogiri', '~> 1.6'
23
23
  spec.add_runtime_dependency 'addic7ed', '~> 2.0'
24
+ spec.add_runtime_dependency 'highline', '~> 1.7'
24
25
 
25
26
  spec.add_development_dependency 'bundler', '~> 1.11'
26
27
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peerflixrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Marchante
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-14 00:00:00.000000000 Z
11
+ date: 2016-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: highline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -168,6 +182,9 @@ files:
168
182
  - Rakefile
169
183
  - bin/peerflixrb
170
184
  - lib/peerflixrb.rb
185
+ - lib/peerflixrb/commands.rb
186
+ - lib/peerflixrb/kat.rb
187
+ - lib/peerflixrb/link.rb
171
188
  - lib/peerflixrb/version.rb
172
189
  - peerflixrb.gemspec
173
190
  homepage: https://github.com/iovis9/peerflixrb
@@ -190,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
207
  version: '0'
191
208
  requirements: []
192
209
  rubyforge_project:
193
- rubygems_version: 2.6.2
210
+ rubygems_version: 2.6.3
194
211
  signing_key:
195
212
  specification_version: 4
196
213
  summary: Wrapper for peerflix with automatic search through KAT and Addic7ed.