Page 3 of 4

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Thu May 27, 2021 4:13 pm
by Andre_H
2 (maybe :wink: ) final questions:

Code: Select all

app.utils.dateTime2Timestamp(itm.lastTimePlayed_UTC
is 2 hours behind my local time. any quick fix for that? if not, not a big thing.

Code: Select all

actions.saveStatistics = {
    title: _('Speichere Statistik-Daten in CustomFields ...'),
    hotkeyAble: false,
    icon: 'saveStatistics',
    disabled: uitools.notMediaListSelected,
    visible: window.uitools.getCanEdit,
    execute: async function () {
        var list = await uitools.getSelectedTracklist().whenLoaded();
        if (list.count === 0) {
            return;
        }
        list.forEach(function(itm) {

           	var modified = false;

		//convert rating from 20-100 to 1-5 value, save as "varrating", save to customfield ...
		let varrating = itm.rating/20;
		if(itm.custom5 != varrating.toString) {
		  itm.custom5 = varrating.toString();
		  modified = true;
		};	
		
		//save playcounter, even if 0 ...
		if(itm.custom6 != itm.playCounter.toString()) {
                  itm.custom6 = itm.playCounter.toString();
		  modified = true;
		};

		//save lastplayed, if playcounter>0 ...
		if(itm.playCounter > 0 && itm.custom7 != app.utils.dateTime2Timestamp(itm.lastTimePlayed_UTC)) {
                  itm.custom7 = app.utils.dateTime2Timestamp(itm.lastTimePlayed_UTC);
		  modified = true;
		};

           if (modified)
		itm.commitAsync(); 

        });       
    }
}

window._menuItems.editTags.action.submenu.push({
        action: actions.saveStatistics,
        order: 90,
        grouporder: 10
});
as "save on demand" works, values are saved and correct (beside that 2hours thing). but: I triggers a save (new timestamp on file) even if there shouldn't be anyhing to change: i hit the button once, anything is updated, i hit the button twice 10 seconds after that (nothing played between, so no new data) => new timestamp on MP3s. Values in fields are the same (the same or overwritten with the same again).

I don't see why...

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Thu May 27, 2021 7:52 pm
by drakinite
Looks like it creates a Date object that *thinks* it's in whatever local timezone you're in. But there's a way around it, by getting your timezone offset from UTC and subtracting it from the provided Date object:

Code: Select all

function UTCtoLocal(date) {
    var now = new Date();
    var offset = now.getTimezoneOffset(); // offset from UTC in minutes
    return new Date(date.valueOf() - offset * 60000);
}
so you should be able to do:

Code: Select all

app.utils.dateTime2Timestamp(UTCtoLocal(itm.lastTimePlayed_UTC))

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Thu May 27, 2021 7:59 pm
by drakinite
Also I found the problem with your code. In this section:

Code: Select all

if(itm.custom5 != varrating.toString) {
	itm.custom5 = varrating.toString();
	modified = true;
};
you forgot the parentheses in varrating.toString(). So it's attempting to compare the custom5 property to the toString function, and not its actual value. (So the if statement always returns true.)

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Fri May 28, 2021 12:42 am
by Andre_H
drakinite wrote: Thu May 27, 2021 7:52 pm But there's a way around it, by getting your timezone offset from UTC and subtracting it from the provided Date object:
will try this later today, thank you!
drakinite wrote: Thu May 27, 2021 7:59 pm Also I found the problem with your code. ...
you forgot the parentheses in varrating.toString().
yes! tested, fixed it. again, thank you! :wink:

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Fri May 28, 2021 12:52 am
by drakinite
Np, good luck!

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Mon Jun 07, 2021 2:26 pm
by Andre_H
Hi guys,

I was on vacation for a couple of days and couldn't work on this anymore. Final report: Everything is now working as intended: I can save my stuff on demand, and it is automaticly saved on every change, including syncing back from MMA.

Many thanks to drakinite and Ludek for the great support, in the end I just muddled together what you programmed. You guys rock! ;-)

If anyone is interested in the final scripts, leave a note, i gladly share them.

Topic is set to [Solved].

Re: [SOLVED] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Mon Jun 07, 2021 9:58 pm
by Peke
Andre_H wrote: Mon Jun 07, 2021 2:26 pm If anyone is interested in the final scripts, leave a note, i gladly share them.
Sure that someone will be interested prepare MMIP files and in case of issues @drakinite should be able to help you.

Re: [SOLVED] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Tue Jun 08, 2021 2:32 am
by Andre_H
Since the two scripts are very individually tailored to my use of the custom tags, i guess hardly anyone will use them directly. I was thinking of posting the final code in full rather than a sample for similar ideas. So, here we go:

File info.json:

Code: Select all

{
    "title": "saveStatisticsToTag",
    "id": "saveStatistics",
    "description": "save statistics to IDTag-CustomFields",
    "version": "1.0.0",
    "author": "programmed by 'ludek' and 'drakinite' (MediaMonkey), cobbled together by Andre_H :-)",
    "type": "general",
    "icon": "saveStatistics.svg"
}
File init.js - for automatic saving on "change"-event (after playing, editing data or syncing back from MMA):

Code: Select all

//* save track-statistics to CustomFields.*//
// Custom1=DateAdded
// Custom2=Source
// Custom3=Media
// Custom4=CoverCheck
// Custom5=Rating
// Custom6=Playcount
// Custom7=LastPlayed


requirejs('actions');
// Execute when the window is ready
window.whenReady(() => {
	app.listen(app, 'trackmodified', function (track) {
		
		var modified = false;
		
		//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
		let varrating = track.rating/20;
		if(track.custom5 != varrating.toString()) {
		  track.custom5 = varrating.toString();
		  modified = true;
		};

		//save playcounter & last played, if playcounter>0 and changed ...
		if(track.playCounter > 0 && track.custom6 != track.playCounter.toString()) {
        	   track.custom6 = track.playCounter.toString();
		   track.custom7 = app.utils.dateTime2Timestamp(UTCtoLocal(track.lastTimePlayed_UTC));
		   modified = true;
		};
		
		if (modified)
		  track.commitAsync();                    
	});
});


function UTCtoLocal(date) {
    var now = new Date();
    var offset = now.getTimezoneOffset(); // offset from UTC in minutes
    return new Date(date.valueOf() - offset * 60000);
}
File actions_add.js - same functionality "on demand", per right-click menu under "edit tags"

Code: Select all

//* save track-statistics to CustomFields.*//
// Custom1=DateAdded
// Custom2=Source
// Custom3=Media
// Custom4=CoverCheck
// Custom5=Rating
// Custom6=Playcount
// Custom7=LastPlayed


actions.saveStatistics = {
    title: _('Speichere Statistik-Daten in CustomFields ...'),
    hotkeyAble: false,
    //icon: 'saveStatistics',
    disabled: uitools.notMediaListSelected,
    visible: window.uitools.getCanEdit,
    execute: async function () {
        var list = await uitools.getSelectedTracklist().whenLoaded();
        if (list.count === 0) {
            return;
        }
        list.forEach(function(itm) {

            	var modified = false;

		//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
		let varrating = itm.rating/20;
		if(itm.custom5 != varrating.toString()) {
		  itm.custom5 = varrating.toString();
		  modified = true;
		};	
		
		//save playcounter, if changed ...
		if(itm.custom6 != itm.playCounter.toString()) {
                  itm.custom6 = itm.playCounter.toString();
		  modified = true;
		};

		//save lastplayed, if playcounter>0 and changed ...
		if(itm.playCounter > 0 && itm.custom7 != app.utils.dateTime2Timestamp(UTCtoLocal(itm.lastTimePlayed_UTC))) {
                  itm.custom7 = app.utils.dateTime2Timestamp(UTCtoLocal(itm.lastTimePlayed_UTC));
		  modified = true;
		};

           if (modified)
		itm.commitAsync(); 

        });       
    }
}


function UTCtoLocal(date) {
    var now = new Date();
    var offset = now.getTimezoneOffset(); // offset from UTC in minutes
    return new Date(date.valueOf() - offset * 60000);
}

window._menuItems.editTags.action.submenu.push({
        action: actions.saveStatistics,
        order: 90,
        grouporder: 10
});
icon commented out, as that file has to be in the right place.

Of course I create the MMIP if someone wants it. PM!

Re: [SOLVED] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Tue Jun 08, 2021 3:26 am
by drakinite
Very nice, great job! You can also feel free to upload it to the Addons site: https://www.mediamonkey.com/addon_system/admin/

I should probably write a snippet on the wiki explaining how to submit, since I recall it being confusing when I first did it.

1) Click on the category you'd like it to be in (in this case, I'd put it in Behavior Tweaks)
2) click Submit New Addon
3) Fill out the necessary information; Support/donate/buy/author links are optional, as well as license type and a thumbnail image. If you upload an image, I'd recommend making it square. Then click save. This step is for meta information about the addon itself.
4) Next, you'll be taken to the "Add Version" page, where you upload the addon itself. You have the choice of uploading a file or providing an external link, and you must enter which MediaMonkey versions it's compatible with. (In this case, I'd put 5.0.0 to 5.0.1.) Then click save.
5) Now, you'll be taken to a review screen; if everything looks good, click Finish.

I should then get a notification (if that part is working) that an addon has been submitted, and I'll review and approve it.

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Tue Jun 08, 2021 7:55 am
by Ludek
Andre_H wrote: Thu May 27, 2021 4:13 pm 2 (maybe :wink: ) final questions:

Code: Select all

app.utils.dateTime2Timestamp(itm.lastTimePlayed_UTC
is 2 hours behind my local time. any quick fix for that? if not, not a big thing.
Just remove the _UTC and use:

Code: Select all

app.utils.dateTime2Timestamp(itm.lastTimePlayed) 

that's all ;-)

Re: [SOLVED] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Wed Jun 09, 2021 9:14 am
by Andre_H
Ludek wrote: Tue Jun 08, 2021 7:55 am Just remove the _UTC and use:

Code: Select all

app.utils.dateTime2Timestamp(itm.lastTimePlayed) 

that's all ;-)
Update:

File info.json:

Code: Select all

{
    "title": "saveStatisticsToTag",
    "id": "saveStatistics",
    "description": "save statistics to IDTag-CustomFields",
    "version": "1.0.0",
    "author": "programmed by 'ludek' and 'drakinite' (MediaMonkey), cobbled together by Andre_H :-)",
    "type": "general",
    "icon": "saveStatistics.svg"
}
File init.js - for automatic saving on "change"-event (after playing, editing data or syncing back from MMA):

Code: Select all

//* save track-statistics to CustomFields.*//
// Custom1=DateAdded
// Custom2=Source
// Custom3=Media
// Custom4=CoverCheck
// Custom5=Rating
// Custom6=Playcount
// Custom7=LastPlayed


requirejs('actions');
// Execute when the window is ready
window.whenReady(() => {
	app.listen(app, 'trackmodified', function (track) {
		
		var modified = false;
		
		//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
		let varrating = track.rating/20;
		if(track.custom5 != varrating.toString()) {
		  track.custom5 = varrating.toString();
		  modified = true;
		};

		//save playcounter & last played, if playcounter>0 and changed ...
		if(track.playCounter > 0 && track.custom6 != track.playCounter.toString()) {
        	   track.custom6 = track.playCounter.toString();
		   track.custom7 = app.utils.dateTime2Timestamp(track.lastTimePlayed));
		   modified = true;
		};
		
		if (modified)
		  track.commitAsync();                    
	});
});
File actions_add.js - same functionality "on demand", per right-click menu under "edit tags"

Code: Select all

//* save track-statistics to CustomFields.*//
// Custom1=DateAdded
// Custom2=Source
// Custom3=Media
// Custom4=CoverCheck
// Custom5=Rating
// Custom6=Playcount
// Custom7=LastPlayed


actions.saveStatistics = {
    title: _('Speichere Statistik-Daten in CustomFields ...'),
    hotkeyAble: false,
    //icon: 'saveStatistics',
    disabled: uitools.notMediaListSelected,
    visible: window.uitools.getCanEdit,
    execute: async function () {
        var list = await uitools.getSelectedTracklist().whenLoaded();
        if (list.count === 0) {
            return;
        }
        list.forEach(function(itm) {

            	var modified = false;

		//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
		let varrating = itm.rating/20;
		if(itm.custom5 != varrating.toString()) {
		  itm.custom5 = varrating.toString();
		  modified = true;
		};	
		
		//save playcounter, if changed ...
		if(itm.custom6 != itm.playCounter.toString()) {
                  itm.custom6 = itm.playCounter.toString();
		  modified = true;
		};

		//save lastplayed, if playcounter>0 and changed ...
		if(itm.playCounter > 0 && itm.custom7 != app.utils.dateTime2Timestamp(itm.lastTimePlayed)) {
                  itm.custom7 = app.utils.dateTime2Timestamp(itm.lastTimePlayed);
		  modified = true;
		};

           if (modified)
		itm.commitAsync(); 

        });       
    }
}

window._menuItems.editTags.action.submenu.push({
        action: actions.saveStatistics,
        order: 90,
        grouporder: 10
});
icon commented out, as that file has to be in the right place.

Re: [SOLVED] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Thu Jun 10, 2021 10:39 am
by Andre_H
Hi guys,

I got the following phenomenon: when I syncronize tracks from MMA where the rating has been changed (thats basicly what i've done all the time while testing; not sure what happens if i change other stuff in MMA), my (your ;-)) script works fine. But if I only listen to tracks in MMA and sync back, MMW correctly updates the data for the playcounter and lastplayed, but the script does not seem to trigger: neither playcount to custom6 or lastplayed to custom7 were updated.

I checked the conditions for triggering again, but did not see the error // the difference between "listen & change rating & sync back" and "just listen & sync back":

Code: Select all

requirejs('actions');
// Execute when the window is ready
window.whenReady(() => {
	app.listen(app, 'trackmodified', function (track) {
		
		var modified = false;
		
		//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
		let varrating = track.rating/20;
		if(track.custom5 != varrating.toString()) {
		  track.custom5 = varrating.toString();
		  modified = true;
		};

		//save playcounter & last played, if playcounter>0 and changed ...
		if(track.playCounter > 0 && track.custom6 != track.playCounter.toString()) {
        	   track.custom6 = track.playCounter.toString();
		   track.custom7 = app.utils.dateTime2Timestamp(track.lastTimePlayed);
		   modified = true;
		};
		
		if (modified)
		  track.commitAsync();                    
	});
});
would you help thinking again please? ;-)

Edit: "Just listening" from MMW5 works fine, script runs.
Edit2: changed Topic from [SOLVED] to [REQ]

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Mon Jun 14, 2021 11:27 am
by Ludek
...But if I only listen to tracks in MMA and sync back, MMW correctly updates the data for the playcounter and lastplayed, but the script does not seem to trigger: neither playcount to custom6 or lastplayed to custom7 were updated.
OK, seeing the reason in MM5 code, will be fixed in 2416+

Thanks!

Re: [REQ] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Mon Jun 14, 2021 1:38 pm
by Andre_H
Will check, when out, and give feedback then.

Thanks again!

Re: [SOLVED] Addon to save Playcounter, LastPlayed, SkipCounter, LastSkipped to tag

Posted: Mon Jun 14, 2021 7:10 pm
by Peke
Hi,
Changed back to [SOLVED] so that you can test it and hopefully not change back to [REQ] ;)