[SOLVED] Code snippet for timestamp Now()

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: [SOLVED] Code snippet for timestamp Now()

Re: [SOLVED] Code snippet for timestamp Now()

by Andre_H » Sun Jan 16, 2022 11:55 am

Hi sonos,

thats what i do (as part of a larger script):

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(track) {

           var modified = false;
	   let varrating = track.rating/20;

		////////////////////////////////////////////////////////////////////////////////////
		// PLAYCOUNTER & LASTPLAYED: "PlayCounter"=1 & "LastPlayed"=Now(), wenn "PlayCounter"=0 und "Rating">1 ...
		if(track.playCounter === 0 && varrating > 1) {
		   track.playCounter = 1;
		   track.lastTimePlayed_UTC = app.utils.timestamp2DateTime(new Date().toISOString());
		   // track.commitAsync(); 
		   modified = true;
		};


           if (modified)
		track.commitAsync(); 

        });       
    }
}

window._menuItems.editTags.action.submenu.push({
        action: actions.saveStatistics,
        order: 10,
        grouporder: 10
});
... so, no direct SQL statements. the script runs on every change of the track as well as on right-click.

if you want the whole script (all the files needed to run it), just PM me.

Re: [SOLVED] Code snippet for timestamp Now()

by sonos » Fri Jan 14, 2022 8:07 am

Andre_H wrote
[Solved] Code snippet for timestamp Now()
Hi Andre_H
How did you achieve this? Did you update the lastTimePlayed field via SQL after listening a Song?

Re: Code snippet for timestamp Now()

by Andre_H » Wed Jan 05, 2022 12:05 pm

Thank you, guys, works now! :wink:

Re: Code snippet for timestamp Now()

by Ludek » Wed Jan 05, 2022 10:52 am

Hi,
I think that something like this should work:

Code: Select all

track.lastTimePlayed_UTC = app.utils.timestamp2DateTime(  new Date().toISOString());
i.e. the param into timestamp2DateTime needs to be in the ISO form 'YYYY-MM-DD HH:MM:SS' but seeing the code now it should accept also the '2022-10-05T14:48:00.000Z' variants, i.e. every ISO 8601 variant longer than 18 chars.

EDIT2: Also seeing that toISOString is UTC, so you must use track.lastTimePlayed_UTC

EDIT3: Also, the date properties (like fileModified, dateAdded, lastTimePlayed) and its UTC variants aren't documented at https://www.mediamonkey.com/docs/api/classes/Track.html
We should add them to the docs! I'll do it.

Thanks for feedback and for creating addons!

Re: Code snippet for timestamp Now()

by TIV73 » Wed Jan 05, 2022 6:06 am

Please disregard the previous snippet. While the code posted works for some fields using dates, the lastTimePlayed field wants a full timestamp with time, not a date. Also make sure to lock the tracks you are trying to update, just writing the properties won't work.

Here's the full snippet:

Code: Select all

var tracklist = uitools.getSelectedTracklist();
tracklist.modifyAsync(function () {
	var track = tracklist.getValue(0);
	track.lastTimePlayed = convertUnixToMSTimestamp(Date.now())
});
edit:
btw, if you need to figure out what kind of data a specific field wants you can either check the track property in the API reference or have a look the tracks of a tracklist using the asJson property of a tracklist which will return the values of all fields.

Re: Code snippet for timestamp Now()

by Andre_H » Wed Jan 05, 2022 4:50 am

@Barry: Thanks for the suggestion, but I don't know how to initiate (automated) SQL updates. The code excerpt shown is part of a larger script (see https://www.mediamonkey.com/forum/viewtopic.php?t=98235), and much more than what is shown in it then exceeds my capabilities.

@TIV73: I'm not sure what exactly isn't working, but nothing is saved. I've tried both variants, I don't get an error message, but nothing is written in the "LastTimePlayed" field. Script itself runs, the playCounter is set after.

Re: Code snippet for timestamp Now()

by TIV73 » Tue Jan 04, 2022 5:34 am

Try one of these

Code: Select all

track.lastTimePlayed =  app.utils.myDecodeDate((new Date()).toISOString().substring(0, 10))
track.lastTimePlayed = app.utils.myDecodeDate(app.utils.dateTime2Timestamp((new Date())))

Re: Code snippet for timestamp Now()

by Barry4679 » Tue Jan 04, 2022 3:27 am

Andre_H wrote: Mon Jan 03, 2022 12:56 pm Hi guys,

maybe someone can link me to a codesample or something, my bumbling attempts to set a value and googling the syntax for it dont work. I simply want to check some fields and write the value of "now()" to the "track.lastTimePlayed"-Field, but so far I only get invalid values (i tried "Date.Now()" and "app.utils.dateTime2Timestamp(Date.now())" in some different variants, they seem to gives invalid values, or i'm doing it wrong ...
if you are going to write these values into the database, you could let sql do the work for you.
See here for SQLITE builtin date and time functions.

eg:

Code: Select all

select julianday('now','localtime')
or

Code: Select all

select strftime('%Y-%m-%d %H:%M:%S','now','localtime')

[SOLVED] Code snippet for timestamp Now()

by Andre_H » Mon Jan 03, 2022 12:56 pm

Hi guys,

maybe someone can link me to a codesample or something, my bumbling attempts to set a value and googling the syntax for it dont work. I simply want to check some fields and write the value of "now()" to the "track.lastTimePlayed"-Field, but so far I only get invalid values (i tried "Date.Now()" and "app.utils.dateTime2Timestamp(Date.now())" in some different variants, they seem to gives invalid values, or i'm doing it wrong ...

Code: Select all

		
  let varrating = track.rating/20;
  if(varrating > 1 && track.playCounter === 0) {	
	track.playCounter = 1;     		// works!
	track.lastTimePlayed=Date.now(); 	// what syntax has to be set here?
	track.commitAsync();
	};
Thank you!

Top