flows.liq 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # BIG TODO:
  2. # - Check for errors
  3. # - Unregister radio and streams
  4. # Register a radio on Liquidsoap Flows.
  5. # @category Liquidsoap
  6. # @param ~radio Name of the radio.
  7. # @param ~website URL of the website of the radio.
  8. # @param ~description Description of the radio.
  9. # @param ~genre Genre of the radio (rock or rap or etc.).
  10. # @param ~streams List of streams for the radio described by \
  11. # a pair of strings consisting of the format of the stream \
  12. # and the url of the stream. The format should be \
  13. # of the form "ogg/128k" consisting of the codec and \
  14. # the bitrate, separated by "/".
  15. def register_flow(~server="",~user="default",~password="default",
  16. ~email="",~radio,~website,~description,~genre,
  17. ~streams,s)
  18. # If the server is "", we get the server from sf.net
  19. server =
  20. if server == "" then
  21. server = http.get("http://savonet.sourceforge.net/flows_server")
  22. html_status = snd(fst(fst(fst(server))))
  23. if html_status == 200 then
  24. snd(server)
  25. else
  26. # If sf is down, we use the hardcoded server
  27. "http://savonet.rastageeks.org/liqflows.py"
  28. end
  29. else
  30. server
  31. end
  32. log(level=4,"Flows server: #{server}")
  33. # Initial variables
  34. ping_period = 600. # Pinging period in seconds
  35. # Fix default parameters
  36. # and set request function.
  37. base_params = [("v", "0.0"),
  38. ("user",user),
  39. ("password",password),
  40. ("email",email),
  41. ("radio",radio)]
  42. def request(~cmd,~params) =
  43. log = log(label=radio)
  44. log(level=4,"Processing command #{cmd} with arguments:")
  45. def log_arg(x) =
  46. label = fst(x)
  47. value = snd(x)
  48. log(level=4," #{label}: #{value}")
  49. end
  50. list.iter(log_arg,params)
  51. cmd = url.encode(cmd)
  52. params = list.append(base_params,params)
  53. def f(z) =
  54. x = fst(z)
  55. y = url.encode(snd(z))
  56. "#{x}=#{y}"
  57. end
  58. params = string.concat(separator="&",list.map(f,params))
  59. url = "#{server}?cmd=#{cmd}&#{params}"
  60. # TODO: do something with errors!
  61. answer = http.get(url)
  62. x = fst(answer)
  63. status = fst(x)
  64. y = fst(status)
  65. protocol = fst(y)
  66. code = snd(y)
  67. desc = snd(status)
  68. headers = snd(x)
  69. data = snd(answer)
  70. log(level=4,"Response status: #{protocol} #{code} #{desc}")
  71. log(level=4,"Response headers:")
  72. list.iter(log_arg,headers)
  73. log(level=4,"Response content: #{data}")
  74. end
  75. # Register radio
  76. params = [("radio_website",website),
  77. ("radio_description",description),
  78. ("radio_genre",genre)]
  79. request(cmd="add radio",params=params)
  80. # Ping
  81. def ping() =
  82. ignore(request(cmd="ping radio",params=[]))
  83. ping_period
  84. end
  85. add_timeout(fast=false,ping_period,ping)
  86. # Register streams
  87. def register_stream(format_url)
  88. format = fst(format_url);
  89. url = snd(format_url);
  90. params = [("stream_format",format),("stream_url",url)]
  91. request(cmd="add stream",params=params)
  92. end
  93. request(cmd="clear streams",params=[])
  94. list.iter(register_stream,streams)
  95. # Metadata update
  96. def metadata(m) =
  97. artist = m["artist"]
  98. title = m["title"]
  99. params = [("m_title",title),("m_artist",artist)]
  100. request(cmd="metadata",params=params)
  101. end
  102. on_metadata(metadata,s)
  103. end