Skip to content

Instantly share code, notes, and snippets.

@khadorkin
Forked from msakai/FlightRadar24.hs
Created August 6, 2021 15:56
Show Gist options
  • Save khadorkin/41ac7a1f84037dbc7e9f50eaec32c8fd to your computer and use it in GitHub Desktop.
Save khadorkin/41ac7a1f84037dbc7e9f50eaec32c8fd to your computer and use it in GitHub Desktop.
Sample for fetching JSON data from flightradar24.com
import Control.Concurrent
import Control.Monad
import Data.Maybe
import Data.Time
import Network.Browser
import Network.HTTP
import Network.HTTP.Proxy
import Network.URI
import System.Locale
main :: IO ()
main = do
let day = fromGregorian 2012 8 2
td = TimeOfDay 23 00 00
tm0 = UTCTime day (timeOfDayToTime td)
tm1 = addUTCTime (160*60) tm0 -- after 2 hours and 40 minutes
tms = takeWhile (<= tm1) $ iterate (addUTCTime 60) tm0 -- 1 minute interval
forM_ tms $ \tm -> do
let fname = formatTime defaultTimeLocale "%Y%m%d%H%M%S.json" tm
putStrLn fname
s <- fetch tm
writeFile fname s
threadDelay $ 10 * 10^6 -- 10 seconds
return ()
fetch :: UTCTime -> IO String
fetch tm = do
let body = urlEncodeVars [("date", formatTime defaultTimeLocale "%F %T" tm)]
req = postRequestWithBody
"http://www.flightradar24.com/PlaybackFlightsService.php"
"application/x-www-form-urlencoded"
body
proxy <- fetchProxy True
(_, rsp)
<- Network.Browser.browse $ do
setAllowRedirects True
setProxy proxy
request req
return $ rspBody rsp
{-
https://www.assembla.com/code/saintamh/subversion/nodes/azimuth/py/flightradar24.py
"UAE747": [
"896015", // 0: "hex" (?)
35.9888, // 1: lat
17.8126, // 2: lng
"279", // 3: aircraft_track (degrees, 0 is north, going clockwise)
"37975", // 4: altitude (feet)
"426", // 5: speek (knots)
"", // 6: squawk
"LMML", // 7: "radar" (some code for ID on a radar?)
"A332", // 8: aircraft_type
"A6-EKQ", // 9: aircraft registration
1327921638 // 10: looks like a timestamp
],
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment