google-isbn (empty) → 0.1
raw patch · 3 files changed
+110/−0 lines, 3 filesdep +aesondep +basedep +bytestringsetup-changed
Dependencies added: aeson, base, bytestring, conduit, conduit-extra, http-conduit, text
Files
- Setup.hs +2/−0
- google-isbn.cabal +39/−0
- src/Google/ISBN.hs +69/−0
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ google-isbn.cabal view
@@ -0,0 +1,39 @@+-- This file has been generated from package.yaml by hpack version 0.28.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: 3badd19264c41d5015ebcd4f9ced182d657b8fb1d40168e88a8d029e88054514++name: google-isbn+version: 0.1+description: Basic utility to search an ISBN using the Google Books webservice+category: Text, Web+homepage: https://github.com/apeyroux/google-isbn#readme+bug-reports: https://github.com/apeyroux/google-isbn/issues+author: Alexandre Peyroux+maintainer: alex@px.io+license: BSD3+build-type: Simple+cabal-version: >= 1.10++source-repository head+ type: git+ location: https://github.com/apeyroux/google-isbn++library+ exposed-modules:+ Google.ISBN+ other-modules:+ Paths_google_isbn+ hs-source-dirs:+ src+ ghc-options: -Wall+ build-depends:+ aeson+ , base >=4.2 && <5+ , bytestring+ , conduit+ , conduit-extra+ , http-conduit+ , text+ default-language: Haskell2010
+ src/Google/ISBN.hs view
@@ -0,0 +1,69 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE LambdaCase #-}++-- |+--+-- Basic utility to search an ISBN using the Google Books webservice+--+-- use: https://developers.google.com/books/docs/v1/using+--++module Google.ISBN (+ ISBN+ , GoogleISBN+ , googleISBN+ ) where++import Data.Aeson+import Data.Aeson.Types+import qualified Data.Text as T+import GHC.Generics+import Network.HTTP.Simple++newtype ISBN = ISBN T.Text++instance Show ISBN where+ show (ISBN i) = "isbn:" <> T.unpack i++data GoogleISBN = GoogleISBN {+ googleISBNTotalItems :: Integer+ , googleISBNItems :: [Book]+} deriving (Show, Generic)++data Book = Book {+ bookTitle :: T.Text+ , bookSubtitle :: T.Text+ , bookPublisher :: Maybe T.Text+ , bookDescription :: T.Text+ , bookPublishedDate :: T.Text+ , bookLanguage :: T.Text+ , bookAuthors :: Maybe [T.Text]+ } deriving (Show, Generic)++instance FromJSON Book where+ parseJSON (Object v) = (v .: "volumeInfo") >>= \b -> Book+ <$> b .: "title"+ <*> b .: "subtitle"+ <*> b .:? "publisher"+ <*> b .: "description"+ <*> b .: "publishedDate"+ <*> b .: "language"+ <*> b .: "authors"+ parseJSON invalid = typeMismatch "Book" invalid++instance FromJSON GoogleISBN where+ parseJSON (Object v) = GoogleISBN+ <$> v .: "totalItems"+ <*> v .: "items"+ parseJSON invalid = typeMismatch "APIResult" invalid++urlApi :: String+urlApi = "https://www.googleapis.com/books/v1/"++googleISBN :: ISBN -> IO (Maybe GoogleISBN)+googleISBN isbn = parseRequest (urlApi <> "volumes?q=" <> show isbn)+ >>= (\req ->+ (\r -> fromJSON r :: Result GoogleISBN) <$> ((\r -> getResponseBody r::Value) <$> httpJSON req) >>= \case+ Success x -> return $ Just x+ Error _ -> return Nothing)