Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- xchat_mplayer by Csigaa, mostly rewritten by Pierre Collignon (03/2011)
- Edited by Daniel Gimpelevich to prevent failure on missing ID3 tags
- Prints a nice "now playing" message on the current irc chan
- /!\ You need mediainfo to run this script /!\
- This rewrite breaks the first dev's wish of never using external cmds, but
- we need these infos!
- NOTE FOR PASTEBIN: save the file as xchat_mplayer.py to your xchat directory
- Contact: [email protected] and [email protected] and [email protected]
- License: Beer-ware (by phk) ;p
- ----------------------------------------------------------------------------
- "THE BEER-WARE LICENSE" (Revision 42):
- Csigaa wrote this file. As long as you retain this notice you
- can do whatever you want with this stuff. If we meet some day, and you think
- this stuff is worth it, you can buy me a beer in return
- ----------------------------------------------------------------------------
- """
- __module_name__ = "xchat_mplayer"
- __module_version__ = "1.05 Fork 1.3"
- __module_description__ = "XChat-MPlayer 'now playing' script by Csigaa and P.C, edited by DonkeyHotei"
- import xchat
- import os
- import re
- import subprocess as sub
- if os.name != 'posix':
- raise NotImplementedError,'non-POSIX systems are not supported'
- # Reads the mediainfo bits by actually calling the command
- def readMediaInfo(path):
- rawData = sub.Popen('mediainfo ' + path.replace("(","\\(").replace(")","\\)").replace(" ","\\ "), shell = True, stdout = sub.PIPE).communicate()[0];
- lines = rawData.split('\n');
- hashTable = {};
- currentCategory = '';
- for line in lines:
- keyValue = line.split(':');
- value = '';
- if len(keyValue) == 1:
- if len(keyValue[0]) > 0:
- currentCategory = keyValue[0];
- hashTable[currentCategory] = {};
- else:
- hashTable[currentCategory][keyValue[0].strip()] = keyValue[1].strip();
- return hashTable;
- def getEntry(category,tag,hashTable):
- try:
- result = hashTable[category][tag]
- except KeyError:
- result = ''
- return result;
- def buildDescription(hashTable):
- artist = getEntry('General','Performer',hashTable);
- track = getEntry('General','Track name',hashTable);
- duration = getEntry('General','Duration',hashTable);
- bitrate = getEntry('General','Overall bit rate',hashTable);
- frequency = getEntry('Audio','Sampling rate',hashTable);
- album = getEntry('General','Album',hashTable);
- albumDate = getEntry('General','Recorded date',hashTable);
- fileSize = getEntry('General','File size',hashTable);
- completeName = artist + ' - ' + track;
- if completeName == ' - ':
- completeName = os.readlink(getEntry('General','Complete name',hashTable))
- completeName = completeName.split('/')
- completeName = completeName.pop()
- profile = '';
- if 'Format profile' in hashTable['Audio']:
- profile = ' ' + hashTable['Audio']['Format profile'] + ' ';
- mode = '';
- if 'Mode' in hashTable['Audio']:
- mode = ' ' + hashTable['Audio']['Mode'] + ' ';
- wholeFormat = hashTable['Audio']['Format'] + profile + mode;
- completeDesc = '♪♪♪ ' + completeName + ' : ' + duration + ' : ' + bitrate + ' : ' + wholeFormat + ' ♪♪♪ ' + frequency + ' ♪♪♪ ' + album + ' (' + albumDate + ')' + ' ♪♪♪ ';
- return completeDesc;
- def getFilename():
- # Get the video file name (identified by naming pattern) from procfs
- # list&walk the directory contents
- proclist = sorted(os.listdir('/proc'))
- for proc in os.listdir('/proc'):
- # if it is a directory with a name of digits only
- if os.path.isdir('/proc/'+proc) and re.match('[0-9]+',proc):
- try:
- exe = os.readlink('/proc/'+proc+'/exe').split('/')
- # if executable name is 'mplayer'
- exe_name = exe.pop()
- if exe_name == 'mplayer' or exe_name == 'mplayer2':
- filelist = os.listdir('/proc/' + proc + '/fd')
- # walk the list of open files
- for file in filelist:
- try:
- path = '/proc/' + proc + '/fd/' + file
- if re.match('.*(avi|mpg|mkv|mp4|nuv|ogg|ogm|wmv|iso|img|mp3|flac)$', os.readlink(path), re.I):
- # if filename found, return
- return path;
- except:
- # if link not readable, skip
- continue
- except:
- # if process directory not readable, skip
- continue
- return None
- def getVersion():
- try:
- import commands
- try:
- # release version, begins with number (incl. optional rc sign with maximum 2 digits); if no match, exception occurs (empty list - 0 index is out of range)
- ver = re.findall('^MPlayer\s\d[.]\d+\w{0,4}',commands.getoutput('mplayer'))[0]
- except:
- # SVN version, begins with 'SVN' (revision number maximum 6 digits)
- ver = re.findall('^MPlayer\sSVN-r\d{0,6}',commands.getoutput('mplayer'))[0]
- return ver
- except:
- # if any error occured above, return only the player name
- return 'MPlayer'
- def mplayer_msg(world,world_eol,userdata):
- fileName = getFilename();
- if type(fileName).__name__ != 'NoneType':
- irccmd = 'me is now playing ' + buildDescription(readMediaInfo(getFilename())) + 'with ' + getVersion() + ' for Linux d[O_o]b';
- xchat.command(irccmd)
- else:
- print 'You are not playing anything !';
- return xchat.EAT_ALL
- xchat.hook_command('mplayer', mplayer_msg)
- xchat.prnt('XChat-MPlayer '+__module_version__+' loaded - use /mplayer to print now playing message')
Advertisement
Add Comment
Please, Sign In to add comment