浏览代码

Add a loop mode and an init script

- add one argument -l --loop
- add one conf key loop-delay
Yann Weber 8 年前
父节点
当前提交
d35735cf65
共有 3 个文件被更改,包括 117 次插入12 次删除
  1. 1
    0
      conf-sample.ini
  2. 53
    12
      fip_current.py
  3. 63
    0
      init_script

+ 1
- 0
conf-sample.ini 查看文件

@@ -1,5 +1,6 @@
1 1
 [conf]
2 2
 debug=false
3
+loop-delay=15
3 4
 host=127.0.0.1:8000
4 5
 mount=/fip.mp3
5 6
 login=admin

+ 53
- 12
fip_current.py 查看文件

@@ -15,6 +15,7 @@
15 15
 # You should have received a copy of the GNU General Public License
16 16
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 17
 
18
+import sys
18 19
 import time
19 20
 import requests
20 21
 import configparser
@@ -40,16 +41,53 @@ def main():
40 41
         type=str,
41 42
         default='conf.ini',
42 43
         help="Configuration file")
44
+    parser.add_argument(
45
+        '-l', '--loop',
46
+        dest='loop',
47
+        action='store_const',
48
+        const=True,
49
+        default=False,
50
+        help="Combined with -u enables loop update")
51
+    """
52
+    parser.add_argument(
53
+        '-d', '--loop-delay',
54
+        dest='delay',
55
+        type=int,
56
+        default=15,
57
+        help="Loop delay in seconds")
58
+    """
43 59
     args = parser.parse_args()
44 60
     conf = configparser.ConfigParser()
45 61
     conf.read(args.conf)
46
-    globals()['DEBUG'] = conf.get('conf', 'debug', fallback="False").lower() == 'true'
62
+    debug = conf.get('conf', 'debug', fallback="False").lower() == 'true'
63
+    globals()['DEBUG'] = debug
47 64
     if args.update:
48 65
         host = conf.get('conf', 'host', fallback='127.0.0.1:8000')
49 66
         mount = conf.get('conf', 'mount', fallback='/example.ogg')
50 67
         login = conf.get('conf', 'login', fallback='admin')
51 68
         password = conf.get('conf', 'password', fallback='hackme')
52
-        icecast_update(host, mount, login, password)
69
+        if args.loop:
70
+            delay = conf.get('conf', 'loop-delay', fallback='15')
71
+            try:
72
+                delay = int(delay)
73
+            except ValueError:
74
+                msg = "loop-delay expected to be an integer '%s' found"
75
+                raise ValueError( msg % delay)
76
+            prev = None
77
+            while True:
78
+                try:
79
+                    if globals()['DEBUG']:
80
+                        print("Update start");
81
+                    prev = icecast_update(host, mount, login, password, prev)
82
+                    if globals()['DEBUG']:
83
+                        print("Updated");
84
+                    time.sleep(delay)
85
+                except Exception as e:
86
+                    prev = None
87
+                    print(str(e), file=sys.stderr)
88
+                
89
+        else:
90
+            icecast_update(host, mount, login, password)
53 91
     else:
54 92
         print(format_current())
55 93
 
@@ -117,7 +155,7 @@ def icecast_infos():
117 155
     return infos
118 156
 
119 157
 
120
-def icecast_update(host, mount, login=None, password=None):
158
+def icecast_update(host, mount, login=None, password=None, prev = None):
121 159
     """ Update metadatas to an Icecast2 mount """
122 160
     infos = icecast_infos()
123 161
     url = "http://{host}/admin/metadata"
@@ -129,16 +167,19 @@ def icecast_update(host, mount, login=None, password=None):
129 167
     auth = None
130 168
     if login is not None and password is not None:
131 169
         auth = (login, password)
170
+    
171
+    if infos != prev:
172
+        try:
173
+            res = requests.get(url, auth=auth, params=params)
174
+        except requests.exceptions.ConnectionError:
175
+            raise RuntimeError("Connection refuse for "+res.url)
176
+
177
+        if res.status_code != 200:
178
+            msg = "Got status code %d for %s"
179
+            msg %= (res.status_code, res.url)
180
+            raise RuntimeError(msg)
132 181
 
133
-    try:
134
-        res = requests.get(url, auth=auth, params=params)
135
-    except requests.exceptions.ConnectionError:
136
-        raise RuntimeError("Connection refuse for "+res.url)
137
-
138
-    if res.status_code != 200:
139
-        msg = "Got status code %d for %s"
140
-        msg %= (res.status_code, res.url)
141
-        raise RuntimeError(msg)
182
+    return infos
142 183
 
143 184
 if __name__ == '__main__':
144 185
     main()

+ 63
- 0
init_script 查看文件

@@ -0,0 +1,63 @@
1
+#! /bin/sh
2
+### BEGIN INIT INFO
3
+# Provides:          fip-current
4
+# Required-Start:    $remote_fs $network icecast2
5
+# Required-Stop:     $remote_fs
6
+# Default-Start:     2 3 4 5
7
+# Default-Stop:      0 1 6
8
+# Short-Description: Icecast2 FIP relay metadata update script
9
+# Description:       Start updating Icecast2 metadatas using FIP radio wep API
10
+### END INIT INFO
11
+
12
+DPATH=/PUT/SCRIPT/PATH/HERE
13
+
14
+SCRIPT="fip_current.py"
15
+DAEMON="${DPATH}/${SCRIPT}"
16
+CONFIG="${DPATH}/conf.ini"
17
+NAME="fip-current"
18
+DESC="fip-current update script"
19
+DAEMON_OPTS="-ul -c $CONFIG"
20
+PIDFILE="/run/fip-current.pid"
21
+
22
+set_path()
23
+{
24
+	echo "DPATH variable not set or wrong in init_script";
25
+	exit 1;
26
+}
27
+
28
+test -x $DAEMON || set_path
29
+
30
+. /lib/lsb/init-functions
31
+
32
+case $1 in
33
+	start)
34
+		
35
+		log_daemon_msg "Starting $DESC"
36
+		if $0 status >/dev/null
37
+		then
38
+			echo -n " allready running"
39
+			log_end_msg 1
40
+		else
41
+			start-stop-daemon --quiet --start --background -m -p $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
42
+			log_end_msg $?
43
+		fi
44
+		
45
+		;;
46
+	stop)
47
+		log_daemon_msg "Stopping $DESC"
48
+		start-stop-daemon --stop -p $PIDFILE --remove-pidfile -x $DAEMON
49
+		log_end_msg $?
50
+		;;
51
+	restart)
52
+		$0 stop
53
+		$0 start
54
+		;;
55
+	status)
56
+		status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
57
+		;;
58
+	*)
59
+		echo "Usage: $0 {start|stop|restart|status}" >&2
60
+		exit 1
61
+		;;
62
+esac
63
+

正在加载...
取消
保存