Commits

Yuri Bochkarev committed 50831a1

+ added scrin.org support

Comments (0)

Files changed (10)

notes/features.txt

     [+] send post in rghost
     [+] add support for UrlEncoded format
 
-    [_] pcLoginSupported in PasteContext
-    [_] pcCredentialsAvailable in PasteContext
+    [_] check if auth is available for the engine
+        [+] Configuration.getEngineAuth
+        [-] pcLoginSupported in PasteContext
+        [-] pcCredentialsAvailable in PasteContext
 
-        tells the engine if available credentials can be passed to the engine
-        supportsLogin=True credentialsAvailable=True = authenticated paste
-        supportsLogin=True credentialsAvailable=False = anonymous post
-        supportsLogin=False credentialsAvailable=False = anonymous post
-        supportsLogin=False credentialsAvailable=True = report error: login is not supported for this engine
+            tells the engine if available credentials can be passed to the engine
+            supportsLogin=True credentialsAvailable=True = authenticated paste
+            supportsLogin=True credentialsAvailable=False = anonymous post
+            supportsLogin=False credentialsAvailable=False = anonymous post
+            supportsLogin=False credentialsAvailable=True = report error: login is not supported for this engine
         
     [_] handle invalid login information in engines
     [_] simplify login handlers
     [_] add yfrog.com
     [_] add tinypic.com
     [_] add imgur.com
-    [_] add scrin.org
+    [+] add scrin.org
+
+    [+] fastpic
+    [+] flashtux
+    [+] imagebin
+    [+] ipicture
+    [+] ompldr
+    [+] radikal (auth)
+    [+] rghost (auth)
 
 [+] configuration
     [_] read HTTP proxy info from config

src/EngineFastpic.hs

   context <- get
   -- download Refresh page and parse it
   refreshResponse <- liftIO $ Engine.fetch (getRequest url) False
-  link <- liftIO $ Processing.extractLinks refreshResponse "input" "value" $ Tools.fileExtension (Engine.pcFileName context) ++ "$"
+  let link = Processing.extractLink (rspBody refreshResponse) "input" "value" $ Tools.fileExtension (Engine.pcFileName context) ++ "$"
   case link of
     Nothing -> return ()
     Just x -> put context { Engine.pcResultLink = link } >> return ()

src/EngineFlashtux.hs

 handler = do
   context <- get
   response <- liftIO $ Engine.sendPostWithFile context
-  link <- liftIO $ Processing.extractLinks response "a" "href" $ Tools.fileExtension (Engine.pcFileName 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 ()

src/EngineImagebin.hs

 handler = do
   context <- get
   response <- liftIO $ Engine.sendPostWithFile context
-  link <- liftIO $ Processing.extractLinks response "img" "src" ""
+  let link = Processing.extractLink (rspBody response) "img" "src" ""
   case link of
     Nothing -> return ()
     Just x -> put context { Engine.pcResultLink = link } >> return ()

src/EngineOmpldr.hs

 handler = do
   context <- get
   response <- liftIO $ Engine.sendPostWithFile context
-  link <- liftIO $ Processing.extractLinks response "a" "href" $ Tools.fileName (Engine.pcFileName 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 ()

src/EngineRadikal.hs

 
 handler :: Engine.PasteHandler ()
 handler = do
-  getLoginPage >> auth >> sendFile
+  config <- ask
+  returnHandler (Configuration.getEngineAuth config "radikal") where
+    returnHandler Nothing = getLoginPage >> sendFile
+    returnHandler _       = getLoginPage >> auth >> sendFile
 
 -- | Retrieves UID and SID from login page
 getLoginPage :: Engine.PasteHandler ()
   liftIO $ msgDebug $ show context
 
   response <- liftIO $ Engine.sendPostWithFile context
-  link <- liftIO $ Processing.extractLinks response "input" "value" $ Tools.fileExtension (Engine.pcFileName 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 ()

src/EngineRghost.hs

 -- commit=Sign+in
 
 handler :: Engine.PasteHandler ()
-handler = getAuthToken >> login >> upload
+handler = do
+  config <- ask
+  returnHandler (Configuration.getEngineAuth config "rghost") where
+    returnHandler Nothing = getAuthToken >> upload
+    returnHandler _       = getAuthToken >> login >> upload
   --getAuthToken >> upload
 --rghostHandler context = return context >>= rghostGetAuthToken >>= rghostUpload
 

src/EngineScrin.hs

+module EngineScrin (config, handler) where
+
+import Data.Maybe
+import Network.HTTP.Headers
+import Network.HTTP
+import Control.Monad.State (modify, get, put)
+import Control.Monad.Reader (ask)
+import Control.Monad.IO.Class (liftIO)
+
+import qualified Data.Map as Map
+import qualified Data.List as List
+
+import qualified Engine
+import qualified Tools
+import qualified Processing
+import qualified Configuration
+import Log (msgDebug, msgInfo)
+
+scrinUploadUrl = "http://scrin.org/"
+
+scrinFields = [
+  Engine.TextField "resize_x" "",
+  Engine.TextField "resize_y" "",
+  Engine.TextField "thumb-sets" "none",
+  Engine.TextField "thumb-size-h" "100",
+  Engine.TextField "thumb-size-w" "100",
+  Engine.TextField "x" "",
+  Engine.TextField "y" ""]
+
+config = Engine.PasteContext {
+  Engine.pcUploadLink    = scrinUploadUrl,
+  Engine.pcFileTagName   = "fileup[]",
+  Engine.pcFileName      = "",
+  Engine.pcFields        = scrinFields,
+  Engine.pcEncodingType  = Engine.MultipartFormData,
+  Engine.pcContents      = "",
+  Engine.pcResultLink    = Nothing,
+  Engine.pcCustomFields  = Map.empty,
+  Engine.pcAllowRedirect = False,
+  Engine.pcCustomHeaders = []
+  }
+
+handler :: Engine.PasteHandler ()
+handler = getPostkeyAndCookies >> sendFile
+
+-- | Retrieves Cookies and Postkey from login page
+getPostkeyAndCookies :: Engine.PasteHandler ()
+getPostkeyAndCookies = do
+  -- start login page
+  context <- get
+  liftIO $ msgDebug "Getting start page..."
+  startPage <- liftIO $ Engine.fetch (getRequest scrinUploadUrl) True
+  let cookies = Engine.cookiesRemoveSet startPage
+  liftIO $ msgDebug $ "cookies: " ++ show cookies
+  put $ Engine.addCustomHeaders context [cookies]
+
+  let mPostkey = Processing.extractLink (rspBody startPage) "input" "value" ""
+  liftIO $ msgDebug $ "postkey: " ++ show mPostkey
+
+  let postkey = fromMaybe "" mPostkey
+      fields = Engine.pcFields context
+      newContext = context { Engine.pcFields = (Engine.TextField "postkey" postkey ) : fields }
+
+  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 ()

src/Processing.hs

-module Processing (getAttrs, extractLinks) where
+module Processing (getAttrs,
+                   extractLink,
+                   extractLinks) where
 
 import Text.HTML.TagSoup
 import Text.Regex.Posix
-import Network.Browser
-import Network.HTTP
 import Data.Maybe
 
 -- | Implementation
     link (TagOpen tag attrs) | tag == tagName = firstAttr attrs
     link _ = Nothing
 
-extractLinks :: Response String -> String -> String -> String -> IO (Maybe String)
-extractLinks r tag attr regexp = do
-  let tags = parseTags (rspBody r) :: [Tag String]
+extractLinks :: String -> String -> String -> String -> [String]
+extractLinks r tag attr regexp =
+  let tags = parseTags r :: [Tag String]
       links = getAttrs tags tag attr
       isPicture x = (x =~ regexp) --(x =~ "\\.jpg$") || (x =~ "\\.png$")
       isPictureUrl x = (isPicture x) --(&&) (isURI x) (True) --(isPicture x)
-      urls = filter isPictureUrl links in
-    --putStrLn $ show urls
-    passUrl urls where
-      passUrl [] = return Nothing --"error: no picture url found"
-      passUrl (x:_) = return $ Just x
-    --mapM_ putStrLn urls
-    --return $ head urls
-  --return "link"
+      urls = filter isPictureUrl links
+  in urls
 
+extractLink :: String -> String -> String -> String -> Maybe String
+extractLink r tag attr regexp = passUrl (extractLinks r tag attr regexp) where
+  passUrl [] = Nothing
+  passUrl (x:_) = Just x
 import qualified EngineFlashtux
 import qualified EngineImagebin
 import qualified EngineImm
+import qualified EngineScrin
 
 engineConfigs = Map.fromList [("fastpic",  (EngineFastpic.config,  EngineFastpic.handler)),
                               ("rghost",   (EngineRghost.config,   EngineRghost.handler)),
                               ("flashtux", (EngineFlashtux.config, EngineFlashtux.handler)),
                               ("imagebin", (EngineImagebin.config, EngineImagebin.handler)),
                               ("radikal",  (EngineRadikal.config,  EngineRadikal.handler)),
+                              ("scrin",    (EngineScrin.config ,   EngineScrin.handler)),
                               ("imm",      (EngineImm.config,      EngineImm.handler))]
 
 usage :: String
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.