1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
{- hpodder component
Copyright (C) 2006 John Goerzen <jgoerzen@complete.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}
{- |
Module : FeedParser
Copyright : Copyright (C) 2006 John Goerzen
License : GNU GPL, version 2 or above
Maintainer : John Goerzen <jgoerzen@complete.org>
Stability : provisional
Portability: portable
Written by John Goerzen, jgoerzen\@complete.org
-}
module FeedParser where
import Types
import Text.XML.HaXml
import Text.XML.HaXml.Parse
import Utils
import Data.Maybe.Utils
import Data.Char
import Data.Either.Utils
import Data.List
data Item = Item {itemtitle :: String,
itemguid :: Maybe String,
enclosureurl :: String,
enclosuretype :: String,
enclosurelength :: String
}
deriving (Eq, Show, Read)
data Feed = Feed {channeltitle :: String,
items :: [Item]}
deriving (Eq, Show, Read)
item2ep pc item =
Episode {podcast = pc, epid = 0,
eptitle = sanitize_basic (itemtitle item),
epurl = sanitize_basic (enclosureurl item),
epguid = fmap sanitize_basic (itemguid item),
eptype = sanitize_basic (enclosuretype item), epstatus = Pending,
eplength = case reads . sanitize_basic . enclosurelength $ item of
[] -> 0
[(x, [])] -> x
_ -> 0,
epfirstattempt = Nothing,
eplastattempt = Nothing,
epfailedattempts = 0}
parse :: FilePath -> String -> IO (Either String Feed)
parse fp name =
do c <- readFile fp
case xmlParse' name (unifrob c) of
Left x -> return (Left x)
Right y ->
do let doc = getContent y
let title = getTitle doc
let feeditems = getEnclosures doc
return $ Right $
(Feed {channeltitle = title, items = feeditems})
where getContent (Document _ _ e _) = CElem e
unifrob ('\xef':'\xbb':'\xbf':x) = x -- Strip off unicode BOM
unifrob x = x
unesc = xmlUnEscape stdXmlEscaper
getTitle doc = forceEither $ strofm "title" (channel doc)
getEnclosures doc =
concat . map procitem $ item doc
where procitem i = map (procenclosure title guid) enclosure
where title = case strofm "title" [i] of
Left x -> "Untitled"
Right x -> x
guid = case strofm "guid" [i] of
Left _ -> Nothing
Right x -> Just x
enclosure = tag "enclosure" `o` children $ i
procenclosure title guid e =
Item {itemtitle = title,
itemguid = guid,
enclosureurl = head0 $ forceMaybe $ stratt "url" e,
enclosuretype = head0 $ case stratt "type" e of
Nothing -> ["application/octet-stream"]
Just x -> x,
enclosurelength = head $ case stratt "length" e of
Nothing -> ["0"]
Just [] -> ["0"]
Just x -> x
}
head0 [] = ""
head0 (x:xs) = x
item = tag "item" `o` children `o` channel
channel =
tag "channel" `o` children `o` tag "rss"
--------------------------------------------------
-- Utilities
--------------------------------------------------
attrofelem :: String -> Content -> Maybe AttValue
attrofelem attrname (CElem inelem) =
case unesc inelem of
Elem name al _ -> lookup attrname al
attrofelem _ _ =
error "attrofelem: called on something other than a CElem"
stratt :: String -> Content -> Maybe [String]
stratt attrname content =
case attrofelem attrname content of
Just (AttValue x) -> Just (concat . map mapfunc $ x)
Nothing -> Nothing
where mapfunc (Left x) = [x]
mapfunc (Right _) = []
-- Finds the literal children of the named tag, and returns it/them
tagof :: String -> CFilter
tagof x = keep /> tag x -- /> txt
-- Retruns the literal string that tagof would find
strof :: String -> Content -> String
strof x y = forceEither $ strof_either x y
strof_either :: String -> Content -> Either String String
strof_either x y =
case tagof x $ y of
[CElem elem] -> Right $ verbatim $ tag x /> txt $ CElem (unesc elem)
z -> Left $ "strof: expecting CElem in " ++ x ++ ", got "
++ verbatim z ++ " at " ++ verbatim y
strofm x y =
if length errors /= 0
then Left errors
else Right (concat plainlist)
where mapped = map (strof_either x) $ y
(errors, plainlist) = conveithers mapped
isright (Left _) = False
isright (Right _) = True
conveithers :: [Either a b] -> ([a], [b])
conveithers inp = worker inp ([], [])
where worker [] y = y
worker (Left x:xs) (lefts, rights) =
worker xs (x:lefts, rights)
worker (Right x:xs) (lefts, rights) =
worker xs (lefts, x:rights)
|