Update icecast2 FIP relay using HTTP/Json API http://zmpd.zered.net:8042/fip-metadata.mp3
python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fip_current.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/python3
  2. import os.path, sys
  3. import requests
  4. import argparse
  5. import configparser
  6. api_url = 'http://www.fipradio.fr/livemeta/7'
  7. def main():
  8. parser = argparse.ArgumentParser(description="Fetch FIP radio stream metadata")
  9. parser.add_argument( '-u', '--icecast-update',
  10. dest = 'update',
  11. action = 'store_const',
  12. const = True,
  13. default = False,
  14. help = "To update an icecast instance")
  15. parser.add_argument( '-c', '--config',
  16. dest = 'conf',
  17. type=str,
  18. default='conf.ini')
  19. args = parser.parse_args()
  20. if args.update:
  21. conf = configparser.ConfigParser()
  22. conf.read(args.conf)
  23. host = conf.get('conf', 'host', fallback='127.0.0.1:8000')
  24. mount = conf.get('conf', 'mount', fallback='/example.ogg')
  25. login = conf.get('conf', 'login', fallback='admin')
  26. password = conf.get('conf', 'password', fallback='hackme')
  27. url_upd = icecast_update(host, mount, login, password)
  28. else:
  29. print(format_current())
  30. def get_all():
  31. r = requests.get(api_url)
  32. if r.status_code != 200:
  33. msg = "Got status code %d for %s"
  34. msg %= (r.status_code, api_url)
  35. raise RuntimeError(msg)
  36. return r.json()
  37. def get_current():
  38. datas = get_all()
  39. position = datas['levels'][0]['position']
  40. item_id = datas['levels'][0]['items'][position]
  41. item = datas['steps'][item_id]
  42. expt = ['authors','title', 'titreAlbum', 'visual', 'lienYoutube']
  43. for k in expt:
  44. if k not in item:
  45. item[k] = ''
  46. return item
  47. def format_current():
  48. item = get_current()
  49. infos = """Title :\t\t{title}
  50. Authors :\t{author}
  51. Album :\t\t{album}
  52. Visual :\t{image}
  53. Youtube :\t{youtube}
  54. """
  55. return infos.format( title = item['title'].title(),
  56. author = item['authors'].title(),
  57. album = item['titreAlbum'].title(),
  58. image = item['visual'],
  59. youtube = item['lienYoutube'])
  60. def icecast_infos():
  61. item = get_current()
  62. infos = item['title']
  63. if len(item['authors']) > 0:
  64. infos += ' - '+item['authors']
  65. if len(item['titreAlbum']):
  66. infos += ' ('+item['titreAlbum']+')'
  67. return infos
  68. def icecast_update(host, mount, login = None, password = None):
  69. infos = icecast_infos()
  70. url = "http://{host}/admin/metadata"#?mount={mount}&mode=updinfo&song={infos}"
  71. url = url.format(host = host)
  72. params = { 'mount': mount,
  73. 'mode': 'updinfo',
  74. 'song': infos}
  75. auth = None
  76. if login is not None and password is not None:
  77. auth = (login, password)
  78. try:
  79. res = requests.get(url, auth=auth, params = params)
  80. except requests.exceptions.ConnectionError:
  81. raise RuntimeError("Connection refuse for "+res.url)
  82. if res.status_code != 200:
  83. msg = "Got status code %d for %s"
  84. msg %= (res.status_code, res.url)
  85. raise RuntimeError(msg)
  86. if __name__ == '__main__':
  87. main()