Exaile Now Playing Xchat Script

This a Xchat script, which can send your currently playing track in Exaile, to the IRC channel you're on in Xchat. You can also control Exaile (play, pause, stop etc), or adjust the volume or the rating of the current song.

It also supports CTCP song request, so if someone sends a CTCP GETSONG to you, the currently playing track will be automatically sent through DCC, to the user who requested it.

Usage
/exaile - Prints your current playing song
/exaile play/pause/stop/next/prev - Control exaile
/exaile rating {rating 1-8} - Set the rating for the current song
/exaile volume up|down -Control the volume of exaile
/exaile send {nick} - Send the current playing song to {nick}

Download Download script as zip

Tag Tags: Python Xchat Exaile

Source

  • exaile_np.py
  1. #
  2. # Exaile Xchat plugin
  3. # This is a Xchat plugin, so place it in your ~/.xchat2 folder
  4. # Not in the plugin folder of exaile
  5. #
  6. # Send your current song to the channel your on
  7. # Written in python
  8. #
  9. # Created by Lucas van Dijk
  10. # www.return1.net
  11. #
  12.  
  13. __module_name__ = "Exaile Xchat Plugin"
  14. __module_version__ = "0.2"
  15. __module_description__ = "Xchat plugin to show serverval exaile information"
  16.  
  17. import xchat
  18. import dbus
  19. import math
  20.  
  21. # Global exaile object
  22. exaile = None
  23.  
  24. def init_dbus():
  25.     bus = dbus.SessionBus()
  26.     obj = bus.get_object('org.exaile.Exaile', '/org/exaile/Exaile')
  27.  
  28.     global exaile
  29.     exaile = dbus.Interface(obj, 'org.exaile.Exaile')
  30.  
  31. def check_exaile():
  32.     global exaile
  33.  
  34.     if exaile == None:
  35.         init_dbus()
  36.  
  37.     try:
  38.         exaile.TestService("Test 1")
  39.     except:
  40.         init_dbus()
  41.  
  42.     try:
  43.         exaile.TestService("Test 2")
  44.     except:
  45.         return False
  46.  
  47.     return True
  48.  
  49. class Config:
  50.     """
  51.         All the config data goes here
  52.         Alter these settings to modify the output of the /exaile command
  53.     """
  54.  
  55.     def __init__(self):
  56.         self.show_rating = True
  57.         self.show_time = True
  58.         self.show_album = False
  59.         self.enable_downloads = True
  60.  
  61. # Init global config object
  62. config = Config()
  63.  
  64. class SongInfo:
  65.     """Holds data about the song"""
  66.  
  67.     def __init__(self):
  68.         self.artist = exaile.GetTrackAttr("artist")
  69.         self.title = exaile.GetTrackAttr("title")
  70.         self.album = exaile.GetTrackAttr("album")
  71.  
  72.         length = float(exaile.GetTrackAttr('__length'))
  73.         length = '%d:%02d' % (length // 60, length % 60)
  74.  
  75.         self.length = length
  76.         self.position = exaile.CurrentPosition()
  77.         try:
  78.             self.rating = int(exaile.GetTrackAttr("rating"))
  79.         except:
  80.             self.rating = 0;
  81.  
  82.         self.filename = exaile.GetTrackAttr("__loc").encode("utf-8")
  83.  
  84.  
  85. class Format:
  86.     black = 1
  87.     darkblue = 2
  88.     green = 3
  89.     red = 4
  90.     darkred = 5
  91.     purple = 6
  92.     orange = 7
  93.     yellow = 8
  94.     lightgreen = 9
  95.     aqua = 10
  96.     lightblue = 11
  97.     blue = 12
  98.     violet = 13
  99.     grey = 14
  100.     lightgrey = 15
  101.     white = 16
  102.  
  103.     def color(self, code):
  104.         return "x03%02.f" % (code)
  105.  
  106.     def normal(self):
  107.         return "x0F"
  108.  
  109.     def bold(self):
  110.         return "x02"
  111.  
  112.     def reverse(self):
  113.         return "x16"
  114.  
  115.     def underline(self):
  116.         return "x1F"
  117.  
  118. def exaile_on_command(word, word_eol, userdata):
  119.     if check_exaile() == False:
  120.         xchat.prnt("Exaile is not playing")
  121.         return xchat.EAT_NONE;
  122.  
  123.     if len(word) == 1:
  124.         now_playing()
  125.     elif word[1] == 'play':
  126.         exaile_control(word)
  127.     elif word[1] == 'stop':
  128.         exaile_control(word)
  129.     elif word[1] == 'pause':
  130.         exaile_control(word)
  131.     elif word[1] == 'next':
  132.         exaile_control(word)
  133.     elif word[1] == 'previous' or word[1] == 'prev':
  134.         exaile_control(word)
  135.     elif word[1] == 'send' and len(word) == 3:
  136.         send_track(word[2])
  137.     elif word[1] == 'rating' and len(word) == 3:
  138.         new_rating = int(word[2])
  139.  
  140.         if new_rating > 8:
  141.             new_rating = 8
  142.  
  143.         if new_rating < 1:
  144.             new_rating = 1
  145.  
  146.         exaile.set_rating(new_rating)
  147.     elif word[1] == 'volume' and len(word) == 3:
  148.         if word[2] == 'up':
  149.             exaile.increase_volume(10)
  150.         else:
  151.             exaile.decrease_volume(10)
  152.     else:
  153.         print_help()
  154.  
  155.     return xchat.EAT_XCHAT
  156.  
  157. def exaile_on_ctcp(word, word_eol, userdata):
  158.     if word[0].upper() == 'GETSONG':
  159.         if check_exaile() == False:
  160.             xchat.command("notice %s Exaile is not playing at the moment." % word[1])
  161.             return
  162.  
  163.         if config.enable_downloads == False:
  164.             xchat.command("notice %s I don't want to share my songs." % word[1])
  165.             return
  166.  
  167.         send_track(word[1])
  168.  
  169. def now_playing():
  170.     song = SongInfo()
  171.  
  172.     format = Format()
  173.  
  174.     string = u'me is listening to%s%s %s - %s %s' % (format.color(format.darkblue), format.bold(), song.artist, song.title, format.normal())
  175.  
  176.     if config.show_time:
  177.         string += u'(%s/%s)' % (song.position, song.length)
  178.  
  179.     if int(song.rating) > 0 and config.show_rating:
  180.         i = 0
  181.         string += u" Rating: %s" % format.color(format.darkred)
  182.         rating = float(song.rating / 2)
  183.         while i < rating:
  184.             string += u"u25CF"
  185.             i += 1
  186.  
  187.         j = 0
  188.         if song.rating % 2 > 0:
  189.             string += u"u25D0"
  190.             j += 1
  191.  
  192.         if (song.rating / 2) < 3.5:
  193.  
  194.             while j < (4 - rating):
  195.                 string += u"u25CB"
  196.                 j += 1
  197.  
  198.         string += u'%s' % format.normal()
  199.  
  200.     if config.show_album:
  201.         string += u', Album:%s %s%s' % (format.color(format.green), song.album, format.normal())
  202.  
  203.     xchat.command(string.encode('utf-8'))
  204.  
  205. def exaile_control(word):
  206.     if word[1] == 'play':
  207.         exaile.play()
  208.     elif word[1] == 'pause':
  209.         exaile.play_pause()
  210.     elif word[1] == 'stop':
  211.         exaile.stop()
  212.     elif word[1] == 'next':
  213.         exaile.next_track()
  214.     elif word[1] == 'previous' or word[1] == 'prev':
  215.         exaile.prev_track()
  216.  
  217. def send_track(nickname):
  218.     song = SongInfo()
  219.  
  220.     xchat.command('dcc send %s "%s"' % (nickname, song.filename))
  221.  
  222. def print_help():
  223.     xchat.prnt("Usage:")
  224.     xchat.prnt("/exaile - Prints your current playing song")
  225.     xchat.prnt("/exaile play/pause/stop/next/prev - Control exaile")
  226.     xchat.prnt("/exaile rating {rating 1-8} - Set the rating for the current song")
  227.     xchat.prnt("/exaile volume up|down - Control the volume of exaile")
  228.     xchat.prnt("/exaile send {nick} - Send the current playing song to {nick}")
  229.  
  230.  
  231. xchat.hook_command('exaile', exaile_on_command)
  232. xchat.hook_print("CTCP Generic", exaile_on_ctcp)
  233.  
  234. xchat.prnt('Loaded Exaile Xchat Plugin 0.2, created by Lucas van Dijk (http://www.return1.net)')
  235.  
  236. try:
  237.     check_exaile()
  238. except:
  239.     pass
  240.