Commit 252083a9 authored by Marinus Schraal's avatar Marinus Schraal

Use Grilo key for bumping play count

Use Grilo set_play_count to bump the play count and store the metadata
through grilo.
parent 9f1a6513
......@@ -60,6 +60,7 @@ class Grilo(GObject.GObject):
Grl.METADATA_KEY_FAVOURITE,
Grl.METADATA_KEY_ID,
Grl.METADATA_KEY_LYRICS,
Grl.METADATA_KEY_PLAY_COUNT,
Grl.METADATA_KEY_THUMBNAIL,
Grl.METADATA_KEY_TITLE,
Grl.METADATA_KEY_TRACK_NUMBER,
......@@ -374,5 +375,19 @@ class Grilo(GObject.GObject):
self.tracker.query(query, self.METADATA_KEYS, options, callback, None)
@log
def bump_play_count(self, media):
"""Bumps the play count of a song
Adds one to the playcount and adds it to the tracker store
:param media: A Grilo media item
"""
count = media.get_play_count()
media.set_play_count(count + 1)
# FIXME: We assume this is the tracker plugin.
# FIXME: Doing this async crashes
self.tracker.store_metadata_sync(media, [Grl.METADATA_KEY_PLAY_COUNT],
Grl.WriteFlags.NORMAL)
grilo = Grilo()
......@@ -43,6 +43,7 @@ from gettext import gettext as _, ngettext
from random import randint
from collections import deque
from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon, ArtSize
from gnomemusic.grilo import grilo
from gnomemusic.playlists import Playlists
import gnomemusic.utils as utils
playlists = Playlists.get_default()
......@@ -916,7 +917,6 @@ class Player(GObject.GObject):
@log
def scrobble_song(self, url):
# Update playlists
playlists.update_playcount(url)
playlists.update_last_played(url)
playlists.update_all_static_playlists()
......@@ -992,6 +992,7 @@ class Player(GObject.GObject):
current_media = self.get_current_media()
self.scrobbled = True
if current_media:
grilo.bump_play_count(self.get_current_media())
just_played_url = self.get_current_media().get_url()
t = Thread(target=self.scrobble_song, args=(just_played_url,))
t.setDaemon(True)
......
......@@ -235,11 +235,6 @@ class Playlists(GObject.GObject):
GLib.PRIORITY_LOW, None,
tag_created_cb, playlist)
@log
def update_playcount(self, song_url):
query = Query.update_playcount(song_url)
self.tracker.update(query, GLib.PRIORITY_LOW, None)
@log
def update_last_played(self, song_url):
cur_time = time.strftime(sparql_dateTime_format, time.gmtime())
......
......@@ -216,6 +216,7 @@ class Query():
nie:title(nmm:musicAlbum(?song)) AS ?album
nfo:duration(?song) AS ?duration
?tag AS ?favourite
nie:usageCounter(?song) AS ?play_count
{
%(where_clause)s
?song a nmm:MusicPiece ;
......@@ -274,6 +275,7 @@ class Query():
nmm:trackNumber(?song) AS ?track_number
nmm:setNumber(nmm:musicAlbumDisc(?song)) AS ?album_disc_number
?tag AS ?favourite
nie:usageCounter(?song) AS ?play_count
WHERE {
?song a nmm:MusicPiece ;
a nfo:FileDataObject ;
......@@ -307,6 +309,7 @@ class Query():
nie:title(nmm:musicAlbum(?song)) AS ?album
nfo:duration(?song) AS ?duration
?tag AS ?favourite
nie:usageCounter(?song) AS ?play_count
WHERE {
?playlist a nmm:Playlist ;
a nfo:MediaList ;
......@@ -404,26 +407,6 @@ class Query():
}
return query
@staticmethod
def update_playcount(song_url):
query = """
INSERT OR REPLACE { ?song nie:usageCounter ?playcount . }
WHERE {
SELECT
IF(bound(?usage), (?usage + 1), 1) AS ?playcount
?song
WHERE {
?song a nmm:MusicPiece .
OPTIONAL { ?song nie:usageCounter ?usage . }
FILTER ( nie:url(?song) = "%(song_url)s" )
}
}
""".replace("\n", " ").strip() % {
'song_url': song_url
}
return query
@staticmethod
def update_last_played(song_url, time):
query = """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment