Class: Alexandria::LibraryThingCSVImport

Inherits:
CSVImport
  • Object
show all
Defined in:
lib/alexandria/import_library_csv.rb

Instance Method Summary collapse

Methods inherited from CSVImport

#index_of, #normalize

Constructor Details

#initialize(header) ⇒ LibraryThingCSVImport

Returns a new instance of LibraryThingCSVImport.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/alexandria/import_library_csv.rb', line 121

def initialize(header)
  super(header)
  @title = index_of("'TITLE'")
  @author = index_of("'AUTHOR (first, last)'")
  @isbn = index_of("'ISBN'")
  @publisher_info = index_of("'PUBLICATION INFO'")
  @publishing_year = index_of("'DATE'")

  # optional extras
  @notes = index_of("'COMMENTS'")
  @rating = index_of("'RATING'")
  @tags = index_of("'TAGS'")
end

Instance Method Details

#row_to_book(row) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/alexandria/import_library_csv.rb', line 135

def row_to_book(row)
  title = normalize(row[@title])
  authors = []
  authors << normalize(row[@author])
  isbn = row[@isbn]
  if isbn
    isbn = Regexp.last_match[1] if isbn =~ /\[([^\]]+)\]/
    isbn = Library.canonicalise_ean(isbn)
  end

  # usually "Publisher (YEAR), Binding, NUM pages"
  # sometimes "Publisher (YEAR), Edition: NUM, Binding, NUM pages"
  publisher_info = normalize(row[@publisher_info])
  publisher = publisher_info
  publisher = Regexp.last_match[1] if publisher_info =~ /([^(]+)\(/
  edition = publisher_info # binding
  edition_info = publisher_info.split(",")
  edition = publisher_info.split(",")[-2] if edition_info.size >= 3

  year = row[@publishing_year].to_i

  book = Alexandria::Book.new(title,
                              authors,
                              isbn,
                              publisher,
                              year,
                              edition)
  book.notes = normalize(row[@notes]) if row[@notes]

  book.rating = row[@rating].to_i if row[@rating]
  if row[@tags]
    tags = normalize(row[@tags]).split(",")
    tags.each do |tag|
      book.tags << tag unless book.tags.include? tag
    end
  end

  log.debug { "LibraryThing loading #{book.title}" }
  book
end