-
Notifications
You must be signed in to change notification settings - Fork 1
/
wikipedia2calibre
executable file
·194 lines (161 loc) · 6.68 KB
/
wikipedia2calibre
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
#!/bin/bash
################################################################################
##### Checks if the user has requested command line help
################################################################################
if [ "$1" == '-h' -o "$1" == '--help' ]
then
echo
echo "Usage: $0 [wikipedia_url]"
echo
echo 'Puts the content of wikipedia_url in your Calibre database'
exit
fi
################################################################################
##### Checks for software needed to run the script
################################################################################
( which zenity > /dev/null ) || ZENITY=no
if [ "$ZENITY" == 'no' ]
then
echo 'ERROR: you need to install Zenity to run Wikipedia to Calibre'
exit
fi
(( which montage && which mogrify ) > /dev/null ) || IMAGEMAGICK=no
if [ "$IMAGEMAGICK" == 'no' ]
then
zenity --error \
--title='Wikipedia to Calibre' \
--text='You need to install ImageMagick to run Wikipedia to Calibre'
exit
fi
(( which web2disk && \
which ebook-convert && \
which ebook-meta && \
which calibredb ) > /dev/null ) || CALIBRE=no
if [ "$CALIBRE" == 'no' ]
then
zenity --error \
--title='Wikipedia to Calibre' \
--text='You need to install Calibre to run Wikipedia to Calibre (duh!)'
exit
fi
################################################################################
##### Determines the Wikipedia URL to retrieve and its components
################################################################################
if [ "$#" -eq 0 ]
then
WIKIPEDIA_URL=$(zenity --entry \
--text='URL of the Wikipedia article to import' \
--title='Wikipedia to Calibre')
[ -z "$WIKIPEDIA_URL" ] && exit # The user pressed "Cancel"
else
WIKIPEDIA_URL="$1"
fi
# The URL, $1, is something like http://it.wikipedia.org/wiki/Montecastrilli
# With HTTPS it becomes https://secure.wikimedia.org/wikipedia/it/wiki/Montecastrilli
WIKIPEDIA_SITE=$(echo "$WIKIPEDIA_URL" | cut -d/ -f3)
if [ "$WIKIPEDIA_SITE" = 'secure.wikimedia.org' ]
then
WIKIPEDIA_LANG=$(echo "$WIKIPEDIA_URL" | cut -d/ -f5)
WIKIPEDIA_VOICE=$(echo "$WIKIPEDIA_URL" | cut -d/ -f7-)
WIKIPEDIA_TITLE="${WIKIPEDIA_VOICE//_/ }"
WIKIPEDIA_SITE="$WIKIPEDIA_LANG.wikipedia.org"
else
WIKIPEDIA_LANG=$(echo "$WIKIPEDIA_SITE" | cut -d. -f1)
WIKIPEDIA_VOICE=$(echo "$WIKIPEDIA_URL" | cut -d/ -f5-)
WIKIPEDIA_TITLE="${WIKIPEDIA_VOICE//_/ }"
fi
################################################################################
##### Prepares the paths variables and the temporary directories
################################################################################
OLD_PWD="$(pwd)"
TMP_DIR="$HOME/.wikipedia2calibre"
EPUB_DIR="$HOME/.wikipedia2calibre/epub"
HTML_DIR="$HOME/.wikipedia2calibre/html"
SCRIPT_PATH=$(cd ${0%/*} && echo $PWD/${0##*/})
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
rm -fr "$TMP_DIR"
mkdir -p "$HTML_DIR"
mkdir -p "$EPUB_DIR"
cd "$TMP_DIR"
( # Here starts the code tracked by zenity's progress bar
################################################################################
##### Prepares the cover image
################################################################################
montage \
-label "${WIKIPEDIA_VOICE//_/ }" \
"$SCRIPT_DIR/wikipedia-cover-template.jpg" \
-geometry +0+0 -pointsize 24 -frame 3 \
cover.jpg > /dev/null
mogrify -resize 590x754! cover.jpg > /dev/null
echo 20
################################################################################
##### Creates the ZIP bundle containing the HTML page
################################################################################
PRINTABLE_URL="http://$WIKIPEDIA_SITE/w/index.php?title=$WIKIPEDIA_VOICE&printable=yes"
echo $PRINTABLE_URL > /home/rnd/pippo.txt
web2disk -d "$HTML_DIR" -r 0 --verbose "$PRINTABLE_URL" > /dev/null
zip -r pippo-with-anchors.zip html # I don't use HTML_DIR to not clutter the .ZIP internal paths
# Quick & dirty fix for better EPUB's table of contents detection
"$SCRIPT_DIR/remove-nontoc-links.py" "$HTML_DIR"/index.xhtml
zip -r pippo.zip html # I don't use HTML_DIR to not clutter the .ZIP internal paths
echo 40
################################################################################
##### Creates the alternative formats
################################################################################
# Direct conversion from HTML to EPUB or PDF gives the following error:
# ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes
ebook-convert pippo.zip pippo.mobi --cover=cover.jpg > /dev/null
ebook-convert pippo.mobi pippo.epub --preserve-cover-aspect-ratio --cover=cover.jpg > /dev/null
#ebook-convert pippo.epub pippo.pdf
echo 60
################################################################################
##### Adds the EPUB to Calibre with appropriate metadata
################################################################################
WIKIPEDIA_TITLE="$WIKIPEDIA_TITLE (Wikipedia $WIKIPEDIA_LANG)"
META="$META --authors=Wikipedia"
META="$META --author-sort=Wikipedia"
META="$META --book-producer=Wikipedia"
META="$META --cover=cover.jpg"
META="$META --language=$WIKIPEDIA_LANG"
META="$META --publisher=Wikipedia"
META="$META --tags=Wikipedia"
ebook-meta pippo.epub $META \
--title="$WIKIPEDIA_TITLE" \
--comments="From $WIKIPEDIA_URL" \
> /dev/null
# Quick & dirty fix for EPUB's table of contents
mv pippo.epub epub.zip
unzip epub.zip -d epub && rm epub.zip
#"$SCRIPT_DIR/fixncx.py" "$EPUB_DIR"/toc.ncx
# Removes the hard wired TOC too
cd epub
LAST_HTML=$(ls *.html | tail -n 1)
grep -v "$LAST_HTML" content.opf > new.opf
mv new.opf content.opf
rm "$LAST_HTML"
cd ..
# And finally rebuilds the epub
(cd epub && zip -r ../epub.zip *)
cp epub.zip pippo.epub
calibredb add pippo.epub > /dev/null
echo 80
################################################################################
##### Adds the other formats to Calibre
################################################################################
QUERY="title:\"=$WIKIPEDIA_TITLE\""
ID=$(calibredb list --search "$QUERY" | grep '^[0-9]\+ ' | cut -d' ' -f 1)
calibredb add_format $ID pippo-with-anchors.zip > /dev/null
#calibredb add_format $ID pippo.pdf
echo 100
################################################################################
##### Final cleanup
################################################################################
cd "$OLD_PWD"
rm -fr "$TMP_DIR"
) |
zenity --progress \
--title='Wikipedia to Calibre' \
--text="Downloadind and importing the article '$WIKIPEDIA_TITLE'..." \
--percentage=0
[ "$?" = -1 ] && \
zenity --error --title='Wikipedia to Calibre' --text="Import canceled."