Skip to content

Instantly share code, notes, and snippets.

@etcinit
Last active April 30, 2016 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etcinit/62faae6a41049f55c9c6c8a6c8938ec0 to your computer and use it in GitHub Desktop.
Save etcinit/62faae6a41049f55c9c6c8a6c8938ec0 to your computer and use it in GitHub Desktop.
A parser for some random Drone format
1
00:00:01,000 --> 00:00:02,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:08
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
2
00:00:02,000 --> 00:00:03,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:09
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
3
00:00:03,000 --> 00:00:04,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:10
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
4
00:00:04,000 --> 00:00:05,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:11
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
5
00:00:05,000 --> 00:00:06,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:12
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
6
00:00:06,000 --> 00:00:07,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:13
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
7
00:00:07,000 --> 00:00:08,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:14
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
8
00:00:08,000 --> 00:00:09,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:15
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
9
00:00:09,000 --> 00:00:10,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:16
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
10
00:00:10,000 --> 00:00:11,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:17
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
11
00:00:11,000 --> 00:00:12,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:18
GPS(110.9038,-2.7953,19) BAROMETER:499.9
ISO:100 Shutter:60 EV: Fnum:F2.8
12
00:00:12,000 --> 00:00:13,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:19
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
13
00:00:13,000 --> 00:00:14,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:20
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
14
00:00:14,000 --> 00:00:15,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:21
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
15
00:00:15,000 --> 00:00:16,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:22
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
16
00:00:16,000 --> 00:00:17,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:23
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
17
00:00:17,000 --> 00:00:18,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:24
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
18
00:00:18,000 --> 00:00:19,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:25
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
19
00:00:19,000 --> 00:00:20,000
HOME(110.9043,-2.7954) 2016.04.17 09:59:26
GPS(110.9038,-2.7953,19) BAROMETER:500.0
ISO:100 Shutter:60 EV: Fnum:F2.8
#!/usr/bin/env stack
-- stack --install-ghc runghc --package megaparsec
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Data.Monoid ((<>))
import Data.Text (Text, pack, intercalate)
import qualified Data.Text.IO as TIO (readFile, putStrLn)
import Text.Megaparsec
import Text.Megaparsec.Text
import Text.Megaparsec.Lexer (float, integer, signed)
import System.Environment
import Text.Printf
data Timestamp = Timestamp Integer Integer Integer Integer deriving (Show)
data Coordinate = Coordinate String String deriving (Show)
data DateTime = DateTime
{ _dtYear :: Integer
, _dtMonth :: Integer
, _dtDay :: Integer
, _dtHour :: Integer
, _dtMinute :: Integer
, _dtSecond :: Integer
} deriving (Show)
data LogEntry = LogEntry
{ _leID :: Integer
, _leStartTime :: Timestamp
, _leEndTime :: Timestamp
, _leHomeCoordinate :: Coordinate
, _leDate :: DateTime
, _leGPSCoordinate :: Coordinate
, _leBarometer :: Double
, _leISO :: Integer
, _leShutter :: Integer
, _leFnum :: Double
} deriving (Show)
coordinate :: Parser Coordinate
coordinate = do
(choice $ string <$> ["HOME", "GPS"]) >> char '('
let coord = manyTill (digitChar <|> char ',' <|> char '-' <|> char '.')
first <- coord (char ',')
second <- coord (char ')')
pure $ Coordinate first second
timestamp :: Parser Timestamp
timestamp = do
hour <- integer
char ':'
minute <- integer
char ':'
second <- integer
char ','
fraction <- integer
pure $ Timestamp hour minute second fraction
datetime :: Parser DateTime
datetime = do
year <- integer
char '.'
month <- integer
char '.'
day <- integer
space
hour <- integer
char ':'
minute <- integer
char ':'
second <- integer
pure $ DateTime year month day hour minute second
entry :: Parser LogEntry
entry = do
id <- integer
eol
startTime <- timestamp
string " --> "
endTime <- timestamp
eol
homeCoords <- coordinate
space
dt <- datetime
eol
gpsCoords <- coordinate
string " BAROMETER:"
barometer <- float
eol
string "ISO:"
iso <- integer
string " Shutter:"
shutter <- integer
string " EV: Fnum:F"
fnum <- signed space float
space
pure $ LogEntry
id startTime endTime homeCoords dt gpsCoords barometer iso shutter fnum
entries :: Parser [LogEntry]
entries = many entry
tshow :: (Show a) => a -> Text
tshow = pack . show
rt :: Integer -> Text
rt = pack . printf "%02d"
renderCoordinate :: Coordinate -> Text
renderCoordinate (Coordinate x y) = (pack x) <> "," <> (pack y)
renderTimestamp :: Timestamp -> Text
renderTimestamp (Timestamp h m s d)
= rt h <> ":" <> rt m <> ":" <> rt s <> "," <> tshow d
renderDateTime :: DateTime -> Text
renderDateTime (DateTime y mo d h mi s)
= rt mo <> "/" <> rt d <> "/" <> tshow y <> " "
<> rt h <> ":" <> rt mi <> ":" <> rt s
renderEntry :: LogEntry -> Text
renderEntry x = intercalate "," $ map (\y -> "\"" <> y x <> "\"")
[ tshow . _leID
, renderTimestamp . _leStartTime
, renderTimestamp . _leEndTime
, renderCoordinate . _leHomeCoordinate
, renderDateTime . _leDate
, renderCoordinate . _leGPSCoordinate
, tshow . _leBarometer
, tshow . _leISO
, tshow . _leShutter
, tshow . _leFnum
]
main :: IO ()
main = do
args <- getArgs
case args of
[] -> putStrLn "Usage: extract <filename>"
file:_ -> do
result <- (runParser entries file <$> TIO.readFile file)
case result of
Right result' -> do
putStrLn ("id,start_time,end_time,home,date,gps,barometer,iso,"
<> "shutter,fnum")
mapM_ (TIO.putStrLn . renderEntry) result'
Left err -> print err
id start_time end_time home date gps barometer iso shutter fnum
1 00:00:01,0 00:00:02,0 110.9043,-2.7954 04/17/2016 09:59:08 110.9038,-2.7953,19 500.0 100 60 2.8
2 00:00:02,0 00:00:03,0 110.9043,-2.7954 04/17/2016 09:59:09 110.9038,-2.7953,19 500.0 100 60 2.8
3 00:00:03,0 00:00:04,0 110.9043,-2.7954 04/17/2016 09:59:10 110.9038,-2.7953,19 499.9 100 60 2.8
4 00:00:04,0 00:00:05,0 110.9043,-2.7954 04/17/2016 09:59:11 110.9038,-2.7953,19 499.9 100 60 2.8
5 00:00:05,0 00:00:06,0 110.9043,-2.7954 04/17/2016 09:59:12 110.9038,-2.7953,19 499.9 100 60 2.8
6 00:00:06,0 00:00:07,0 110.9043,-2.7954 04/17/2016 09:59:13 110.9038,-2.7953,19 499.9 100 60 2.8
7 00:00:07,0 00:00:08,0 110.9043,-2.7954 04/17/2016 09:59:14 110.9038,-2.7953,19 499.9 100 60 2.8
8 00:00:08,0 00:00:09,0 110.9043,-2.7954 04/17/2016 09:59:15 110.9038,-2.7953,19 499.9 100 60 2.8
9 00:00:09,0 00:00:10,0 110.9043,-2.7954 04/17/2016 09:59:16 110.9038,-2.7953,19 499.9 100 60 2.8
10 00:00:10,0 00:00:11,0 110.9043,-2.7954 04/17/2016 09:59:17 110.9038,-2.7953,19 499.9 100 60 2.8
11 00:00:11,0 00:00:12,0 110.9043,-2.7954 04/17/2016 09:59:18 110.9038,-2.7953,19 499.9 100 60 2.8
12 00:00:12,0 00:00:13,0 110.9043,-2.7954 04/17/2016 09:59:19 110.9038,-2.7953,19 500.0 100 60 2.8
13 00:00:13,0 00:00:14,0 110.9043,-2.7954 04/17/2016 09:59:20 110.9038,-2.7953,19 500.0 100 60 2.8
14 00:00:14,0 00:00:15,0 110.9043,-2.7954 04/17/2016 09:59:21 110.9038,-2.7953,19 500.0 100 60 2.8
15 00:00:15,0 00:00:16,0 110.9043,-2.7954 04/17/2016 09:59:22 110.9038,-2.7953,19 500.0 100 60 2.8
16 00:00:16,0 00:00:17,0 110.9043,-2.7954 04/17/2016 09:59:23 110.9038,-2.7953,19 500.0 100 60 2.8
17 00:00:17,0 00:00:18,0 110.9043,-2.7954 04/17/2016 09:59:24 110.9038,-2.7953,19 500.0 100 60 2.8
18 00:00:18,0 00:00:19,0 110.9043,-2.7954 04/17/2016 09:59:25 110.9038,-2.7953,19 500.0 100 60 2.8
19 00:00:19,0 00:00:20,0 110.9043,-2.7954 04/17/2016 09:59:26 110.9038,-2.7953,19 500.0 100 60 2.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment