forked from ierton/vkhs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
find-reposts.hs
executable file
·70 lines (48 loc) · 1.73 KB
/
find-reposts.hs
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
#!/usr/bin/env runhaskell
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
import Web.VKHS
import Web.VKHS.Imports
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.HashSet (HashSet)
import qualified Data.HashSet as HashSet
type WallDescriptor = (Int,Int)
post_id :: WallDescriptor
post_id = (-6222142,98078)
wallDesc :: WallRecord -> WallDescriptor
wallDesc WallRecord{..} = (wr_owner_id,wr_id)
printUrl :: (MonadIO m) => WallDescriptor -> m ()
printUrl (o,i) = tputStrLn $ "https://vk.com/im?w=wall" <> tshow o <> "_" <> tshow i
makeUrl :: WallDescriptor -> Text
makeUrl (o,i) = "https://vk.com/im?w=wall" <> tshow o <> "_" <> tshow i
whileM :: (Monad m) => m Bool -> m ()
whileM m = do
x <- m
case x of
True -> return ()
False -> whileM m
-- FIXME: return many invalid posts
main :: IO ()
main = runVK_ defaultOptions { o_verbosity = Normal } $ do
Just wr0 <- getWallById post_id
flip execStateT (HashSet.empty, [post_id]) $ do
whileM $ do
(visited,frontier) <- get
case frontier of
[] -> return True {- break -}
(pid:frontier1) -> do
tputStrLn $ makeUrl pid
mwr <- lift $ getWallById pid
case mwr of
Nothing -> do
put (visited, frontier1)
return True
Just wr -> do
wr1 <- map wallDesc <$> pure (wr_copy_history wr)
wr2 <- map wallDesc <$> lift (getWallReposts wr)
frontier2 <- pure $
HashSet.toList (HashSet.fromList (wr1 <> wr2) `HashSet.difference` visited)
put ( HashSet.insert (wallDesc wr) visited
, frontier1 <> frontier2)
return False