-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-book-wishlist
executable file
·99 lines (80 loc) · 2.81 KB
/
export-book-wishlist
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
#!/usr/bin/env python3
import json
import re
from argparse import ArgumentParser
from typing import Iterator, List, NamedTuple, Optional
import pandas as pd
from dateutil.parser import parse as parse_date
date_pattern = re.compile(r'^(?:([0-9]{2})/)?(?:([0-9]{2})/)?([0-9]{4})$')
if True:
sheet_owned_books = 'Books'
sheet_owned_comics = 'Comics'
sheet_wishlist_books = 'Books - Wishlist'
sheet_wishlist_comics = 'Comics - Wishlist'
column_title = 'Title'
column_authors = 'Authors'
column_categories = 'Categories'
column_published_date = 'Published date'
column_isbn = 'ISBN'
column_read = 'Read'
bool_true = 'Yes'
else:
sheet_owned_books = 'Livres'
sheet_owned_comics = 'Bandes Dessinées'
sheet_wishlist_books = 'Livres - Liste de souhaits'
sheet_wishlist_comics = 'Bandes Dessinées - Liste de sou'
column_title = 'Titre'
column_authors = 'Auteurs'
column_categories = 'Genres'
column_published_date = 'Date de publication'
column_isbn = 'ISBN'
column_read = 'Lu'
bool_true = 'Oui'
class Book(NamedTuple):
title: str
authors: List[str]
genres: List[str]
publication_year: Optional[int]
isbn: str
comic: bool
read: bool
owned: bool
def parse_book(book: pd.Series, comic: bool, owned: bool) -> dict:
publication = book[column_published_date]
publication_date = None
publication_year = None
if publication:
try:
publication_year = int(publication)
except ValueError:
publication_dt = parse_date(publication)
publication_date = publication_dt.date().isoformat()
publication_year = publication_dt.year
return Book(
title=book[column_title],
authors=book[column_authors].split(', '),
genres=book[column_categories].split(', '),
publication_year=publication_year,
isbn=book[column_isbn],
comic=comic,
read=book[column_read] == bool_true,
owned=owned,
)._asdict()
def parse_books(books: pd.DataFrame, comic: bool, owned: bool) -> Iterator[dict]:
for _, book in books.iterrows():
yield parse_book(book, comic=comic, owned=owned)
def main() -> None:
parser = ArgumentParser()
parser.add_argument(
'filename', help='XLS files from MyLibrary, e.g. MyLibrary.xls'
)
args = parser.parse_args()
df = pd.read_excel(args.filename, na_filter=False, sheet_name=None)
books: list[dict] = []
books += parse_books(df[sheet_owned_books], comic=False, owned=True)
books += parse_books(df[sheet_wishlist_books], comic=False, owned=False)
books += parse_books(df[sheet_owned_comics], comic=True, owned=True)
books += parse_books(df[sheet_wishlist_comics], comic=True, owned=False)
print(json.dumps(books))
if __name__ == '__main__':
main()