yotsuba 0.2.0 → 0.3.0

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: 16007be3d5d092565144cf0cf084846a0b881ed6
4
- data.tar.gz: 5ca4d65b96c42ff5ae3e5ca9217e38f742060929
3
+ metadata.gz: cebbf4e4596c2d1ba5105d93b01d08f217973d18
4
+ data.tar.gz: 248551e062b33657f79bcfe217c726790780a096
5
5
  SHA512:
6
- metadata.gz: 271b8be2a043ee6e55de1e323f36f34a4365e09d42674c535031a3654724d96f744306533e90e89590236d35d16fa3ea36b7de135a8306bbd6f458367ba79bad
7
- data.tar.gz: 0443bc79d9f83bd72e445b1947aca5f17a94aa206cf44f13a7bc4abfe055f2a4abb35b32be25cb6447d85b9842292271d51fab105912c267315eedd2deabf694
6
+ metadata.gz: 50deeea2270c036f0206f5c30511c39fa507b714ee900e8cc6e36a951135d9e31b267aa907d817ba45d62f8db0cdba425ca6b9a2763fbd02a4cfea345a02f495
7
+ data.tar.gz: 1d30a778451ff5f89f32d00e2d69d3cb0ea385e5323f57e2deb64667eb4531da4050eca95cdeaa0570c299267ec64bcfdde240e9639eef350c12762bfd700dc0
data/lib/yotsuba/anime.rb CHANGED
@@ -25,36 +25,48 @@ module Yotsuba
25
25
  end
26
26
 
27
27
  def self.all
28
- Yotsuba.get_animes if @@all_animes.length == 0
28
+ precache_animes
29
29
  @@all_animes
30
30
  end
31
31
 
32
32
  def self.[](key)
33
- Yotsuba.get_animes if @@all_animes.length == 0
34
- results = []
35
- single = false
33
+ return Anime.find_by(title: key) if key.is_a?(String)
34
+ return Anime.find_by(id: key) if key.is_a?(Fixnum)
35
+ return Anime.search(key) if key.is_a?(Regexp)
36
+
37
+ raise(ArgumentError, "Argument should be a String (title), Fixnum (id), or Regexp (matches title).")
38
+ end
36
39
 
37
- search = Proc.new { |a|
38
- single = true
39
- results << a if a.title == key
40
- } if key.is_a?(String)
40
+ def self.find_by(hash = {title: nil, id: nil})
41
+ precache_animes
42
+ @@all_animes.each do |anime|
43
+ match = check_property(anime, :id, hash) || check_property(anime, :title, hash)
44
+ return match if match
45
+ end
46
+ end
41
47
 
42
- search = Proc.new { |a|
43
- single = true
44
- results << a if a.id == key
45
- } if key.is_a?(Fixnum)
48
+ def self.find(id)
49
+ Anime.find_by(id: id)
50
+ end
46
51
 
47
- search = Proc.new { |a|
48
- single = false
49
- results << a if a.title.match(key)
50
- } if key.is_a?(Regexp)
52
+ def self.search(regexp)
53
+ precache_animes
54
+ results = []
55
+ @@all_animes.each do |anime|
56
+ results << anime if anime.title.match(regexp)
57
+ end
58
+ return results
59
+ end
51
60
 
52
- raise(ArgumentError, "Argument should be a String (title), Fixnum (id), or Regexp (matches title).") if search.nil?
61
+ private
53
62
 
54
- @@all_animes.each &search
55
- return single ? results[0] : results
63
+ def self.precache_animes
64
+ Yotsuba.get_animes if @@all_animes.length == 0
65
+ end
56
66
 
57
- end
67
+ def self.check_property(object, symbol, hash)
68
+ hash[symbol] && object.respond_to?(symbol) && object.send(symbol) == hash[symbol] ? object : nil
69
+ end
58
70
 
59
71
  end
60
72
  end
@@ -7,27 +7,30 @@ module Yotsuba
7
7
  class Download
8
8
  include Concurrent::Async
9
9
 
10
- attr_reader :status
10
+ @@all_downloads = []
11
11
 
12
- def initialize(options = {filename: nil, link: nil, part_links: nil, output_dir: nil })
13
- @filename = options[:filename]
14
- @link = options[:link]
15
- @part_links = options[:part_links]
16
- @output_dir = File.absolute_path(options[:output_dir]) if options[:output_dir]
17
- @status = "Queued"
18
-
19
- @output_dir ||= "."
12
+ attr_reader :status, :file, :id
20
13
 
21
- if @link.nil? && @part_links && @part_links.length == 1
22
- @link = @part_links.first
23
- @part_links = nil
14
+ def initialize(options = {animefile: nil, output_dir: "." })
15
+ @file = options[:animefile]
16
+ @output_dir = File.absolute_path(options[:output_dir])
17
+ @multiple = (@file.download_links.length > 1) if @file
18
+ @status = "Queued"
19
+ if self.valid?
20
+ @id = @@all_downloads.length + 1
21
+ @@all_downloads << self if self.valid?
24
22
  end
23
+
25
24
  init_mutex # Required by Concurrent::Async
26
25
  end
27
26
 
27
+ def valid?
28
+ self.file != nil
29
+ end
30
+
28
31
  def run
29
- @path = File.join(@output_dir, @filename) if @link
30
- @path = File.join(@output_dir, @filename+".zip") if @part_links
32
+ @path = @multiple ? File.join(@output_dir, @filename+".zip") : @path = File.join(@output_dir, @filename)
33
+
31
34
  FileUtils.mkdir_p @output_dir
32
35
 
33
36
  if File.exists? @path
@@ -35,15 +38,10 @@ module Yotsuba
35
38
  return
36
39
  end
37
40
 
38
- if @part_links
39
- @part_links.each do |link|
40
- create_request(link).run
41
- end
42
- finish_request
43
- elsif @link
44
- create_request(@link).run
45
- finish_request
41
+ @file.download_links.each do |link|
42
+ create_request(link).run
46
43
  end
44
+ finish_request
47
45
  end
48
46
 
49
47
  def run_async
@@ -54,10 +52,41 @@ module Yotsuba
54
52
  File.delete(@path) if @path
55
53
  end
56
54
 
57
- def bytes_downloaded
55
+ def bytes_written
58
56
  @path ? File.size(@path) : 0
59
57
  end
60
58
 
59
+ def percent_downloaded
60
+ 100.0 * self.bytes_written / self.file.size
61
+ end
62
+
63
+ def self.all
64
+ @@all_downloads
65
+ end
66
+
67
+ def self.[](key)
68
+ return self.find_by(id: key) if key.is_a?(Fixnum)
69
+ return self.find_by(file: key) if key.is_a?(AnimeFile)
70
+ return self.find_by(filename: key) if key.is_a?(String)
71
+ return self.find_by
72
+ end
73
+
74
+ def self.find_by(hash = {id: nil, file: nil, filename: nil, status: nil, percent_downloaded: nil})
75
+ @@all_downloads.each do |download|
76
+ match = check_property(download, :id, hash) ||
77
+ check_property(download, :file, hash) ||
78
+ check_property(download, :filename, hash) ||
79
+ check_property(download, :status, hash) ||
80
+ check_property(download, :percent_downloaded, hash)
81
+
82
+ return match if match
83
+ end
84
+ end
85
+
86
+ def self.find(id)
87
+ return Download.find_by(id: id)
88
+ end
89
+
61
90
  private
62
91
 
63
92
  def create_request(link)
@@ -81,5 +110,9 @@ module Yotsuba
81
110
  @file_handle.close
82
111
  end
83
112
 
113
+ def check_property(object, symbol, hash)
114
+ hash[symbol] && object.respond_to?(symbol) && object.send(symbol) == hash[symbol] ? object : nil
115
+ end
116
+
84
117
  end
85
118
  end
data/lib/yotsuba/file.rb CHANGED
@@ -22,7 +22,15 @@ module Yotsuba
22
22
  end
23
23
 
24
24
  def download(output_dir)
25
- Download.new(filename: self.name, part_links: self.download_links, output_dir: output_dir)
25
+ @download ||= Download.new(animefile: self, output_dir: output_dir)
26
+ end
27
+
28
+ def ==(other_file)
29
+ other_file && self.id == other_file.id
30
+ end
31
+
32
+ def eq(other_file)
33
+ self == other_file
26
34
  end
27
35
 
28
36
  end
@@ -1,3 +1,3 @@
1
1
  module Yotsuba
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yotsuba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Suchipi Izumi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-14 00:00:00.000000000 Z
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler