Skip to content

Instantly share code, notes, and snippets.

@th0ma5w
Created August 30, 2018 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save th0ma5w/2ba955a23ad04f9f8f5e3dd2ef581fc7 to your computer and use it in GitHub Desktop.
Save th0ma5w/2ba955a23ad04f9f8f5e3dd2ef581fc7 to your computer and use it in GitHub Desktop.
MSNBC Audio Kodi Plugin
import json, urllib2, sys, xbmcgui, xbmcplugin
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', 'CURL')]
tunein_json_url = "https://tunein.com/tuner/tune/?tuneType=Station&preventNextTune=true&waitForAds=false&audioPrerollEnabled=false&partnerId=qZjjnm85&stationId=297990"
tunein_data = json.loads(opener.open(tunein_json_url).read())
stream_url = tunein_data["DirectStreams"][0]["Url"]
addon_handle = int(sys.argv[1])
xbmcplugin.setContent(addon_handle, 'movies')
li = xbmcgui.ListItem("MSNBC Audio", iconImage="defaultvideo.png")
li.setProperty('IsPlayable','True')
li.setInfo(type="video", infoLabels={'title':'MSNBC Audio'})
xbmcplugin.addDirectoryItem(
handle=addon_handle,
url=stream_url,
listitem=li,
isFolder = False,
)
xbmcplugin.endOfDirectory(addon_handle)
# Import needed libraries
import json, urllib2, sys, xbmcgui, xbmcplugin
# TuneIn provides a JSON file which contains a URL which can be played by Kodi,
# This URL contains a unique ID which is only valid for a limited time, so we must get it anew each time.
# Build a URL opener
opener = urllib2.build_opener()
# Add a header for the User-Agent which for some reason seems to be required by the tunein.com service
opener.addheaders = [('User-Agent', 'CURL')]
# The actual URL to the JSON data for the MSNBC audio feed. Could be edited to point to other TuneIn feeds.
tunein_json_url = "https://tunein.com/tuner/tune/?tuneType=Station&preventNextTune=true&waitForAds=false&audioPrerollEnabled=false&partnerId=qZjjnm85&stationId=297990"
# Actually load the URL with our opener, and then parse the JSON data into a Python dictionary.
tunein_data = json.loads(opener.open(tunein_json_url).read())
# Pull the playable stream URL from the object, in the "DirectStreams" key, pick the first item "0",
# and then that item has a property "Url" which contains what we're looking for.
stream_url = tunein_data["DirectStreams"][0]["Url"]
# Get the "handle" for this addon which is needed by Kodi functions for GUI manipulation
addon_handle = int(sys.argv[1])
# Set the content to be movies (could be something else?)
xbmcplugin.setContent(addon_handle, 'movies')
# Create a new list item object within Kodi, set a title, and set an icon using a built-in image name
li = xbmcgui.ListItem("MSNBC Audio", iconImage="defaultvideo.png")
# Set this list item as playable
li.setProperty('IsPlayable','True')
# Set the list item type and set the metadata also to have a title for it
li.setInfo(type="video", infoLabels={'title':'MSNBC Audio'})
# Actually add the list item to the plugin's generated menu with all of the above.
# It is not a folder, so don't make it look like a folder and don't expect a new list.
xbmcplugin.addDirectoryItem(
handle=addon_handle,
url=stream_url,
listitem=li,
isFolder = False,
)
# End the GUI building operation and finish.
xbmcplugin.endOfDirectory(addon_handle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment