Commit f0a8c2e6 authored by Mudada's avatar Mudada

create / delete directory

parent a51250b8
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
module Main where module Main where
import SEARX import SEARX
import System.Directory
main :: IO () main :: IO ()
main = do main = do
...@@ -10,5 +11,7 @@ main = do ...@@ -10,5 +11,7 @@ main = do
case res of case res of
(Left err) -> print err (Left err) -> print err
(Right r) -> do (Right r) -> do
a <- parseWebsite "https://www.lemonde.fr/economie/article/2019/09/23/thomas-cook-semble-se-diriger-vers-une-faillite_6012630_3234.html" fp <- setUpDirectory
print a article <- parseWebsite fp "https://www.lemonde.fr/politique/article/2019/10/07/apres-l-attentat-a-la-prefecture-de-police-christophe-castaner-sous-le-feu-des-critiques_6014514_823448.html"
removeDirectoryRecursive $ fp <> "/readability"
print article
...@@ -50,26 +50,35 @@ data Article = ...@@ -50,26 +50,35 @@ data Article =
} }
deriving (Generic, Show, FromJSON, ToJSON) deriving (Generic, Show, FromJSON, ToJSON)
parseWebsite :: T.Text -> IO Article setUpDirectory :: IO FilePath
parseWebsite url = setUpDirectory =
withSystemTempDirectory "" $ \tmpdir -> do do
withCurrentDirectory tmpdir $ dir <- getCurrentDirectory
_ <- withCurrentDirectory dir $
traverse_ traverse_
callCommand callCommand
["npm init --yes", "npm install --save jsdom request-promise-native", [ "npm init --yes"
"git clone https://github.com/mozilla/readability.git"] , "npm install --save jsdom request-promise-native"
, "git clone https://github.com/mozilla/readability.git"
]
getCurrentDirectory
parseWebsite :: FilePath -> T.Text -> IO Article
parseWebsite tmpdir url =
do
withJSSession withJSSession
defJSSessionOpts {nodeWorkDir = Just tmpdir} defJSSessionOpts {nodeWorkDir = Just tmpdir}
[block| [block|
var {JSDOM} = require('jsdom'); var {JSDOM} = require('jsdom');
var rp = require('request-promise-native'); var rp = require('request-promise-native');
var Readability = require('./readability'); var Readability = require('./readability');
const val = await rp($url); const val = await rp($url);
const doc = new JSDOM(val, {url: $url}) const doc = new JSDOM(val, {url: $url})
const reader = new Readability(doc.window.document); const reader = new Readability(doc.window.document);
const article = reader.parse(); const article = reader.parse();
return article; return article;
|] |]
specConcatEith :: Semigroup a => Either b a -> Either b a -> Either b a specConcatEith :: Semigroup a => Either b a -> Either b a -> Either b a
specConcatEith (Left _) b = b specConcatEith (Left _) b = b
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment