Name: Anonymous 2013-06-13 0:37
how would you go about getting the current post number for any board?
E.G. current post number: 1234567
E.G. current post number: 1234567
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP
import Control.Applicative
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as BS
data Board = Board { getCode :: String
, getTitle :: String }
instance FromJSON Board where
parseJSON (Object v) = Board <$> v .: "board" <*> v .: "title"
data Thread = Thread { getPosts :: [Post] }
instance FromJSON Thread where
parseJSON (Object v) = Thread <$> v .: "posts"
data Post = Post { getNo :: Int }
instance FromJSON Post where
parseJSON (Object v) = Post <$> v .: "no"
data BoardListWrapper = BoardListWrapper { getBoards :: [Board] }
instance FromJSON BoardListWrapper where
parseJSON (Object v) = BoardListWrapper <$> v .: "boards"
data ThreadListWrapper = ThreadListWrapper { getThreads :: [Thread] }
instance FromJSON ThreadListWrapper where
parseJSON (Object v) = ThreadListWrapper <$> v .: "threads"
getFirstPage :: Board -> IO [Thread]
getFirstPage board = do
let boardURL = "http://api.4chan.org/" ++ getCode board ++ "/0.json"
response <- simpleHTTP $ getRequest boardURL
pageJSON <- BS.pack <$> getResponseBody response
let Just (ThreadListWrapper threads) = decode pageJSON
return $ threads
newestPostNo :: [Thread] -> Int
newestPostNo = maximum . map getNo . concatMap getPosts
showBoard :: Board -> String
showBoard (Board code title) =
"/" ++ code ++ "/ (" ++ title ++ ")"
logPostCount :: Board -> IO ()
logPostCount board = do
firstPage <- getFirstPage board
let no = newestPostNo firstPage
putStrLn $ showBoard board ++ ": " ++ show no
main = do
response <- simpleHTTP $ getRequest "http://api.4chan.org/boards.json"
boardsJSON <- BS.pack <$> getResponseBody response
let Just (BoardListWrapper boards) = decode boardsJSON
mapM_ logPostCount boards/3/ (3DCG): 360414
/a/ (Anime & Manga): 87433417
/adv/ (Advice): 12453752
/an/ (Animals & Nature): 1443140
/asp/ (Alternative Sports): 129302
/b/ (Random): 486741149
/c/ (Anime/Cute): 2049061
/cgl/ (Cosplay & EGL): 6907731
/ck/ (Food & Cooking): 4558984
/cm/ (Cute/Male): 2854122
/co/ (Comics & Cartoons): 50656893
/d/ (Hentai/Alternative): 4834760
/diy/ (Do-It-Yourself): 473491
/e/ (Ecchi): 1586890
/f/ (Flash): 2002124
/fa/ (Fashion): 6325346
/fit/ (Health & Fitness): 20613826
/g/ (Technology): 34543182
/gd/ (Graphic Design): 78434
/gif/ (Adult GIF): 5701797
/h/ (Hentai): 3541636
/hc/ (Hardcore): 402757
/hm/ (Handsome Men): 684083
/hr/ (High Resolution): 1844908
/i/ (Oekaki): 406604
/ic/ (Artwork/Critique): 1471184
/int/ (International): 11786817
/jp/ (Otaku Culture): 11034169
/k/ (Weapons): 16723015
/lgbt/ (LGBT): 737015
/lit/ (Literature): 3847955
/m/ (Mecha): 9225332
/mlp/ (Pony): 11344780
/mu/ (Music): 37120586
/n/ (Transportation): 505361
/o/ (Auto): 8284237
/out/ (Outdoors): 138067
/p/ (Photography): 2017804
/po/ (Papercraft & Origami): 495785
/pol/ (Politically Incorrect): 15505706
/q/ (4chan Discussion): 638854
/r/ (Request): 11421474
/r9k/ (ROBOT9001): 7321862
/s/ (Sexy Beautiful Women): 14298497
/s4s/ (Shit 4chan Says): 449467
/sci/ (Science & Math): 5831482
/soc/ (Social): 16380812
/sp/ (Sports): 36541744
/t/ (Torrents): 565782
/tg/ (Traditional Games): 25415332
/toy/ (Toys): 3589777
/trv/ (Travel): 730559
/tv/ (Television & Film): 34098782
/u/ (Yuri): 1405804
/v/ (Video Games): 194528496
/vg/ (Video Game Generals): 38370695
/vp/ (Pok‚mon): 12655743
/vr/ (Retro Games): 796848
/w/ (Anime/Wallpapers): 1529962
/wg/ (Wallpapers/General): 5380477
/wsg/ (Worksafe GIF): 186235
/x/ (Paranormal): 12812735
/y/ (Yaoi): 2040189