SHARE
TWEET

Untitled

a guest Jun 1st, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. # encoding: utf-8
  3.  
  4. __module_name__ = "(He)xchat Audacious Now Playing"
  5. __module_author__ = "dreamer2908"
  6. __module_version__ = "1.0"
  7. __module_description__ = "Get NP information from Audacious"
  8.  
  9. import xchat, sys, os, math
  10.  
  11. position = 0
  12. length = 0
  13. artist = u''
  14. album = u''
  15. tracknumber = 0
  16. title = u''
  17. bitrate = 0
  18. samplerate = 0
  19. fmt = u''
  20. quality = u''
  21.  
  22. def getAuIFace():
  23.     from dbus import Bus, DBusException
  24.     bus = Bus(Bus.TYPE_SESSION)
  25.     try:
  26.         return bus.get_object('org.mpris.audacious', '/Player')
  27.     except: # DBusException: # catch all exceptions
  28.         xchat.prnt("Either Audacious is not running or you have something wrong with your D-Bus setup.")
  29.         return None
  30.  
  31. def getValue(auData, key, keyFB, default):
  32.     try:
  33.         return auData[key]
  34.     except KeyError:
  35.         try:
  36.             return auData[keyFB]
  37.         except:
  38.             return default
  39.     except:
  40.         return default
  41.  
  42. def toUtf8(text):
  43.     return unicode(text).encode('utf-8')
  44.  
  45. def getNpInfo():
  46.     import urllib
  47.     global position, artist, album, tracknumber, title, bitrate, samplerate, length, fmt, quality
  48.  
  49.     auIFace = getAuIFace()
  50.  
  51.     if auIFace != None:
  52.         auData = auIFace.GetMetadata()
  53.         try:
  54.             position = auIFace.PositionGet() / 1000
  55.         except:
  56.             position = 0
  57.         artist = getValue(auData, 'artist', '', None)
  58.         album = getValue(auData, 'album', '', None)
  59.         tracknumber = getValue(auData, 'tracknumber', '', -1)
  60.         title = getValue(auData, 'title', '', '')
  61.         bitrate = '%d kbps' % getValue(auData, 'bitrate', 'audio-bitrate', 0)
  62.  
  63.         # dbus.String(u'quality'): dbus.String(u'Stereo, 44100 Hz', variant_level=1),
  64.         quality = getValue(auData, 'quality', '', "Stereo, 0 Hz")
  65.         tmp = quality.split(', ')
  66.         samplerate = tmp[len(tmp) - 1]
  67.         length = getValue(auData, 'mtime', '', 0) / 1000
  68.  
  69.         # get format from file extention
  70.         # or dbus.String(u'codec'): dbus.String(u'MPEG-1 layer 3', variant_level=1),
  71.         location = urllib.unquote(getValue(auData, 'location', '', ''))
  72.         namae, ext = os.path.splitext(location)
  73.         if len(ext) > 0:
  74.             fmt = ext.upper().strip('.')
  75.         else:
  76.             fmt = 'Unknown'
  77.        
  78.         if len(title) < 1:
  79.             dirName, fileName = os.path.split(location)
  80.             title = fileName
  81.  
  82.         return True
  83.     return False
  84.  
  85. def formatTime(time):
  86.     if time >= 3600:
  87.         result = '%d:' % math.floor(time / 3600)
  88.     else:
  89.         result = ''
  90.     time = time % 3600
  91.     result += '%02d' % math.floor(time / 60)
  92.     time = time % 60
  93.     result += ':%02d' % math.floor(time)
  94.     return result
  95.  
  96. def nowPlaying(word, word_eol, userdata):
  97.     if getNpInfo():  
  98.         if len(title) < 1:
  99.             text = "me is playing nothing on Audacious"
  100.         else:
  101.             text = "me is playing on Audacious: "
  102.             text += '[ %s / %s ] ' % (formatTime(position), formatTime(length))
  103.  
  104.             text += '\"' + title + '\" '
  105.             if artist != None:
  106.                 text += 'by "%s" ' % artist
  107.             elif album != None:
  108.                 text += 'by "Unknown artist" '
  109.  
  110.             if album != None:
  111.                 if tracknumber > 0:
  112.                     text += '(track #%d' % tracknumber + ' of album \"' + album + '\") '
  113.                 else:
  114.                     text += '(album \"' +  album + '\") '
  115.  
  116.             text += '| ' + fmt + ' | ' + samplerate + ' | ' + bitrate
  117.  
  118.         xchat.command(text)
  119.  
  120.     return xchat.EAT_ALL
  121.  
  122.  
  123. def nowPlaying2(word, word_eol, userdata):
  124.     if getNpInfo():  
  125.         if len(title) < 1:
  126.             text = "me is playing nothing on Audacious"
  127.         else:
  128.             text = "me > "
  129.  
  130.             if artist != None:
  131.                 text += '%s - ' % artist
  132.             elif album != None:
  133.                 text += 'Unknown artist - '
  134.  
  135.             text += '%s ' % title
  136.  
  137.             if album != None:
  138.                 if tracknumber > 0:
  139.                     text += ' - [ %s #%d ] ' % (album, tracknumber)
  140.                 else:
  141.                     text += ' - [ %s ] ' % (album)
  142.  
  143.             text += '- [ %s / %s ] ' % (formatTime(position), formatTime(length))
  144.  
  145.         xchat.command(text)
  146.  
  147.     return xchat.EAT_ALL
  148.  
  149. def test(word, word_eol, userdata):
  150.     auIFace = getAuIFace()
  151.     auData = auIFace.GetMetadata()
  152.     print(auData)
  153.     print(auIFace.PositionGet())
  154.    
  155.     return xchat.EAT_ALL
  156.  
  157. xchat.hook_command("aud", nowPlaying, help="Displays current playing song in Audacious (long).")
  158. xchat.hook_command("aud2", nowPlaying2, help="Displays current playing song in Audacious (short).")
  159. xchat.hook_command("audt", test, help="Test Audacious")
  160. xchat.prnt(u'%s v%s plugin loaded' % (__module_name__, __module_version__))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top