nyaa_anime 0.7.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e624f06033c4f20b98887562663b271fbd3342d5
4
- data.tar.gz: 4912a5bb4e60dca9463ae3682a9d4ced3ab30b7d
3
+ metadata.gz: 1ba7aa90f66024323a49d1989fdf783419e8bfd3
4
+ data.tar.gz: d6e20d46a46a18ef7545e153b38c34ee27884db6
5
5
  SHA512:
6
- metadata.gz: 45b2ca6bd017059bfece00d1bbc130e4e3273b2f403c26281bdc091a38243a5a08c52b2ef512c737cb332bac678ffddf8fb75bfc19a6928c4a91b440f385785e
7
- data.tar.gz: c3fd3e63b529b7d5c7c936d7333caf08bfc86ee38720bcfbb38c471e12715c35dd8d6fd6ba7bc7a3e21227c1f5c0ca2c9f334546a014bb368151148b59ab3ad2
6
+ metadata.gz: 5771be98e39ffeb8ebf1f2de2a28f6090bff8428fbc7e992195e301193e6870d42cd105e0f230d299d1e6a577cfb9b91de342f142c3372a64d433798a86fff55
7
+ data.tar.gz: 97927ef0ff0c716e27d8ae14cf9b99f9814c66d92e0dd602d01961ed8617934d122850893f94e7f441243133878187f6ae15a8418d5b77c9ca117a45d231c72b
@@ -1,30 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- scripts = ["search", "list", "remove"]
4
-
5
- if ARGV[0] == "get"
6
- ARGV[0], ARGV[1] = "search", "-naq"
7
- elsif ARGV[0] == "help" && ARGV[1]
8
- if scripts.include? ARGV[1]
9
- ARGV[0], ARGV[1] = ARGV[1], "--help"
10
- else
11
- puts "Error: No such command \"#{ARGV[1]}\"."
12
- exit
13
- end
14
- end
15
-
16
- script = ARGV[0]
17
- if scripts.include? script
18
- require "nyaa_anime/#{script}"
19
- else
20
- puts "Usage: nyaa <command> [<args>]\n" <<
21
- " nyaa_anime <command> [<args>]\n\n" <<
22
- " If you like this tool, please donate via PayPal\n" <<
23
- " to wujoshuawu@gmail.com to keep the developer afloat.\n\n" <<
24
- "Commands:\n" <<
25
- " search Search for and download anime\n" <<
26
- " get Equivalent to `nyaa search -naq`\n" <<
27
- " list List downloaded torrent files\n" <<
28
- " remove Delete download torrent files from a series\n" <<
29
- " help Get help about a command (`nyaa help <command>`)\n"
3
+ begin
4
+ require 'nyaa_anime/main'
5
+ rescue Interrupt
6
+ warn " - Aborted."
7
+ exit 1
30
8
  end
@@ -6,7 +6,6 @@ class NyaaAnime
6
6
  require 'nokogiri'
7
7
  require 'cgi'
8
8
  require 'colorize'
9
- require 'levenshtein'
10
9
  require 'set'
11
10
  require 'fileutils'
12
11
  require 'parallel'
@@ -16,8 +15,7 @@ class NyaaAnime
16
15
  NYAA_SEARCH_DEFAULT = "#{NYAA_SEARCH}&cats=1_37&filter=2&term="
17
16
  NYAA_DL_DIR = "#{Dir.home}/Downloads/nyaa/"
18
17
  NYAA_ENTRIES_PER_PAGE = 100
19
- MAX_PAGES = 5
20
- TITLE_TOLERANCE_RANGE = (1..4)
18
+ MAX_PAGES = 20
21
19
  NEW_COLOR = :green
22
20
  ALREADY_DOWNLOADED_COLOR = :cyan
23
21
  CL_WIDTH = 70
@@ -27,20 +25,19 @@ class NyaaAnime
27
25
 
28
26
  FileUtils.mkdir_p NYAA_DL_DIR
29
27
 
30
- def search(title, quiet=false)
31
- puts "Searching for \"#{title}\"..." unless quiet
28
+ def search(title)
32
29
  @downloads = {}
33
30
  @titles = []
34
31
  url = "#{NYAA_SEARCH_DEFAULT}#{CGI.escape(title)}&offset="
35
32
  titles = []
36
33
  dl_urls = []
37
34
  offset = 1
38
- while true
35
+ loop do
39
36
  begin
40
37
  results = Nokogiri::HTML(open("#{url}#{offset}")).css('item')
41
- rescue SocketError
42
- puts "Error: Could not connect to NyaaTorrents."
43
- exit
38
+ rescue SocketError, EOFError
39
+ $stderr.print "Error: Could not connect to NyaaTorrents.\n"
40
+ exit 1
44
41
  end
45
42
  titles.concat results.css('title').map { |t| t.content }
46
43
  dl_urls.concat results.css('link').map { |l| l.next.content }
@@ -49,13 +46,12 @@ class NyaaAnime
49
46
  end
50
47
  titles.each_with_index { |t, i| @downloads[t] = dl_urls[i] }
51
48
  @titles = titles.sort!
52
- categorize_titles
53
- colorized_titles.each_with_index { |t, i| puts "#{i+1}: #{t}" } unless quiet
54
- nil
49
+ categorize_titles!
50
+ colorized_titles
55
51
  end
56
52
 
57
53
  def find_new_titles(quiet=false)
58
- search_terms = NyaaAnime.series_of_downloaded_titles
54
+ search_terms = NyaaAnime.distros_of_downloaded_titles
59
55
  new_titles = Set.new
60
56
  new_downloads = {}
61
57
  count_s_size = "[/] ".size + search_terms.count.to_s.size * 2
@@ -66,89 +62,86 @@ class NyaaAnime
66
62
  lock = Mutex.new
67
63
  Parallel.each(search_terms, in_threads: THREAD_COUNT) do |t|
68
64
  n = NyaaAnime.new
69
- n.search t, true
65
+ n.search t
70
66
  lock.synchronize do
71
67
  new_titles.merge n.new_titles
72
68
  n.new_titles.each { |ti| new_downloads[ti] = n.downloads[ti] }
73
69
  if !quiet
74
70
  msg = "/#{search_terms.count}] #{t[0...max_entry_size-count_s_size]}..." <<
75
71
  ("." * [max_entry_size - (count_s_size + t.size), 0].max) <<
76
- (n.new_titles.any? ? "#{n.new_titles.count} found!".colorize(:green) : "none found.")
72
+ (n.new_titles.any? ? "#{n.new_titles.count} found!".colorize(NEW_COLOR) : "none found.")
77
73
  puts "[#{index.to_s.rjust(search_terms.count.to_s.size, "0")}#{msg}"
78
74
  index += 1
79
75
  end
80
76
  end
81
77
  end
82
78
  @new_titles = new_titles
83
- @titles = new_titles.to_a.sort!
84
79
  @downloads = new_downloads
85
80
  @already_downloaded_titles = Set.new
86
81
  @version_differing_titles = Set.new
87
82
  @other_titles = Set.new
88
- if downloads.count > 0
89
- puts "#{downloads.count} new episode#{"s" if downloads.count > 1} found!".colorize(:green)
90
- end
91
- @titles.each_with_index { |t, i| puts "#{i+1}: #{t.colorize(NEW_COLOR)}" }
83
+ @titles = new_titles.to_a.sort!
92
84
  end
93
85
 
94
86
  def colorized_titles
95
87
  @titles.map do |t|
96
88
  if @already_downloaded_titles.include? t
97
- t.colorize(ALREADY_DOWNLOADED_COLOR)
89
+ t.colorize ALREADY_DOWNLOADED_COLOR
98
90
  elsif @new_titles.include? t
99
- t.colorize(NEW_COLOR)
91
+ t.colorize NEW_COLOR
100
92
  else
101
93
  t
102
94
  end
103
95
  end
104
96
  end
105
97
 
106
- def categorize_titles
98
+ def categorize_titles!
107
99
  @already_downloaded_titles = Set.new
108
100
  @new_titles = Set.new
109
101
  @other_titles = Set.new
110
102
  @version_differing_titles = Set.new
111
103
  dls = NyaaAnime.downloaded_torrents
104
+ downloaded_distros = NyaaAnime.distros_of_downloaded_titles
112
105
  stripped_dls = NyaaAnime.stripped_downloaded_titles
113
- hashless_dls = dls.map { |t| NyaaAnime.hashless_title t }
114
106
  @titles.each do |t|
115
- stripped_t = NyaaAnime.stripped_title t
116
- hashless_t = NyaaAnime.hashless_title t
117
107
  if dls.include? "#{t}.torrent"
118
108
  @already_downloaded_titles.add t
109
+ elsif stripped_dls.include? NyaaAnime.stripped_title(t)
110
+ @version_differing_titles.add t
111
+ elsif downloaded_distros.include? NyaaAnime.distro_of_title(t)
112
+ @new_titles.add t
119
113
  else
120
- if stripped_dls.include? stripped_t
121
- @version_differing_titles.add t
122
- else
123
- new_title_pushed = false
124
- stripped_dls.each_with_index do |dlt, i|
125
- dist = Levenshtein.distance(dlt, stripped_t)
126
- if TITLE_TOLERANCE_RANGE.include? dist and
127
- dist == Levenshtein.distance(hashless_dls[i], hashless_t)
128
- @new_titles.add t
129
- new_title_pushed = true
130
- break
131
- end
132
- end
133
- if !new_title_pushed
134
- @other_titles.add t
135
- end
136
- end
114
+ @other_titles.add t
137
115
  end
138
116
  end
139
117
  nil
140
118
  end
141
119
 
142
120
  def self.stripped_title(torrent)
143
- torrent.sub(/(\s*\[\w*\])*(\.\w+)+\z/, "")
121
+ torrent.gsub("_", " ").sub(/(\s*\[\w*\])*(\.\w+)*\z/, "")
144
122
  end
145
123
 
146
124
  def self.hashless_title(torrent)
147
- torrent.sub(/(\s*\[\w{8}\])?(\.\w+)+\z/, "")
125
+ torrent.gsub("_", " ").sub(/(\s*\[\w{8,}\])?(\.\w+)*\z/, "")
126
+ end
127
+
128
+ def self.episode_of_title(torrent)
129
+ ep = NyaaAnime.hashless_title(torrent)[/\s+-\s+(\d+(\.\d+)?(v\d+)?-?)+\s*/, 1]
130
+ if ep.nil?
131
+ ep = NyaaAnime.hashless_title(torrent)[/\s+(\d+(\.\d+)?(v\d+)?-?)+\s*/, 1]
132
+ end
133
+ unless ep.nil?
134
+ ep.split("-").map { |e| e[/0*(\d+)/, 1] }
135
+ end
148
136
  end
149
137
 
150
- def self.series_of_title(torrent)
151
- NyaaAnime.hashless_title(torrent).sub(/\s+-\s+\d+(\.\d+)?(v\d+)?\s*/, " ")
138
+ def self.distro_of_title(torrent)
139
+ distro = NyaaAnime.hashless_title(torrent).sub!(/\s+-\s+(\d+(\.\d+)?(v\d+)?-?)+\s*/, " ")
140
+ if distro
141
+ distro
142
+ else
143
+ NyaaAnime.hashless_title(torrent).sub(/\s+(\d+(\.\d+)?(v\d+)?-?)+\s*/, " ")
144
+ end
152
145
  end
153
146
 
154
147
  def self.downloaded_torrents
@@ -161,41 +154,43 @@ class NyaaAnime
161
154
  dls
162
155
  end
163
156
 
164
- def self.series_of_downloaded_titles
157
+ def self.distros_of_downloaded_titles
165
158
  dls = Set.new
166
- NyaaAnime.downloaded_torrents.each { |t| dls.add NyaaAnime.series_of_title(t) }
159
+ NyaaAnime.downloaded_torrents.each { |t| dls.add NyaaAnime.distro_of_title(t) }
167
160
  dls
168
161
  end
169
162
 
170
- def self.downloaded_torrents_by_series
163
+ def self.torrents_by_distro(torrents)
171
164
  dls = {}
172
- NyaaAnime.downloaded_torrents.each { |t| (dls[NyaaAnime.series_of_title(t)] ||= []) << t }
165
+ torrents.each { |t| (dls[NyaaAnime.distro_of_title(t)] ||= []) << t }
173
166
  dls
174
167
  end
175
168
 
176
- def download_index(index)
177
- if title = @titles[index]
178
- download(title)
179
- end
169
+ def self.downloaded_torrents_by_distro
170
+ NyaaAnime.torrents_by_distro NyaaAnime.downloaded_torrents
180
171
  end
181
172
 
182
- def download(title)
173
+ def download_single(title)
183
174
  torrent = open @downloads[title]
184
175
  filename = torrent.meta["content-disposition"][/filename="(.+\.torrent)"/, 1]
185
176
  open("#{NYAA_DL_DIR}#{filename}", "w") { |f| f.write torrent.read }
186
177
  "#{NYAA_DL_DIR}\"#{title}.torrent\""
187
178
  end
188
179
 
189
- def download_batch(titles)
190
- Parallel.map(titles) { |t| download(t) }.sort!
180
+ def download(titles)
181
+ if titles.is_a? Array
182
+ Parallel.map(titles) { |t| download_single(t) }.sort!
183
+ else
184
+ download_single titles
185
+ end
191
186
  end
192
187
 
193
188
  def download_all
194
- download_batch @titles
189
+ download @titles
195
190
  end
196
191
 
197
192
  def download_all_new
198
- download_batch @new_titles
193
+ download @new_titles
199
194
  end
200
195
 
201
196
  end
@@ -1,23 +1,31 @@
1
1
  require 'optparse'
2
2
 
3
3
  options = {}
4
- OptionParser.new do |opts|
5
- opts.banner = "Usage: nyaa list [-a]\n\n" <<
4
+ option_parser = OptionParser.new do |opts|
5
+ opts.banner = "Usage: nyaa list [-a]\n" <<
6
+ " nyaa ls [-a]\n\n" <<
6
7
  " If you like this tool, please donate via PayPal\n" <<
7
8
  " to wujoshuawu@gmail.com to keep the developer afloat.\n\n"
8
9
 
9
10
  opts.on("-a", "--all", "List all individual files") do
10
11
  options[:list_all] = true
11
12
  end
12
- end.parse!
13
+ end
14
+ begin
15
+ option_parser.parse!
16
+ rescue OptionParser::InvalidOption => e
17
+ warn e
18
+ puts option_parser
19
+ exit 1
20
+ end
13
21
 
14
22
  require 'nyaa_anime'
15
23
 
16
- dt_s = NyaaAnime.downloaded_torrents_by_series
17
- ss = dt_s.keys.sort!
18
- ss.each_with_index do |series, idx|
19
- puts "#{idx+1}: #{series}"
24
+ dt_ds = NyaaAnime.downloaded_torrents_by_distro
25
+ ds = dt_ds.keys.sort!
26
+ ds.each_with_index do |distro, idx|
27
+ puts "#{idx+1}: #{distro}"
20
28
  if options[:list_all]
21
- dt_s[series].each { |torrent| puts " #{torrent}" }
29
+ dt_ds[distro].each { |torrent| puts " #{torrent}" }
22
30
  end
23
31
  end
@@ -0,0 +1,39 @@
1
+ scripts = ["search", "list", "remove"]
2
+
3
+ aliases = { "s"=>"search", "g"=>"get", "ls"=>"list", "rm"=>"remove", "h"=>"help" }
4
+ if command = aliases[ARGV[0]]
5
+ ARGV[0] = command
6
+ end
7
+
8
+ if ARGV[0] == "get"
9
+ ARGV[0], ARGV[1] = "search", "-naq"
10
+ elsif ARGV[0] == "help" && ARGV[1]
11
+ if command = aliases[ARGV[1]]
12
+ ARGV[1] = command
13
+ end
14
+ if scripts.include? ARGV[1]
15
+ ARGV[0], ARGV[1] = ARGV[1], "--help"
16
+ else
17
+ warn "Error: No such command \"#{ARGV[1]}\"."
18
+ exit 1
19
+ end
20
+ end
21
+
22
+ script = ARGV[0]
23
+ if scripts.include? script
24
+ require "nyaa_anime/#{script}"
25
+ else
26
+ warn "Error: No such command \"#{ARGV[0]}\"."
27
+ puts "Usage: nyaa <command> [<args>]\n" <<
28
+ " nyaa_anime <command> [<args>]\n\n" <<
29
+ " If you like this tool, please donate via PayPal\n" <<
30
+ " to wujoshuawu@gmail.com to keep the developer afloat.\n\n" <<
31
+ "Commands:\n" <<
32
+ " search s Search for and download anime\n" <<
33
+ " get g Equivalent to `nyaa search -naq`\n" <<
34
+ " list ls List downloaded torrent files\n" <<
35
+ " remove rm Delete downloaded torrent files from a\n" <<
36
+ " distribution of an anime series\n" <<
37
+ " help h Get help about a command\n" <<
38
+ " (`nyaa help <command>`)\n"
39
+ end
@@ -3,8 +3,9 @@ require 'optparse'
3
3
  ARGV[1] ||= "--help"
4
4
 
5
5
  options = {}
6
- OptionParser.new do |opts|
6
+ option_parser = OptionParser.new do |opts|
7
7
  opts.banner = "Usage: nyaa remove <anime>\n" <<
8
+ " nyaa rm ...\n" <<
8
9
  " nyaa remove -n <index>\n" <<
9
10
  " nyaa remove -a\n\n" <<
10
11
  " If you like this tool, please donate via PayPal\n" <<
@@ -14,18 +15,25 @@ OptionParser.new do |opts|
14
15
  options[:remove_all] = true
15
16
  end
16
17
 
17
- opts.on("-n", "--number [N]", "Remove torrents of series as",
18
- "numbered by `nyaa list`") do |n|
18
+ opts.on("-n", "--number [N]", "Remove torrents of an anime distribution",
19
+ "as numbered by `nyaa list`") do |n|
19
20
  options[:number] = n.to_i
20
21
  end
21
- end.parse!
22
+ end
23
+ begin
24
+ option_parser.parse!
25
+ rescue OptionParser::InvalidOption => e
26
+ warn e
27
+ puts option_parser
28
+ exit 1
29
+ end
22
30
 
23
31
  require 'nyaa_anime'
24
32
 
25
- TORRENTS_BY_SERIES = NyaaAnime.downloaded_torrents_by_series
26
- ss = TORRENTS_BY_SERIES.keys.sort!
33
+ TORRENTS_BY_DISTRO = NyaaAnime.downloaded_torrents_by_distro
34
+ ds = TORRENTS_BY_DISTRO.keys.sort!
27
35
 
28
- if ss.size == 0
36
+ if ds.size == 0
29
37
  puts "No files in #{NyaaAnime::NYAA_DL_DIR}."
30
38
  end
31
39
 
@@ -39,23 +47,28 @@ if options[:remove_all]
39
47
  exit
40
48
  end
41
49
 
42
- def delete_series(series)
43
- ts = TORRENTS_BY_SERIES[series].map { |t| "#{NyaaAnime::NYAA_DL_DIR}#{t}" }
44
- if agree "Delete all #{ts.size} torrent files for #{series}? (y/n) "
50
+ def delete_distro(distro)
51
+ ts = TORRENTS_BY_DISTRO[distro].map { |t| "#{NyaaAnime::NYAA_DL_DIR}#{t}" }
52
+ if agree "Delete all #{ts.size} torrent files for #{distro}? (y/n) "
45
53
  File.delete(*ts)
46
54
  end
47
55
  end
48
56
 
49
57
  if options[:number]
50
58
  num = options[:number]
51
- if (1..ss.size).include? num
52
- delete_series ss[num-1]
59
+ if (1..ds.size).include? num
60
+ delete_distro ds[num-1]
53
61
  else
54
- puts "Error: Number (#{num}) out of range 1-#{ss.size}."
62
+ warn "Error: Number (#{num}) out of range 1-#{ds.size}."
63
+ exit 1
55
64
  end
56
65
  exit
57
66
  end
58
67
 
59
68
  term = ARGV[1..-1].join " "
60
- ss = TORRENTS_BY_SERIES.keys.select { |series| !!series[/#{term}/i] }
61
- ss.each { |series| delete_series series }
69
+ ds = TORRENTS_BY_DISTRO.keys.select { |distro| !!distro[/#{term}/i] }
70
+ if ds.count == 0
71
+ warn "Error: No torrent files matching \"#{term}\"."
72
+ exit 1
73
+ end
74
+ ds.each { |distro| delete_distro distro }
@@ -13,12 +13,17 @@ require 'optparse'
13
13
  ARGV[1] ||= "--help"
14
14
 
15
15
  options = {}
16
- OptionParser.new do |opts|
16
+ option_parser = OptionParser.new do |opts|
17
17
  opts.banner = "Usage: nyaa search [options] <anime>\n" <<
18
+ " nyaa s [options] <anime>\n" <<
18
19
  " nyaa get (equivalent to `nyaa search -naq`)\n\n" <<
19
20
  " If you like this tool, please donate via PayPal\n" <<
20
21
  " to wujoshuawu@gmail.com to keep the developer afloat.\n\n"
21
22
 
23
+ opts.on("-i", "--individual", "List all results individually") do
24
+ options[:list_individually] = true
25
+ end
26
+
22
27
  opts.on("-n", "--new", "Find all new episodes of",
23
28
  "previously downloaded anime") do
24
29
  options[:find_new] = true
@@ -49,7 +54,7 @@ OptionParser.new do |opts|
49
54
  if FANSUBBERS[f]
50
55
  options[:subber] = FANSUBBERS[f]
51
56
  elsif f.nil?
52
- puts "Error: Expected [FANSUBBER] with the -f option"
57
+ warn "Error: Expected [FANSUBBER] with the -f option"
53
58
  ARGV.push "--help"
54
59
  else
55
60
  options[:subber] = f
@@ -60,45 +65,76 @@ OptionParser.new do |opts|
60
65
  "`--fansubber=#{SPEEDSUBBER}`") do
61
66
  options[:subber] = SPEEDSUBBER
62
67
  end
63
- end.parse!
68
+ end
69
+ begin
70
+ option_parser.parse!
71
+ rescue OptionParser::InvalidOption => e
72
+ warn e
73
+ puts option_parser
74
+ exit 1
75
+ end
64
76
 
65
77
  if !options[:download_all]
66
78
  options[:quiet] = false
67
79
  end
68
80
 
69
81
  require 'nyaa_anime'
82
+ require 'colorize'
70
83
  nyaa = NyaaAnime.new
71
84
 
72
85
  if options[:find_new]
73
- nyaa.find_new_titles !!options[:quiet]
74
- else
75
- if ARGV.size == 2
76
- search_term = ARGV[1]
86
+ titles = nyaa.find_new_titles !!options[:quiet]
87
+ if nyaa.downloads.count == 0
88
+ puts "No new episodes found."
89
+ exit
77
90
  else
78
- search_term = ARGV[1..-1].join " "
91
+ puts "#{nyaa.downloads.count} new episode#{"s" if nyaa.downloads.count > 1} found!".colorize(NyaaAnime::NEW_COLOR)
92
+ titles.each_with_index { |t, i| puts "#{i+1}: #{t.colorize(NyaaAnime::NEW_COLOR)}" }
79
93
  end
80
- if subber = options[:subber]
81
- search_term = "[#{subber}] #{search_term}"
94
+ else
95
+ search_term = ARGV[1..-1].join " "
96
+ search_term = "[#{options[:subber]}] #{search_term}" if options[:subber]
97
+ search_term += " #{options[:resolution]}" if options[:resolution]
98
+ if search_term == ""
99
+ warn "Error: No search term provided."
100
+ exit 1
82
101
  end
83
- if res = options[:resolution]
84
- search_term += " #{res}"
102
+ puts "Searching for \"#{search_term}\"..." unless options[:quiet]
103
+ results = nyaa.search search_term
104
+ if options[:list_individually]
105
+ results.each_with_index { |t, i| puts "#{i+1}: #{NyaaAnime.hashless_title t}" } unless options[:quiet]
106
+ else
107
+ results = NyaaAnime.torrents_by_distro results
108
+ unless options[:quiet]
109
+ results.keys.sort!.each_with_index do |ds, i|
110
+ if results[ds].count == 1
111
+ puts "#{i+1}: #{NyaaAnime.hashless_title ds}"
112
+ next
113
+ end
114
+ first_ep = NyaaAnime.episode_of_title(results[ds].first)
115
+ last_ep = NyaaAnime.episode_of_title(results[ds].last)
116
+ if first_ep && last_ep
117
+ ep_tag = "(#{first_ep.first}~#{last_ep.last})"
118
+ else
119
+ ep_tag = "(#{results[ds].count} torrent#{"s" if results[ds].count > 1})"
120
+ end
121
+ puts "#{i+1}: #{ep_tag.bold} #{ds}"
122
+ end
123
+ end
85
124
  end
86
- if search_term == ""
87
- puts "Error: No search term provided."
125
+ if nyaa.downloads.count == 0
126
+ puts "No results for \"#{search_term}\"."
88
127
  exit
128
+ else
129
+ puts "#{nyaa.downloads.count} result#{"s" if nyaa.downloads.count > 1}, #{nyaa.new_titles.count} new."
89
130
  end
90
- nyaa.search search_term, !!options[:quiet]
91
- end
92
-
93
- if nyaa.downloads.count == 0
94
- puts "No downloads found."
95
- exit
96
131
  end
97
132
 
98
133
  require 'os'
99
134
  require 'highline/import'
100
135
 
101
136
  def prompt_to_open(torrents)
137
+ torrents = [torrents] unless torrents.is_a? Array
102
138
  if torrents.count == 1
103
139
  puts "Downloaded #{File.basename torrents[0]}."
104
140
  else
@@ -120,37 +156,33 @@ if options[:download_all]
120
156
  exit
121
157
  end
122
158
 
123
- if nyaa.downloads.count > 1
124
- prompt = "Download (1-#{nyaa.downloads.count}, 'a(ll)'"
125
- prompt << ", 'n(ew)'" if nyaa.new_titles.count > 0 && !options[:find_new]
126
- prompt << ", or [ENTER] to cancel): "
127
- else
128
- if agree "Download? (y/n): "
129
- prompt_to_open [nyaa.download_index(0)]
159
+ if results.count > 1
160
+ prompt = "Download (1-#{results.count}, 'a(ll)'" <<
161
+ "#{", 'n(ew)'" if nyaa.new_titles.count > 0 && !options[:find_new]}" <<
162
+ ", or [ENTER] to cancel): "
163
+ choice = ask(prompt) do |q|
164
+ q.validate = Proc.new do |input|
165
+ input == "" ||
166
+ ("new"[/\A#{input}/i] && nyaa.new_titles.count > 0 && !options[:find_new]) ||
167
+ "all"[/\A#{input}/i] ||
168
+ (1..results.count).include?(input.to_i)
169
+ end
170
+ q.responses[:not_valid] = ""
130
171
  end
131
- exit
132
- end
133
172
 
134
- while true
135
- print prompt
136
- index = $stdin.gets.chomp.downcase
137
- begin
138
- if index == ""
139
- exit
140
- end
141
- if "new".match(/\A#{index}/) && nyaa.new_titles.count > 0 && !options[:find_new]
142
- prompt_to_open nyaa.download_all_new
143
- exit
144
- end
145
- if "all".match(/\A#{index}/)
146
- prompt_to_open nyaa.download_all
147
- exit
148
- end
149
- if (1..nyaa.downloads.count).include?(index.to_i)
150
- prompt_to_open [nyaa.download_index(index.to_i-1)]
151
- exit
152
- end
153
- rescue RegexpError
154
- next
173
+ if choice == ""
174
+ elsif choice[/\An/i]
175
+ prompt_to_open nyaa.download_all_new
176
+ elsif choice[/\Aa/i]
177
+ prompt_to_open nyaa.download_all
178
+ else
179
+ torrent = results.is_a?(Array) ? results[choice.to_i-1] : results[results.keys.sort![choice.to_i-1]]
180
+ prompt_to_open nyaa.download(torrent)
181
+ end
182
+
183
+ else
184
+ if agree "Download? (y/n): "
185
+ torrent = results.is_a?(Array) ? results.first : results.values.first
186
+ prompt_to_open nyaa.download(torrent)
155
187
  end
156
188
  end
@@ -1,3 +1,3 @@
1
1
  class NyaaAnime
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.2"
3
3
  end
@@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.add_runtime_dependency 'highline', '~> 1.6', '>= 1.6.21'
28
28
  spec.add_runtime_dependency 'nokogiri', '~> 1.6', '>= 1.6.2.1'
29
29
  spec.add_runtime_dependency 'colorize', '~> 0.7', '>= 0.7.3'
30
- spec.add_runtime_dependency 'levenshtein-ffi', '~> 1.1'
31
30
  spec.add_runtime_dependency 'os', '~> 0.9', '>=0.9.6'
32
31
 
33
32
  spec.add_development_dependency "bundler", "~> 1.6"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyaa_anime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Tsubaki Wu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-22 00:00:00.000000000 Z
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parallel
@@ -90,20 +90,6 @@ dependencies:
90
90
  - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: 0.7.3
93
- - !ruby/object:Gem::Dependency
94
- name: levenshtein-ffi
95
- requirement: !ruby/object:Gem::Requirement
96
- requirements:
97
- - - "~>"
98
- - !ruby/object:Gem::Version
99
- version: '1.1'
100
- type: :runtime
101
- prerelease: false
102
- version_requirements: !ruby/object:Gem::Requirement
103
- requirements:
104
- - - "~>"
105
- - !ruby/object:Gem::Version
106
- version: '1.1'
107
93
  - !ruby/object:Gem::Dependency
108
94
  name: os
109
95
  requirement: !ruby/object:Gem::Requirement
@@ -177,6 +163,7 @@ files:
177
163
  - bin/nyaa_anime
178
164
  - lib/nyaa_anime.rb
179
165
  - lib/nyaa_anime/list.rb
166
+ - lib/nyaa_anime/main.rb
180
167
  - lib/nyaa_anime/remove.rb
181
168
  - lib/nyaa_anime/search.rb
182
169
  - lib/nyaa_anime/version.rb