Package screenlets :: Package plugins :: Module LastFMProxy

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.LastFMProxy

      1  #!/usr/bin/env python
      2  # This application is released under the GNU General Public License
      3  # v3 (or, at your option, any later version). You can find the full
      4  # text of the license under http://www.gnu.org/licenses/gpl.txt.
      5  # By using, editing and/or distributing this software you agree to
      6  # the terms and conditions of this license.
      7  # Thank you for using free software!
      8
      9  # LastFMProxy API by atie
     10
     11  import os
     12  import string
     13  import gobject
     14  import mpdclient2
     15  import urllib2
     16  from GenericPlayer import GenericAPI
     17
     18 -class LastFMProxyAPI(GenericAPI):
     19          __name__ = 'LastFMProxy API'
     20          __version__ = '0.0'
     21          __author__ = 'atie'
     22          __desc__ = 'LastFMProxy API to a Music Player'
     23
     24          playerAPI = None
     25
     26          __timeout = None
     27          __interval = 3
     28
     29          callbackFn = False
     30          __curplaying = None
     31
     32 -        def __init__(self, session_bus):
     33                  # Ignore the session_bus. Initialize a mpdclient connection
     34                  GenericAPI.__init__(self, session_bus)
     35
     36          # Check if the player is active : Returns Boolean
     37          # A handle to the dbus interface is passed in : doesn't need to be used
     38 -        def is_active(self, dbus_iface):
     39                  app = mpdclient2.connect()
     40                  if not app: return False
     41                  else:
     42                          proc = os.popen("""ps axo "%p,%a" | grep "last" | grep -v grep|cut -d',' -f1""").read()
     43                          procs = proc.split('\n')
     44                          if len(procs) > 1:
     45                                  return True
     46                          else:
     47                                  return False
     48
     49
     50          # Make a connection to the Player
     51 -        def connect(self):
     52                  self.playerAPI = mpdclient2.connect()
     53
     54          # Get LastFMProxy dump
     55 -        def getdump(self):
     56                  try:
     57                          dump = urllib2.urlopen('http://localhost:1881/np').read()
     58                  except urllib2.HTTPError, e:
     59                          print "Cannot retrieve URL: HTTP Error Code", e.code
     60                  except urllib2.URLError, e:
     61                          print "Cannot retrieve URL: " + e.reason[1]
     62                  return dump
     63
     64 -        def getBetween(self, dump, first, last):
     65                  x = len(first)
     66                  begin = dump.find(first) +x
     67                  end = dump.find(last, begin)
     68                  return dump[begin:end]
     69
     70          # The following return Strings
     71          # FIXME, maybe.
     72 -        def get_title(self):
     73                  #return getattr(self.playerAPI.currentsong(), 'np_title = ', ';')
     74                  dump = self.getdump()
     75                  return self.getBetween(dump, 'np_title = \'', '\';')
     76
     77          # FIXME if necessary
     78 -        def get_album(self):
     79                  dump = self.getdump()
     80                  return self.getBetween(dump, 'np_album = \'', '\';')
     81
     82          # FIXME if necessary
     83 -        def get_artist(self):
     84                  dump = self.getdump()
     85                  return self.getBetween(dump, 'np_creator = \'', '\';')
     86
     87      # FIXME, if necessary, currently by the amazoncoverartsearch
     88 -        def get_cover_path(self):
     89                  #return os.environ['HOME']+"/.covers/"+self.get_artist()+\
     90          # " - "+self.get_album()+".jpg"
     91                  #return ""
     92                  # No need to search Amazon, one image file for now playing
     93                  #path = os.environ['HOME']+"/.covers/image_by_lfproxy.jpg"
     94                  path = os.environ['HOME']+"/.covers/"+self.get_artist()+\
     95           " - "+self.get_album()+".jpg"
     96                  dump = self.getdump()
     97                  f = open(path, 'wb')
     98                  image = urllib2.urlopen(self.getBetween(dump, 'np_image = \'',
     99                  '\'')).read()
    100                  f.write(image)
    101                  f.close()
    102                  return path
    103
    104
    105          # Returns Boolean
    106 -        def is_playing(self):
    107                  if self.playerAPI.status().state in ['play']:
    108                          return True
    109                  else: return False
    110
    111          # The following do not return any values
    112 -        def play_pause(self):
    113                  self.playerAPI.pause(1)
    114
    115 -        def next(self):
    116                  self.playerAPI.next()
    117
    118 -        def previous(self):
    119                  self.playerAPI.previous()
    120
    121 -        def register_change_callback(self, fn):
    122                  self.callback_fn = fn
    123                  # Could not find a callback signal for mpd, so just calling after some time interval
    124                  if self.__timeout:
    125                          gobject.source_remove(self.__timeout)
    126                  self.__timeout = gobject.timeout_add(self.__interval * 1000, self.info_changed)
    127                  #self.playerAPI.connect_to_signal("playingUriChanged", self.info_changed)
    128
    129 -        def info_changed(self, signal=None):
    130                  # Only call the callback function if Data has changed
    131                  if self.__curplaying != getattr(self.playerAPI.currentsong(),
    132                  'title', ''):
    133                          self.__curplaying = getattr(self.playerAPI.currentsong(), 'title', '')
    134                          self.callback_fn()
    135
    136                  if self.__timeout:
    137                          gobject.source_remove(self.__timeout)
    138                  self.__timeout = gobject.timeout_add(self.__interval * 1000, self.info_changed)
    139