Yuri Bochkarev avatar Yuri Bochkarev committed 3342ef5

* duplicated code reduced and partially generalised in Engine module

Comments (0)

Files changed (8)

notes/features.txt

         [_] -c config
     [+] logging
         [+] add Log module with msgDebug, msgInfo functions
+    [_] learn oauth authorization principles
 
 [_] engines
     [+] add imm.io
     [-] add tinypic.com (seems to be old (2009), full of flash and with captcha)
     [+] add imgur.com (auth, apikey=420de151712e1f55f03221c4939c2080)
     [+] add scrin.org
-    [_] add minus.com (http://minus.com/pages/api)
+    [-] add minus.com (http://minus.com/pages/api, no anonymous API access)
     [_] add yandex.fotki
-    [_] add twitpic.com (http://dev.twitpic.com)
+    [_] add twitpic.com (http://dev.twitpic.com, apikey=a1a5b6fe0ab2f61cbf88c7cb4fc4728b)
 
     [+] fastpic
     [+] flashtux
   
   cookiesRemoveSet,
   mergeCookies,
-  
+
+  uploadAndGrabHtml,
+  saveFirstLink,
+  saveFirstLinkExtended,
+
   PasteHandler,
   PasteContext(..),
   PasteContextMap,
   InputField(..),
   InputFields,
-  EncodingType(..)
+  EncodingType(..),
+  LinkFilterType(..)
   
   ) where
 
 
 import qualified Tools
 import qualified Proxy
+import qualified Processing
 import qualified Configuration
 import Log (msgDebug, msgInfo, dumpString)
 
   let newState = state { pcContents = fileContent, pcFileName = filename }
   (_, resultContext) <- runStateT (runReaderT (runPasteHandlerA handler) config) newState
   return $ pcResultLink resultContext
+
+-- | Useful built-in helpers which solve common paste problems
+
+data LinkFilterType = FileName | FileExtension | FileEmpty
+
+getFilter :: PasteContext -> LinkFilterType -> String
+getFilter context FileName      = Tools.fileName (pcFileName context)
+getFilter context FileExtension = Tools.fileExtension (pcFileName context) ++ "$"
+getFilter context FileEmpty     = ""
+
+-- Grabs links from page according to given field names and regexp
+uploadAndGrabHtml :: String
+                  -> String
+                  -> LinkFilterType
+                  -> PasteHandler [String]
+uploadAndGrabHtml attr value flt = do
+  context <- get
+  response <- liftIO $ sendPostWithFile context
+  return $ Processing.extractLinks (rspBody response) attr value $ getFilter context flt
+
+-- Save first link of the input as it is
+saveFirstLink :: [String] -> Engine.PasteHandler ()
+saveFirstLink = saveFirstLinkExtended "" ""
+
+-- Save first link with possible prefix and postfix
+saveFirstLinkExtended :: String -> String -> [String] -> Engine.PasteHandler ()
+saveFirstLinkExtended prefix postfix links = do
+  context <- get
+  case links of
+    (link:_) -> put context { Engine.pcResultLink = Just $ concat [prefix, link, postfix] } >> return ()
+    _        -> return ()

src/EngineFlashtux.hs

 module EngineFlashtux (config, handler) where
 
-import Data.Maybe
-import Network.HTTP.Headers
-import Network.HTTP
-import Control.Monad.State (modify, get, put)
-import Control.Monad.IO.Class (liftIO)
-
 import qualified Data.Map as Map
 
 import qualified Engine
-import qualified Tools
-import qualified Processing
 
 flashtuxUploadUrl = "http://img.flashtux.org/index.php"
 
   }
 
 handler :: Engine.PasteHandler ()
-handler = do
-  context <- get
-  response <- liftIO $ Engine.sendPostWithFile context
-  let link = Processing.extractLink (rspBody response) "a" "href" $ Tools.fileExtension (Engine.pcFileName context) ++ "$"
-  case link of
-    Nothing -> return ()
-    Just x -> put context { Engine.pcResultLink = link } >> return ()
+handler = Engine.uploadAndGrabHtml
+            "a" "href"
+            Engine.FileExtension
+            >>= Engine.saveFirstLink

src/EngineImagebin.hs

 module EngineImagebin (config, handler) where
 
-import Data.Maybe
-import Network.HTTP.Headers
-import Network.HTTP
-import Control.Monad.State (modify, get, put)
-import Control.Monad.IO.Class (liftIO)
-
 import qualified Data.Map as Map
 
 import qualified Engine
-import qualified Tools
-import qualified Processing
 
 imagebinUploadUrl = "http://imagebin.org/index.php"
 
   }
 
 handler :: Engine.PasteHandler ()
-handler = do
-  context <- get
-  response <- liftIO $ Engine.sendPostWithFile context
-  let link = Processing.extractLink (rspBody response) "img" "src" ""
-  case link of
-    Nothing -> return ()
-    Just x -> put context { Engine.pcResultLink = link } >> return ()
+handler = Engine.uploadAndGrabHtml
+            "img" "src"
+            Engine.FileEmpty
+            >>= Engine.saveFirstLinkExtended "http://imagebin.org" ""

src/EngineIpicture.hs

 module EngineIpicture (config, handler) where
 
-import Data.Maybe
-import Network.HTTP.Headers
-import Network.HTTP
-import Control.Monad.State (modify, get, put)
-import Control.Monad.IO.Class (liftIO)
-
 import qualified Data.Map as Map
 
 import qualified Engine
-import qualified Tools
-import qualified Processing
 import qualified EngineFastpic
 
 ipictureUploadUrl = "http://ipicture.ru/Upload/"

src/EngineOmpldr.hs

 module EngineOmpldr (config, handler) where
 
-import Data.Maybe
-import Network.HTTP.Headers
-import Network.HTTP
-import Control.Monad.State (modify, get, put)
-import Control.Monad.IO.Class (liftIO)
-
 import qualified Data.Map as Map
 
 import qualified Engine
-import qualified Tools
-import qualified Processing
 
 ompldrUploadUrl = "http://ompldr.org/upload"
 
   }
 
 handler :: Engine.PasteHandler ()
-handler = do
-  context <- get
-  response <- liftIO $ Engine.sendPostWithFile context
-  let link = Processing.extractLink (rspBody response) "a" "href" $ Tools.fileName (Engine.pcFileName context)
-  case link of
-    Nothing -> return ()
-    Just x -> put context { Engine.pcResultLink = Just ("http://ompldr.org" ++ x) } >> return ()
+handler = Engine.uploadAndGrabHtml
+            "a" "href"
+            Engine.FileName
+            >>= Engine.saveFirstLinkExtended "http://ompldr.org" ""

src/EngineRadikal.hs

   put authorizedContext
   
 sendFile :: Engine.PasteHandler ()
-sendFile = do
-  context <- get
-
-  liftIO $ msgDebug "Sending post with file"
-  liftIO $ msgDebug $ show context
-
-  response <- liftIO $ Engine.sendPostWithFile context
-  let link = Processing.extractLink (rspBody response) "input" "value" $ Tools.fileExtension (Engine.pcFileName context) ++ "$"
-  case link of
-    Nothing -> return ()
-    Just x -> put context { Engine.pcResultLink = link } >> return ()
+sendFile = 
+  Engine.uploadAndGrabHtml
+    "input" "value"
+    Engine.FileExtension
+    >>= Engine.saveFirstLink

src/EngineScrin.hs

   put newContext
 
 sendFile :: Engine.PasteHandler ()
-sendFile = do
-  context <- get
-
-  liftIO $ msgDebug "Sending post with file"
-  liftIO $ msgDebug $ show context
-
-  response <- liftIO $ Engine.sendPostWithFile context
-  let links = Processing.extractLinks (rspBody response) "input" "value"
-              $ Tools.fileExtension (Engine.pcFileName context) ++ "$"
-  liftIO $ msgDebug $ show "links: " ++ show links
-
-  case reverse links of
-    (link:_) -> put context { Engine.pcResultLink = Just link } >> return ()
-    _        -> return ()
+sendFile = Engine.uploadAndGrabHtml
+             "input" "value"
+             Engine.FileExtension
+             >>= (Engine.saveFirstLink . reverse)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.