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

Source Code for Module screenlets.plugins.LastFMProxy

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