atom.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. # see http://purl.mro.name/Photos2Atom
  3. #
  4. cd "$(dirname "${0}")" || exit 1
  5. if [ "" = "${2}" ] ; then
  6. # Should we enforce that $${2} is a url?
  7. cat <<Endofmessage
  8. Extract meta data from a set of images and write the photo feed atom xml to stdout.
  9. SYNOPSIS
  10. $0 <Feed Name> <baseurl> [src/image.jpg]...
  11. Endofmessage
  12. exit 1
  13. fi
  14. title="${1}"
  15. shift
  16. base="${1}"
  17. shift
  18. if [ "$(date -d '@123' --iso-8601=seconds 2>/dev/null)" = "1970-01-01T01:02:03+01:00" ] ; then
  19. file_date_iso8601 () {
  20. date -r "${1}" --iso-8601=seconds
  21. }
  22. elif [ "$(date -r 123 +'%FT%T%z' | sed 's/..$/:&/g' 2>/dev/null)" = "1970-01-01T01:02:03+01:00" ] ; then
  23. file_date_iso8601 () {
  24. date -r "${1}" +'%FT%T%z' | sed 's/..$/:&/g'
  25. }
  26. else
  27. file_date_iso8601 () {
  28. '1970-01-01T00:00:00+00:00'
  29. }
  30. fi
  31. file_size () {
  32. wc -c < "${0}" | tr -d ' '
  33. }
  34. youngest=$(ls -tr "$@" | tail -n 1)
  35. cat <<Endofmessage
  36. <?xml version="1.0" encoding="utf-8"?>
  37. <?xml-stylesheet type='text/xsl' href='assets/atom2html.xslt'?>
  38. <!--
  39. https://developer.mozilla.org/en/docs/XSL_Transformations_in_Mozilla_FAQ#Why_isn.27t_my_stylesheet_applied.3F
  40. Note that Firefox will override your XSLT stylesheet if your XML is
  41. detected as an RSS or Atom feed. A known workaround is to add a
  42. sufficiently long XML comment to the beginning of your XML file in
  43. order to "push" the <.feed> or <.rss> tag out of the first 512 bytes,
  44. which is analyzed by Firefox to determine if it's a feed or not. See
  45. the discussion on bug
  46. https://bugzilla.mozilla.org/show_bug.cgi?id=338621#c72 for more
  47. information.
  48. For best results serve both atom feed and xslt as 'text/xml' or
  49. 'application/xml' without charset specified.
  50. -->
  51. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:georss="http://www.georss.org/georss">
  52. <title>${title}</title>
  53. <generator uri="http://purl.mro.name/Photos2Atom">Photos2Atom</generator>
  54. <id>${base}/</id>
  55. <updated>$(file_date_iso8601 "${youngest}")</updated>
  56. <link href="${base}/" rel="self" type="application/atom+xml"/>
  57. <link href="${base}/" rel="alternate" type="text/html"/>
  58. <author>
  59. <name>John Doe</name>
  60. <uri>http://example.com/~jd</uri>
  61. </author>
  62. Endofmessage
  63. while [ "" != "${1}" ]
  64. do
  65. src="${1}"
  66. file="$(basename "${src}")"
  67. mime="$(file --brief --mime-type "${src}")"
  68. size="$(file_size "_build/200/${file}"y%)"
  69. cat <<Endofmessage
  70. <entry>
  71. <id>${base}/#${file}</id>
  72. <updated>$(file_date_iso8601 "${src}")</updated>
  73. <title></title>
  74. <summary/>
  75. <content src="${base}/1200/${file}" type="${mime}"/>
  76. <media:thumbnail url="${base}/200/${file}"/>
  77. <!-- georss:point>47.874091670000006 12.639475000000001</georss:point -->
  78. <link href="${base}/200/${file}" rel="previewimage" length="${size}" type="${mime}"/>
  79. <link href="${base}/1200/${file}" rel="enclosure" length="$(file_size "_build/1200/${file}")" type="${mime}"/>
  80. <link href="${base}/1200/${file}" type="${mime}"/>
  81. </entry>
  82. Endofmessage
  83. shift
  84. done
  85. cat <<Endofmessage
  86. </feed>
  87. Endofmessage