Created
October 23, 2016 17:05
-
-
Save chaoskagami/3f4e1209fa6a9db3df35188a78d9dd26 to your computer and use it in GitHub Desktop.
mpc.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
__module_name__ = "mpc.py" | |
__module_author__ = "brr [berr.yt] (alteration)" | |
__module_version__ = "20160412" | |
__module_description__ = "now playing for media player classic" | |
#--------------------------------------------------------------# | |
infourl = "http://localhost:13579/variables.html" | |
# Available variables for format: $file $filepatharg $filepath $filedirarg $filedir $state $statestring $position $positionstring $duration $durationstring $volumelevel $muted $playbackrate $size $reloadtime $version $media $ext $dir | |
format = "Now playing: $dir / $file / MPC-HC $version / $positionstring/$durationstring / $size" | |
command = 'say' | |
#--------------------------------------------------------------# | |
import hexchat | |
from urllib2 import urlopen | |
from re import search, split as resplit, sub | |
def extract_text(text, id): | |
info = search('<p id="' + id + '">(.*)<\/p>', text) | |
if info is None: | |
print '[!] Error retrieving MPC info for field "' + id + '". The page format may have changed.' | |
return hexchat.EAT_ALL | |
else: | |
info = [x.strip() for x in resplit('&[^; ]+;',info.group(1)) if x != ''] | |
return str(info[0]) | |
def mpc(word, word_eol, userdata): | |
try: | |
info = urlopen(infourl).read() | |
except: | |
print '[!] Error retrieving MPC info. Is MPC running and its localhost webserver enabled?' | |
return hexchat.EAT_ALL | |
strs = ["filepatharg", "filepath", "filedirarg", "filedir", "file", "statestring", "state", "positionstring", "position", "durationstring", "duration", "volumelevel", "muted", "playbackrate", "size", "reloadtime", "version"] | |
msg = str(format) | |
filepath = "" | |
filedir = "" | |
for findme in strs: | |
val = extract_text(info, findme) | |
if findme == "file": | |
filepath = val | |
elif findme == "filedir": | |
filedir = val | |
msg = msg.replace("$" + findme, val) | |
media = filepath.rsplit('.', 1) | |
ext = media[1] | |
media = media[0] | |
dirnam = filedir.rsplit('\\', 1)[1] | |
msg = msg.replace("$ext", ext) | |
msg = msg.replace("$media", media) | |
msg = msg.replace("$dir", dirnam) | |
hexchat.command(command + ' ' + msg) | |
return hexchat.EAT_ALL | |
hexchat.hook_command('mpc', mpc, help="Usage: /mpc\n--> Says what you're watching in MPC") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment