imgurder (empty) → 1.0
raw patch · 4 files changed
+133/−0 lines, 4 filesdep +basedep +curldep +directorysetup-changed
Dependencies added: base, curl, directory, haskell98, hxt, hxt-xpath
Files
- LICENSE +26/−0
- Setup.hs +2/−0
- imgurder.cabal +18/−0
- imgurder.hs +87/−0
+ LICENSE view
@@ -0,0 +1,26 @@+Dan Colish <dcolish@gmail.com>+Copyright (c) 2010+All rights reserved++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:+ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+ * Neither the name of the <organization> nor the+ names of its contributors may be used to endorse or promote products+ derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY Daniel Colish ''AS IS'' AND ANY+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL Daniel Colish BE LIABLE FOR ANY+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ imgurder.cabal view
@@ -0,0 +1,18 @@+Name: imgurder+Version: 1.0+License: BSD3+License-file: LICENSE+Author: Dan Colish+Maintainer: dcolish@gmail.com+Build-Type: Simple+Cabal-Version: >=1.2+Synopsis: Uploader for Imgur+Category: Web+Description: A simple uploader for the Imgur api. You should register+ for an api key++Executable imgurder+ Main-is: imgurder.hs+ Build-Depends: base >= 3 && < 5, hxt-xpath >= 8.5.4, hxt >= 8.3.2,+ curl >= 1.3.5, directory >= 1.0.0.3, haskell98 >= 1+
+ imgurder.hs view
@@ -0,0 +1,87 @@+--+-- Imgur Uploader+-- Dan Colish <dcolish@gmail.com>+-- Copyright (c) 2010+-- All rights reserved+--+-- Usage: runghc imgur.hs <filepath>+-- Be sure to add an api key before using it and put it in ~/imgurder+-- A sample configuration file would be:+-- `echo '<your api key>' > ~/.imgurder`+--++import Data.IORef+import Data.List+import Data.Maybe+import Data.Tree.Class+import Network.Curl+import System (getArgs)+import System.Directory+import System.IO+import Text.XML.HXT.DOM.TypeDefs+import qualified Text.XML.HXT.DOM.XmlNode as N+import qualified Text.XML.HXT.Parser.XmlParsec as H+import qualified Text.XML.HXT.XPath.XPathEval as X+++myCurlPost :: String -> String -> [HttpPost]+myCurlPost apikey myImage =+ [ HttpPost { postName = "image"+ , contentType = Nothing , content = ContentFile myImage+ , extraHeaders = []+ , showName = Nothing },+ HttpPost { postName = "key"+ , contentType = Nothing+ , content = ContentString apikey+ , extraHeaders = []+ , showName = Nothing }]+++pathTags :: [String]+pathTags = [ "/rsp/image_hash"+ ,"/rsp/delete_hash"+ ,"/rsp/original_image"+ ,"/rsp/large_thumbnail"+ ,"/rsp/small_thumbnail"+ ,"/rsp/imgur_page"+ ,"/rsp/delete_page" ]+++xpathQN:: String -> XmlTree -> String+xpathQN str = fromJust . N.getQualifiedName . getNode . head . X.getXPath str+++xpathTxt:: String -> XmlTree -> String+xpathTxt str = fromJust . N.getText . head . getChildren . head . X.getXPath str+++keyVal :: XmlTree -> String -> String+keyVal res str = unwords [(xpathQN str res), " = ", (xpathTxt str res)]+++formattedResult :: XmlTree -> [String]+formattedResult res = map (keyVal res) pathTags+++loadConf :: IO String+loadConf = do+ h <- getHomeDirectory+ key <- readFile (h ++ "/.imgurder")+ return key+++main :: IO ()+main = withCurlDo $ do+ [file] <- getArgs+ key <- loadConf+ initialize+ ref <- newIORef []+ curlMultiPost "http://imgur.com/api/upload.xml"+ [CurlWriteFunction (gatherOutput ref), CurlVerbose False]+ $ myCurlPost key file+ response <- fmap reverse $ readIORef ref+ putStrLn "== Imgur Upload Complete ==\n"+ putStrLn . unlines . formattedResult . result $ response+ where+ result = head . H.xread . concatMap (unwords.tail.lines)+