pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Python pastebin - collaborative debugging tool View Help


Posted by Csigaa on Sat 22 Nov 19:45
report abuse | View followups from Anonymous | download | new post

  1. """
  2. xchat_mplayer by Csigaa
  3. Prints 'NICK is playing FILENAME [MPlayer version]' action to the active channel/dialog.
  4.  
  5. This version is entirely based on procfs, so finally works on
  6. all POSIX-compilant OS (*BSD, Solaris, Linux, etc) without calling any commands
  7. (except for version number, but it is now optional)
  8.  
  9. NOTE FOR PASTEBIN: save the file as xchat_mplayer.py to your xchat directory
  10.  
  11. Contact: csigaa@gmail.com
  12. License: Beer-ware (by phk) ;p
  13. ----------------------------------------------------------------------------
  14. "THE BEER-WARE LICENSE" (Revision 42):
  15. Csigaa wrote this file. As long as you retain this notice you
  16. can do whatever you want with this stuff. If we meet some day, and you think
  17. this stuff is worth it, you can buy me a beer in return
  18. ----------------------------------------------------------------------------
  19.  
  20. """
  21.  
  22. __module_name__ = "xchat_mplayer"
  23. __module_version__ = "1.04"
  24. __module_description__ = "XChat-MPlayer 'now playing' script by Csigaa"
  25.  
  26. import xchat
  27. import os
  28. import re
  29.  
  30. if os.name != 'posix':
  31.     raise NotImplementedError,'non-POSIX systems are not supported'
  32.  
  33. def getFilename():
  34.     # Get the video file name (identified by naming pattern) from procfs
  35.    
  36.     # list&walk the directory contents
  37.     proclist = sorted(os.listdir('/proc'))
  38.     for proc in os.listdir('/proc'):
  39.         # if it is a directory with a name of digits only
  40.         if os.path.isdir('/proc/'+proc) and re.match('[0-9]+',proc):
  41.             try:
  42.                 exe = os.readlink('/proc/'+proc+'/exe').split('/')
  43.                 # if executable name is 'mplayer'
  44.                 if exe.pop() == 'mplayer':
  45.                     filelist = os.listdir('/proc/'+proc+'/fd')
  46.                     # walk the list of open files
  47.                     for file in filelist:
  48.                         try:
  49.                             path = os.readlink('/proc/'+proc+'/fd/'+file)
  50.                             if re.match('.*(avi|mpg|mkv|mp4|nuv|ogg|ogm|wmv|iso|img)$',path,re.I):
  51.                                 # if video filename found, return
  52.                                 return path.split('/').pop()
  53.                         except:
  54.                             # if link not readable, skip
  55.                             continue
  56.             except:
  57.                 # if process directory not readable, skip
  58.                 continue
  59.     return None
  60.  
  61. def getVersion():
  62.     try:
  63.         import commands
  64.         try:
  65.             # 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)
  66.             ver = re.findall('^MPlayer\s\d[.]\d+\w{0,4}',commands.getoutput('mplayer'))[0]
  67.         except:
  68.             # SVN version, begins with 'dev-SVN' (revision number maximum 8 digits); dev- string is removed for shortness
  69.             ver = re.sub('dev-','',re.findall('^MPlayer\sdev-SVN-r\d{0,8}',commands.getoutput('mplayer'))[0])
  70.         return ver
  71.     except:
  72.         # if any error occured above, return only the player name
  73.         return 'MPlayer'
  74.  
  75. def mplayer_msg(world,world_eol,userdata):
  76.     fn = getFilename()
  77.     ver = getVersion()
  78.     if type(fn) == str:
  79.         # we've got a string for fn
  80.         irccmd = 'me is playing '+fn+' ['+ver+']'
  81.         xchat.command(irccmd)
  82.         return xchat.EAT_ALL
  83.     else:
  84.         # we've got None (or something went very-very wrong)
  85.         return xchat.EAT_ALL
  86.  
  87. xchat.hook_command('mplayer',mplayer_msg)
  88. xchat.prnt('XChat-MPlayer '+__module_version__+' loaded - use /mplayer to print now playing message')

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me