(2023-02-05, 04:07)jepsizofye Wrote: but ive never used an xbmc monitor so im not sure when onplaybackstarts fires
Good morning!!! So, i'm not so familiar too with the monitor thing hahaha, but, I think that is fired when the playback starts, not the song or video itself, when the player starts. and it's fired just once until the player stops. if some song finish and the next one start, onPlayBackStarted() will not fired again, because it's already stared and the player no stopped yet.
for this reason I'm made:
python:
if current_song == '':
monitor.onPlayBackStarted()
current_song = song_title
because current_song it's set empty just when script starts with kodi, and then when playback stared the current song will be the actual song, when playbackstop the current song will be set empty again. When playback starts againg, the loop is restarted.
(2023-02-05, 03:50)jbinkley60 Wrote: This appears to be a scope variable issue I mentioned earlier.
Not yet
: I made some heavy tests here and this is the result:
the songs has this userratings:
Then I play "Reach - Wake Me Up" until it get the rating 2 and skipped. The rating 2 must be saved on "Reach - Wake Me Up" but:
the song reach (irony?) the rating 2:
Song skipped and the the next song playing (noted the orange color on "Soundgarden - Kickstand)
and the result:
The first song did not receive the rating, and the second one changed from 5 to 3. This change it's because the original rating from Kickstand was 5 and the plus 2 divided by 2 is equal to 3.5. then is set 3.
(2023-02-05, 04:07)jepsizofye Wrote: so, see if you can set "song_id" from onPlayBackStarted on line 15 instead
Then I test this suggestion, but as I mentioned in a start this post, onPlayBackStarted() is fired just once, so, if I move xbmc.executeJSONRPC to inside the function "def onPlayBackStarted(self):" the song_id will never be updated.
so... at least I have a very good sleep hahahaha.
And i wanna ask: My code? It's in good shape? The problem with living in a small town in the interior of Brazil is this... I don't know anyone here who knows python.... or who uses kodi....
EDIT:
I added the option to hide notification. Just keep the "Song Skipped". This will be completely remove when (if) the service has been completed:
python:
import xbmc
import xbmcaddon
import json
import time
addon = xbmcaddon.Addon()
addon_name = addon.getAddonInfo('name')
class Monitor(xbmc.Monitor):
def __init__(self):
self.is_library_song = False
pass
def status_check(self):
# perform monitor status check
pass
def onPlayBackStarted(self):
self.is_library_song = True
# When the song ends, either by user action, or when the song is
# the last in the playlist and the "repeat" function is disabled.
def onPlayBackStopped(self, new_rating, current_time):
if self.is_library_song and current_time > 5:
# Saving the new rating as the song stops.
xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"AudioLibrary.SetSongDetails","params":{"songid": %d, "userrating":%d},"id":1}' % (song_id, new_rating))
if show_notification == 'true':
xbmc.executebuiltin("Notification(%s, %s)" % (addon_name, '{} rated to {}'.format(song_title, new_rating)))
current_song = ''
self.is_library_song = False
def calculate_rating(self, new_rating, song_id, song_title, last_id):
if last_id !=0 and song_id != last_id:
xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"AudioLibrary.SetSongDetails","params":{"songid": %d, "userrating":%d},"id":1}' % (song_id, new_rating))
if show_notification == 'true':
if song_rating == 0:
xbmc.executebuiltin("Notification(%s, %s, time=2000)" % (addon_name, '{} rated to {}'.format(song_title, new_rating)))
else:
xbmc.executebuiltin("Notification(%s, %s, time=2000)" % (addon_name, '{} rated to {}'.format(song_title, new_rating)))
current_song = ''
last_id=song_id
monitor = Monitor()
player = xbmc.Player()
calculated_rating = 0
new_rating = 0
current_time = 0
total_time = 0
# Variables to check song skipping
song_title = ''
current_song = ''
last_id = ''
show_notification = ''
real_show_notification = ''
while not monitor.abortRequested():
try:
while True:
if xbmc.Player().isPlayingAudio():
if addon.getSetting("show_notification") == 'true':
show_notification = 'true'
else:
show_notification = 'false'
if addon.getSetting("real_show_notification") == 'true':
real_show_notification = 'true'
else:
real_show_notification = 'false'
if not xbmc.getInfoLabel('MusicPlayer.Title') == '':
# Retrieve data from the currently playing song
song_title = xbmc.getInfoLabel('MusicPlayer.Title')
song_rating = xbmc.getInfoLabel('MusicPlayer.UserRating')
song_length = int(xbmc.Player().getTotalTime())
current_time = int(player.getTime())
song_parts = int(song_length / 11)
if song_rating == '':
song_rating = 0
else:
song_rating = int(song_rating)
if current_time > 0:
calculated_rating = int(current_time / song_parts)
if song_rating == 0 or song_rating == '':
new_rating = int(current_time/song_parts)
if song_rating > 0:
new_rating = int((song_rating + calculated_rating) / 2)
xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"JSONRPC.Introspect","id":1}')
result = xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"Player.GetItem","params":{"playerid":0,"properties":["userrating"]},"id":1}')
song_details = json.loads(result)["result"]["item"]
song_id = song_details["id"]
if current_song == '':
monitor.onPlayBackStarted()
current_song = song_title
song_time_left = (song_length - current_time)
# Resting only two seconds before the song finish.
if song_time_left <= 2:
monitor.calculate_rating(new_rating, song_id, song_title, last_id)
current_song = xbmc.getInfoLabel('MusicPlayer.Title')
# HERE NEED TO FIND A WAY TO SAVE THE NEW_RATING FROM LINE 66 OR 68
# IN THE SKIPPED SONG, NOT IN THE NEXT SONG
if song_time_left > 2 and current_song != song_title:
xbmc.executebuiltin("Notification(%s, %s, time=2000)" % ('{}'.format(current_song), 'Song Skipped'))
monitor.calculate_rating(new_rating, song_id, song_title, last_id)
current_song = xbmc.getInfoLabel('MusicPlayer.Title')
if real_show_notification == 'true':
xbmc.executebuiltin("Notification(%s, %s, time=2000)" % (addon_name, '{} rated to {}'.format(song_title, new_rating)))
else:
monitor.onPlayBackStopped(new_rating, current_time)
monitor.status_check()
if monitor.waitForAbort(1):
break
except Exception as e:
xbmc.executebuiltin("Notification(%s, %s)" % ("An error occurred: ", e))
The Kodi forum has an option to set "spoiler"?. Look at the size of this "walltext" hahahahaah.