[gnome-music/wip/smart_playlists: 3/5] When song is being loaded mark it as being played in tracker



commit ba9f66df6fceab8a9d33df548d9571bae6d6d2c6
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Tue Apr 22 15:53:14 2014 +0200

    When song is being loaded mark it as being played in tracker

 gnomemusic/grilo.py  |   14 +++++++++++++-
 gnomemusic/player.py |    4 ++++
 gnomemusic/query.py  |   13 ++++++++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)
---
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 2f24f97..9414caf 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -25,12 +25,19 @@
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
 
-from gi.repository import Grl, GLib, GObject
+from gi.repository import Grl, GLib, GObject, Tracker
 from gnomemusic.query import Query
 from gnomemusic import log
 import logging
 logger = logging.getLogger(__name__)
 
+try:
+    tracker = Tracker.SparqlConnection.get(None)
+except Exception as e:
+    from sys import exit
+    logger.error("Cannot connect to tracker, error '%s'\Exiting" % str(e))
+    exit(1)
+
 
 class Grilo(GObject.GObject):
 
@@ -203,6 +210,11 @@ class Grilo(GObject.GObject):
         query = Query.get_album_for_id(album_id)
         self.tracker.query(query, self.METADATA_THUMBNAIL_KEYS, options, callback, data)
 
+    @log
+    def update_play_data(self, url, data=None):
+        # Update play time
+        query = Query.set_last_played_for_url(url)
+        tracker.update(query, 0, None)
 
 Grl.init(None)
 
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index ee43438..92fb056 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -37,6 +37,7 @@ from gettext import gettext as _
 from random import randint
 from queue import LifoQueue
 from gnomemusic.albumArtCache import AlbumArtCache
+from gnomemusic.grilo import grilo
 
 from gnomemusic import log
 import logging
@@ -404,6 +405,9 @@ class Player(GObject.GObject):
         self.emit('playlist-item-changed', self.playlist, currentTrack)
         self.emit('current-changed')
 
+        # Update play count and last date played
+        grilo.update_play_data(url)
+
     @log
     def _on_cache_lookup(self, pixbuf, path, data=None):
         if pixbuf is not None:
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index 2c3610a..d4cbb1e 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -24,6 +24,7 @@
 # modify this code, you may extend this exception to your version of the
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
+from datetime import datetime
 
 
 class Query():
@@ -254,7 +255,7 @@ class Query():
           ?song a nmm:MusicPiece ;
                 a nfo:FileDataObject
         }
-        ORDER BY nie:contentAccessed(?song)
+        ORDER BY DESC(nie:contentAccessed(?song))
     '''.replace('\n', ' ').strip()
 
     @staticmethod
@@ -329,3 +330,13 @@ class Query():
     '''.replace('\n', ' ').strip() % {'url': url}
 
         return query
+
+    @staticmethod
+    def set_last_played_for_url(url):
+        date = datetime.now().isoformat()
+        query = '''
+            DELETE { ?song nie:contentAccessed ?time }
+            INSERT { ?song nie:contentAccessed '%(date)s'}
+            WHERE  { ?song nie:url '%(url)s' }
+        '''.replace('\n', ' ').strip() % {'url': url, "date": date}
+        return query


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