generate_liquidsoap_cfg.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import logging
  2. import os
  3. import sys
  4. import time
  5. import traceback
  6. from api_clients.api_client import AirtimeApiClient
  7. def generate_liquidsoap_config(ss):
  8. data = ss['msg']
  9. fh = open('/etc/airtime/liquidsoap.cfg', 'w')
  10. fh.write("################################################\n")
  11. fh.write("# THIS FILE IS AUTO GENERATED. DO NOT CHANGE!! #\n")
  12. fh.write("################################################\n")
  13. for d in data:
  14. key = d['keyname']
  15. str_buffer = d[u'keyname'] + " = "
  16. if d[u'type'] == 'string':
  17. val = '"%s"' % d['value']
  18. else:
  19. val = d[u'value']
  20. val = val if len(val) > 0 else "0"
  21. str_buffer = "%s = %s\n" % (key, val)
  22. fh.write(str_buffer.encode('utf-8'))
  23. auth_path = os.path.dirname(os.path.realpath(__file__))
  24. fh.write('log_file = "/var/log/airtime/pypo-liquidsoap/<script>.log"\n')
  25. fh.write('auth_path = "%s/liquidsoap_auth.py"\n' % auth_path)
  26. fh.close()
  27. def run():
  28. logging.basicConfig(format='%(message)s')
  29. attempts = 0
  30. max_attempts = 10
  31. successful = False
  32. while not successful:
  33. try:
  34. ac = AirtimeApiClient(logging.getLogger())
  35. ss = ac.get_stream_setting()
  36. generate_liquidsoap_config(ss)
  37. successful = True
  38. except Exception, e:
  39. print "Unable to connect to the Airtime server."
  40. logging.error(str(e))
  41. logging.error("traceback: %s", traceback.format_exc())
  42. if attempts == max_attempts:
  43. logging.error("giving up and exiting...")
  44. sys.exit(1)
  45. else:
  46. time.sleep(3)
  47. attempts += 1