|
@@ -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()
|