Browse Source

Bugfix + conf & argparse

Yann Weber 8 years ago
parent
commit
9044a8bd59
2 changed files with 57 additions and 13 deletions
  1. 5
    0
      conf-sample.ini
  2. 52
    13
      fip_current.py

+ 5
- 0
conf-sample.ini View File

1
+[conf]
2
+host=127.0.0.1:8000
3
+mount=/fip.mp3
4
+login=admin
5
+password=hackme

+ 52
- 13
fip_current.py View File

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

Loading…
Cancel
Save