-
Notifications
You must be signed in to change notification settings - Fork 0
/
booki.py
executable file
·698 lines (537 loc) · 17.6 KB
/
booki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#!/usr/bin/env python3
import csv
import sys
import os
import time
import subprocess
import tempfile
import hashlib
import urllib.request
import json
from datetime import datetime
from pathlib import Path
EDITOR = os.environ.get('EDITOR', 'nano')
BOOK_SHORT_ID_LENGTH = 6
base_url = 'https://openlibrary.org/'
isbn_url = 'isbn/'
json_ext = '.json'
class Shelf:
shelves_dir = '~/.local/share/booki/shelves'
default_header = ['id', 'book_id']
def __init__(self, shelf_name, shelf_path=None):
self.shelf_name = shelf_name
if not shelf_path:
shelf_path = Shelf.shelves_dir
self.shelf_file = Path(shelf_path).expanduser() / self.shelf_name
self.data = None
self.book_ids = []
self.is_changed = False
if self.exists():
self.shelf_header = self._get_header_from_shelf()
else:
self.shelf_header = Shelf.default_header
def get_books(self):
if not self.data:
self._load_data_and_header()
return self.data
def get_book_count(self):
if not self.data:
self._load_data_and_header()
return len(self.data)
'''
def get_book_short_ids(self):
if not self.data:
self._load_data_and_header()
return list(self.data.keys())
'''
def get_book(self, book_short_id):
if self.has_book(book_short_id):
return self.data[book_short_id]
else:
return None
def update_book(self, book_short_id, book):
if self.has_book(book_short_id):
self.data[book_short_id] = book
self.is_changed = True
def add_book(self, book):
if not self.data:
self._load_data_and_header()
short_id = self._get_short_id(book)
if short_id not in self.data.keys():
self.data[short_id] = book
self.is_changed = True
def remove_book(self, book_short_id):
if self.has_book(book_short_id):
self.data.pop(book_short_id)
self.is_changed = True
'''
def _gen_id(self):
return hashlib.sha256(str(datetime.now()).encode()).hexdigest()
'''
def has_book(self, book_id):
if not self.data:
self._load_data_and_header()
return book_id in self.data.keys()
def get_universe_books(self):
if not self.data:
self._load_data_and_header()
return self.book_ids
def exists(self):
return self.shelf_file.exists()
def create(self):
if not self.exists():
with open(str(self.shelf_file), 'w') as fil:
header_to_write = '|'.join(self.get_header())
fil.write(header_to_write + '\n')
def add_attribute(self, attribute_name):
if attribute_name in self.shelf_header:
print("can't add '{}'; already exists".format(attribute_name))
return False
if not self.data:
self._load_data_and_header()
self.shelf_header.append(attribute_name)
if self.data:
for book in self.data.values():
book[attribute_name] = ""
self.is_changed = True
return True
def get_header(self):
return self.shelf_header
def get_header_without_ids(self):
new_header = self.shelf_header[:]
new_header.remove('id')
if 'book_id' in new_header:
new_header.remove('book_id')
return new_header
def save(self):
if self.exists() and self.is_changed:
with open(str(self.shelf_file), 'w') as fil:
writer = csv.DictWriter(fil, lineterminator='\n', delimiter='|', escapechar='\\', quoting=csv.QUOTE_NONE, quotechar='', fieldnames=self.get_header())
writer.writeheader()
for book in self.data.values():
writer.writerow(book)
def _get_short_id(self, book):
return book['id'][0:BOOK_SHORT_ID_LENGTH]
def _get_header_from_shelf(self):
with open(str(self.shelf_file), 'r') as fil:
first_line = fil.readline().strip()
return first_line.split('|')
def _load_data_and_header(self):
if self.exists():
self.data = {}
with open(str(self.shelf_file), 'r') as fil:
reader = csv.DictReader(fil, delimiter='|', quoting=csv.QUOTE_NONE)
self.shelf_header = reader.fieldnames
book_list = list(reader)
for book in book_list:
short_id = self._get_short_id(book)
self.data[short_id] = book
if 'book_id' in book:
self.book_ids.append(book['book_id'])
else:
self.book_ids.append(book['id'])
class Universe(Shelf):
def __init__(self):
super().__init__('theuniverse', '~/.local/share/booki')
self.shelf_header = ['id', 'isbn', 'title', 'author', 'page_count']
universe_o = Universe()
if not universe_o.exists():
universe_o.create()
def _get_shelves():
shelvesdir = Path('~/.local/share/booki/shelves').expanduser()
if not shelvesdir.exists():
shelvesdir.mkdir()
shelves = {fil.name: Shelf(fil.name) for fil in shelvesdir.iterdir()}
return {name: shelves[name] for name in sorted(shelves)}
shelves_map = _get_shelves()
def user_entry_from_file(in_map, comment=None):
before_contents = ""
if comment is not None:
before_contents = "# {}\n".format(comment)
before_contents += '\n'.join(["{}: {}".format(x, in_map[x]) for x in in_map.keys()])
with tempfile.NamedTemporaryFile(delete=False) as tmpfil:
tmpfil.write(before_contents.encode('utf-8'))
tmpfil.flush()
subprocess.run([EDITOR, tmpfil.name], stdin=sys.stdout)
with open(tmpfil.name, 'r') as newfil:
after_contents = newfil.read()
os.unlink(tmpfil.name)
after_contents_list = after_contents.strip().split('\n')
for line in after_contents_list:
if line[0] == "#":
continue
line_list = line.split(':')
key = line_list[0].strip()
if key in in_map:
in_map[key] = ":".join(line_list[1:]).strip()
return in_map
def print_books(books):
if len(books) > 0:
print("\n".join(map(format_book_for_print, books)))
def format_book_for_print(book):
if 'short_id' not in book:
book['short_id'] = book['id'][0:BOOK_SHORT_ID_LENGTH]
if 'book_id' in book:
book_id = book['book_id']
else:
book_id = book['id']
on_shelves = [name for name, shelf in shelves_map.items() if book_id in shelf.get_universe_books()]
read_marker = "> " if 'read' in on_shelves else ""
return "{} {}{} by {}".format(book['short_id'], read_marker, book['title'], book['author'])
def book_list_sort(book_list):
by_title = sorted(book_list, key=lambda book: book['title'])
by_author_first_name = sorted(by_title, key=lambda book: book['author'].split(' ')[0])
by_author_last_name = sorted(by_author_first_name, key=lambda book: book['author'].split(' ')[-1])
return by_author_last_name
def _get_shelf_book_tuple(line_in):
line_in_list = line_in.split(' ')
# check for bad input
if len(line_in_list) == 1 and len(line_in_list[0]) == '':
return None
shelf_and_id_list = line_in_list[0].split('.')
if len(shelf_and_id_list) == 1:
first_item = shelf_and_id_list[0]
if len(first_item) == 0:
return None
# if the first item is not empty, it's a universe book
else:
return (universe_o, universe_o.get_book(first_item))
elif len(shelf_and_id_list) == 2:
shelf_name = shelf_and_id_list[0]
book_id = shelf_and_id_list[1]
if shelf_name not in shelves_map:
print("shelf doesn't exist: " + shelf_name)
return None
shelf = shelves_map[shelf_name]
return (shelf, shelf.get_book(book_id))
else:
return None
def discover(args):
if len(args) != 1:
print("need an ISBN")
return
full_url = base_url + isbn_url + args[0] + json_ext
with urllib.request.urlopen(full_url) as fil:
json_text = fil.read().decode('utf-8')
try:
json_array = json.loads(json_text)
except json.decoder.JSONDecodeError:
print("ERROR with: " + isbn)
return
universe_header = universe_o.get_header_without_ids()
out_map = {x: '' for x in universe_header}
out_map['isbn'] = args[0]
author_url = ''
if 'title' in json_array:
out_map['title'] = json_array['title']
if 'authors' in json_array:
author_url = json_array['authors'][0]['key']
if 'number_of_pages' in json_array:
out_map['page_count'] = json_array['number_of_pages']
if 'by_statement' in json_array:
out_map['author'] = json_array['by_statement']
full_author_url = base_url + author_url + json_ext
with urllib.request.urlopen(full_author_url) as fil:
author_json_text = fil.read().decode('utf-8')
author_json_array = None
try:
author_json_array = json.loads(author_json_text)
except json.decoder.JSONDecodeError:
pass
if author_json_array and 'name' in author_json_array:
out_map['author'] = author_json_array['name']
comment = "discovering a book from ISBN" + args[0]
user_input = user_entry_from_file(out_map, comment)
add_book_to_universe(user_input)
def add_book_to_universe(book):
book_id = hashlib.sha256(str(book).encode()).hexdigest()
if not universe_o.has_book(book_id[0:BOOK_SHORT_ID_LENGTH]):
book['id'] = book_id
universe_o.add_book(book)
universe_o.save()
print("added book to universe!")
print_books([book])
shelves_in = input("add book to shelves: ")
shelves_list = [x.strip() for x in shelves_in.split(',')]
if len(shelves_list) == 1 and shelves_list[0] == "":
return
for shelf in shelves_list:
if shelf in shelves_map:
add_book_to_shelf(book, shelves_map[shelf])
print("added to " + shelf)
shelves_map[shelf].save()
else:
print("no shelf named '{}'".format(shelf.shelf_name))
else:
print("that book already exists!")
def add(args):
universe_header = universe_o.get_header_without_ids()
u_map = {x: '' for x in universe_header}
comment = "adding a book from scratch"
user_input = user_entry_from_file(u_map, comment)
add_book_to_universe(user_input)
def _get_books_from_shelf_with_short_ids(shelf):
book_list = []
shelf_books = shelf.get_books()
for shelf_book_id in shelf_books.keys():
shelf_book = shelf_books[shelf_book_id]
book = universe_o.get_book(shelf_book['book_id'][0:BOOK_SHORT_ID_LENGTH]).copy()
book['short_id'] = "{}.{}".format(shelf.shelf_name, shelf_book_id)
book_list.append(book)
return book_list
def browse(args):
if len(args) < 1:
print("usage: browse <shelf_name> <<search info>>")
return
if args[0] not in shelves_map:
print("no shelf named '" + args[0] + "'")
return
shelf = shelves_map[args[0]]
book_list = _get_books_from_shelf_with_short_ids(shelf)
if len(args) == 1:
print_books(book_list)
else:
search(args[1:], book_list)
def search(args, list_to_search=None):
if list_to_search is None:
list_to_search = universe_o.get_books().values()
if len(args) == 0:
sorted_list = book_list_sort(list_to_search)
print_books(sorted_list)
return
if len(args) % 2 != 0:
print("need an even number of arguments")
return
search_types = ['title', 'author']
if args[0] not in search_types:
print("search type must be in: " + str(search_types))
return
search_term = args[1].lower()
search_type = "in"
if search_term[0] == '^' and search_term[-1] == '$':
search_type = "full_string"
search_term = search_term[1:-1]
if search_term[0] == '^':
search_type = "start"
search_term = search_term[1:]
elif search_term[-1] == '$':
search_type = "end"
search_term = search_term[0:-1]
ret = []
for book in list_to_search:
candidate = book[args[0]].lower()
if search_type == "in" and search_term in candidate:
ret.append(book)
elif search_type == "full_string" and candidate.startswith(search_term) and candidate.endswith(search_term):
ret.append(book)
elif search_type == "start" and candidate.startswith(search_term):
ret.append(book)
elif search_type == "end" and candidate.endswith(search_term):
ret.append(book)
search(args[2:], ret)
def add_book_to_shelf(book, shelf):
new_id = hashlib.sha256(str(datetime.now()).encode()).hexdigest()
headers = shelf.get_header_without_ids()
header_map = {}
if len(headers) != 0:
comment = "adding to {}: {}".format(shelf.shelf_name, format_book_for_print(book))
header_map = {x: "" for x in headers}
out = user_entry_from_file(header_map, comment)
header_map['id'] = new_id
header_map['book_id'] = book['id']
shelf.add_book(header_map)
def addto(args):
if len(args) != 1:
print("usage: addto <shelf_name> (accepts stdin)")
return
if args[0] not in shelves_map:
print("no shelf named '" + args[0] + "'")
return
shelf = shelves_map[args[0]]
stdin = list(sys.stdin)
for line in stdin:
book = _get_shelf_book_tuple(line)[1]
add_book_to_shelf(book, shelf)
shelf.save()
print("shelved " + str(len(stdin)) + " books")
def pull(args):
if len(args) != 0:
print("usage: pull (accepts stdin)")
return
stdin = list(sys.stdin)
for line in stdin:
shelf, book = _get_shelf_book_tuple(line)
if shelf.shelf_name == universe_o.shelf_name: # can't pull a book from the universe
break
shelf.remove_book(book['id'][0:BOOK_SHORT_ID_LENGTH])
shelf.save()
print_books([universe_o.get_book(book["book_id"][0:BOOK_SHORT_ID_LENGTH])])
def shelves(args):
if len(args) != 0:
print("usage: 'shelves'")
return
for shelf_name, shelf in shelves_map.items():
print("{}: {} books".format(shelf_name, str(shelf.get_book_count())))
def new(args):
if len(args) != 1:
print("usage: 'add <shelf_name'")
return
if args[0] in shelves_map:
print("already have a shelf named '{}'!".format(args[0]))
return
shelf = Shelf(args[0])
new_attributes = input("attributes (sep. by comma): ")
attr_list = [x.strip() for x in new_attributes.split(',')]
out_count = _add_attributes_to_shelf(attr_list, shelf)
shelf.create()
print("created shelf: " + shelf.shelf_name)
def _add_attributes_to_shelf(attribute_list, shelf):
cnt = 0
for att in attribute_list:
if att != "":
ret = shelf.add_attribute(att)
if ret:
cnt += 1
return cnt
def show(args):
if len(args) != 0:
print("usage: 'edit' (with stdin)")
return
stdin = list(sys.stdin)
for line in stdin:
shelf, book = _get_shelf_book_tuple(line)
if not shelf or not book:
print("error: " + str(shelf) + " " + str(book))
break
print(line.strip('\n'))
headers = shelf.get_header_without_ids()
for attr in headers:
print(" - {}: {}".format(attr, book[attr]))
def describe(args):
if len(args) != 1:
print("usage: 'describe <shelf_name>'")
return
if args[0] not in shelves_map:
print("no shelf named '" + args[0] + "'")
return
shelf = shelves_map[args[0]]
attribute_list = shelf.get_header_without_ids()
if len(attribute_list) == 0:
print("no current attributes for " + shelf.shelf_name)
else:
print("current attributes of {}: {}".format(shelf.shelf_name, ", ".join(attribute_list)))
def findon(args):
if len(args) != 1:
print("usage: 'findon <shelf_name>'")
return
if args[0] not in shelves_map:
print("no shelf named '" + args[0] + "'")
return
shelf = shelves_map[args[0]]
shelf_books = shelf.get_books()
shelf_books_by_universe_id = {book["book_id"][0:BOOK_SHORT_ID_LENGTH]: book["id"][0:BOOK_SHORT_ID_LENGTH] for book in shelf_books.values()}
stdin = list(sys.stdin)
for line in stdin:
target_shelf, book = _get_shelf_book_tuple(line)
if target_shelf.shelf_name == shelf.shelf_name:
print_books([book])
continue
if target_shelf.shelf_name != "theuniverse":
book = universe_o.get_book(book["book_id"][0:BOOK_SHORT_ID_LENGTH])
if book["id"][0:BOOK_SHORT_ID_LENGTH] in shelf_books_by_universe_id.keys():
new_book = shelf.get_book(shelf_books_by_universe_id[book["id"][0:BOOK_SHORT_ID_LENGTH]])
book["short_id"] = f"{shelf.shelf_name}.{new_book['id'][0:BOOK_SHORT_ID_LENGTH]}"
print_books([book])
def extend(args):
if len(args) != 1:
print("usage: 'extend <shelf_name>'")
return
if args[0] not in shelves_map:
print("no shelf named '" + args[0] + "'")
return
shelf = shelves_map[args[0]]
describe(args)
new_attributes = input("new attributes (sep. by comma): ")
attr_list = [x.strip() for x in new_attributes.split(',')]
out_count = _add_attributes_to_shelf(attr_list, shelf)
shelf.save()
print("added {} new attribute{} to {}".format(str(out_count), '' if out_count == 1 else 's', shelf.shelf_name))
def edit(args):
if len(args) != 0:
print("usage: 'edit' (with stdin)")
return
stdin = list(sys.stdin)
if len(stdin) != 1:
print("you can only edit one entry at a time!")
return
target_shelf, book = _get_shelf_book_tuple(stdin[0])
if not target_shelf or not book:
print("error!")
return
if book:
# get the universe book
if 'book_id' in book:
universe_book = universe_o.get_book(book['book_id'][0:BOOK_SHORT_ID_LENGTH])
else:
universe_book = book
# get the headers without IDs
header_list = target_shelf.get_header_without_ids()
if len(header_list) == 0:
print("no additional data to edit")
return
# add data from book
book_map = { x: book[x] for x in header_list }
# user edit
comment = "editing on {}: {}".format(target_shelf.shelf_name, format_book_for_print(universe_book))
user_input = user_entry_from_file(book_map, comment)
# update the book on the shelf
for key, val in user_input.items():
book[key] = val
# TODO: this should be cleaner, probably part of a book itself
if 'short_id' in book:
book.pop('short_id')
target_shelf.update_book(book['id'][0:BOOK_SHORT_ID_LENGTH], book)
target_shelf.save()
print("updated!")
else:
print("can't find book on shelf...")
return
def main():
args = sys.argv[1:]
if len(args) == 0:
print("booki!")
print("find new books:")
print(" - add")
print(" - discover <isbn>")
print("search for books")
print(" - search <author|title> <query>")
print("manage shelves")
print(" - shelves")
print(" - browse <shelf_name> <<search terms>>")
print(" - addto <shelf_name> (accepts stdin)")
print(" - pull (accepts stdin)")
print(" - new <shelf_name>")
print(" - extend <shelf_name>")
print(" - edit (accepts stdin)")
print(" - describe <shelf_name>")
print(" - show (accepts stdin)")
return
option_dict = { 'search': search,
'shelves': shelves,
'addto': addto,
'add': add,
'discover': discover,
'new': new,
'browse': browse,
'extend': extend,
'edit': edit,
'describe': describe,
'show': show,
'pull': pull,
'findon': findon}
if args[0] in option_dict.keys():
option_dict[args[0]](args[1:])
else:
print("bad command")
if __name__ == "__main__":
main()