Python way to check if internet radio stream / live is available

I am working on collecting Internet radio streams such as m3u, with a link to the stream inside (e.g. http://aska.ru-hoster.com:8053/autodj ).

I did not find an example of how to check if a link / live is available .

Any help appreciated !

UPD:

Perhaps the main question should be:

Maybe the stream is broken? If so, will the link for this stream still be available or will there simply be a 404 error in the browser? If the link available to open an even stream is dead, what other methods to check the stream?

+4
source share
1 answer

Are you trying to check if a streaming URL exists?
If so, it will be exactly the same as checking any other URL if one exists.

One way is to try to get the URL using urllib, and check the received status code.

200 - exists
Everything else (for example, 404) - does not exist or you cannot access it.

For instance:

import urllib
url = 'http://aska.ru-hoster.com:8053/autodj'

code = urllib.urlopen(url).getcode()
#if code == 200:  #Edited per @Brad comment
 if str(code).startswith('2') or str(code).startswith('3') :
    print 'Stream is working'
else:
    print 'Stream is dead'

EDIT-1

So far, the URL will be detected or not. It will not catch if the URL exists and the media link is broken.

vlc URL-, . , , .

URL-

url = 'http://aska.ru-hoster.com:8053/autodj'
>>> 
Stream is working. Current state = State.Playing   

URL-

url = 'http://aska.ru-hoster.com:8053/autodj12345'
>>> 
Stream is dead. Current state = State.Error

. VLC, .

import vlc
import time

url = 'http://aska.ru-hoster.com:8053/autodj'
#define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')

#Define VLC player
player=instance.media_player_new()

#Define VLC media
media=instance.media_new(url)

#Set player media
player.set_media(media)

#Play the media
player.play()


#Sleep for 5 sec for VLC to complete retries.
time.sleep(5)
#Get current state.
state = str(player.get_state())

#Find out if stream is working.
if state == "vlc.State.Error" or state == "State.Error":
    print 'Stream is dead. Current state = {}'.format(state)
    player.stop()
else:
    print 'Stream is working. Current state = {}'.format(state)
    player.stop()
+1

Source: https://habr.com/ru/post/1685239/


All Articles