[教學] 用 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
  1. #!/bin/bash
  2. if [ -s ${1}.epub ]; then
  3.   unzip ${1}.epub -d ${1}
  4. else
  5.   for L in A D E I B
  6.   do
  7.     curl -s "https://www.haodoo.net/?M=d&P=${L}${1}.epub" --output ${1}.epub
  8.     if [ -s ${1}.epub ]; then
  9.       unzip ${1}.epub -d ${1}
  10.       break
  11.     fi
  12.   done
  13. fi
  14. if [ ! -s ${1}.epub ]; then
  15.   echo "No epub found for ${1}.epub"
  16.   rm -f ${1}.epub
  17.   exit
  18. fi
  19. if [ ! -d ${1} ]; then
  20.   echo "Error for ${1}.epub"
  21.   exit
  22. fi

  23. curl -s https://haodoo.net/covers/${1}.jpg --output ${1}/OEBPS/cover.jpg
  24. if [ ! -s ${1}/OEBPS/cover.jpg ]; then
  25.   echo "No cover found for ${1}.epub"
  26.   exit
  27. fi
  28. cd ${1}
  29. # change to use gsed instead for macOS
  30. sed -i'' '/id="BookId"/a <meta name="cover" content="cover-image"\/>\r' OEBPS/content.opf
  31. 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
  32. cat > "OEBPS/cover.html" <<EOF
  33. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  34. <html xmlns="http://www.w3.org/1999/xhtml">
  35. <head>
  36. <title>Cover</title>
  37. <style type="text/css"> img { max-width: 100%; } </style>
  38. </head>
  39. <body>
  40. <div id="cover-image">
  41. <img src="cover.jpg" alt="Book Cover"/>
  42. </div>
  43. </body>
  44. </html>
  45. EOF

  46. zip -r ../${1}a.epub mimetype META-INF OEBPS
  47. grep 'aut\|title' OEBPS/content.opf | sed -e 's/<[^>]*>//g' -e 's/^ *//g'
  48. cd ..
  49. rm -fr ${1}
  50. ls ${1}*.epub
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

書的營養在於內容.

TOP

本帖最後由 javacomhk 於 2022-7-9 18:05 編輯
書的營養在於內容.
杜龍 發表於 2022-7-9 05:03



    當然營養係重要,但包裝也吸引到讀書嘅興趣。

你依家去 download 隻歌  download 條片都有 album art 有 preview icon 啦。書本封面都一樣啫。個網站有封面架嘛,就係唔整落去本電子書。的電子書響個電子 bookshelf 點可以沒封面咁老土架。

TOP