forked from langsci/83
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbodxml.py
140 lines (122 loc) · 4.61 KB
/
bodxml.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
import yaml
from datetime import datetime
from pyPdf import PdfFileReader
import shutil
import zipfile
template = """<?xml version="1.0" encoding="utf-8"?>
<BoD>
<Header>
<FromCompany>Language Science Press</FromCompany>
<FromCompanyNumber>112345678</FromCompanyNumber>
<SentDate>{date}</SentDate>
<SentTime>{time}</SentTime>
<FromPerson>Sebastian Nordhoff</FromPerson>
<FromEmail>[email protected]</FromEmail>
</Header>
<MasteringOrder>
<Product>
<MasteringType>Upload</MasteringType>
<EAN>{isbn}</EAN>
{authorstring}
{editorstring}
<Title>{title}</Title>
<Series>{series}, Bd. {seriesnumber}</Series>
<EditionNumber>{edition}</EditionNumber>
<PublicationDate>{date}</PublicationDate>
<Blurb>{blurb}</Blurb>
<Height>240</Height>
<Width>170</Width>
<Pages>{pagecount}</Pages>
<ColouredPages>{colorpagecount}</ColouredPages>
<ColouredPagesPosition>{colorpagesstring}</ColouredPagesPosition>
<Paper>white</Paper>
<Quality>Standard</Quality>
<Binding>{binding}</Binding>
{back}
<Finish>matt</Finish>
<Language>{booklanguage}</Language>
<Subject Scheme="{scheme[0]}">{scheme[1]}</Subject>
<InternationalDistribution>Yes</InternationalDistribution>
<Price>
<PriceValue>{europrice}</PriceValue>
<PriceCurrency>EUR</PriceCurrency>
</Price>
<Price>
<PriceValue>{gbprice}</PriceValue>
<PriceCurrency>GBP</PriceCurrency>
</Price>
<Price>
<PriceValue>{usdprice}</PriceValue>
<PriceCurrency>USD</PriceCurrency>
</Price>
</Product>
</MasteringOrder>
</BoD>
"""
authortemplate="""
<Contributor>
<ContributorRole>author</ContributorRole>
<ContributorName>{1}, {0}</ContributorName>
<ContributorShortBio>{2}</ContributorShortBio>
</Contributor>
"""
editortemplate="""
<Contributor>
<ContributorRole>editor</ContributorRole>
<ContributorName>{1}, {0}</ContributorName>
<ContributorShortBio>{2}</ContributorShortBio>
</Contributor>
"""
print "extracting metadata"
metadata = yaml.load(open('metadata.yaml','r'))
try:
authors = '\n'.join([authortemplate.format(*a) for a in metadata["creators"]["authors"]])
except TypeError:
authors = ''
try:
editors= '\n'.join([editortemplate.format(*a) for a in metadata["creators"]["editors"]])
except TypeError:
editors = ''
metadata['authorstring'] = authors
metadata['editorstring'] = editors
metadata['date'] = '{:%Y%m%d}'.format(datetime.now())
metadata['time'] = '{:%H:%M}'.format(datetime.now())
metadata['isbn'] = metadata['isbns']['softcover'].replace('-','')
metadata['isbnsc'] = metadata['isbns']['softcover'].replace('-','')
metadata['colorpagecount'] = len(metadata['colorpages'])
metadata['colorpagesstring'] = ','.join([str(x) for x in metadata['colorpages']])
metadata['pagecount'] = PdfFileReader(open('bodcontent.pdf','rb')).getNumPages()
metadata['binding'] = 'PB'
metadata['back'] = ''
outputSC = template.format(**metadata)
metadata['isbn'] = metadata['isbns']['hardcover'].replace('-','')
metadata['isbnhc'] = metadata['isbns']['hardcover'].replace('-','')
metadata['binding'] = 'HC'
metadata['back'] = '<Back>rounded</Back>'
outputHC = template.format(**metadata)
print "Creating xml file for softcover", metadata['isbnsc']
scxml = open("bod/%s_MasteringOrder.xml"%metadata['isbnsc'],'w')
scxml.write(outputSC)
scxml.close()
print "Creating xml file for hardcover", metadata['isbnhc']
hcxml = open("bod/%s_MasteringOrder.xml"%metadata['isbnhc'],'w')
hcxml.write(outputHC)
hcxml.close()
print "renaming pdf files according to ISBNs"
shutil.copy('Bookblock.pdf','bod/%s_Bookblock.pdf'%metadata['isbnsc'])
shutil.copy('Bookblock.pdf','bod/%s_Bookblock.pdf'%metadata['isbnhc'])
shutil.copy('coverSC.pdf','bod/%s_cover.pdf'%metadata['isbnsc'])
shutil.copy('coverHC.pdf','bod/%s_coverHC.pdf'%metadata['isbnhc'])
print "Creating zip file for softcover", metadata['isbnsc']
zfsc = zipfile.ZipFile('bod/%s_MasteringOrder.zip'%metadata['isbnsc'], mode='w')
zfsc.write('bod/%s_MasteringOrder.xml'%metadata['isbnsc'])
zfsc.write('bod/%s_Bookblock.pdf'%metadata['isbnsc'])
zfsc.write('bod/%s_cover.pdf'%metadata['isbnsc'])
zfsc.close()
print "Creating zip file for hardcover", metadata['isbnhc']
zfhc = zipfile.ZipFile('bod/%s_MasteringOrder.zip'%metadata['isbnhc'], mode='w')
zfhc.write('bod/%s_MasteringOrder.xml'%metadata['isbnhc'])
zfhc.write('bod/%s_Bookblock.pdf'%metadata['isbnhc'])
zfhc.write('bod/%s_coverHC.pdf'%metadata['isbnhc'])
zfhc.close()
print "All files created. Files are in /bod"