feat: add grimm-scroller script
This commit is contained in:
43
grimm-scroller/Server.hs
Normal file
43
grimm-scroller/Server.hs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
module Main where
|
||||||
|
|
||||||
|
import Data.Text (pack, Text)
|
||||||
|
import Control.Monad (forever, forM_)
|
||||||
|
import System.IO (stderr, hPutStrLn)
|
||||||
|
import Control.Concurrent (threadDelay, forkIO)
|
||||||
|
import qualified Network.WebSockets as WS
|
||||||
|
import qualified Data.Text.IO as T
|
||||||
|
import Control.Concurrent.Chan.Unagi
|
||||||
|
import Options.Applicative
|
||||||
|
|
||||||
|
data Options = Options
|
||||||
|
{ host :: String
|
||||||
|
, port :: Int
|
||||||
|
, delay :: Double
|
||||||
|
, loop :: Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
options :: Parser Options
|
||||||
|
options = Options
|
||||||
|
<$> strOption (long "host" <> short 'h' <> metavar "ADDRESS" <> value "127.0.0.1" <> showDefault <> help "The host to listen on")
|
||||||
|
<*> option auto (long "port" <> short 'p' <> metavar "PORT" <> help "The port to listen on")
|
||||||
|
<*> option auto (long "delay" <> value 1000 <> metavar "MILLISECONDS" <> showDefault <> help "Delay between sending messages")
|
||||||
|
<*> switch (long "loop" <> short 'l' <> help "Whether to loop the input")
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
theOptions <- execParser $ info (options <**> helper) $ fullDesc <> progDesc "Broadcast text to websocket"
|
||||||
|
(inChan, _) <- newChan
|
||||||
|
forkIO $ forever $ do
|
||||||
|
inputLine <- T.getLine
|
||||||
|
writeChan inChan inputLine
|
||||||
|
threadDelay $ truncate $ 1000 * delay theOptions
|
||||||
|
WS.runServer (host theOptions) (port theOptions) (application inChan)
|
||||||
|
|
||||||
|
application :: InChan Text -> WS.ServerApp
|
||||||
|
application inChan pending = do
|
||||||
|
outChan <- dupChan inChan
|
||||||
|
hPutStrLn stderr ("New client connected: " ++ show (WS.pendingRequest pending))
|
||||||
|
conn <- WS.acceptRequest pending
|
||||||
|
WS.withPingThread conn 30 (return ()) $ forever $ do
|
||||||
|
text <- readChan outChan
|
||||||
|
WS.sendTextData conn (text)
|
||||||
4
grimm-scroller/default.nix
Normal file
4
grimm-scroller/default.nix
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{ writers, haskellPackages, ... }:
|
||||||
|
writers.writeHaskellBin "web-socket-sink" {
|
||||||
|
libraries = with haskellPackages; [websockets text concurrency unagi-chan optparse-applicative];
|
||||||
|
} (builtins.readFile ./Server.hs)
|
||||||
330164
grimm-scroller/dwb-compact.json
Normal file
330164
grimm-scroller/dwb-compact.json
Normal file
File diff suppressed because it is too large
Load Diff
3
grimm-scroller/fetch.sh
Normal file
3
grimm-scroller/fetch.sh
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
curl 'https://api.woerterbuchnetz.de/open-api/dictionaries/DWB/lemmata/*' \
|
||||||
|
| jq -c '.result_set | .[] | {lemma: .lemma, id: .wbnetzid}' > dwb-compact.json
|
||||||
59
grimm-scroller/wclient.html
Normal file
59
grimm-scroller/wclient.html
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>WebSocket Playground</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color: black;
|
||||||
|
color: white;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 120%;
|
||||||
|
font-weight: bold;
|
||||||
|
overflow-y: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
#lemmata {
|
||||||
|
text-transform: uppercase;
|
||||||
|
list-style: none;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
li {
|
||||||
|
margin: 1rem 0;
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
text-decoration: none;
|
||||||
|
color: unset;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<ul id="lemmata"></ul>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
const ws = new WebSocket("ws://88.99.83.173:9160/");
|
||||||
|
|
||||||
|
ws.onopen = () => {
|
||||||
|
console.log("WebSocket Client Connected");
|
||||||
|
};
|
||||||
|
|
||||||
|
const lemmata = document.getElementById("lemmata");
|
||||||
|
|
||||||
|
ws.onmessage = (e) => {
|
||||||
|
const lemmaJson = e.data;
|
||||||
|
const lemma = JSON.parse(lemmaJson);
|
||||||
|
|
||||||
|
const lemmaA = document.createElement("a");
|
||||||
|
lemmaA.href = "https://www.woerterbuchnetz.de/DWB?lemid=" + lemma.id;
|
||||||
|
lemmaA.innerHTML = lemma.lemma;
|
||||||
|
lemmaA.target = "_blank";
|
||||||
|
|
||||||
|
const lemmaLi = document.createElement("li");
|
||||||
|
lemmaLi.appendChild(lemmaA);
|
||||||
|
|
||||||
|
lemmata.appendChild(lemmaLi);
|
||||||
|
|
||||||
|
const scrollElement = document.scrollingElement || document.body;
|
||||||
|
scrollElement.scrollTop = scrollElement.scrollHeight;
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user