summaryrefslogtreecommitdiff
path: root/booklist.py
blob: 3ece6f7da81b1e4c6e95c42682bc54db27040d03 (plain)
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
#!/usr/bin/env python3

"""
Parsing/managing a book wishlist.

 - parse list
 - store list
 - get extra info (isbn &c)
 - group by author
 - intelligent detection of author misspelling/title misspelling
    - online matches? goodreads?

"""

import sys
from pprint import pprint
from collections import defaultdict

def read_booklist(file_name="booklist.txt"):

    booklist = []
    try:
        with open(file_name, 'r') as file:
            for line in file:
                author, title = line.rstrip().split(' - ')
                booklist.append([author, title])

    except OSError as exc:
        tb = sys.exc_info()[-1]
        lineno = tb.tb_lineno
        filename = tb.tb_frame.f_code.co_filename
        print(f'{exc.strerror} at {filename} line {lineno}.')
        sys.exit(exc.errno)

    return booklist


def main():
    """ main function, do stuff """
    booklist = read_booklist("booklist.txt")
    pprint(booklist)

    newbooklist = defaultdict(list)

    for k, v in booklist:
        newbooklist[k].append(v)

    pprint(newbooklist)



if __name__ == '__main__':
    main()