screenlets.plugins.LastFMProxy

Package screenlets :: Package plugins :: Module LastFMProxy |
[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.LastFMProxy

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