-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
145 lines (120 loc) · 4.92 KB
/
convert.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
""" turns a csv of new books into html """
import argparse
import csv
from titlecase import titlecase
from gooey import Gooey, GooeyParser
def make_tuples(input_data):
""" make tuples out of the csv data """
data_list = []
# iterate through the lines of the csv file. Leave out the headers.
for line in input_data[1:]:
# make the HTML for the title. Use the titlecase library for better capitalization
bib_info1 = "<strong>" + titlecase(line[2]) + "</strong>"
# make the HTML for the author. Leave out the word 'author'.
if line[1]:
if line[1][-9:] == ", author.":
author = line[1][:-9]
elif line[1][-9:] == "- author.":
author = line[1][:-8]
else:
author = line[1]
bib_info2 = "<li><em>Author: </em>" + author + "</li>"
else:
# if there is no author, assign an empty string
bib_info2 = ""
# make the HTML for the call number
if line[6]:
bib_info3 = "<li><em>Call number: </em>" + line[6] + "</li>"
else:
# if there is no call number, assign an empty string
bib_info3 = ""
# make the format (print or ebook)
if line[5].lower().strip() == "print":
bib_info5 = "<li><em>Format: </em>Print book</li>"
elif line[5]:
bib_info5 = "<li><em>Format: </em>" + line[5] + "</li>"
else:
bib_info5 = ""
# make the URL based on the ISBN
# failing that make the URL based on the title
# failing that make no link
if line[3]:
url = (
"https://cuny-kb.primo.exlibrisgroup.com/discovery/search?query=isbn,exact,"
+ line[3]
+ "&tab=Everything&search_scope=IZ_CI_AW&sortby=rank&vid=01CUNY_KB:CUNY_KB&lang=en&offset=0"
)
bib_info4 = '<li><a href="' + url + '">Search the catalog</a></li>'
elif line[2]:
url = (
"https://cuny-kb.primo.exlibrisgroup.com/discovery/search?query=title,contains,"
+ line[2]
+ "&tab=Everything&search_scope=MyInst_and_CI&sortby=rank&vid=01CUNY_KB:CUNY_KB&lang=en&offset=0"
)
bib_info4 = '<li><a href="' + url + '">Search the catalog</a></li>'
else:
bib_info4 = ""
# assemble all the HTML for the item
bib_info = (
"<li>"
+ bib_info1
+ "<ul>"
+ bib_info2
+ bib_info3
+ bib_info5
+ bib_info4
+ "</ul></li>"
)
# make a tuple for the item. The first item is LC Class, the second is the HTML.
lc_class = line[0]
collection = line[4]
item_tuple = (lc_class, collection, bib_info)
if line[2]:
data_list.append(item_tuple)
# return the list of tuples
return data_list
def make_html(input_tuples, args):
""" makes html out of the tuples """
# open a file for the HTML
with open(args.infile[:-3] + "html", "w", encoding="utf-16") as file_2:
# start a big unordered list
file_2.write("<ul>")
# keep track of what LC class/collection is the current one, iterate through the tuples
current_lc_class = ""
current_collection = ""
for item in input_tuples:
# if it's a new LC class, or a new header, create a new header
if item[1] == "Juvenile Collection" and item[1] != current_collection:
file_2.write("</ul><h3>" + item[1] + "</h3><ul>")
elif item[1] == "Leisure Collection" and item[1] != current_collection:
file_2.write("</ul><h3>" + item[1] + "</h3><ul>")
if item[0] != current_lc_class:
file_2.write("</ul><h3>" + item[0] + "</h3><ul>")
# then write the item, then reassign the current LC class and collection
file_2.write(item[2])
current_lc_class = item[0]
current_collection = item[1]
# close the big list
file_2.write("</ul>")
@Gooey(program_name="Convert a CSV of new books to HTML")
def main():
""" run it! """
# make the GUI application using Gooey
parser = GooeyParser(
description="Parses a csv of new books to produce an html file."
"\nThe resulting html output file will be in the same folder as the csv."
)
search_group = parser.add_argument_group(
"Specify files", gooey_options={"columns": 1}
)
search_group.add_argument(
"infile", metavar="Infile", type=str, help="a csv file", widget="FileChooser"
)
args = parser.parse_args()
# do all the work! Make the tuples and then turn them into HTML
with open(args.infile, "r", encoding="latin-1") as file_1:
data = list(csv.reader(file_1))
tuples = make_tuples(data)
make_html(tuples, args)
if __name__ == "__main__":
main()