savonet / liquidsoap

Liquidsoap is a statically typed scripting general-purpose language with dedicated operators and backend for all thing media, streaming, file generation, automation, HTTP backend and more.
http://liquidsoap.info
GNU General Public License v2.0
1.4k stars 130 forks source link

Problem with encoding of data passed to system method #506

Closed Evreke closed 6 years ago

Evreke commented 6 years ago

Liquidsoap is running in docker container (using my own image with Ubuntu:16.04 and regular apt install liquidsoap) I am trying to make feature for my web radio that will show last N played tracks in frontend. Decision is to use Python script which will take 2 arguments, then write\append them to json file...

I am using this part of code

def new_track(m) =
    artist = m["artist"]
    title = m["title"]
    system("python3 ./scripts/last_10_songs.py \"#{artist}\" \"#{title}\"")
end

My python script fails to write Cyrillic strings to json but no problem with Latin characters.

One guy from community told me to get bytes of passed string from liquidsoap to determine output encoding, but i have no success to determine encoding of bytes array.

Is there anyway to determine what type of encoding variables artist and title have.

toots commented 6 years ago

Hi @Evreke,

You might want to try the string.recode method:

Convert a string. Effective only if Camomile is enabled.
Type: (?in_enc:string,?out_enc:string,string)->string
Category: String
Parameters:
 * in_enc : string (default: "")
     Input encoding. Autodetected if empty.
 * out_enc : string (default: "UTF-8")
     Output encoding.
 * (unlabeled) : string (default: None)

For instance, if you want to convert a string to ECMA-CYRILLIC you would do:

s = string.recode(out_enc="ECMA-CYRILLIC",s)

Hope that helps, feel free to re-open if you have more questions or if the problem still occurs.

toots commented 6 years ago

Although, you might want to double check on which encoding to use. I believe that the json standard expects UTF8 strings only.