Last active
August 26, 2016 19:02
-
-
Save jmccance/de314127a67622e396c3c4c55ab4c954 to your computer and use it in GitHub Desktop.
ProofPoint extraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.jmccance.proofpoint | |
import scala.util.parsing.combinator._ | |
object ProofPoint extends App { | |
object ProofPointParser extends RegexParsers { | |
def character: Parser[String] = """.""".r ^^ (_.toString) | |
def slash: Parser[String] = "_" ^^ (_ => "/") | |
def encodedCharacter: Parser[String] = | |
"""-[A-F0-9]+""".r ^^ (s => Integer.parseInt(s.tail, 16).toChar.toString) | |
def url: Parser[String] = rep(encodedCharacter | slash | character) ^^ (_.mkString) | |
def apply(input: String): String = parseAll(url, input) match { | |
case Success(result, _) => result | |
case failure: NoSuccess => scala.sys.error(failure.msg) | |
} | |
} | |
def extractEncodedUrl(s: String): Option[String] = { | |
val ProofPointEncodedUrl = """^http.*u=([^&]+)&.*$""".r | |
s match { | |
case ProofPointEncodedUrl(url) => Some(url) | |
case _ => None | |
} | |
} | |
extractEncodedUrl(args.head) match { | |
case Some(encodedUrl) => println(ProofPointParser(encodedUrl)) | |
case None => scala.sys.error("Could not find URL in input.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment