Skip to content

Commit

Permalink
verify playlist entries
Browse files Browse the repository at this point in the history
As it turned out not all playlist entries contain valid URLs. In
order to find a valid URL we iterate over the playlist and check
if the server of the current item is available.

The first valid server found will be used for the channel URL.
  • Loading branch information
ssapalski committed Aug 10, 2017
1 parent 2def155 commit 8951091
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion resources/lib/audioaddict/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Functionality triggered if the user starts/plays a channel.
"""

import urlparse
import requests
import xbmcplugin

from audioaddict.api import AudioAddictApi
Expand All @@ -20,7 +22,8 @@ def play_stream(addon, settings):
stream_key = get_stream_key(addon, network)
playlist = get_playlist(addon, network_key, stream_key, channel_key)

stream_url = "%s|User-Agent=%s&Referer=%s" % (playlist[0],
channel_url = get_valid_channel_url(playlist)
stream_url = "%s|User-Agent=%s&Referer=%s" % (channel_url,
settings.user_agent,
network.referer)

Expand Down Expand Up @@ -59,3 +62,19 @@ def get_playlist(addon, network_key, stream_key, channel_key):
raise e

return playlist


def get_valid_channel_url(playlist):
for channel_url in playlist:
parsed_url = urlparse.urlparse(channel_url)
if server_available(parsed_url.netloc):
return channel_url


def server_available(domain):
try:
response = requests.head("http://%s" % domain)
except requests.exceptions.ConnectionError as e:
return False

return True

0 comments on commit 8951091

Please sign in to comment.