|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +module SearchRepos where |
| 3 | + |
| 4 | +import qualified Github.Search as Github |
| 5 | +import qualified Github.Data as Github |
| 6 | +import Control.Monad (forM,forM_) |
| 7 | +import Data.Maybe (fromMaybe) |
| 8 | +import Data.List (intercalate) |
| 9 | +import System.Environment (getArgs) |
| 10 | +import Text.Printf (printf) |
| 11 | +import Data.Time.Clock (getCurrentTime, UTCTime(..)) |
| 12 | +import Data.Time.LocalTime (utc,utcToLocalTime,localDay,localTimeOfDay,TimeOfDay(..)) |
| 13 | +import Data.Time.Calendar (toGregorian) |
| 14 | + |
| 15 | +main = do |
| 16 | + args <- getArgs |
| 17 | + date <- case args of |
| 18 | + (x:_) -> return x |
| 19 | + otherwise -> today |
| 20 | + let query = "q=language%3Ahaskell created%3A>" ++ date ++ "&per_page=100" |
| 21 | + let auth = Nothing |
| 22 | + result <- Github.searchRepos' auth query |
| 23 | + case result of |
| 24 | + Left e -> putStrLn $ "Error: " ++ show e |
| 25 | + Right r -> do forM_ (Github.searchReposRepos r) (\r -> do |
| 26 | + putStrLn $ formatRepo r |
| 27 | + putStrLn "" |
| 28 | + ) |
| 29 | + putStrLn $ "Count: " ++ show n ++ " Haskell repos created since " ++ date |
| 30 | + where n = Github.searchReposTotalCount r |
| 31 | + |
| 32 | +-- | return today (in UTC) formatted as YYYY-MM-DD |
| 33 | +today :: IO String |
| 34 | +today = do |
| 35 | + now <- getCurrentTime |
| 36 | + let day = localDay $ utcToLocalTime utc now |
| 37 | + (y,m,d) = toGregorian day |
| 38 | + in return $ printf "%d-%02d-%02d" y m d |
| 39 | + |
| 40 | +formatRepo :: Github.Repo -> String |
| 41 | +formatRepo r = |
| 42 | + let fields = [ ("Name", Github.repoName) |
| 43 | + ,("URL", Github.repoHtmlUrl) |
| 44 | + ,("Description", orEmpty . Github.repoDescription) |
| 45 | + ,("Created-At", formatDate . Github.repoCreatedAt) |
| 46 | + ,("Pushed-At", formatMaybeDate . Github.repoPushedAt) |
| 47 | + ] |
| 48 | + in intercalate "\n" $ map fmt fields |
| 49 | + where fmt (s,f) = fill 12 (s ++ ":") ++ " " ++ f r |
| 50 | + orEmpty = fromMaybe "" |
| 51 | + fill n s = s ++ replicate n' ' ' |
| 52 | + where n' = max 0 (n - length s) |
| 53 | + |
| 54 | +formatMaybeDate = maybe "???" formatDate |
| 55 | + |
| 56 | +formatDate = show . Github.fromGithubDate |
0 commit comments