123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #!/usr/bin/python3
-
- import os.path, sys
- import requests
- import argparse
- import configparser
-
- api_url = 'http://www.fipradio.fr/livemeta/7'
-
- def main():
- parser = argparse.ArgumentParser(description="Fetch FIP radio stream metadata")
- parser.add_argument( '-u', '--icecast-update',
- dest = 'update',
- action = 'store_const',
- const = True,
- default = False,
- help = "To update an icecast instance")
- parser.add_argument( '-c', '--config',
- dest = 'conf',
- type=str,
- default='conf.ini')
- args = parser.parse_args()
- if args.update:
- conf = configparser.ConfigParser()
- conf.read(args.conf)
- host = conf.get('conf', 'host', fallback='127.0.0.1:8000')
- mount = conf.get('conf', 'mount', fallback='/example.ogg')
- login = conf.get('conf', 'login', fallback='admin')
- password = conf.get('conf', 'password', fallback='hackme')
- url_upd = icecast_update(host, mount, login, password)
- else:
- print(format_current())
-
- def get_all():
-
- r = requests.get(api_url)
- if r.status_code != 200:
- msg = "Got status code %d for %s"
- msg %= (r.status_code, api_url)
- raise RuntimeError(msg)
-
- return r.json()
-
- def get_current():
- datas = get_all()
- position = datas['levels'][0]['position']
- item_id = datas['levels'][0]['items'][position]
- item = datas['steps'][item_id]
- expt = ['authors','title', 'titreAlbum', 'visual', 'lienYoutube']
- for k in expt:
- if k not in item:
- item[k] = ''
-
- return item
-
- def format_current():
- item = get_current()
- infos = """Title :\t\t{title}
- Authors :\t{author}
- Album :\t\t{album}
- Visual :\t{image}
- Youtube :\t{youtube}
- """
-
- return infos.format( title = item['title'].title(),
- author = item['authors'].title(),
- album = item['titreAlbum'].title(),
- image = item['visual'],
- youtube = item['lienYoutube'])
-
- def icecast_infos():
- item = get_current()
- infos = item['title']
- if len(item['authors']) > 0:
- infos += ' - '+item['authors']
- if len(item['titreAlbum']):
- infos += ' ('+item['titreAlbum']+')'
- return infos
-
- def icecast_update(host, mount, login = None, password = None):
- infos = icecast_infos()
- url = "http://{host}/admin/metadata"#?mount={mount}&mode=updinfo&song={infos}"
- url = url.format(host = host)
- params = { 'mount': mount,
- 'mode': 'updinfo',
- 'song': infos}
- auth = None
- if login is not None and password is not None:
- auth = (login, password)
-
- try:
- res = requests.get(url, auth=auth, params = params)
- except requests.exceptions.ConnectionError:
- raise RuntimeError("Connection refuse for "+res.url)
-
- if res.status_code != 200:
- msg = "Got status code %d for %s"
- msg %= (res.status_code, res.url)
- raise RuntimeError(msg)
-
- if __name__ == '__main__':
- main()
|