summaryrefslogtreecommitdiff
path: root/photo_date_range.hs
diff options
context:
space:
mode:
Diffstat (limited to 'photo_date_range.hs')
-rwxr-xr-xphoto_date_range.hs55
1 files changed, 55 insertions, 0 deletions
diff --git a/photo_date_range.hs b/photo_date_range.hs
new file mode 100755
index 0000000..ba6bfc5
--- /dev/null
+++ b/photo_date_range.hs
@@ -0,0 +1,55 @@
+#!/usr/bin/env stack
+-- stack --install-ghc runghc --package turtle --package attoparsec
+{- photo_date_range script
+ -
+ - Copyright 2017 Félix Sipma <felix.sipma@no-log.org>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+{-# LANGUAGE OverloadedStrings #-}
+
+import Turtle
+import qualified Control.Foldl as Fold
+import Prelude hiding (FilePath)
+import Data.Time.Format (parseTimeM, defaultTimeLocale, formatTime)
+import qualified Data.Text as T
+import qualified Data.Text.IO as T
+
+
+parser :: Parser FilePath
+parser = argPath "src" "source directory containing the images"
+
+main = sh $ do
+ src <- options "Print date range of Exif tagged images" parser
+ let dates = inproc "exiftool" ["-p", "${createdate#;DateFmt(\"%Y.%m.%d\")}", "-q", format fp src] empty
+ sdates = inproc "sort" [] dates
+ hdates <- fold sdates Fold.list
+ case hdates of
+ [] -> err "Provided images generated no date, check \"CreateDate\" tags."
+ [ h ] -> echo h
+ d1 : q -> case (reverse q) of
+ [] -> echo d1
+ d2 : _ -> do
+ d1' <- parseTimeM True defaultTimeLocale "%Y.%m.%d" (T.unpack $ lineToText d1) :: Shell UTCTime
+ d2' <- parseTimeM True defaultTimeLocale "%Y.%m.%d" (T.unpack $ lineToText d2) :: Shell UTCTime
+ liftIO $ T.putStr $ printrange d1' d2'
+ where
+ year = T.pack . formatTime defaultTimeLocale "%Y"
+ month = T.pack . formatTime defaultTimeLocale "%m"
+ day = T.pack . formatTime defaultTimeLocale "%d"
+ formatdate = makeFormat (\d' -> year d' <> "." <> month d' <> "." <> day d')
+ printdate d' = format formatdate d'
+ printrange d1 d2 = if year d1 == year d2
+ then
+ if month d1 == month d2
+ then
+ if day d1 == day d2
+ then
+ printdate d1
+ else
+ format (formatdate % "-" % s) d1 (day d2)
+ else
+ format (formatdate % "-" % s % s) d1 (month d2) (day d2)
+ else
+ format (formatdate % "-" % formatdate) d1 d2