viewpoint-particle

Author Topic: Morse Code url  (Read 67 times)

stanl

  • Pundit
  • *****
  • Posts: 1390
Morse Code url
« on: February 09, 2021, 05:58:01 am »
Interesting url that translates text to Morse Code.  I know PS has a beep with parameters, but is there a way for WB to play back the code as beeps in a script.  below doesn't use Newtonsoft or Jim's Extender but the dots/dashes can be isolated into a list, array or map. Can they be either translated or played back in WB.
Code: Winbatch

;Winbatch 2020 - URL translates text to Morse Code
;Stan Littlefield 2/9/2021
;/////////////////////////////////////////////////////////////////////////////////////
gosub udfs
IntControl(73,1,0,0,0)
Msg = 'Winbatch 2021'
cUrl = "https://api.funtranslations.com/translate/morse.json?text=":Msg
request = Createobject("WinHttp.WinHttpRequest.5.1")
request.Open("GET", cUrl, @False )
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
request.SetRequestHeader("Accept", "application/json")
request.Send()
jdata = request.ResponseText
request = 0
Message("Json Morse Code",parseit(GetJSON(jdata)))
Exit
;====================================================================================
:WBERRORHANDLER
request=0
geterror()
Message("Error Encountered",errmsg)
Exit


;====================================================================================
:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine
#DefineFunction GetJSON(strJSON)
If strJSON == "" Then Return ""
objJSC = CreateObject ("MSScriptControl.ScriptControl")
objJSC.Language = "JScript"
objJSC.AddCode(: `function json2txt(obj,path){var txt='';for(var key in obj){if(obj.hasOwnProperty(key)){if('object'==typeof(obj[key])){txt+=json2txt(obj[key],path+(path?'|':'')+key);}else{txt+=path+'|'+key+','+obj[key]+'\n';}}}return txt;}`)
Return objJSC.Eval(: `json2txt(` : strJSON : `,'')`)
; JS code from Patrick Fisher at "http://stackoverflow.com/questions/10221229/list-all-keys-and-values-of-json" ; 2012-04-19T03:48:24.
#EndFunction
#DefineFunction parseit(json)
text = json
text = StrReplace(text,",","=")
text = StrReplace(text,"|","")
text = StrReplace(text,@LF,@CRLF)
Return text
#EndFunction
Return
;====================================================================================


 

JTaylor

  • Pundit
  • *****
  • Posts: 1595
    • Data & Stuff Inc.
Re: Morse Code url
« Reply #1 on: February 09, 2021, 07:33:30 am »
This probably isn't what you mean but if using the individual sound files works you wouldn't need to translate.....

https://commons.wikimedia.org/wiki/Category:Audio_files_of_Morse_code


Jim

stanl

  • Pundit
  • *****
  • Posts: 1390
Re: Morse Code url
« Reply #2 on: February 09, 2021, 08:17:02 am »
This probably isn't what you mean


it isn't.


Code: [Select]

$Text = 'Winbatch 2021'
 
# URL-encode text
Add-Type -AssemblyName System.Web
$encoded = [System.Web.HttpUtility]::UrlEncode($Text)
 
# compose web service URL
$Url = "https://api.funtranslations.com/translate/morse.json?text=$encoded"
 
# call web service
$morse = (Invoke-RestMethod -UseBasicParsing -Uri $url).contents.translated
 
Foreach ($char in $morse.ToCharArray())
{
    switch ($char)
    {
        '.'      { [Console]::Beep(800, 300) }
        '-'      { [Console]::Beep(800, 900) }
        ' '      { Start-Sleep -Milliseconds 500 }
        default  { Write-Warning "Unknown char: $_"
                   [Console]::Beep(2000, 500 ) }
 
    }
    Write-Host $char -NoNewline
    Start-Sleep -Milliseconds 200
}
Write-Host "OK"