Code cleaning

Adding GPL licence
Commenting and code cleaning
Adding a README
This commit is contained in:
Yann Weber 2016-05-29 02:26:50 +02:00
commit ac7483d5d0
3 changed files with 772 additions and 34 deletions

View file

@ -1,24 +1,43 @@
#!/usr/bin/python3
#
# Copyright 2016 Yann Weber
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os.path, sys
import requests
import argparse
import configparser
from argparse import ArgumentParser
API_URL = 'http://www.fipradio.fr/livemeta/7'
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')
""" Parse arguments and run wanted function """
parser = ArgumentParser(description="Fetch FIP radio stream metadata")
parser.add_argument(
'-u', '--icecast-update',
dest='update',
action='store_const',
const=True,
default=False,
help="Run Icecast2 metadata update")
parser.add_argument(
'-c', '--config',
dest='conf',
type=str,
default='conf.ini',
help="Configuration file")
args = parser.parse_args()
if args.update:
conf = configparser.ConfigParser()
@ -27,33 +46,38 @@ def main():
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)
icecast_update(host, mount, login, password)
else:
print(format_current())
def get_all():
r = requests.get(api_url)
if r.status_code != 200:
""" Return a dict containing all FIP API metadatas """
res = requests.get(API_URL)
if res.status_code != 200:
msg = "Got status code %d for %s"
msg %= (r.status_code, api_url)
msg %= (res.status_code, API_URL)
raise RuntimeError(msg)
return res.json()
return r.json()
def get_current():
""" Return a dict containing currently playing song metadatas """
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']
expt = ['authors', 'title', 'titreAlbum', 'visual', 'lienYoutube']
for k in expt:
if k not in item:
item[k] = ''
return item
def format_current():
""" Return a string representing formated current playing song
metadatas """
item = get_current()
infos = """Title :\t\t{title}
Authors :\t{author}
@ -61,14 +85,17 @@ 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'])
return infos.format( title = item['title'].title(),
author = item['authors'].title(),
album = item['titreAlbum'].title(),
image = item['visual'],
youtube = item['lienYoutube'])
def icecast_infos():
""" Return formated Icecast2 metadatas from FIP current song
metadatas """
item = get_current()
infos = item['title']
if len(item['authors']) > 0:
@ -77,19 +104,22 @@ def icecast_infos():
infos += ' ('+item['titreAlbum']+')'
return infos
def icecast_update(host, mount, login = None, password = None):
def icecast_update(host, mount, login=None, password=None):
""" Update metadatas to an Icecast2 mount """
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}
url = "http://{host}/admin/metadata"
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)
res = requests.get(url, auth=auth, params=params)
except requests.exceptions.ConnectionError:
raise RuntimeError("Connection refuse for "+res.url)
@ -97,6 +127,6 @@ def icecast_update(host, mount, login = None, password = None):
msg = "Got status code %d for %s"
msg %= (res.status_code, res.url)
raise RuntimeError(msg)
if __name__ == '__main__':
main()