作者: javacomhk 時間: 2022-7-7 19:48 標題: 用 shell script 去下載 「好讀」網站的 epub 檔案並加上封面
本帖最後由 javacomhk 於 2022-7-11 07:15 編輯
這篇是關於從「好讀」網站下載的 epub 檔案要加上封面的方案 https://www.hkepc.com/forum/viewthread.php?fid=9&tid=2668443
現在提供下載並加上封面的 shell script 去實現自動化製作。這 shell script 要安裝 curl zip unzip package (而 macOS 要在 HomeBrew 下裝 gnu sed 及改為使用 gsed),並可在 Linux、WSL2、macOS terminal 或 Android 手機Termux app 使用,在手機上製作方便可即時上傳製作後的epub 上傳至 Google Play Book 圖書 app。要將以下shell script 源碼製作為 haodooepub.sh 檔案。
例如 : 沈從文《邊城》在「好讀」的 epub 編號是 394
在 Terminal 使用指令便可
bash haodooepub.sh 394
便會下載 394.epub 並製作加了封面的 394a.epub
- #!/bin/bash
- if [ -s ${1}.epub ]; then
- unzip ${1}.epub -d ${1}
- else
- for L in A D E I B
- do
- curl -s "https://www.haodoo.net/?M=d&P=${L}${1}.epub" --output ${1}.epub
- if [ -s ${1}.epub ]; then
- unzip ${1}.epub -d ${1}
- break
- fi
- done
- fi
- if [ ! -s ${1}.epub ]; then
- echo "No epub found for ${1}.epub"
- rm -f ${1}.epub
- exit
- fi
- if [ ! -d ${1} ]; then
- echo "Error for ${1}.epub"
- exit
- fi
- curl -s https://haodoo.net/covers/${1}.jpg --output ${1}/OEBPS/cover.jpg
- if [ ! -s ${1}/OEBPS/cover.jpg ]; then
- echo "No cover found for ${1}.epub"
- exit
- fi
- cd ${1}
- # change to use gsed instead for macOS
- sed -i'' '/id="BookId"/a <meta name="cover" content="cover-image"\/>\r' OEBPS/content.opf
- sed -i'' '/id="ncx"/a <item id="cover-image" href="cover.jpg" media-type="image/jpeg"\/>\r\n<item id="cover" href="cover.html" media-type="application/xhtml+xml"\/>\r' OEBPS/content.opf
- cat > "OEBPS/cover.html" <<EOF
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Cover</title>
- <style type="text/css"> img { max-width: 100%; } </style>
- </head>
- <body>
- <div id="cover-image">
- <img src="cover.jpg" alt="Book Cover"/>
- </div>
- </body>
- </html>
- EOF
- zip -r ../${1}a.epub mimetype META-INF OEBPS
- grep 'aut\|title' OEBPS/content.opf | sed -e 's/<[^>]*>//g' -e 's/^ *//g'
- cd ..
- rm -fr ${1}
- ls ${1}*.epub
作者: 杜龍 時間: 2022-7-9 13:03
書的營養在於內容.
作者: javacomhk 時間: 2022-7-9 18:58
本帖最後由 javacomhk 於 2022-7-9 18:05 編輯
當然營養係重要,但包裝也吸引到讀書嘅興趣。
你依家去 download 隻歌 download 條片都有 album art 有 preview icon 啦。書本封面都一樣啫。個網站有封面架嘛,就係唔整落去本電子書。的電子書響個電子 bookshelf 點可以沒封面咁老土架。

