Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuGolYolLom/8355048682a485663d62f6d9d223ea1b to your computer and use it in GitHub Desktop.
Save SuGolYolLom/8355048682a485663d62f6d9d223ea1b to your computer and use it in GitHub Desktop.
Detailed VLC Now Playing script for Xchat.
# @author Weidi Zhang <http://github.com/ebildude123>
# @license MIT
# @description VLC Now Playing script for Xchat
# This script assumes that the password to access your VLC web api page is "123456", but you can change the vlcPassword variable.
import xchat, urllib, urllib2, base64, json, os, HTMLParser
__module_name__ = "NowPlaying"
__module_version__ = "0.1"
__module_description__ = "Detailed VLC now playing information for Xchat by Weidi Zhang"
vlcPassword = "123456" # change accordingly
import urllib, urllib2, base64, json, os, HTMLParser
def hhmmss(secs):
mins, secs = divmod(secs, 60)
hours, mins = divmod(mins, 60)
return "%02d:%02d:%02d" % (hours, mins, secs)
def fetchVLC(page):
encodedAuth = base64.standard_b64encode(":%s" % vlcPassword)
statReq = urllib2.Request(page)
statReq.add_header("Authorization", "Basic %s" % encodedAuth)
getVlc = urllib2.urlopen(statReq).read()
return getVlc
def nowPlaying(word, word_eol, userdata):
getVlc = fetchVLC("http://localhost:8080/requests/status.json")
vlcJson = json.loads(getVlc)
if (vlcJson["state"] == "playing") or (vlcJson["state"] == "paused"):
vlcCategory = vlcJson["information"]["category"]
if vlcCategory["meta"].has_key("title"):
title = vlcCategory["meta"]["title"]
else:
title = vlcCategory["meta"]["filename"]
title = os.path.splitext(title)[0]
title = HTMLParser.HTMLParser().unescape(title)
if vlcCategory["meta"].has_key("artist"):
artist = vlcCategory["meta"]["artist"]
else:
artist = "Unknown"
codec = vlcCategory["Stream 0"]["Codec"]
position = hhmmss(vlcJson["time"])
length = hhmmss(vlcJson["length"])
if vlcCategory["Stream 0"].has_key("Bitrate"):
bitrate = vlcCategory["Stream 0"]["Bitrate"]
else:
getVlcList = fetchVLC("http://localhost:8080/requests/playlist.json")
playlistJson = json.loads(getVlcList)
children = playlistJson["children"][0]["children"]
for child in children:
if (child.has_key("current")) and (child["current"] == "current"):
filename = child["uri"]
break
filename = urllib.unquote_plus(filename.replace("file:///", ""))
fileBits = os.path.getsize(filename) * 8
bitrate = str(round(fileBits / vlcJson["length"] / 1000)) + " kb/s"
version = vlcJson["version"]
state = vlcJson["state"].title()
xchat.command("msg " + xchat.get_info("channel") + " State: " + state + " | Title: " + title + " | Artist: " + artist + " | Codec: " + codec + " | Bitrate: " + bitrate + " | Time: " + position + "/" + length + " | VLC v" + version)
else:
xchat.command("msg " + xchat.get_info("channel") + " Nothing is playing right now.")
return xchat.EAT_ALL
xchat.hook_command("vlc", nowPlaying, help = "/vlc will send VLC now playing info to the current channel")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment