From 596ddaacdb80b54d0ee53ce2279b55c65a30a5e9 Mon Sep 17 00:00:00 2001 From: warwickh Date: Wed, 8 Sep 2021 16:25:36 +1000 Subject: [PATCH 1/6] First working scrobble service --- addon.xml | 17 +++--- lib/libsonic/connection.py | 4 +- resources/language/English/strings.po | 3 + resources/language/French/strings.po | 4 ++ resources/language/German/strings.po | 4 ++ resources/settings.xml | 1 + service.py | 86 +++++++++++++++++++++++++++ 7 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 service.py diff --git a/addon.xml b/addon.xml index bce25b5..05ba157 100644 --- a/addon.xml +++ b/addon.xml @@ -1,13 +1,14 @@ - - - - - - - audio - + + + + + + + audio + + Subsonic music addon for Kodi. Extension Subsonic pour Kodi. diff --git a/lib/libsonic/connection.py b/lib/libsonic/connection.py index ff7b22a..07ab583 100644 --- a/lib/libsonic/connection.py +++ b/lib/libsonic/connection.py @@ -230,9 +230,11 @@ class Connection(object): viewName = '%s.view' % methodName req = self._getRequest(viewName) + xbmc.log("Pinging %s"%str(req.full_url),xbmc.LOGDEBUG) try: res = self._doInfoReq(req) - except: + except Exception as e: + print("Ping failed %s"%e) return False if res['status'] == 'ok': return True diff --git a/resources/language/English/strings.po b/resources/language/English/strings.po index a176b9c..cf0e1e6 100644 --- a/resources/language/English/strings.po +++ b/resources/language/English/strings.po @@ -173,3 +173,6 @@ msgctxt "#30043" msgid "Merge album folders" msgstr "" +msgctxt "#30044" +msgid "Scrobble to Last.FM" +msgstr "" diff --git a/resources/language/French/strings.po b/resources/language/French/strings.po index fbbdd11..88f7515 100644 --- a/resources/language/French/strings.po +++ b/resources/language/French/strings.po @@ -172,3 +172,7 @@ msgstr "" msgctxt "#30043" msgid "Merge album folders" msgstr "" + +msgctxt "#30044" +msgid "Scrobble to Last.FM" +msgstr "" diff --git a/resources/language/German/strings.po b/resources/language/German/strings.po index 2a0bdd6..46ba647 100644 --- a/resources/language/German/strings.po +++ b/resources/language/German/strings.po @@ -171,3 +171,7 @@ msgstr "" msgctxt "#30043" msgid "Merge album folders" msgstr "" + +msgctxt "#30044" +msgid "Scrobble to Last.FM" +msgstr "" diff --git a/resources/settings.xml b/resources/settings.xml index bd921f0..0f09285 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -27,6 +27,7 @@ + diff --git a/service.py b/service.py new file mode 100644 index 0000000..a321a3f --- /dev/null +++ b/service.py @@ -0,0 +1,86 @@ +import re +import xbmc +import xbmcvfs +import os +import xbmcaddon +# Add the /lib folder to sys +sys.path.append(xbmcvfs.translatePath(os.path.join(xbmcaddon.Addon("plugin.audio.subsonic").getAddonInfo("path"), "lib"))) + +import libsonic + +from simpleplugin import Plugin +from simpleplugin import Addon + +# Create plugin instance +plugin = Plugin() +connection = None + +scrobbled = False + +def get_connection(): + global connection + + if connection==None: + connected = False + # Create connection + try: + connection = libsonic.Connection( + baseUrl=Addon().get_setting('subsonic_url'), + username=Addon().get_setting('username', convert=False), + password=Addon().get_setting('password', convert=False), + port=Addon().get_setting('port'), + apiVersion=Addon().get_setting('apiversion'), + insecure=Addon().get_setting('insecure'), + legacyAuth=Addon().get_setting('legacyauth'), + useGET=Addon().get_setting('useget'), + ) + connected = connection.ping() + except: + pass + + if connected==False: + popup('Connection error') + return False + + return connection + +def scrobble_track(track_id): + connection = get_connection() + + if connection==False: + return + res = connection.scrobble(track_id) + xbmc.log("response %s"%(scrobble), xbmc.LOGINFO) + if res['status'] == 'ok': + return True + else: + return False + +if __name__ == '__main__': + monitor = xbmc.Monitor() + + while not monitor.abortRequested(): + if monitor.waitForAbort(10): + break + if (xbmc.getCondVisibility("Player.HasMedia")): + try: + if(Addon().get_setting('scrobble')): + currentFileName = xbmc.getInfoLabel("Player.Filenameandpath") + currentFileProgress = xbmc.getInfoLabel("Player.Progress") + pattern = re.compile(r'plugin:\/\/plugin\.audio\.subsonic\/\?action=play_track&id=(.*?)&') + currentTrackId = re.findall(pattern, currentFileName)[0] + #xbmc.log("Name %s Id %s Progress %s"%(currentFileName,currentTrackId,currentFileProgress), xbmc.LOGDEBUG) + if (int(currentFileProgress)<50): + scrobbled = False + elif (int(currentFileProgress)>=50 and scrobbled == False): + xbmc.log("Scrobbling Track Id %s"%(currentTrackId), xbmc.LOGDEBUG) + scrobble_track(currentTrackId) + scrobbled = True + else: + pass + except Exception as e: + xbmc.log("Script failed %e"%e, xbmc.LOGINFO) + else: + pass + #xbmc.log("Playing stopped", xbmc.LOGINFO) + From 8c4f31db057d0110b27ca8cafee8ee0abc22b8d0 Mon Sep 17 00:00:00 2001 From: warwickh Date: Wed, 8 Sep 2021 16:47:08 +1000 Subject: [PATCH 2/6] More efficient settings and return value --- service.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/service.py b/service.py index a321a3f..ab7ad35 100644 --- a/service.py +++ b/service.py @@ -15,6 +15,7 @@ from simpleplugin import Addon plugin = Plugin() connection = None +scrobbleEnabled = Addon().get_setting('scrobble') scrobbled = False def get_connection(): @@ -48,9 +49,9 @@ def scrobble_track(track_id): connection = get_connection() if connection==False: - return + return False res = connection.scrobble(track_id) - xbmc.log("response %s"%(scrobble), xbmc.LOGINFO) + #xbmc.log("response %s"%(res), xbmc.LOGINFO) if res['status'] == 'ok': return True else: @@ -64,7 +65,7 @@ if __name__ == '__main__': break if (xbmc.getCondVisibility("Player.HasMedia")): try: - if(Addon().get_setting('scrobble')): + if(scrobbleEnabled): currentFileName = xbmc.getInfoLabel("Player.Filenameandpath") currentFileProgress = xbmc.getInfoLabel("Player.Progress") pattern = re.compile(r'plugin:\/\/plugin\.audio\.subsonic\/\?action=play_track&id=(.*?)&') @@ -74,8 +75,9 @@ if __name__ == '__main__': scrobbled = False elif (int(currentFileProgress)>=50 and scrobbled == False): xbmc.log("Scrobbling Track Id %s"%(currentTrackId), xbmc.LOGDEBUG) - scrobble_track(currentTrackId) - scrobbled = True + success = scrobble_track(currentTrackId) + if success: + scrobbled = True else: pass except Exception as e: From 150b5012da03d4c7501086c9f16073778fa089aa Mon Sep 17 00:00:00 2001 From: warwickh Date: Wed, 8 Sep 2021 17:00:23 +1000 Subject: [PATCH 3/6] Update readme to include scrobbling --- README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e139f1a..cc4fd9e 100644 --- a/README.md +++ b/README.md @@ -16,24 +16,23 @@ Leia compatible version available in alternate branch * Download songs * Star songs * Navidrome compatibility added (please report any issues) +* Scrobble to Last.FM ## Installation +From repository +[repository.warwickh-0.9.0.zip](https://github.com/warwickh/repository.warwickh/raw/master/matrix/zips/repository.warwickh/repository.warwickh-0.9.0.zip) (Please report any issues) + +From GitHub * Click the code button and download * Enable unknown sources and install from zip in Kodi - + or - * Navigate to your `.kodi/addons/` folder * Clone this repository: `git clone https://github.com/warwickh/plugin.audio.subsonic.git` * (Re)start Kodi. - Note: You may need to install dependencies manually if installing this way. I recommend installing from zip first, then updating using git clone -Repository installation now available -[repository.warwickh-0.9.0.zip](https://github.com/warwickh/repository.warwickh/raw/master/matrix/zips/repository.warwickh/repository.warwickh-0.9.0.zip) (Please report any issues) - ## TODO -* Scrobble to Last.FM (http://forum.kodi.tv/showthread.php?tid=195597&pid=2429362#pid2429362) * Improve the caching system * Search filter GUI for tracks and albums From 23359734334cb580a462a3e54153e3e92ea6bb32 Mon Sep 17 00:00:00 2001 From: Warwick Harris Date: Thu, 9 Sep 2021 09:40:18 +1000 Subject: [PATCH 4/6] Some error fixes --- service.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/service.py b/service.py index ab7ad35..db1cbf7 100644 --- a/service.py +++ b/service.py @@ -18,6 +18,11 @@ connection = None scrobbleEnabled = Addon().get_setting('scrobble') scrobbled = False +def popup(text, time=5000, image=None): + title = plugin.addon.getAddonInfo('name') + icon = plugin.addon.getAddonInfo('icon') + xbmc.executebuiltin('Notification(%s, %s, %d, %s)' % (title, text, + time, icon)) def get_connection(): global connection @@ -53,13 +58,15 @@ def scrobble_track(track_id): res = connection.scrobble(track_id) #xbmc.log("response %s"%(res), xbmc.LOGINFO) if res['status'] == 'ok': + popup('Scrobbled track') return True else: + popup('Scrobble failed') return False if __name__ == '__main__': monitor = xbmc.Monitor() - + xbmc.log("Service started", xbmc.LOGINFO) while not monitor.abortRequested(): if monitor.waitForAbort(10): break @@ -80,6 +87,8 @@ if __name__ == '__main__': scrobbled = True else: pass + except IndexError: + print ("Not a Subsonic track") except Exception as e: xbmc.log("Script failed %e"%e, xbmc.LOGINFO) else: From 0932e650f8050033cf11c0affcb77abb90c4f195 Mon Sep 17 00:00:00 2001 From: warwickh Date: Thu, 9 Sep 2021 11:48:06 +1000 Subject: [PATCH 5/6] Notify service start --- service.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/service.py b/service.py index db1cbf7..5190f6c 100644 --- a/service.py +++ b/service.py @@ -58,15 +58,16 @@ def scrobble_track(track_id): res = connection.scrobble(track_id) #xbmc.log("response %s"%(res), xbmc.LOGINFO) if res['status'] == 'ok': - popup('Scrobbled track') + popup("Scrobbled track") return True else: - popup('Scrobble failed') + popup("Scrobble failed") return False if __name__ == '__main__': monitor = xbmc.Monitor() - xbmc.log("Service started", xbmc.LOGINFO) + xbmc.log("Subsonic service started", xbmc.LOGINFO) + popup("Subsonic service started") while not monitor.abortRequested(): if monitor.waitForAbort(10): break @@ -89,8 +90,9 @@ if __name__ == '__main__': pass except IndexError: print ("Not a Subsonic track") + scrobbled = True except Exception as e: - xbmc.log("Script failed %e"%e, xbmc.LOGINFO) + xbmc.log("Subsonic service failed %e"%e, xbmc.LOGINFO) else: pass #xbmc.log("Playing stopped", xbmc.LOGINFO) From 67bd25496e1718c54e01d7cd6e33f01dac72543c Mon Sep 17 00:00:00 2001 From: Warwick Harris Date: Thu, 9 Sep 2021 12:10:43 +1000 Subject: [PATCH 6/6] Testing when setting doesn't exist --- service.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/service.py b/service.py index 5190f6c..835a6f4 100644 --- a/service.py +++ b/service.py @@ -15,7 +15,11 @@ from simpleplugin import Addon plugin = Plugin() connection = None -scrobbleEnabled = Addon().get_setting('scrobble') +try: + scrobbleEnabled = Addon().get_setting('scrobble') +except: + scrobbleEnabled = False + scrobbled = False def popup(text, time=5000, image=None):