Skip to content

Commit

Permalink
Increase play count and update now playing on /scrobble rather than /…
Browse files Browse the repository at this point in the history
…stream
  • Loading branch information
spl0k committed Jun 2, 2024
1 parent 88997d5 commit f178410
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
14 changes: 13 additions & 1 deletion supysonic/api/annotation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2013-2022 Alban 'spl0k' Féron
# Copyright (C) 2013-2024 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.

Expand All @@ -12,6 +12,7 @@
from ..db import Track, Album, Artist, Folder
from ..db import StarredTrack, StarredAlbum, StarredArtist, StarredFolder
from ..db import RatingTrack, RatingFolder
from ..db import now
from ..lastfm import LastFm
from ..listenbrainz import ListenBrainz

Expand Down Expand Up @@ -175,6 +176,17 @@ def scrobble():
t, submission = map(request.values.get, ("time", "submission"))
t = int(t) / 1000 if t else int(time.time())

if not submission or submission not in (True, "true", "True", 1, "1"):
date = now()
res.play_count = res.play_count + 1
res.last_play = date
res.save()

user = request.user
user.last_play = res
user.last_play_date = date
user.save()

lfm = LastFm(current_app.config["LASTFM"], request.user)
lbz = ListenBrainz(current_app.config["LISTENBRAINZ"], request.user)

Expand Down
13 changes: 2 additions & 11 deletions supysonic/api/media.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2013-2022 Alban 'spl0k' Féron
# Copyright (C) 2013-2024 Alban 'spl0k' Féron
# 2018-2019 Carey 'pR0Ps' Metcalfe
#
# Distributed under terms of the GNU AGPLv3 license.
Expand All @@ -24,7 +24,7 @@
from zipstream import ZipStream

from ..cache import CacheMiss
from ..db import Track, Album, Artist, Folder, now
from ..db import Track, Album, Artist, Folder
from ..covers import EXTENSIONS

from . import get_entity, get_entity_id, api_routing
Expand Down Expand Up @@ -208,15 +208,6 @@ def handle_transcoding():
else:
response = send_file(res.path, mimetype=dst_mimetype, conditional=True)

res.play_count = res.play_count + 1
res.last_play = now()
res.save()

user = request.user
user.last_play = res
user.last_play_date = now()
user.save()

return response


Expand Down
5 changes: 4 additions & 1 deletion tests/api/test_annotation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2022 Alban 'spl0k' Féron
# Copyright (C) 2017-2024 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.

Expand Down Expand Up @@ -184,6 +184,9 @@ def test_scrobble(self):
self._make_request("scrobble", {"id": str(self.trackid)})
self._make_request("scrobble", {"id": str(self.trackid), "submission": True})
self._make_request("scrobble", {"id": str(self.trackid), "submission": False})
self.assertEqual(
Track[self.trackid].play_count, 4
) # (GET+POST) * (missing submission + submission False)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions tests/api/test_media.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2022 Alban 'spl0k' Féron
# Copyright (C) 2017-2024 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.

Expand Down Expand Up @@ -94,7 +94,7 @@ def test_stream(self):
) as rv:
self.assertEqual(rv.status_code, 200)
self.assertEqual(len(rv.data), 23)
self.assertEqual(Track[self.trackid].play_count, 1)
self.assertEqual(Track[self.trackid].play_count, 0)

def test_download(self):
self._make_request("download", error=10)
Expand Down
3 changes: 2 additions & 1 deletion tests/api/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,11 @@ def test_search3(self):

# empty query
rv, child = self._make_request("search3", {"query": ""}, tag="searchResult3")
self.assertEqual(len(child), 27) # 3 + 3*2 + 3*2*3
self.assertEqual(len(child), 27) # 3 + 3*2 + 3*2*3
self.assertEqual(len(self._xpath(child, "./artist")), 3)
self.assertEqual(len(self._xpath(child, "./album")), 6)
self.assertEqual(len(self._xpath(child, "./song")), 18)


if __name__ == "__main__":
unittest.main()

0 comments on commit f178410

Please sign in to comment.