haskell-google-trends 0.0.1 → 0.0.2
raw patch · 3 files changed
+60/−23 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Google.Trends: queryTrendsNoLogin :: Maybe (String, Int) -> String -> IO (Maybe [(Integer, String, Integer)])
- Google.Trends: queryTrendsWithLogin :: String -> String -> String -> IO (Maybe [(Integer, String, Integer)])
+ Google.Trends: queryTrendsWithLogin :: String -> String -> Maybe (String, Int) -> String -> IO (Maybe [(Integer, String, Integer)])
Files
- Google/Trends.hs +29/−17
- README.md +25/−5
- haskell-google-trends.cabal +6/−1
Google/Trends.hs view
@@ -1,5 +1,5 @@ {-# LANGUAGE OverloadedStrings, FlexibleContexts #-}-module Google.Trends(queryTrendsWithLogin) where+module Google.Trends(queryTrendsWithLogin, queryTrendsNoLogin) where import qualified Data.Text as T import qualified Data.Text.Encoding as TE@@ -21,12 +21,6 @@ urlTrends = "http://www.google.com/trends" urlTrendsComponent :: String urlTrendsComponent = "http://www.google.com/trends/fetchComponent"-urlTrendsReport :: String-urlTrendsReport = "http://www.google.com/trends/trendsReport"-urlCookieCheck :: String-urlCookieCheck = "https://www.google.com/accounts/CheckCookie"-urlGoogle :: String-urlGoogle = "http://www.google.com" defaultUserAgent = do bs <- getLatestBrowserString "Firefox"@@ -36,12 +30,18 @@ {- Google Login -} loginHeaders options ua = options & -{- (proxy ?~ httpProxy "localhost" 8080) &-} (header "Accept" .~ ["text/plain"]) & (header "User-Agent" .~ [ua]) & (header "Content-Type" .~ ["application/x-www-form-urlencoded"]) & (header "Referrer" .~ [urlServiceLoginBoxAuthBS]) +loginHeadersWithProxy options (host, port) ua = options &+ (proxy ?~ httpProxy (BS.pack host) port) &+ (header "Accept" .~ ["text/plain"]) &+ (header "User-Agent" .~ [ua]) &+ (header "Content-Type" .~ ["application/x-www-form-urlencoded"]) &+ (header "Referrer" .~ [urlServiceLoginBoxAuthBS])+ findLoginInputs tags = [toPair tag | tag <- tags, tag ~== TagOpen ("input" :: String) []] where toPair tag = (toStrictString (fromAttrib "name" tag), fromAttrib "value" tag)@@ -56,11 +56,13 @@ makeLoginForm newForm inputs email pass where newForm = ((key := value):form) -doLogin :: String -> String -> (Options -> Ss.Session -> IO a) -> (Options -> Ss.Session -> IO a) -> IO a-doLogin email pass continue fail = do+doLogin :: String -> String -> Maybe (String, Int) -> (Options -> Ss.Session -> IO a) -> (Options -> Ss.Session -> IO a) -> IO a+doLogin email pass maybeProxy continue fail = do Ss.withSession $ \session -> do userAgent <- defaultUserAgent- let headers = loginHeaders defaults userAgent+ let headers = case maybeProxy of+ Nothing -> loginHeaders defaults userAgent+ Just proxy -> loginHeadersWithProxy defaults proxy userAgent inputs <- getLoginInputs headers session let form = makeLoginForm [] inputs email pass resp <- Ss.postWith headers session urlServiceLoginBoxAuth form@@ -77,9 +79,11 @@ (param "cid" .~ ["TIMESERIES_GRAPH_0"]) & (param "export" .~ ["5"]) -queryTrends keywords headers session = do+queryTrends keywords headers maybeSession = do let options = queryParams headers (T.pack keywords)- resp <- Ss.getWith options session urlTrendsComponent+ resp <- case maybeSession of+ Nothing -> getWith options urlTrendsComponent+ Just session -> Ss.getWith options session urlTrendsComponent let body = resp ^. responseBody let parsed = T.unpack (TE.decodeUtf8 (toStrictString body)) return $ [processPoint point | point <- parseTrends parsed]@@ -114,11 +118,19 @@ processPoint :: [String] -> (Integer, String, Integer) processPoint (year:month:count:[]) = (read year :: Integer, monthName month, read count :: Integer) -queryTrendsWithLogin :: String -> String -> String -> IO (Maybe [(Integer, String, Integer)])-queryTrendsWithLogin email pass keywords = do- doLogin email pass continue fail+queryTrendsNoLogin :: Maybe (String, Int) -> String -> IO (Maybe [(Integer, String, Integer)])+queryTrendsNoLogin maybeProxy keywords = do+ let options = case maybeProxy of+ Nothing -> defaults + Just (host, port) -> defaults & (proxy ?~ httpProxy (BS.pack host) port)+ results <- queryTrends keywords options Nothing+ return $ Just results++queryTrendsWithLogin :: String -> String -> Maybe (String, Int) -> String -> IO (Maybe [(Integer, String, Integer)])+queryTrendsWithLogin email pass maybeProxy keywords = do+ doLogin email pass maybeProxy continue fail where continue h s = do - results <- queryTrends keywords h s+ results <- queryTrends keywords h (Just s) return $ Just results fail _ _ = return Nothing
README.md view
@@ -3,19 +3,39 @@ Very simple library for accessing Google Trends +INSTALL+-------++`cabal install haskell-google-trends`+ HOW TO USE ---------- -Currently library exports only one function.+Currently library exports two functions.+Both return maybe a list of tuples of form (Year, Month, Value). -### queryTrendsWithLogin :: String -> String -> String -> IO (Maybe [(Integer, String, Integer)])-`Returns maybe a list of tuples of form (Year, Month, Value)`+```haskell +queryTrendsWithLogin :: String -> String -> Maybe (String, Int) -> String -> IO (Maybe [(Integer, String, Integer)])+queryTrendsWithLogin email password maybeProxy keywords -**Example:**+queryTrendsNoLogin :: Maybe (String, Int) -> String -> IO (Maybe [(Integer, String, Integer)])+queryTrendsNoLogin maybeProxy keywords+```++**Example (No proxy):** ```haskell import Google.Trends main = do- Just results <- queryTrendsWithLogin "your-login@gmail.com" "password" "pizza"+ Just results <- queryTrendsWithLogin "your-login@gmail.com" "password" Nothing "pizza"+ print [(month, value) | (year, month, value) <- results, year == 2010]+```++**Example (With proxy):**+```haskell+import Google.Trends++main = do+ Just results <- queryTrendsNoLogin (Just ("54.153.7.21", 8083)) "pizza" print [(month, value) | (year, month, value) <- results, year == 2010] ```
haskell-google-trends.cabal view
@@ -2,17 +2,22 @@ -- further documentation, see http://haskell.org/cabal/users-guide/ name: haskell-google-trends-version: 0.0.1+version: 0.0.2 synopsis: Simple library for accessing Google Trends license: PublicDomain license-file: LICENSE author: grzegorzgoldapl maintainer: contact@grzegorzgolda.com+homepage: https://github.com/grzegorzgoldapl/haskell-google-trends -- copyright: category: Web build-type: Simple extra-source-files: README.md cabal-version: >=1.10++source-repository head+ type: git+ location: git://github.com/grzegorzgoldapl/haskell-google-trends.git library exposed-modules: Google.Trends