alexandria-book-collection-manager 0.7.1 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/dependabot.yml +9 -0
- data/.gitignore +5 -2
- data/.hound.yml +2 -0
- data/.rubocop.yml +113 -45
- data/.rubocop_todo.yml +82 -170
- data/.simplecov +5 -1
- data/.travis.yml +45 -0
- data/.yardopts +1 -1
- data/CHANGELOG.md +60 -0
- data/ChangeLog.0 +33 -35
- data/Gemfile +6 -5
- data/INSTALL.md +164 -0
- data/README.md +52 -42
- data/Rakefile +95 -109
- data/TODO.md +9 -1
- data/alexandria-book-collection-manager.gemspec +52 -45
- data/bin/alexandria +31 -53
- data/doc/AUTHORS +61 -0
- data/doc/BUGS +31 -0
- data/doc/FAQ +365 -0
- data/doc/HACKING +19 -0
- data/doc/NEWS +341 -0
- data/doc/alexandria.1 +120 -0
- data/doc/cuecat_support.rdoc +67 -0
- data/doc/dependency_decisions.yml +80 -0
- data/lib/alexandria.rb +29 -37
- data/lib/alexandria/about.rb +52 -51
- data/lib/alexandria/book_providers.rb +94 -101
- data/lib/alexandria/book_providers/adlibris.rb +45 -85
- data/lib/alexandria/book_providers/amazon_aws.rb +105 -113
- data/lib/alexandria/book_providers/amazon_ecs_util.rb +293 -324
- data/lib/alexandria/book_providers/barnes_and_noble.rb +54 -53
- data/lib/alexandria/book_providers/douban.rb +29 -51
- data/lib/alexandria/book_providers/proxis.rb +42 -59
- data/lib/alexandria/book_providers/pseudomarc.rb +79 -99
- data/lib/alexandria/book_providers/siciliano.rb +68 -70
- data/lib/alexandria/book_providers/thalia.rb +46 -45
- data/lib/alexandria/book_providers/web.rb +17 -33
- data/lib/alexandria/book_providers/worldcat.rb +74 -102
- data/lib/alexandria/book_providers/z3950.rb +170 -174
- data/lib/alexandria/config.rb +5 -3
- data/lib/alexandria/console.rb +10 -21
- data/lib/alexandria/default_preferences.rb +37 -0
- data/lib/alexandria/execution_queue.rb +17 -15
- data/lib/alexandria/export_format.rb +47 -0
- data/lib/alexandria/export_library.rb +188 -302
- data/lib/alexandria/import_library.rb +114 -155
- data/lib/alexandria/import_library_csv.rb +46 -96
- data/lib/alexandria/library_collection.rb +79 -0
- data/lib/alexandria/library_sort_order.rb +45 -0
- data/lib/alexandria/library_store.rb +233 -0
- data/lib/alexandria/logging.rb +15 -19
- data/lib/alexandria/models/book.rb +15 -20
- data/lib/alexandria/models/library.rb +81 -363
- data/lib/alexandria/net.rb +7 -6
- data/lib/alexandria/preferences.rb +73 -91
- data/lib/alexandria/scanners.rb +4 -2
- data/lib/alexandria/scanners/{cuecat.rb → cue_cat.rb} +24 -20
- data/lib/alexandria/scanners/keyboard.rb +10 -8
- data/lib/alexandria/smart_library.rb +135 -171
- data/lib/alexandria/ui.rb +17 -15
- data/lib/alexandria/ui/about_dialog.rb +49 -0
- data/lib/alexandria/ui/{dialogs/acquire_dialog.rb → acquire_dialog.rb} +129 -152
- data/lib/alexandria/ui/alert_dialog.rb +64 -0
- data/lib/alexandria/ui/bad_isbns_dialog.rb +41 -0
- data/lib/alexandria/ui/{dialogs/barcode_animation.rb → barcode_animation.rb} +18 -15
- data/lib/alexandria/ui/{dialogs/book_properties_dialog.rb → book_properties_dialog.rb} +44 -61
- data/lib/alexandria/ui/{dialogs/book_properties_dialog_base.rb → book_properties_dialog_base.rb} +84 -89
- data/lib/alexandria/ui/builder_base.rb +9 -27
- data/lib/alexandria/ui/callbacks.rb +188 -186
- data/lib/alexandria/ui/columns.rb +2 -0
- data/lib/alexandria/ui/completion_models.rb +12 -23
- data/lib/alexandria/ui/confirm_erase_dialog.rb +33 -0
- data/lib/alexandria/ui/conflict_while_copying_dialog.rb +34 -0
- data/lib/alexandria/ui/dndable.rb +10 -8
- data/lib/alexandria/ui/error_dialog.rb +25 -0
- data/lib/alexandria/ui/export_dialog.rb +139 -0
- data/lib/alexandria/ui/icons.rb +49 -65
- data/lib/alexandria/ui/iconview.rb +15 -13
- data/lib/alexandria/ui/iconview_tooltips.rb +43 -58
- data/lib/alexandria/ui/import_dialog.rb +157 -0
- data/lib/alexandria/ui/init.rb +23 -33
- data/lib/alexandria/ui/keep_bad_isbn_dialog.rb +36 -0
- data/lib/alexandria/ui/libraries_combo.rb +18 -14
- data/lib/alexandria/ui/listview.rb +77 -88
- data/lib/alexandria/ui/main_app.rb +26 -26
- data/lib/alexandria/ui/misc_dialogs.rb +10 -0
- data/lib/alexandria/ui/multi_drag_treeview.rb +30 -41
- data/lib/alexandria/ui/{dialogs/new_book_dialog.rb → new_book_dialog.rb} +168 -215
- data/lib/alexandria/ui/new_book_dialog_manual.rb +139 -0
- data/lib/alexandria/ui/new_provider_dialog.rb +100 -0
- data/lib/alexandria/ui/new_smart_library_dialog.rb +74 -0
- data/lib/alexandria/ui/preferences_dialog.rb +313 -0
- data/lib/alexandria/ui/provider_preferences_base_dialog.rb +95 -0
- data/lib/alexandria/ui/provider_preferences_dialog.rb +35 -0
- data/lib/alexandria/ui/really_delete_dialog.rb +53 -0
- data/lib/alexandria/ui/{sidepane.rb → sidepane_manager.rb} +62 -72
- data/lib/alexandria/ui/skip_entry_dialog.rb +33 -0
- data/lib/alexandria/ui/smart_library_properties_dialog.rb +60 -0
- data/lib/alexandria/ui/{dialogs/smart_library_properties_dialog_base.rb → smart_library_properties_dialog_base.rb} +96 -172
- data/lib/alexandria/ui/smart_library_rule_box.rb +119 -0
- data/lib/alexandria/ui/sound.rb +13 -13
- data/lib/alexandria/ui/ui_manager.rb +262 -283
- data/lib/alexandria/undo_manager.rb +3 -0
- data/lib/alexandria/version.rb +6 -19
- data/lib/alexandria/web_themes.rb +24 -21
- data/po/Makefile +2 -2
- data/po/cs.po +993 -880
- data/po/cy.po +957 -874
- data/po/de.po +990 -869
- data/po/el.po +989 -869
- data/po/es.po +985 -865
- data/po/fr.po +986 -870
- data/po/ga.po +907 -823
- data/po/gl.po +981 -865
- data/po/it.po +986 -868
- data/po/ja.po +969 -853
- data/po/mk.po +983 -863
- data/po/nb.po +979 -863
- data/po/nl.po +983 -864
- data/po/pl.po +1020 -969
- data/po/pt.po +988 -861
- data/po/pt_BR.po +984 -868
- data/po/ru.po +992 -873
- data/po/sk.po +987 -869
- data/po/sv.po +977 -861
- data/po/uk.po +975 -865
- data/po/zh_TW.po +976 -860
- data/schemas/alexandria.schemas +25 -3
- data/share/alexandria/glade/acquire_dialog__builder.glade +15 -12
- data/share/alexandria/glade/book_properties_dialog__builder.glade +171 -299
- data/share/alexandria/glade/main_app__builder.glade +24 -33
- data/share/alexandria/glade/new_book_dialog__builder.glade +27 -59
- data/share/alexandria/glade/preferences_dialog__builder.glade +250 -290
- data/share/gnome/help/alexandria/C/introduction.xml +0 -8
- data/share/gnome/help/alexandria/C/searching.xml +1 -1
- data/share/gnome/help/alexandria/C/smart-libraries.xml +2 -2
- data/share/gnome/help/alexandria/C/working-with-libraries.xml +1 -1
- data/share/gnome/help/alexandria/fr/alexandria.xml +1 -1
- data/share/gnome/help/alexandria/ja/introduction.xml +0 -8
- data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
- data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
- data/spec/alexandria/book_providers_spec.rb +77 -210
- data/spec/alexandria/book_spec.rb +16 -12
- data/spec/alexandria/console_spec.rb +27 -0
- data/spec/alexandria/export_library_spec.rb +130 -0
- data/spec/alexandria/library_spec.rb +130 -172
- data/spec/alexandria/library_store_spec.rb +37 -0
- data/spec/alexandria/preferences_spec.rb +46 -17
- data/spec/alexandria/scanners/cue_cat_spec.rb +52 -0
- data/spec/alexandria/smart_library_spec.rb +32 -25
- data/spec/alexandria/ui/about_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/acquire_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/alert_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/bad_isbns_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/book_properties_dialog_spec.rb +17 -0
- data/spec/alexandria/ui/confirm_erase_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/conflict_while_copying_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/error_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/export_dialog_spec.rb +15 -0
- data/spec/alexandria/ui/icons_spec.rb +26 -0
- data/spec/alexandria/ui/iconview_spec.rb +9 -21
- data/spec/alexandria/ui/import_dialog_spec.rb +41 -0
- data/spec/alexandria/ui/keep_bad_isbn_dialog_spec.rb +17 -0
- data/spec/alexandria/ui/main_app_spec.rb +8 -33
- data/spec/alexandria/ui/new_book_dialog_manual_spec.rb +15 -0
- data/spec/alexandria/ui/new_book_dialog_spec.rb +22 -0
- data/spec/alexandria/ui/new_provider_dialog_spec.rb +30 -0
- data/spec/alexandria/ui/new_smart_library_dialog_spec.rb +39 -0
- data/spec/alexandria/ui/preferences_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/provider_preferences_dialog_spec.rb +34 -0
- data/spec/alexandria/ui/really_delete_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/sidepane_manager_spec.rb +15 -0
- data/spec/alexandria/ui/skip_entry_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/smart_library_properties_dialog_spec.rb +32 -0
- data/spec/alexandria/ui/sound_spec.rb +4 -2
- data/spec/alexandria/ui/ui_manager_spec.rb +45 -20
- data/spec/end_to_end/basic_run_spec.rb +57 -0
- data/spec/spec_helper.rb +66 -33
- data/tasks/setup.rb +5 -3
- data/tasks/spec.rake +18 -3
- data/util/rake/fileinstall.rb +38 -40
- data/util/rake/gettextgenerate.rb +15 -70
- data/util/rake/omfgenerate.rb +10 -10
- metadata +176 -60
- data/INSTALL.rdoc +0 -148
- data/dogtail/basic_run_test.py +0 -9
- data/lib/alexandria/book_providers/bol_it.rb +0 -160
- data/lib/alexandria/book_providers/deastore.rb +0 -273
- data/lib/alexandria/book_providers/ibs_it.rb +0 -147
- data/lib/alexandria/book_providers/mcu.rb +0 -169
- data/lib/alexandria/book_providers/renaud.rb +0 -140
- data/lib/alexandria/book_providers/webster_it.rb +0 -167
- data/lib/alexandria/ui/dialogs/about_dialog.rb +0 -59
- data/lib/alexandria/ui/dialogs/alert_dialog.rb +0 -70
- data/lib/alexandria/ui/dialogs/bad_isbns_dialog.rb +0 -43
- data/lib/alexandria/ui/dialogs/export_dialog.rb +0 -171
- data/lib/alexandria/ui/dialogs/import_dialog.rb +0 -196
- data/lib/alexandria/ui/dialogs/misc_dialogs.rb +0 -85
- data/lib/alexandria/ui/dialogs/new_book_dialog_manual.rb +0 -154
- data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +0 -74
- data/lib/alexandria/ui/dialogs/preferences_dialog.rb +0 -578
- data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +0 -57
- data/spec/alexandria/scanners/cuecat_spec.rb +0 -65
- data/spec/alexandria/ui/dialogs_spec.rb +0 -94
- data/spec/alexandria/ui/sidepane_spec.rb +0 -27
- data/spec/alexandria/ui/ui_utilities_spec.rb +0 -60
- data/spec/alexandria/utilities_spec.rb +0 -50
- data/tasks/dogtail.rake +0 -4
@@ -1,38 +1,28 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# Alexandria is free software; you can redistribute it and/or
|
6
|
-
# modify it under the terms of the GNU General Public License as
|
7
|
-
# published by the Free Software Foundation; either version 2 of the
|
8
|
-
# License, or (at your option) any later version.
|
9
|
-
#
|
10
|
-
# Alexandria is distributed in the hope that it will be useful,
|
11
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
-
# General Public License for more details.
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This file is part of Alexandria.
|
14
4
|
#
|
15
|
-
#
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
require 'gettext'
|
5
|
+
# See the file README.md for authorship and licensing information.
|
6
|
+
|
7
|
+
require "gettext"
|
8
|
+
require "tmpdir"
|
20
9
|
|
21
10
|
module Alexandria
|
22
11
|
class ImportFilter
|
23
12
|
attr_reader :name, :patterns, :message
|
24
13
|
|
14
|
+
include Logging
|
25
15
|
include GetText
|
26
16
|
extend GetText
|
27
|
-
bindtextdomain(Alexandria::TEXTDOMAIN, charset:
|
17
|
+
bindtextdomain(Alexandria::TEXTDOMAIN, charset: "UTF-8")
|
28
18
|
|
29
19
|
def self.all
|
30
20
|
[
|
31
|
-
new(_(
|
32
|
-
new(_(
|
33
|
-
[
|
34
|
-
new(_(
|
35
|
-
new(_(
|
21
|
+
new(_("Autodetect"), ["*"], :import_autodetect),
|
22
|
+
new(_("Archived Tellico XML (*.bc, *.tc)"),
|
23
|
+
["*.tc", "*.bc"], :import_as_tellico_xml_archive),
|
24
|
+
new(_("ISBN List (*.txt)"), ["*.txt"], :import_as_isbn_list),
|
25
|
+
new(_("GoodReads CSV"), ["*.csv"], :import_as_csv_file)
|
36
26
|
]
|
37
27
|
end
|
38
28
|
|
@@ -45,7 +35,7 @@ module Alexandria
|
|
45
35
|
end
|
46
36
|
|
47
37
|
def invoke(library_name, filename)
|
48
|
-
|
38
|
+
log.debug { "Selected: #{@message} -- #{library_name} -- #{filename}" }
|
49
39
|
Library.send(@message, library_name, filename,
|
50
40
|
@on_iterate_cb, @on_error_cb)
|
51
41
|
end
|
@@ -61,121 +51,100 @@ module Alexandria
|
|
61
51
|
|
62
52
|
class Library
|
63
53
|
def self.import_autodetect(*args)
|
64
|
-
|
54
|
+
log.debug { args.inspect }
|
65
55
|
filename = args[1]
|
66
|
-
|
67
|
-
|
68
|
-
if filename[-4..-1] ==
|
56
|
+
log.debug { "Filename is #{filename} and ext is #{filename[-4..-1]}" }
|
57
|
+
log.debug { "Beginning import: #{args[0]}, #{args[1]}" }
|
58
|
+
if filename[-4..-1] == ".txt"
|
69
59
|
import_as_isbn_list(*args)
|
70
|
-
elsif [
|
71
|
-
|
72
|
-
|
73
|
-
rescue => e
|
74
|
-
puts e.message
|
75
|
-
puts e.backtrace.join("\n>> ")
|
76
|
-
end
|
77
|
-
elsif ['.csv'].include? filename[-4..-1]
|
60
|
+
elsif [".tc", ".bc"].include? filename[-3..-1]
|
61
|
+
import_as_tellico_xml_archive(*args)
|
62
|
+
elsif [".csv"].include? filename[-4..-1]
|
78
63
|
import_as_csv_file(*args)
|
79
64
|
else
|
80
|
-
|
81
|
-
raise 'Not supported type'
|
65
|
+
raise _("Unsupported type")
|
82
66
|
end
|
83
67
|
end
|
84
68
|
|
85
69
|
def self.import_as_tellico_xml_archive(name, filename,
|
86
70
|
on_iterate_cb, _on_error_cb)
|
87
|
-
|
71
|
+
log.debug { "Starting import_as_tellico_xml_archive... " }
|
88
72
|
return nil unless system("unzip -qqt \"#{filename}\"")
|
89
|
-
|
73
|
+
|
74
|
+
tmpdir = File.join(Dir.tmpdir, "tellico_export")
|
90
75
|
FileUtils.rm_rf(tmpdir) if File.exist?(tmpdir)
|
91
76
|
Dir.mkdir(tmpdir)
|
92
77
|
Dir.chdir(tmpdir) do
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
raise unless xml.root.elements.size == 1
|
100
|
-
collection = xml.root.elements[1]
|
101
|
-
raise unless collection.name == 'collection'
|
102
|
-
type = collection.attribute('type').value.to_i
|
103
|
-
raise unless (type == 2) || (type == 5)
|
104
|
-
|
105
|
-
content = []
|
106
|
-
entries = collection.elements.to_a('entry')
|
107
|
-
(total = entries.size).times do |n|
|
108
|
-
entry = entries[n]
|
109
|
-
elements = entry.elements
|
110
|
-
# Feed an array in here, tomorrow.
|
111
|
-
keys = ['isbn', 'publisher', 'pub_year', 'binding']
|
112
|
-
|
113
|
-
book_elements = [neaten(elements['title'].text)]
|
114
|
-
book_elements += if !elements['authors'].nil?
|
115
|
-
[elements['authors'].elements.to_a.map \
|
116
|
-
{ |x| neaten(x.text) }]
|
117
|
-
else
|
118
|
-
[[]]
|
119
|
-
end
|
120
|
-
book_elements += keys.map { |key|
|
121
|
-
neaten(elements[key].text) if elements[key]
|
122
|
-
}
|
123
|
-
# isbn
|
124
|
-
if book_elements[2].nil? || book_elements[2].strip.empty?
|
125
|
-
book_elements[2] = nil
|
126
|
-
else
|
127
|
-
begin
|
128
|
-
book_elements[2] = book_elements[2].strip
|
129
|
-
book_elements[2] = Library.canonicalise_ean(book_elements[2])
|
130
|
-
rescue => ex
|
131
|
-
puts book_elements[2]
|
132
|
-
puts ex.message
|
133
|
-
puts ex.backtrace.join("\n> ")
|
134
|
-
raise ex
|
135
|
-
end
|
136
|
-
end
|
137
|
-
book_elements[4] = book_elements[4].to_i unless book_elements[4].nil? # publishing_year
|
138
|
-
puts book_elements.inspect
|
139
|
-
cover = if elements['cover']
|
140
|
-
neaten(elements['cover'].text)
|
141
|
-
end
|
142
|
-
puts cover
|
143
|
-
book = Book.new(*book_elements)
|
144
|
-
if elements['rating'] && Book::VALID_RATINGS.member?(elements['rating'].text.to_i)
|
145
|
-
book.rating = elements['rating'].text.to_i
|
146
|
-
end
|
147
|
-
book.notes = neaten(elements['comments'].text) if elements['comments']
|
148
|
-
content << [book, cover]
|
149
|
-
on_iterate_cb.call(n + 1, total) if on_iterate_cb
|
150
|
-
end
|
78
|
+
system("unzip -qq \"#{filename}\"")
|
79
|
+
file = File.exist?("bookcase.xml") ? "bookcase.xml" : "tellico.xml"
|
80
|
+
xml = REXML::Document.new(File.open(file))
|
81
|
+
raise unless ["bookcase", "tellico"].include? xml.root.name
|
82
|
+
# FIXME: handle multiple collections
|
83
|
+
raise unless xml.root.elements.size == 1
|
151
84
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
85
|
+
collection = xml.root.elements[1]
|
86
|
+
raise unless collection.name == "collection"
|
87
|
+
|
88
|
+
type = collection.attribute("type").value.to_i
|
89
|
+
raise unless (type == 2) || (type == 5)
|
90
|
+
|
91
|
+
content = []
|
92
|
+
entries = collection.elements.to_a("entry")
|
93
|
+
(total = entries.size).times do |n|
|
94
|
+
entry = entries[n]
|
95
|
+
elements = entry.elements
|
96
|
+
# Feed an array in here, tomorrow.
|
97
|
+
keys = ["isbn", "publisher", "pub_year", "binding"]
|
98
|
+
|
99
|
+
book_elements = [neaten(elements["title"].text)]
|
100
|
+
book_elements += if !elements["authors"].nil?
|
101
|
+
[elements["authors"].elements.to_a.map \
|
102
|
+
{ |x| neaten(x.text) }]
|
103
|
+
else
|
104
|
+
[[]]
|
105
|
+
end
|
106
|
+
book_elements += keys.map do |key|
|
107
|
+
neaten(elements[key].text) if elements[key]
|
161
108
|
end
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
109
|
+
book_elements[2] = Library.canonicalise_ean(book_elements[2])
|
110
|
+
# publishing_year
|
111
|
+
book_elements[4] = book_elements[4].to_i unless book_elements[4].nil?
|
112
|
+
log.debug { book_elements.inspect }
|
113
|
+
cover = (neaten(elements["cover"].text) if elements["cover"])
|
114
|
+
log.debug { cover }
|
115
|
+
book = Book.new(*book_elements)
|
116
|
+
if elements["rating"]
|
117
|
+
rating = elements["rating"].text.to_i
|
118
|
+
book.rating = rating if Book::VALID_RATINGS.member? rating
|
119
|
+
end
|
120
|
+
book.notes = neaten(elements["comments"].text) if elements["comments"]
|
121
|
+
content << [book, cover]
|
122
|
+
on_iterate_cb&.call(n + 1, total)
|
166
123
|
end
|
124
|
+
|
125
|
+
# TODO: Pass in library store as an argument
|
126
|
+
library = LibraryCollection.instance.library_store.load_library name
|
127
|
+
content.each do |book, cover|
|
128
|
+
library.save_cover(book, File.join(Dir.pwd, "images", cover)) unless cover.nil?
|
129
|
+
library << book
|
130
|
+
library.save(book)
|
131
|
+
end
|
132
|
+
return [library, []]
|
133
|
+
rescue StandardError => ex
|
134
|
+
log.info { ex.message }
|
135
|
+
return nil
|
167
136
|
end
|
168
137
|
end
|
169
138
|
|
170
139
|
def self.import_as_csv_file(name, filename, on_iterate_cb, _on_error_cb)
|
171
|
-
require
|
140
|
+
require "alexandria/import_library_csv"
|
172
141
|
books_and_covers = []
|
173
142
|
line_count = IO.readlines(filename).reduce(0) { |count, _line| count + 1 }
|
174
143
|
|
175
144
|
import_count = 0
|
176
145
|
max_import = line_count - 1
|
177
146
|
|
178
|
-
reader = CSV.open(filename,
|
147
|
+
reader = CSV.open(filename, "r")
|
179
148
|
# Goodreads & LibraryThing now use csv header lines
|
180
149
|
header = reader.shift
|
181
150
|
importer = identify_csv_type(header)
|
@@ -187,28 +156,23 @@ module Alexandria
|
|
187
156
|
if book.isbn
|
188
157
|
# if we can search by ISBN, try to grab the cover
|
189
158
|
begin
|
190
|
-
dl_book, dl_cover =
|
159
|
+
dl_book, dl_cover = BookProviders.isbn_search(book.isbn)
|
191
160
|
if dl_book.authors.size > book.authors.size
|
192
161
|
# LibraryThing only supports a single author, so
|
193
162
|
# attempt to include more author information if it's
|
194
163
|
# available
|
195
164
|
book.authors = dl_book.authors
|
196
165
|
end
|
197
|
-
unless book.edition
|
198
|
-
book.edition = dl_book.edition
|
199
|
-
end
|
166
|
+
book.edition = dl_book.edition unless book.edition
|
200
167
|
cover = dl_cover
|
201
|
-
rescue
|
202
|
-
|
203
|
-
# note failure
|
168
|
+
rescue StandardError
|
169
|
+
log.debug { "Failed to get cover for #{book.title} #{book.isbn}" }
|
204
170
|
end
|
205
171
|
end
|
206
172
|
|
207
173
|
books_and_covers << [book, cover]
|
208
174
|
import_count += 1
|
209
|
-
|
210
|
-
on_iterate_cb.call(import_count, max_import)
|
211
|
-
end
|
175
|
+
on_iterate_cb&.call(import_count, max_import)
|
212
176
|
end
|
213
177
|
rescue CSV::IllegalFormatError
|
214
178
|
unless failed_once
|
@@ -217,12 +181,12 @@ module Alexandria
|
|
217
181
|
# probably Goodreads' wonky ISBN fields ,,="043432432X",
|
218
182
|
# this is a hack to fix up such files
|
219
183
|
data = File.read(filename)
|
220
|
-
data.gsub!(
|
221
|
-
csv_fixed = Tempfile.new(
|
184
|
+
data.gsub!(/,="/, ',"')
|
185
|
+
csv_fixed = Tempfile.new("alexandria_import_csv_fixed_")
|
222
186
|
csv_fixed.write(data)
|
223
187
|
csv_fixed.close
|
224
188
|
|
225
|
-
reader = CSV.open(csv_fixed.path,
|
189
|
+
reader = CSV.open(csv_fixed.path, "r")
|
226
190
|
header = reader.shift
|
227
191
|
importer = identify_csv_type(header)
|
228
192
|
|
@@ -230,12 +194,13 @@ module Alexandria
|
|
230
194
|
end
|
231
195
|
end
|
232
196
|
|
233
|
-
library
|
197
|
+
# TODO: Pass in library store as an argument
|
198
|
+
library = LibraryCollection.instance.library_store.load_library name
|
234
199
|
|
235
200
|
books_and_covers.each do |book, cover_uri|
|
236
|
-
|
201
|
+
log.debug { "Saving #{book.isbn} cover" }
|
237
202
|
library.save_cover(book, cover_uri) unless cover_uri.nil?
|
238
|
-
|
203
|
+
log.debug { "Saving #{book.isbn}" }
|
239
204
|
library << book
|
240
205
|
library.save(book)
|
241
206
|
end
|
@@ -244,20 +209,15 @@ module Alexandria
|
|
244
209
|
|
245
210
|
def self.import_as_isbn_list(name, filename, on_iterate_cb,
|
246
211
|
on_error_cb)
|
247
|
-
|
212
|
+
log.debug { "Starting import_as_isbn_list... " }
|
248
213
|
isbn_list = IO.readlines(filename).map do |line|
|
249
|
-
|
250
|
-
|
251
|
-
begin
|
252
|
-
[line.chomp, canonicalise_isbn(line.chomp)] unless line == "\n"
|
253
|
-
rescue => e
|
254
|
-
puts e.message
|
255
|
-
[line.chomp, nil]
|
256
|
-
end
|
214
|
+
log.debug { "Trying line #{line}" }
|
215
|
+
[line.chomp, canonicalise_isbn(line.chomp)] unless line == "\n"
|
257
216
|
end
|
258
|
-
|
217
|
+
log.debug { "Isbn list: #{isbn_list.inspect}" }
|
259
218
|
isbn_list.compact!
|
260
219
|
return nil if isbn_list.empty?
|
220
|
+
|
261
221
|
max_iterations = isbn_list.length * 2
|
262
222
|
current_iteration = 1
|
263
223
|
books = []
|
@@ -266,34 +226,33 @@ module Alexandria
|
|
266
226
|
isbn_list.each do |isbn|
|
267
227
|
begin
|
268
228
|
if isbn[1]
|
269
|
-
books <<
|
229
|
+
books << BookProviders.isbn_search(isbn[1])
|
270
230
|
else
|
271
231
|
bad_isbns << isbn[0]
|
272
232
|
end
|
273
|
-
rescue =>
|
274
|
-
|
233
|
+
rescue BookProviders::SearchEmptyError => ex
|
234
|
+
log.debug { ex.message }
|
275
235
|
failed_lookup_isbns << isbn[1]
|
276
|
-
|
236
|
+
log.debug { "NOTE : ignoring on_error_cb #{on_error_cb}" }
|
277
237
|
# return nil unless
|
278
238
|
# (on_error_cb and on_error_cb.call(e.message))
|
279
239
|
end
|
280
240
|
|
281
|
-
|
282
|
-
on_iterate_cb.call(current_iteration += 1, max_iterations)
|
283
|
-
end
|
241
|
+
on_iterate_cb&.call(current_iteration += 1, max_iterations)
|
284
242
|
end
|
285
|
-
|
286
|
-
|
287
|
-
|
243
|
+
log.debug { "Bad Isbn list: #{bad_isbns.inspect}" } if bad_isbns
|
244
|
+
|
245
|
+
# TODO: Pass in library store as an argument
|
246
|
+
library = LibraryCollection.instance.library_store.load_library name
|
247
|
+
|
248
|
+
log.debug { "Going with these #{books.length} books: #{books.inspect}" }
|
288
249
|
books.each do |book, cover_uri|
|
289
|
-
|
250
|
+
log.debug { "Saving #{book.isbn} cover..." }
|
290
251
|
library.save_cover(book, cover_uri) unless cover_uri.nil?
|
291
|
-
|
252
|
+
log.debug { "Saving #{book.isbn}..." }
|
292
253
|
library << book
|
293
254
|
library.save(book)
|
294
|
-
|
295
|
-
on_iterate_cb.call(current_iteration += 1, max_iterations)
|
296
|
-
end
|
255
|
+
on_iterate_cb&.call(current_iteration += 1, max_iterations)
|
297
256
|
end
|
298
257
|
[library, bad_isbns, failed_lookup_isbns]
|
299
258
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright (C) 2010 Cathal Mc Ginley
|
2
4
|
#
|
3
5
|
# Alexandria is free software; you can redistribute it and/or
|
@@ -18,10 +20,10 @@
|
|
18
20
|
# New CSV import code taken from Palatina and modified for
|
19
21
|
# Alexandria. (29 Apr 2010)
|
20
22
|
|
21
|
-
require
|
22
|
-
require
|
23
|
-
require
|
24
|
-
require
|
23
|
+
require "csv"
|
24
|
+
require "date"
|
25
|
+
require "htmlentities"
|
26
|
+
require "tempfile"
|
25
27
|
|
26
28
|
module Alexandria
|
27
29
|
class CSVImport
|
@@ -45,21 +47,21 @@ module Alexandria
|
|
45
47
|
class GoodreadsCSVImport < CSVImport
|
46
48
|
def initialize(header)
|
47
49
|
super(header)
|
48
|
-
@title = index_of(
|
49
|
-
@author = index_of(
|
50
|
-
@additional_authors = index_of(
|
51
|
-
@isbn = index_of(
|
52
|
-
@publisher = index_of(
|
53
|
-
@publishing_year = index_of(
|
54
|
-
@edition = index_of(
|
50
|
+
@title = index_of("Title")
|
51
|
+
@author = index_of("Author")
|
52
|
+
@additional_authors = index_of("Additional Authors")
|
53
|
+
@isbn = index_of("ISBN")
|
54
|
+
@publisher = index_of("Publisher")
|
55
|
+
@publishing_year = index_of("Year Published")
|
56
|
+
@edition = index_of("Binding")
|
55
57
|
|
56
58
|
# optional extras
|
57
|
-
@notes = index_of(
|
58
|
-
@rating = index_of(
|
59
|
-
@read_count = index_of(
|
60
|
-
@date_read = index_of(
|
61
|
-
@bookshelves = index_of(
|
62
|
-
@mainshelf = index_of(
|
59
|
+
@notes = index_of("Private Notes")
|
60
|
+
@rating = index_of("My Rating")
|
61
|
+
@read_count = index_of("Read Count")
|
62
|
+
@date_read = index_of("Date Read")
|
63
|
+
@bookshelves = index_of("Bookshelves") # save names as tags
|
64
|
+
@mainshelf = index_of("Exclusive Shelf")
|
63
65
|
end
|
64
66
|
|
65
67
|
def row_to_book(row)
|
@@ -67,11 +69,10 @@ module Alexandria
|
|
67
69
|
authors = []
|
68
70
|
authors << normalize(row[@author])
|
69
71
|
additional = row[@additional_authors]
|
70
|
-
additional.split(
|
72
|
+
additional.split(",").each do |add|
|
71
73
|
authors << normalize(add)
|
72
74
|
end
|
73
|
-
isbn = row[@isbn]
|
74
|
-
isbn = Library.canonicalise_ean(isbn) if isbn
|
75
|
+
isbn = Library.canonicalise_ean(row[@isbn])
|
75
76
|
publisher = normalize(row[@publisher])
|
76
77
|
year = row[@publishing_year].to_i
|
77
78
|
edition = normalize(row[@edition])
|
@@ -88,33 +89,30 @@ module Alexandria
|
|
88
89
|
book.redd = true if count > 0
|
89
90
|
end
|
90
91
|
if row[@date_read]
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
book.redd = true
|
95
|
-
rescue
|
96
|
-
#
|
97
|
-
end
|
92
|
+
date = Date.strptime(str, "%d/%m/%y") # e.g. "14/01/10" => 2010-01-14
|
93
|
+
book.redd_when = date
|
94
|
+
book.redd = true
|
98
95
|
end
|
99
96
|
if row[@mainshelf]
|
100
|
-
|
97
|
+
case row[@mainshelf]
|
98
|
+
when "read"
|
101
99
|
book.redd = true
|
102
|
-
|
100
|
+
when "to-read"
|
103
101
|
book.redd = false
|
104
|
-
book.tags = [
|
105
|
-
|
102
|
+
book.tags = ["to read"]
|
103
|
+
when "currently-reading"
|
106
104
|
book.redd = false
|
107
|
-
book.tags = [
|
105
|
+
book.tags = ["currently reading"]
|
108
106
|
end
|
109
107
|
end
|
110
108
|
if row[@bookshelves]
|
111
109
|
shelves = normalize(row[@bookshelves]).split
|
112
110
|
shelves.each do |shelf|
|
113
|
-
tag = shelf.tr(
|
111
|
+
tag = shelf.tr("-", " ")
|
114
112
|
book.tags << tag unless book.tags.include? tag
|
115
113
|
end
|
116
114
|
end
|
117
|
-
|
115
|
+
log.debug { "Goodreads loading #{book.title}" }
|
118
116
|
book
|
119
117
|
end
|
120
118
|
end
|
@@ -140,9 +138,7 @@ module Alexandria
|
|
140
138
|
authors << normalize(row[@author])
|
141
139
|
isbn = row[@isbn]
|
142
140
|
if isbn
|
143
|
-
if isbn =~ /\[([^\]]+)\]/
|
144
|
-
isbn = Regexp.last_match[1]
|
145
|
-
end
|
141
|
+
isbn = Regexp.last_match[1] if isbn =~ /\[([^\]]+)\]/
|
146
142
|
isbn = Library.canonicalise_ean(isbn)
|
147
143
|
end
|
148
144
|
|
@@ -150,14 +146,10 @@ module Alexandria
|
|
150
146
|
# sometimes "Publisher (YEAR), Edition: NUM, Binding, NUM pages"
|
151
147
|
publisher_info = normalize(row[@publisher_info])
|
152
148
|
publisher = publisher_info
|
153
|
-
if publisher_info =~ /([
|
154
|
-
publisher = Regexp.last_match[1]
|
155
|
-
end
|
149
|
+
publisher = Regexp.last_match[1] if publisher_info =~ /([^(]+)\(/
|
156
150
|
edition = publisher_info # binding
|
157
|
-
edition_info = publisher_info.split(
|
158
|
-
if edition_info.size >= 3
|
159
|
-
edition = publisher_info.split(',')[-2]
|
160
|
-
end
|
151
|
+
edition_info = publisher_info.split(",")
|
152
|
+
edition = publisher_info.split(",")[-2] if edition_info.size >= 3
|
161
153
|
|
162
154
|
year = row[@publishing_year].to_i
|
163
155
|
|
@@ -171,13 +163,13 @@ module Alexandria
|
|
171
163
|
|
172
164
|
book.rating = row[@rating].to_i if row[@rating]
|
173
165
|
if row[@tags]
|
174
|
-
tags = normalize(row[@tags]).split(
|
166
|
+
tags = normalize(row[@tags]).split(",")
|
175
167
|
tags.each do |tag|
|
176
168
|
book.tags << tag unless book.tags.include? tag
|
177
169
|
end
|
178
170
|
end
|
179
171
|
|
180
|
-
|
172
|
+
log.debug { "LibraryThing loading #{book.title}" }
|
181
173
|
book
|
182
174
|
end
|
183
175
|
end
|
@@ -191,62 +183,20 @@ module Alexandria
|
|
191
183
|
is_librarything = false
|
192
184
|
is_goodreads = false
|
193
185
|
header.each do |head|
|
194
|
-
|
186
|
+
case head
|
187
|
+
when "'PUBLICATION INFO'"
|
195
188
|
is_librarything = true
|
196
189
|
break
|
197
|
-
|
190
|
+
when "Year Published"
|
198
191
|
is_goodreads = true
|
199
192
|
break
|
200
193
|
end
|
201
194
|
end
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
raise 'Not Recognized' unless is_librarything || is_goodreads
|
195
|
+
|
196
|
+
return LibraryThingCSVImport.new(header) if is_librarything
|
197
|
+
return GoodreadsCSVImport.new(header) if is_goodreads
|
198
|
+
|
199
|
+
raise _("Not Recognized") unless is_librarything || is_goodreads
|
208
200
|
end
|
209
201
|
end
|
210
|
-
|
211
|
-
# def read_csv_file(csv_file)
|
212
|
-
|
213
|
-
# reader = CSV.open(csv_file, 'r')
|
214
|
-
# # goodreads & librarything now use csv header lines
|
215
|
-
# header = reader.shift
|
216
|
-
|
217
|
-
# #header.each {|h| puts h }
|
218
|
-
# importer = identify_csv_type(header)
|
219
|
-
|
220
|
-
# retries = 0
|
221
|
-
# begin
|
222
|
-
# reader.each do |row|
|
223
|
-
# #puts row
|
224
|
-
# bk = importer.row_to_book(row)
|
225
|
-
# puts "\"#{bk.title}\" by #{bk.authors.join(', ')}; isbn : #{bk.isbn} #{bk.publisher} | #{bk.publishing_year.inspect} binding:#{bk.edition}"
|
226
|
-
# if bk.notes.size > 0
|
227
|
-
# puts " >>> #{bk.notes}"
|
228
|
-
# end
|
229
|
-
# end
|
230
|
-
# rescue
|
231
|
-
# if retries < 1
|
232
|
-
# retries += 1
|
233
|
-
# puts "Retrying..."
|
234
|
-
|
235
|
-
# # probably Goodreads' wonky ISBN fields ,,="043432432X", and such
|
236
|
-
# # hack to fix up these files
|
237
|
-
# data = File.read(csv_file)
|
238
|
-
# data.gsub!(/\,\=\"/, ',"')
|
239
|
-
# csv_fixed = Tempfile.new("#{csv_file}_fixed")
|
240
|
-
# csv_fixed.write(data)
|
241
|
-
# csv_fixed.close
|
242
|
-
# reader = CSV.open(csv_fixed.path, 'r')
|
243
|
-
# #puts csv_fixed.path
|
244
|
-
# header = reader.shift
|
245
|
-
|
246
|
-
# retry
|
247
|
-
# end
|
248
|
-
|
249
|
-
# end
|
250
|
-
|
251
|
-
# end
|
252
202
|
end
|