feed_generator 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/bash
  2. #
  3. # This will generate feed.rss with the content of your posts.
  4. #
  5. # Copyright 2015 K. Zimmermann - https://notabug.org/kzimmermann
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # -- Change this if you're using a different database file!
  21. DB="main.db"
  22. # This function checks all recent posts stored in the database:
  23. discover() {
  24. echo $(sqlite3 $DB "SELECT id FROM articles ORDER BY pubdate DESC")
  25. }
  26. # This function queries the database:
  27. # $1 is the data you want.
  28. # $2 is the id of the entry (obtained through discover())
  29. fetcher() {
  30. if [ -z $2 ]
  31. then
  32. echo "Error: no ID provided!"
  33. exit 1
  34. else
  35. case "$1" in
  36. "title" )
  37. result=$(sqlite3 $DB "SELECT title FROM articles WHERE id=$2")
  38. ;;
  39. "summary" )
  40. result=$(sqlite3 $DB "SELECT body FROM articles WHERE id=$2" | head -1)
  41. ;;
  42. "pubdate" )
  43. result=$(sqlite3 $DB "SELECT pubdate FROM articles WHERE id=$2")
  44. ;;
  45. * )
  46. result="Error: could not fetch data '$1'"
  47. ;;
  48. esac
  49. fi
  50. echo -e "$result"
  51. }
  52. post_list=$(discover)
  53. # Quick test to see if fetching the two first lines work.
  54. #for id in $post_list
  55. #do
  56. # echo -e $(fetcher "summary" $id | head -2)
  57. #done
  58. # time to build the file!
  59. cat <<EOF > feed.rss
  60. <?xml version="1.0" encoding="utf-8"?>
  61. <!--
  62. Generated automatically as a test for kzimmermann's blog.
  63. -->
  64. <rss version="2.0">
  65. <channel>
  66. <title>Klaus Zimmermann speaks</title>
  67. <link>http://kzimmermann.nerdpol.ovh</link>
  68. <description>
  69. Klaus Zimmermann's article repository on coding, hacking and the
  70. experiences on hosting a website in your own room.
  71. </description>
  72. <!-- Publishing begins -->
  73. EOF
  74. for id in $post_list
  75. do
  76. cat <<EOF >> feed.rss
  77. <item>
  78. <title>$(fetcher "title" $id)</title>
  79. <link>http://kzimmermann.nerdpol.ovh?article&amp;id=$id</link>
  80. <guid>http://kzimmermann.nerdpol.ovh?article&amp;id=$id</guid>
  81. <pubDate>$(date -d $(fetcher "pubdate" $id ) -u)</pubDate>
  82. <description>
  83. $(fetcher "summary" $id)
  84. </description>
  85. </item>
  86. EOF
  87. done
  88. cat <<EOF >> feed.rss
  89. </channel>
  90. </rss>
  91. EOF