[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#703053: lastmp: switch from python-libmpdclient to python-mpd



Control: tags -1 + patch

On Thu, Mar 14, 2013 at 06:22:37PM +0100, Helmut Grohne wrote:
> The lastmp package is using the python-libmpdclient (mpdclient2) module.
> This library has been abandoned upstream[1] in favour of python-mpd.
> Would it be possible to switch this tool over to the other library?

I attached a patch for this. Note that this patch is based on the
library documentation and completely untested (due to the lack of a
last.fm account). Could anyone test the patch and report back a result
to me and this bug?

The patch applies to the lastmp python script and you will have to
install python-mpd to make it work.

Thanks

Helmut
--- lastmp	2010-04-19 03:41:31.000000000 +0200
+++ lastmp.new	2013-03-15 09:48:42.000000000 +0100
@@ -5,8 +5,9 @@
 import time
 import getopt
 import signal
+import socket
 
-import mpdclient2
+import mpd
 import lastfm
 import lastfm.client
 import lastfm.config
@@ -26,11 +27,11 @@
 
 class Song:
     def __init__(self, sobj):
-        self.artist = getattr(sobj, 'artist', '')
-        self.title = getattr(sobj, 'title', '')
-        self.album = getattr(sobj, 'album', '')
-        self.length = int(getattr(sobj, 'time', 0))
-        self.file = getattr(sobj, 'file', '')
+        self.artist = sobj.get('artist', '')
+        self.title = sobj.get('title', '')
+        self.album = sobj.get('album', '')
+        self.length = int(sobj.get('time', 0))
+        self.file = sobj.get('file', '')
 
     def __eq__(self, other):
         if other == None:
@@ -64,6 +65,13 @@
         else:
             return d
 
+def mpd_connect(mpd_args):
+    mpd = mpd.MPDClient()
+    mpd.connect(mpd_args["host"], mpd_args["port"])
+    if mpd_args["password"]:
+        mpd.password(mpd_args["password"])
+    return mpd
+
 class MPDMonitor:
     def __init__(self, cli):
         self.cli = cli
@@ -74,25 +82,25 @@
         self.mpd = None
 
     def wake(self):
-        status = self.mpd.do.status()
-        song = Song(self.mpd.do.currentsong())
+        status = self.mpd.status()
+        song = Song(self.mpd.currentsong())
 
-        if not hasattr(status, 'state'):
+        if 'state' not in status:
             raise MPDAuthError
 
-        if not self.prevstatus or status.state != self.prevstatus.state:
-            self.cli.log.debug('Changed state: %s' % status.state)
+        if not self.prevstatus or status["state"] != self.prevstatus["state"]:
+            self.cli.log.debug('Changed state: %s' % status["state"])
 
-        if status.state in ('play', 'pause'):
-            pos, length = map(float, status.time.split(':'))
+        if status["state"] in ('play', 'pause'):
+            pos, length = map(float, status["time"].split(':'))
             if length == 0: length = lastfm.MAX_LEN
 
-            if status.state == 'play':
+            if status["state"] == 'play':
                 if song != self.prevsong or \
-                        self.prevstatus.state == 'stop':
+                        self.prevstatus["state"] == 'stop':
                     self.cli.log.info(u'New song: %s' % song)
                     if (self.prevsong and pos > self.sleep +
-                            FUZZ + int(status.xfade)) or \
+                            FUZZ + int(status["xfade"])) or \
                             (self.prevsong is None and
                             pos/length > lastfm.SUB_PERCENT or
                             pos > lastfm.SUB_SECONDS):
@@ -141,7 +149,7 @@
         while True:
             try:
                 if not self.mpd:
-                    self.mpd = mpdclient2.connect(**self.mpd_args)
+                    self.mpd = mpd_connect(self.mpd_args)
                     self.cli.log.info('Connected to MPD')
                     self.prevstatus = None
                     self.prevsong = None
@@ -151,12 +159,12 @@
                     self.played_enough = False
                 if self.mpd:
                     self.wake()
-            except (EOFError, mpdclient2.socket.error):
+            except (EOFError, socket.error):
                 if not failed:
                     self.cli.log.error("Can't connect or lost connection to MPD")
                 self.mpd = None
                 failed = True
-            except MPDAuthError:
+            except (MPDAuthError, mpd.CommandError):
                 if not failed:
                     self.cli.log.error("Can't read info from MPD (bad password?)")
                 failed = True

Reply to: