kindle_manager 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/kindle_manager/client.rb +23 -20
- data/lib/kindle_manager/file_store.rb +6 -5
- data/lib/kindle_manager/version.rb +1 -1
- data/lib/kindle_manager.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d0d0cbbeb7edce8852f3de2f76c624ce3471108
|
4
|
+
data.tar.gz: ce85a47e95df6d2cabcac0168c61350dd550b7b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82a79e14d3eaa9c53a8efe63b6374295bd08341a358575173cdd1efefa2497838d1004db517427b0a571fb2777b32264b0b3bcbf7c1e0f3bbc5e7fd2c5736858
|
7
|
+
data.tar.gz: 14720099a4849797df39e4e9a22a8063f01604475cc8f994a7581fe7d5bea9212fbe6fef88fab1677dde59eeb3362827e1c27cee2bc7b4fa911330f3b8732f0c
|
data/README.md
CHANGED
@@ -26,6 +26,12 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
Create _.env_ following the instructions of https://github.com/kyamaguchi/amazon_auth
|
28
28
|
|
29
|
+
```
|
30
|
+
amazon_auth
|
31
|
+
|
32
|
+
vi .env
|
33
|
+
```
|
34
|
+
|
29
35
|
And `Dotenv.load` or `gem 'dotenv-rails'` may be required when you use this in your app.
|
30
36
|
|
31
37
|
### Run
|
@@ -37,7 +43,7 @@ bin/console
|
|
37
43
|
In console
|
38
44
|
|
39
45
|
```
|
40
|
-
client = KindleManager::Client.new
|
46
|
+
client = KindleManager::Client.new(debug: true)
|
41
47
|
client.fetch_kindle_list
|
42
48
|
books = client.load_kindle_books
|
43
49
|
|
@@ -5,6 +5,7 @@ module KindleManager
|
|
5
5
|
def initialize(options = {})
|
6
6
|
@debug = options.fetch(:debug, false)
|
7
7
|
@limit = options.fetch(:limit, nil)
|
8
|
+
@options = options
|
8
9
|
begin
|
9
10
|
@client = AmazonAuth::Client.new
|
10
11
|
rescue => e
|
@@ -19,22 +20,17 @@ module KindleManager
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def store
|
22
|
-
|
23
|
-
@store ||= KindleManager::FileStore.new(nil, latest: true)
|
24
|
-
end
|
25
|
-
|
26
|
-
def setup_file_store_with_session
|
27
|
-
@store = KindleManager::FileStore.new(session)
|
23
|
+
@store ||= KindleManager::FileStore.new(@options)
|
28
24
|
end
|
29
25
|
|
30
26
|
def fetch_kindle_list
|
31
27
|
sign_in
|
32
|
-
|
28
|
+
store.session = session
|
33
29
|
go_to_kindle_management_page
|
34
30
|
begin
|
35
31
|
load_next_kindle_list
|
36
32
|
rescue => e
|
37
|
-
byebug
|
33
|
+
byebug if defined?(Byebug)
|
38
34
|
# retry ?
|
39
35
|
puts e
|
40
36
|
end
|
@@ -54,8 +50,16 @@ module KindleManager
|
|
54
50
|
end
|
55
51
|
|
56
52
|
def go_to_kindle_management_page
|
53
|
+
puts "Visiting kindle management page" if @debug
|
57
54
|
wait_for_selector('#shopAllLinks', 5)
|
58
|
-
|
55
|
+
3.times do
|
56
|
+
session.all('a').find{|e| e['href'] =~ %r{/gp/digital/fiona/manage/} }.click
|
57
|
+
wait_for_selector('.navHeader_myx', 5)
|
58
|
+
if session.first('.navHeader_myx')
|
59
|
+
puts "Page found '#{session.first('.navHeader_myx').text}'" if @debug
|
60
|
+
break
|
61
|
+
end
|
62
|
+
end
|
59
63
|
end
|
60
64
|
|
61
65
|
def load_next_kindle_list
|
@@ -65,21 +69,22 @@ module KindleManager
|
|
65
69
|
if @limit && @limit < number_of_fetched_books
|
66
70
|
break
|
67
71
|
elsif has_more_button?
|
68
|
-
|
72
|
+
snapshot_page
|
69
73
|
@current_loop = 0
|
70
74
|
|
71
|
-
puts "Clicking 'Show More'"
|
75
|
+
puts "Clicking 'Show More'" if @debug
|
72
76
|
session.execute_script "window.scrollBy(0,-800)"
|
73
77
|
show_more_button.click
|
74
78
|
sleep 1
|
75
79
|
raise('Clicking of more button may have failed') if has_more_button?
|
76
80
|
else
|
77
|
-
puts "
|
81
|
+
puts "Loading books with scrolling #{@current_loop+1}" if @debug
|
78
82
|
session.execute_script "window.scrollBy(0,10000)"
|
79
83
|
end
|
80
84
|
sleep 5
|
81
85
|
@current_loop += 1
|
82
86
|
end
|
87
|
+
puts "Stopped loading" if @debug
|
83
88
|
snapshot_page
|
84
89
|
end
|
85
90
|
|
@@ -100,8 +105,11 @@ module KindleManager
|
|
100
105
|
end
|
101
106
|
|
102
107
|
def number_of_fetched_books
|
103
|
-
|
104
|
-
|
108
|
+
re = (AmazonInfo.domain =~ /\.jp\z/ ? /(\d+)〜(\d+)/ : /(\d+) - (\d+)/)
|
109
|
+
text = session.first('.contentCount_myx').text
|
110
|
+
m = text.match(re)
|
111
|
+
return m[2].to_i if m.present?
|
112
|
+
raise("Couldn't get the number of fetched books [#{text}]")
|
105
113
|
end
|
106
114
|
|
107
115
|
def loading?
|
@@ -110,13 +118,8 @@ module KindleManager
|
|
110
118
|
|
111
119
|
def snapshot_page
|
112
120
|
store.record_page
|
113
|
-
debug_print_page
|
114
|
-
end
|
115
|
-
|
116
|
-
def debug_print_page
|
117
121
|
if @debug
|
118
|
-
puts Time.current.strftime("%Y-%m-%d %H:%M:%S")
|
119
|
-
puts "Loop: #{@current_loop}"
|
122
|
+
puts "Saving page" + Time.current.strftime("%Y-%m-%d %H:%M:%S")
|
120
123
|
puts session.first('.contentCount_myx').text if session.first('.contentCount_myx')
|
121
124
|
puts
|
122
125
|
end
|
@@ -1,12 +1,13 @@
|
|
1
1
|
module KindleManager
|
2
2
|
class FileStore
|
3
|
-
attr_accessor :dir_name
|
3
|
+
attr_accessor :dir_name, :session
|
4
4
|
|
5
|
-
def initialize(
|
5
|
+
def initialize(options = {})
|
6
6
|
@dir_name = options.fetch(:dir_name) do
|
7
|
-
options[:
|
7
|
+
tmp_dir_name = options[:create] ? nil : find_latest_dir_name
|
8
|
+
tmp_dir_name.presence || Time.current.strftime("%Y%m%d%H%M%S")
|
8
9
|
end
|
9
|
-
@session = session
|
10
|
+
@session = options.fetch(:session, nil)
|
10
11
|
end
|
11
12
|
|
12
13
|
def base_dir
|
@@ -48,7 +49,7 @@ module KindleManager
|
|
48
49
|
end
|
49
50
|
|
50
51
|
def find_latest_dir_name
|
51
|
-
self.class.list_download_dirs.sort.last.split('/').last
|
52
|
+
self.class.list_download_dirs.sort.last.to_s.split('/').last
|
52
53
|
end
|
53
54
|
|
54
55
|
private
|
data/lib/kindle_manager.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kindle_manager
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kazuho Yamaguchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: amazon_auth
|