Skip to content

Commit d5ef050

Browse files
committed
Add an option for mess-html for cache direcotyr
1 parent 354dc10 commit d5ef050

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

app/html/Main.hs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
{-# LANGUAGE RecordWildCards #-}
2+
{-# LANGUAGE ApplicativeDo #-}
23

34
module Main where
45

5-
import Prelude hiding (readFile, writeFile)
6+
import Prelude hiding (writeFile)
67

78
import Data.List (sortBy)
89
import Data.Ord (comparing)
910
import Control.Monad (forM_)
11+
import Control.Monad.Writer (runWriter)
12+
import Control.Arrow (second)
1013
import Data.Binary (decode)
1114

15+
import System.FilePath ((</>))
16+
1217
import Data.VkMess
1318
( Snapshot(..)
1419
, readFile
1520
, writeFile
1621
, mDate
22+
, listToWriter
23+
, replaceSnapshotUrls
1724
)
1825

1926
import Options.Applicative
@@ -28,20 +35,39 @@ import Text.Html.VkMess
2835
, standalone
2936
)
3037

31-
optparser :: IO FilePath
38+
data Options = Options
39+
{ inFile :: FilePath
40+
, cacheDir :: Maybe FilePath
41+
}
42+
43+
sample :: Parser Options
44+
sample = do
45+
inFile <- argument str $
46+
metavar "DUMP"
47+
<> help "Input file"
48+
cacheDir <- optional $ strOption $
49+
long "cache"
50+
<> short 'c'
51+
<> help "Directory containing cache produced by mess-cache"
52+
pure $ Options {..}
53+
54+
optparser :: IO Options
3255
optparser = execParser opts
3356
where
34-
opts = info (inFile <**> helper)
57+
opts = info (sample <**> helper)
3558
( fullDesc
3659
<> progDesc "Render messages fetched by mess-fetch as html")
37-
inFile = argument str $
38-
metavar "FILE"
39-
<> help "Input file"
4060

4161
main :: IO ()
4262
main = do
43-
inFile <- optparser
44-
(Snapshot ms self users chats) <- decode <$> readFile inFile
63+
Options {..} <- optparser
64+
mm <- case cacheDir of
65+
Just d -> listToWriter <$> map (second (d </>)) <$> map (\[k, v] -> (k, v))
66+
<$> map words <$> lines <$> Prelude.readFile (d </> "index.txt")
67+
Nothing -> pure pure
68+
(Snapshot ms self users chats) <- fst <$> runWriter
69+
<$> replaceSnapshotUrls mm
70+
<$> decode <$> Data.VkMess.readFile inFile
4571
writeFile "index.html" $ renderHtml $ mainHtml users chats self ms
4672
writeFile "messages.html" $ renderHtml $ standalone "All messages"
4773
$ messagesHtml users self

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extra-source-files:
1010
- README.md
1111
dependencies:
1212
- base >=4.7 && <5
13+
- mtl >=2.2 && <3
1314
- text >=1.2 && <2
1415
- unix-time >=0.3 && <1
1516
- aeson >=1.1 && <2

src/Data/VkMess.hs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Data.VkMess
1616
, Conversation(..), Conversations(..)
1717
, convTitle, convExtId
1818
, getSnapshotUrls
19+
, replaceSnapshotUrls
20+
, listToWriter
1921
) where
2022

2123
import Prelude hiding (readFile, writeFile)
@@ -29,12 +31,16 @@ import Data.Maybe (fromMaybe, fromJust, catMaybes)
2931
import Data.Bool (bool)
3032
import Data.Monoid (Sum(..))
3133

34+
import Control.Monad.Writer (Writer, tell)
35+
3236
-- For deriving Monod instances
3337
import Generics.Deriving.Monoid (memptydefault, mappenddefault)
3438

3539
import Data.Foldable (toList)
3640
import Data.Set (Set, singleton, fromList)
37-
import Data.List (sort)
41+
import Data.List (sort, maximumBy, deleteBy)
42+
import Data.Ord (comparing)
43+
import Data.Function (on)
3844
import Control.Monad (forM, liftM)
3945
import Data.Text (Text, pack)
4046
import Data.UnixTime (fromEpochTime, UnixTime)
@@ -233,3 +239,30 @@ getSnapshotUrls (Snapshot {..})
233239
attachmentUrls (Link _ _ _) = []
234240
attachmentUrls (AudioMsg x) = [x]
235241
attachmentUrls (Other _) = []
242+
243+
listToWriter :: [(FilePath, FilePath)] -> FilePath -> Writer [FilePath] FilePath
244+
listToWriter xs k = case lookup k xs of
245+
Just x -> pure x
246+
Nothing -> tell [k] >> pure k
247+
248+
replaceSnapshotUrls :: (FilePath -> Writer [FilePath] FilePath) -> Snapshot -> Writer [FilePath] Snapshot
249+
replaceSnapshotUrls m ss = do
250+
sDialogs' <- forM (sDialogs ss) $ \(conv, ms) -> do
251+
conv' <- replaceConv conv
252+
ms' <- mapM replaceMessage ms
253+
pure (conv', ms')
254+
pure $ ss { sDialogs = sDialogs' }
255+
where
256+
replaceConv (ConvUser i n p) = ConvUser i n <$> m p
257+
replaceConv x = pure x
258+
replaceMessage ms = do
259+
fwd <- mapM replaceMessage $ mFwd ms
260+
att <- mapM replaceAtt $ mAtt ms
261+
pure $ ms { mFwd = fwd, mAtt = att }
262+
replaceAtt (Photo x) = do
263+
let (i, v) = maximumBy (comparing fst) x
264+
v' <- m v
265+
pure $ Photo $ (i, v') : deleteBy ((==) `on` fst) (i, undefined) x
266+
replaceAtt (Sticker x) = Sticker <$> m x
267+
replaceAtt (AudioMsg x) = AudioMsg <$> m x
268+
replaceAtt x = pure x

0 commit comments

Comments
 (0)