5

I work on collecting internet radio stream files such as m3u with a link to stream inside (for example http://aska.ru-hoster.com:8053/autodj).

I didn`t find example on how it is possible to check if the link is avaliable/live.

Any help is appreciated!

UPD:

Maybe the main question should sound like:

Could be a stream broken? If yes, will the link for that stream be still available or there will be simply 404 error in browser? If link still available to open even stream is dead, what are other methods to check the stream?

Brad
  • 159,648
  • 54
  • 349
  • 530
Michael
  • 1,170
  • 2
  • 14
  • 35
  • Do you have any code showing off your current attempt? Essentially you just 'get' the _m3u_, and handle the error/exception when this fails. ...if you fail to collect it, it's not there... – bryn Sep 07 '17 at 14:53
  • @bryn actually I am writing scrapper to download all that files from website, but the question is not related to how to do that. My question is how to check this stream files to figure out if the stream is not broken, so I can delete unnecessary files. Ok, I can rephrase the task: How to check link to internet radio stream (http://aska.ru-hoster.com:8053/autodj) for avaliability, so output should be like: 'Stream is working' / 'Stream is dead' – Michael Sep 07 '17 at 15:06
  • @michael , do check my answer and let me know if it helps. – Anil_M Sep 07 '17 at 16:23
  • @Anil_M thank you for answering friend, your answer is helpful. I may be wrong, but there could be situation when web-site still exists for a stream but it doesn`t work anymore or broken. One more time, I may be wrong, let me know if you are more aware about this, then simply checking url for 404 error has sense. – Michael Sep 07 '17 at 16:55
  • @Michael , Check out my edits for a solution based on `vlc` to figure out if a media link is broken. May be u can use both parts for robust detection of URL and media at URL. – Anil_M Sep 07 '17 at 19:12
  • @Anil_M great! now I think that is exactly what needed, I didn`t check yet, but already see It has sense. Thanks a lot body, appreciate so much your suggestion. – Michael Sep 07 '17 at 21:15

1 Answers1

4

Are you trying to check if the streaming URL exists?
If yes, it will be just like checking any other url if it exists.

One way will be to try getting url using urllib and check for returned status code.

200 - Exists
Anything else (e.g. 404) -Doesn't exist or you can't access it.

For example:

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

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

EDIT-1

While above will catch if a URL exists or not. It will not catch if URL exists and media link is broken.

One possible solution using vlc is to get the media from url, try to play it and get its status while playing. If media doesnt exists, we will get error which can be used to determine link status.

With working URL we get

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

With broken URL we get,

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

Below is basic logic to achieve above. You may want to check VLC site to catch other error types and better methods.

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()
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • 1
    This isn't quite accurate. Any status code `2xx` is a success, even if it's not `200`. For example, there are streaming servers that are going to return a `206 Partial Content`. You're also missing all the `3xx` forms of redirection. It's very common to have a stream redirect to other servers, to help balance load. – Brad Sep 11 '17 at 04:05
  • You are correct @Brad (Confirmed as per https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). I didn't look deeper into it since the intent was not to provide complete solution but just pointer to it. I have updated my answer that now catches `2xx` and `3xx` codes as `success`. Thanks for the pointers. – Anil_M Sep 11 '17 at 12:52