Skip to content

Instantly share code, notes, and snippets.

@luislee818
Last active February 8, 2018 18:41
Show Gist options
  • Save luislee818/d33e1dc885287e4ff09016dcf005b6f5 to your computer and use it in GitHub Desktop.
Save luislee818/d33e1dc885287e4ff09016dcf005b6f5 to your computer and use it in GitHub Desktop.
Download free PragPub issues from 2009.07 to 2013.07
import Text.Printf
import System.Process
-- Downloads free PragPub issues from 2009.07 to 2013.07 from https://pragprog.com/magazines
-- relies on 'wget' command for downloading
-- run `processAll` to download
-- constants
savePath = "/Users/Dapeng/Downloads/PragPub/"
startYear = 2009
startMonth = 6
allIssues = [1..49]
allFormats = ["pdf", "epub", "mobi"]
-- functions
processAll :: IO ()
processAll =
let
everything = [(num, fmt) | num <- allIssues, fmt <- allFormats]
in
sequence_ . map (\issue -> process issue >>= putStrLn) $ everything
process :: (Integer, String) -> IO String
process = download . parse
parse :: (Integer, String) -> (Integer, String, String)
parse (num, fmt) =
(num, fmt, toLongName num fmt)
toLongName :: Integer -> String -> String
toLongName num fmt =
printf "PragPub - %s.%s" (toYearMonth num) fmt
toYearMonth :: Integer -> String
toYearMonth num =
let
d = (startMonth + num) `div` 12
r = (startMonth + num) `mod` 12
(year, month) =
case r of
0 -> (startYear + d, 12)
_ -> (startYear + d, r)
in
show year ++ "." ++ printf "%02d" month
download :: (Integer, String, String) -> IO String
download (num, fmt, name) =
let
url = printf "https://pragprog.com/magazines/download/%d.%s" num fmt :: String
path = savePath ++ name
in
readProcess "wget" [url, "-O", path] ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment