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

Get help for different MediaMonkey 5 Addons.

Moderators: jiri, drakinite, Addon Administrators

Andre_H
Posts: 81
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

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

Post 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...
MMW 5.0.1.2412 on Windows 2016 Std.
MMA Pro on Android 10 Phone and Android 11 Tab

MP3Tag, MP3Diags, IrfanView
drakinite
Posts: 474
Joined: Tue May 12, 2020 10:06 am
Contact:

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

Post 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))
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
drakinite
Posts: 474
Joined: Tue May 12, 2020 10:06 am
Contact:

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

Post 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.)
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
Andre_H
Posts: 81
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

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

Post 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:
MMW 5.0.1.2412 on Windows 2016 Std.
MMA Pro on Android 10 Phone and Android 11 Tab

MP3Tag, MP3Diags, IrfanView
drakinite
Posts: 474
Joined: Tue May 12, 2020 10:06 am
Contact:

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

Post by drakinite »

Np, good luck!
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
Andre_H
Posts: 81
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

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

Post 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].
MMW 5.0.1.2412 on Windows 2016 Std.
MMA Pro on Android 10 Phone and Android 11 Tab

MP3Tag, MP3Diags, IrfanView
Peke
Posts: 14288
Joined: Tue Jun 10, 2003 7:21 pm
Location: Serbia
Contact:

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

Post 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.
Best regards,
Peke
MediaMonkey Team lead QA/Tech Support guru
Admin of Free MediaMonkey addon Site HappyMonkeying
Image
Image
How to add SCREENSHOTS to forum
Andre_H
Posts: 81
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

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

Post 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!
MMW 5.0.1.2412 on Windows 2016 Std.
MMA Pro on Android 10 Phone and Android 11 Tab

MP3Tag, MP3Diags, IrfanView
drakinite
Posts: 474
Joined: Tue May 12, 2020 10:06 am
Contact:

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

Post 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.
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
Ludek
Posts: 4051
Joined: Fri Mar 09, 2007 9:00 am

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

Post 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 ;-)
Andre_H
Posts: 81
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

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

Post 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.
MMW 5.0.1.2412 on Windows 2016 Std.
MMA Pro on Android 10 Phone and Android 11 Tab

MP3Tag, MP3Diags, IrfanView
Andre_H
Posts: 81
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

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

Post 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]
MMW 5.0.1.2412 on Windows 2016 Std.
MMA Pro on Android 10 Phone and Android 11 Tab

MP3Tag, MP3Diags, IrfanView
Ludek
Posts: 4051
Joined: Fri Mar 09, 2007 9:00 am

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

Post 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!
Andre_H
Posts: 81
Joined: Thu Jan 21, 2021 2:04 pm
Location: Germany

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

Post by Andre_H »

Will check, when out, and give feedback then.

Thanks again!
MMW 5.0.1.2412 on Windows 2016 Std.
MMA Pro on Android 10 Phone and Android 11 Tab

MP3Tag, MP3Diags, IrfanView
Peke
Posts: 14288
Joined: Tue Jun 10, 2003 7:21 pm
Location: Serbia
Contact:

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

Post by Peke »

Hi,
Changed back to [SOLVED] so that you can test it and hopefully not change back to [REQ] ;)
Best regards,
Peke
MediaMonkey Team lead QA/Tech Support guru
Admin of Free MediaMonkey addon Site HappyMonkeying
Image
Image
How to add SCREENSHOTS to forum
Post Reply