<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Einstein (scrapEinstein) where

import Network.HTTP
import Text.HTML.TagSoup
import Data.List.Split (splitEvery)
import Control.Monad (guard, liftM2)
import Data.Char (isNumber)
import Data.Maybe (listToMaybe)
import qualified Data.Text as T

-- I use tag soup as it apperently fixes unicode characters

openURL :: String -> IO String
openURL x = getResponseBody =<< simpleHTTP (getRequest x)

scrapEinstein :: IO (Maybe (Int, [[String]]))
scrapEinstein = do
    body <- openURL "http://www.butlercatering.se/einstein.html"
    return (parse body)
                   
printMenuToStdout :: IO()
printMenuToStdout = do
    Just (week, sss) <- scrapEinstein 
    putStrLn $ "\nVecka " ++ show week ++ ":"
    mapM_ putStrLn $ map unlines sss

parse :: String -> Maybe (Int, [[String]])
parse body = liftM2 (,) mweek mmenu
  where
    tags = parseTags body
    goodContents = filter (elem '•') [ s | TagText s <- tags ]
    cleanContents = map (dropWhile (`elem` [' ', '•', '\n', '\r'])) goodContents
    splitted = splitEvery 3  cleanContents
    mmenu = guard (map length splitted == [3, 3, 3, 3, 3]) >> Just splitted
    textNum = T.takeWhile isNumber $ snd $ T.breakOnEnd "Meny V " (T.pack body)<commit_msg>hlint on Einstein.hs<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Einstein (scrapEinstein) where

import Network.HTTP
import Text.HTML.TagSoup
import Data.List.Split (splitEvery)
import Control.Monad (guard, liftM2)
import Data.Char (isNumber)
import Data.Maybe (listToMaybe)
import qualified Data.Text as T

-- I use tag soup as it apperently fixes unicode characters

openURL :: String -> IO String
openURL x = getResponseBody =<< simpleHTTP (getRequest x)

scrapEinstein :: IO (Maybe (Int, [[String]]))
scrapEinstein = do
    body <- openURL "http://www.butlercatering.se/einstein.html"
    return (parse body)
                   
printMenuToStdout :: IO()
printMenuToStdout = do
    Just (week, sss) <- scrapEinstein 
    putStrLn $ "\nVecka " ++ show week ++ ":"
    mapM_ (putStrLn . unlines) sss

parse :: String -> Maybe (Int, [[String]])
parse body = liftM2 (,) mweek mmenu
  where
    tags = parseTags body
    goodContents = filter (elem '•') [ s | TagText s <- tags ]
    cleanContents = map (dropWhile (`elem` " •\n\r")) goodContents
    splitted = splitEvery 3  cleanContents
    mmenu = guard (map length splitted == [3, 3, 3, 3, 3]) >> Just splitted
    textNum = T.takeWhile isNumber $ snd $ T.breakOnEnd "Meny V " (T.pack body)
{'fraction_non_alphanumeric': 0.12544660579595077, 'fraction_numerical': 0.006351726875744343, 'mean_word_length': 3.573502722323049, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3650582', 'n_tokens_mistral': 857, 'n_tokens_neox': 809, 'n_words': 266}