importfeed: Added some additional --template variables for date and time
This commit was sponsored by Ethan Aubin.
This commit is contained in:
parent
57edd81849
commit
4229713e63
4 changed files with 49 additions and 7 deletions
|
@ -10,6 +10,9 @@ git-annex (8.20200618) UNRELEASED; urgency=medium
|
|||
including starting up a large number of external special remote processes.
|
||||
(Regression introduced in version 8.20200501)
|
||||
* test: Fix some test cases that assumed git's default branch name.
|
||||
* importfeed: Added some additional --template variables:
|
||||
itempubyear, itempubmonth, itempubday, itempubhour,
|
||||
itempubminute, itempubsecond.
|
||||
|
||||
-- Joey Hess <id@joeyh.name> Thu, 18 Jun 2020 12:21:14 -0400
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{- git-annex command
|
||||
-
|
||||
- Copyright 2013-2017 Joey Hess <id@joeyh.name>
|
||||
- Copyright 2013-2020 Joey Hess <id@joeyh.name>
|
||||
-
|
||||
- Licensed under the GNU AGPL version 3 or higher.
|
||||
-}
|
||||
|
@ -16,6 +16,8 @@ import qualified Data.Set as S
|
|||
import qualified Data.Map as M
|
||||
import Data.Time.Clock
|
||||
import Data.Time.Format
|
||||
import Data.Time.Calendar
|
||||
import Data.Time.LocalTime
|
||||
import qualified Data.Text as T
|
||||
import System.Log.Logger
|
||||
|
||||
|
@ -313,15 +315,43 @@ feedFile :: Utility.Format.Format -> ToDownload -> String -> FilePath
|
|||
feedFile tmpl i extension = Utility.Format.format tmpl $
|
||||
M.map sanitizeFilePath $ M.fromList $ extractFields i ++
|
||||
[ ("extension", extension)
|
||||
, extractField "itempubdate" [pubdate $ item i]
|
||||
, extractField "itempubdate" [itempubdate]
|
||||
, extractField "itempubyear" [itempubyear]
|
||||
, extractField "itempubmonth" [itempubmonth]
|
||||
, extractField "itempubday" [itempubday]
|
||||
, extractField "itempubhour" [itempubhour]
|
||||
, extractField "itempubminute" [itempubminute]
|
||||
, extractField "itempubsecond" [itempubsecond]
|
||||
]
|
||||
where
|
||||
pubdate itm = case getItemPublishDate itm :: Maybe (Maybe UTCTime) of
|
||||
Just (Just d) -> Just $
|
||||
formatTime defaultTimeLocale "%F" d
|
||||
itm = item i
|
||||
|
||||
pubdate = case getItemPublishDate itm :: Maybe (Maybe UTCTime) of
|
||||
Just (Just d) -> Just d
|
||||
_ -> Nothing
|
||||
|
||||
itempubdate = case pubdate of
|
||||
Just pd -> Just $
|
||||
formatTime defaultTimeLocale "%F" pd
|
||||
-- if date cannot be parsed, use the raw string
|
||||
_ -> replace "/" "-" . T.unpack
|
||||
Nothing-> replace "/" "-" . T.unpack
|
||||
<$> getItemPublishDateString itm
|
||||
|
||||
(itempubyear, itempubmonth, itempubday) = case pubdate of
|
||||
Nothing -> (Nothing, Nothing, Nothing)
|
||||
Just pd ->
|
||||
let (y, m, d) = toGregorian (utctDay pd)
|
||||
in (Just (show y), Just (show m), Just (show d))
|
||||
|
||||
(itempubhour, itempubminute, itempubsecond) = case pubdate of
|
||||
Nothing -> (Nothing, Nothing, Nothing)
|
||||
Just pd ->
|
||||
let tod = timeToTimeOfDay (utctDayTime pd)
|
||||
in ( Just (show (todHour tod))
|
||||
, Just (show (todMin tod))
|
||||
-- avoid fractional seconds
|
||||
, Just (takeWhile (/= '.') (show (todSec tod)))
|
||||
)
|
||||
|
||||
extractMetaData :: ToDownload -> MetaData
|
||||
extractMetaData i = case getItemPublishDate (item i) :: Maybe (Maybe UTCTime) of
|
||||
|
|
|
@ -47,9 +47,14 @@ To make the import process add metadata to the imported files from the feed,
|
|||
(All of the above are also added as metadata when annex.genmetadata is
|
||||
set.)
|
||||
|
||||
The extension variable is the extension of the file in the feed,
|
||||
or sometimes ".m" if no extension can be determined.
|
||||
|
||||
The template also has some variables for when an item was published.
|
||||
|
||||
itempubdate (YYY-MM-DD or if the feed's date cannot be parsed, the raw
|
||||
itempubyear (YYYY), itempubmonth (MM), itempubday (DD), itempubhour (HH),
|
||||
itempubminute (MM), itempubsecond (SS),
|
||||
itempubdate (YYYY-MM-DD or if the feed's date cannot be parsed, the raw
|
||||
value from the feed).
|
||||
|
||||
(These use the UTC time zone, not the local time zone.)
|
||||
|
|
|
@ -3,3 +3,7 @@ It would be great to be able to use the pubDate of the entries with the --templa
|
|||
Text.Feed.Query has a getItemPublishDate (and a getFeedPubDate, if we want some kind of ${feeddate}).
|
||||
|
||||
The best would be to allow a reformating of the date(s) with (for example) %Y-%m-%D
|
||||
|
||||
> itempubdate was added years ago and I forgot to close this,
|
||||
> but I've now also added itempubmonth, itempubday, etc. [[done]]
|
||||
> --[[Joey]]
|
||||
|
|
Loading…
Add table
Reference in a new issue