<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Main (
  main

-- exported for testing
, parse
, parseMany
) where

import           Data.Foldable (forM_)
import           System.IO (hPutStrLn, stderr)
import           System.Time (getClockTime)
import           Control.Concurrent (threadDelay)
import qualified Data.ByteString.Lazy.Char8 as L
import           Control.Monad.IO.Class
import           Data.Aeson.Generic
import           Data.Conduit
import           Network.HTTP.Types
import           Network.HTTP.Conduit

import           Parse

-- | Output file.
jsonFile :: FilePath
jsonFile = "log.json"

-- | URL to Hackage upload log.
uploadLogUrl :: String
uploadLogUrl = "http://hackage.haskell.org/packages/archive/log"

-- |
-- Download the Hackage upload log, parse it, and write latest package versions
-- into a JSON file.
--
-- Repeatedly check if the upload log has changed, and if so, regenerate the
-- JSON file.
main :: IO ()
main = go ""
  where
    go etag = do
      (e, r) <- update etag
      forM_ r (L.writeFile jsonFile . encode . parseMany)

      -- sleep for 60 seconds
      threadDelay 60000000
      go e

    update :: Ascii -> IO (Ascii, Maybe L.ByteString)
    update etag = withManager $ \manager -> do
      e <- getEtag manager
      if etag == e
        then do<commit_msg>Change output format to JSONP<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Main (
  main

-- exported for testing
, parse
, parseMany
) where

import           Data.Foldable (forM_)
import           System.IO (hPutStrLn, stderr)
import           System.Time (getClockTime)
import           Control.Concurrent (threadDelay)
import qualified Data.ByteString.Lazy.Char8 as L
import           Control.Monad.IO.Class
import           Data.Aeson.Generic
import           Data.Conduit
import           Network.HTTP.Types
import           Network.HTTP.Conduit

import           Parse

-- | Output file.
file :: FilePath
file = "hackage-package-versions.jsonp"

-- | URL to Hackage upload log.
uploadLogUrl :: String
uploadLogUrl = "http://hackage.haskell.org/packages/archive/log"

-- | Parse given data, and write it as JSONP.
writeJSONP :: L.ByteString -> IO ()
writeJSONP = L.writeFile file . addPadding . encode . parseMany
  where
    addPadding = L.append "hackagePackageVersions="

-- |
-- Download the Hackage upload log, parse it, and write latest package versions
-- into a JSONP file.
--
-- Repeatedly check if the upload log has changed, and if so, regenerate the
-- JSONP file.
main :: IO ()
main = go ""
  where
    go etag = do
      (e, r) <- update etag
      forM_ r writeJSONP

      -- sleep for 60 seconds
      threadDelay 60000000
      go e

    update :: Ascii -> IO (Ascii, Maybe L.ByteString)
    update etag = withManager $ \manager -> do
      e <- getEtag manager
      if etag == e
        then do
{'fraction_non_alphanumeric': 0.09355290819901892, 'fraction_numerical': 0.007708479327259986, 'mean_word_length': 2.347010550996483, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7494485', 'n_tokens_mistral': 893, 'n_tokens_neox': 850, 'n_words': 287}