Help - Search - Members - Calendar
Full Version: Extract weekdays and month names from timestamp
Mp3tag Forums > Mp3tag - International > Support
LaurenBacall
I'm trying to create an Export script which will export to a standards-compliant podcast RSS feed.

However (as usual tongue.gif) I've encountered a few problems.



1. How can I copy the weekdays and month names from the File Create timestamp, it seems Mp3Tag re-formats them into digits before inserting them into the tag.

The RFC 2822 standard for RSS feeds is: Thu, 31 05 2012 10:56:46 GMT


Seeing as I had no way to extract the weekdays I tried to at least convert the 2-digit month name into a name, one for each month, but my RegExp failed. Could anyone correct this (excuse my ignorance):

Original (in %pubdate% tag): 31 05 2012 10:56:46 GMT

What I wanted my code to do: 31 May 2012 10:56:46 GMT

My code:
$if($eql($regexp(%pubdate%,^\d\d\s(\d\d)*$,$1),'05'),$regexp(%pubdate%,(\d\d\s)\d\d(\s*),$1May$2),%pubdate%)


Solution (thanks to DetlevD for the brilliant code and support):
$left(%pubdate%,3)$replace($mid(%pubdate%,4,2),'01','Jan','02','Feb','03',
'Mar','04','Apr','05','May','06','Jun','07','Jul','08','Aug','09',
'Sep','10','Oct','11','Nov','12','Dec')$right(%pubdate%,18)


2. I'm trying to set a limit to the number of exported $loop entries. Using the format: $loop(%fieldname%,num) as stated in the help documents doesn't appear to work, as I'm sorting by %releasetime%, and each item's value is different.

Eg: $loop(%releasetime%,20)
<item><title>%title%</title></item>
$loopend()


Where '20' is the number of looped <item>s I'd like in the Export output. However this doesn't work, so I'm guessing the number of loop entries should be controlled by some function that would wrap around the <item> tag, but I'm unsure. I tried $repeat(<item>.....</item>,20) but that didn't work.

The idea is if the user had 100 items selected and then used Export that only 20 entries (with exclusions based on extension and directory name) would be appear the output. I have the conditional exclusions set up, all I need now is a way to set a limit on the number of displayed entries.


3. How can the $loop be sorted in the reverse order? Currently the newest (as sorted by the timestamp) are last, but I'd like them to be first in the list. Sorting them differently in the main Mp3Tag window doesn't change their order.

I found this topic but I'm unsure how I would apply it to the format: YYYY-MM-DDThh:mm:ssZ.


Solution:
$loop($sub(99999999999999,$regexp(%releasetime%,(\d\d\d\d)-(\d\d)-(\d\d)\w(\d\d)*'[:]+'(\d\d)*'[:]+'(\d\d)\w,$1$2$3$4$$5$6)))


4. How would it be possible to only include items in the $loop that match certain extensions, and hide others (similar to a conditional export)?

Eg: display Mp3s and Mp4s in the $loop, but not FLACs or WAVs.


Solution - wrap the entire <item> tag within this conditional $if parameter:
$ifgreater($strstr('|mp3|m4a|','|'%_extension%'|'),0,<item>
.........inner <item> content here.........
</item>,%dummy%)

Older solution (works, but only allows for two matching extensions, DetlevD's revised code above allows for more extensions to be added):
$if($or($eql($lower(%_extension%),'mp3'),$eql($lower(%_extension%),'m4a')),<item>
.........inner <item> content here.........
</item>,%dummy%)


5. In my testing I noticed that that paragraphs and line breaks need to formatted in HTML to be displayed as such in feed readers. The text content is entered in a multi-line comment box, saved to the Comment tag, which the Export script would then format to HTML.

The first line of the Comments would start with a <p> and end with a </p>. Additionally any line of text following two carriage returns (?) would start with a <p> followed by the text and end with a </p>. Line breaks would only have to start with a <br />.

What I'm hoping to achieve:

First line of text.....

Another line.....
Line break.


to

<p>First line of text.....</p>

<p>Another line.....
<br />Line break.</p>



After the remainding problems have been fixed I'm thinking of submitting it to the Export configuration sub-forum for people to use who make podcasts, complete with annotations and alternative codes. The Export script would be fully automatic, turning Mp3Tag into a useful feed generator walkman.gif !


Thanks smile.gif
DetlevD
QUOTE (LaurenBacall @ Jun 3 2012, 11:28) *
...4. How would it be possible to only include items in the $loop that match certain extensions, and hide others?
Eg: display Mp3s and Mp4s in the $loop, but not FLACs or WAVs. ...

See manual ...
%_extension%, $or, $eql, $if

DD.20120603.1540.CEST
DetlevD
QUOTE (LaurenBacall @ Jun 3 2012, 11:28) *
I'm trying to create an Export script which will export to a standards-compliant podcast RSS feed. ...
1. How can I copy the weekdays and month names from the File Create timestamp, it seems Mp3Tag re-formats them into digits before inserting them into the tag.
The RFC 2822 standard for RSS feeds is: Thu, 31 05 2012 10:56:46 GMT ...

See current RFC 5322 ...
http://tools.ietf.org/html/rfc5322#section-3.3

On my computer system (set to Germany and ISO8601) the Mp3tag system variables provide these values:
%_file_create_date% ... 2012-02-15
%_file_create_datetime% ... 2012-02-15 06:29:14
%_file_create_datetime_raw% ... 1329283754

Mp3tag does not support weekday names or month names.
Mp3tag does not provide any function for date time calculations or formatting.

See also ...
http://forums.mp3tag.de/index.php?showtopic=9335

DD.20120603.1622.CEST
DetlevD
QUOTE (LaurenBacall @ Jun 3 2012, 11:28) *
... Solution (thanks to DetlevD for the brilliant code and support):
$left(%pubdate%,3)$replace($mid(%pubdate%,4,2),'01','Jan','02','Feb','03',
'Mar','04','Apr','05','May','06','June','07','July','08','Aug','09',
'Sept','10','Oct','11','Nov','12','Dec')$right(%pubdate%,18) ...

Please check again, there are month names which are not conform with the standard RFC 5322.

QUOTE (LaurenBacall @ Jun 3 2012, 11:28) *
... 2. I'm trying to set a limit to the number of exported $loop entries. Using the format: $loop(%fieldname%,num) as stated in the help documents doesn't appear to work, as I'm sorting by %releasetime%, and each item's value is different. ...

Please provide a small example script.

QUOTE (LaurenBacall @ Jun 3 2012, 11:28) *
... 4. How would it be possible to only include items in the $loop that match certain extensions, and hide others (similar to a conditional export)?
Eg: display Mp3s and Mp4s in the $loop, but not FLACs or WAVs.

Solution - wrap the entire <item> tag within this conditional $if parameter:
$if($or($eql($lower(%_extension%),'mp3'),$eql($lower(%_extension%),'m4a')),<item>
.........inner <item> content here.........
</item>,%dummy%) ...

Another proposal to solve this problem ...
$ifgreater($strstr('|mp3|m4a|','|'%_extension%'|'),0,'yes','no')
... or ...
$ifgreater($strstr('.mp3.m4a','.'%_extension%),0,'yes','no')


DD.20120604.1354.CEST
LaurenBacall
QUOTE (DetlevD @ Jun 4 2012, 21:50) *
Please provide a small example script.


Updated main post.

DetlevD
QUOTE (LaurenBacall @ Jun 3 2012, 11:28) *
...2. I'm trying to set a limit to the number of exported $loop entries. Using the format: $loop(%fieldname%,num) as stated in the help documents doesn't appear to work, ...

See also ...
http://forums.mp3tag.de/index.php?showtopic=15804

I do not think, that the second parameter 'num' within the function '$loop(%fieldname%,num)' can control the number of loops.
The documention description may be incorrect.

I understand the second parameter as a boolean switch, which allows only two values 0 or 1.
The documention description may be incorrect.

$loop(%fieldname%,0) ...
... allows each item from the item list to be processed.

$loop(%fieldname%,1) ...
... allows only the first item of ambiguous items from the item list to be processed.
... restricts the focus to the first item of a group of items with the same value.

DD.20120607.1328.CEST
LaurenBacall
QUOTE (DetlevD @ Jun 7 2012, 21:29) *
I do not think, that the second parameter 'num' within the function '$loop(%fieldname%,num)' can control the number of loops.
The documention description may be incorrect.

I understand the second parameter as a boolean switch, which allows only two values 0 or 1.
The documention description may be incorrect.



I now know why Mp3Tag would crash when such a value is added to the loop. Thanks for the clarification, the help file could be a little clearer on this.
DetlevD
QUOTE (LaurenBacall @ Jun 7 2012, 13:56) *
I now know why Mp3Tag would crash when such a value is added to the loop. Thanks for the clarification, the help file could be a little clearer on this.

Hm, you can use a user defined variable to count how much iterations have been done so far.
See Mp3tag Export Scripting Language functions: $put, $puts, $get.

Set variable in display mode (will be printed) ...
$put(MyCounter,$add($get(MyCounter),1))
... counts from $loop starting with 1 by 1 until $loopend.

Set variable in silent mode (will be not printed) ...
$puts(MyCounter,$add($get(MyCounter),1))
... counts from $loop starting with 1 by 1 until $loopend.

Use content of variable ...
$ifgreater($get(MyCounter),20,'do nothing','do something')

But do not expect that the Mp3tag Export Scripting Language do behave like other programming languages!

Here is a test script to visualize the visibility of user defined variables within an export script.
Note the quirky behaviour, when loops are embedded.
Click to view attachment

DD.20120607.1438.CEST
DetlevD
QUOTE (LaurenBacall @ Jun 3 2012, 11:28) *
...5. In my testing I noticed that that paragraphs and line breaks need to formatted in HTML to be displayed as such in feed readers. The text content is entered in a multi-line comment box, saved to the Comment tag, which the Export script would then format to HTML.
The first line of the Comments would start with a <p> and end with a </p>. Additionally any line of text following two carriage returns (?) would start with a <p> followed by the text and end with a </p>. Line breaks would only have to start with a <br />.
What I'm hoping to achieve:
First line of text.....
Another line.....
Line break.

to
<p>First line of text.....</p>
<p>Another line.....
<br />Line break.</p>
...

Here is hex-view of a sample content copied from a COMMENT tag-field ...

"000000000 4C 69 6E 65 20 31 0D 0A-4C 69 6E 65 20 32 0D 0A |Line 1..Line 2..|"
"000000010 4C 69 6E 65 20 33 0D 0A-0D 0A 4C 69 6E 65 20 34 |Line 3....Line 4|"
"000000020 0D 0A 4C 69 6E 65 20 35- |..Line 5 |"


So a single CRLF (0D0A) should be converted into a HTML linebreak (BR) and a double CRLF should be converted into a HTML paragraph end tag?

You can use Mp3tag functions $replace or $regexp to solve this task.

For example:

Trim the comment string at both edges ...
$regexp(%COMMENT%,'\A\s+|\s+\Z',)
Note, the regular expression character class "\s" includes all "whitespace" characters ...
$char(9), $char(10), $char(11), $char(12), $char(13), $char(32).

Set paragraph end and start tags ...
$regexp(%COMMENT%,'(\r\n){2}','</p><p>')

Set break tags ...
$regexp(%COMMENT%,'(\r\n)','<br />')

Enclose the string in paragraph start and end tags ... putting it all together ...
'<p>'$regexp($regexp($regexp(%COMMENT%,'\A\s+|\s+\Z',),'(\r\n){2}','</p><p>'),'(\r\n)','<br />')'</p>'

.. .will result into HTML line ...
<p>Line 1<br />Line 2<br />Line 3</p><p>Line 4<br />Line 5</p>

DD.20120607.1808.CEST
LaurenBacall
The HTML formatting code works beautifully! Where there's a will there's a RegEx laugh.gif !

Thanks to your suggested code I've (almost) managed to successfully limit the number of outputted items in the $loop!


It has a bug that I can't figure out, though:

If none of the files selected in Mp3Tag to be Exported are from an excluded directory (excluded extensions don't cause this problem) then the limit function works as expected.

However, if there is one file selected that is from an excluded directory then the loop outputs 1 less <item>. If two files in the selection are from an excluded directory then a further 1 less <item> is outputted.

Eg (with an example limit of '3'):

Selected in Mp3Tag:
1
2
3
4

Export output:
1
2
3

--------

Selected in Mp3Tag:
1
2
3
4 [excluded directory, can be in any position in the selection]

Export output:
1
2



Adding a sub-$loop (sorted by the variable) fixes the problem for selected excluded files but adds 1 more <item> if the selection doesn't contain excluded files, so the problem is then inversed!


The code I'm using (I know, looks a bit messy):

$loop($sub(99999999999999,$regexp(%releasetime%,(\d\d\d\d)-(\d\d)-(\d\d)\w(\d\d)*'[:]+'(\d\d)*'[:]+'(\d\d)\w,$1$2$3$4$$5$6)))
$ifgreater($strstr('|Edit|Edits|edit|edits|','|'%_directory%'|'),0,%dummy%,$ifgreater($strstr('|mp3|m4a|','|'%_extension%'|'),0,$puts(LoopCounter,$add($get(LoopCounter),1))$ifgreater($get(LoopCounter),3,%dummy%,<item>
<title>%title%</title>
</item>),%dummy%))
$loopend()


Basically the 'Edit|Edits|edit|edits|' section is the names of the directories to be excluded from the $loop output. It seems that the following %dummy% could be causing the issue, but I'm uncertain how I should change the code to fix the problem.
DetlevD
QUOTE (LaurenBacall @ Jun 8 2012, 14:53) *
... Thanks to your suggested code I've (almost) managed to successfully limit the number of outputted items in the $loop!
It has a bug that I can't figure out ... but I'm uncertain how I should change the code to fix the problem.

Just for the test I reformatted the code, what helped me to understand ... and the result looks ok to me.
But ... there is something confusing ... why the counter is updated by plus one on each loop, even in the case, when the script logic does not run through the code part, where the counter code resides?

Hm, that is what I've said above ... do not expect that the Mp3tag Export Scripting Language do behave like other programming languages!

I need some time to poke around ... in the meantime you can see yourself ...

01: $filename($getEnv('USERPROFILE')'\Desktop\TEST.1.txt',UTF-8)
02: $loop(%_path%)
03: $ifgreater($strstr('|Edit|Edits|edit|edits|','|'%_directory%'|'),0,
04: '# Excluded Folder: '%_folderpath%,
05: $ifgreater($strstr('|mp3|m4a|','|'%_extension%'|'),0,
06: $put(LC,$add($get(LC),1))
07: $ifgreater($get(LC),20,
08: '# Excluded File: '%_path%,
09: '<item><title>'%TITLE%'</title></item>'),
10: '# Excluded Ext: '%_extension%
11: ))
12: $loopend()



01: $filename($getEnv('USERPROFILE')'\Desktop\TEST.3.txt',UTF-8)
02: $loop(%_path%)'=== '$put(LC,$add($get(LC),1))' ==='
03: $ifgreater($strstr('|Edit|Edits|edit|edits|','|'%_directory%'|'),0,
04: '# Excluded Folder: '%_folderpath%,
05: $ifgreater($strstr('|mp3|m4a|','|'%_extension%'|'),0,
06: $ifgreater($get(LC),20,
07: '# Excluded File: '%_path%,
08: '<item><title>'%TITLE%'</title></item>'),
09: '# Excluded Ext: '%_extension%
10: ))
11: $repeat('=',20)
12: $loopend()


The trouble is, that $put is not allowed to be used within other functions, that means not even in a $if function branch.
$put is only allowed on the first logical level within $loop.
Any $if decision must be done in the inner part of the $put parameter list.
I always stumble over this hurdle.

DD.20120608.1903.CEST
DetlevD
Here comes my Mp3tag Export Scripting code, that no one can pay.
I feel like after punching a stack of 1024 punch cards.
Hopefully it can help to solve your problem.

In this state the script gives plenty of room to play with.
Try out changing the $puts to $put and see what information will be displayed in the output file.
Tweak the variable intOutputMax to set the maximal number of items to be written out.

If you have understood what is going on there, then do the fine tuning.
Tie the code lines together and make the code human unreadable by removing the line feeds at the right places.
Hopefully you get human readable output.

01: $filename($getEnv('USERPROFILE')'\Desktop\TEST.4.txt',UTF-8)
02: $loop($sub(999999999,%_counter%))
03: $puts(intOutputMax,20)
04: $puts(intOutputCount,$if($eql(%_counter%,1),0,$get(intOutputCount)))
05: $puts(blnExcludeFileExt,$if($eql(%_counter%,1),0,$get(blnExcludeFileExt)))
06: $puts(blnExcludeFolder,$if($eql(%_counter%,1),0,$get(blnExcludeFolder)))
07: $puts(blnExcludeOutput,$if($eql(%_counter%,1),0,$get(blnExcludeOutput)))
08: $repeat('=',80)
09: $puts(REM,'File Nr ..........: '%_counter%)
10: $puts(REM,'strOutputText ....: ')$puts(strOutputText,'<item><title>'%TITLE%'</title></item>')
11: $puts(REM,'intOutputCount ...: ')$puts(intOutputCount,$add(1,$get(intOutputCount)))
12: $puts(REM,'blnExcludeOutput  : ')$puts(blnExcludeOutput,$ifgreater($get(intOutputCount),$get(intOutputMax),1,0))
13: $puts(REM,'blnExcludeFolder  : ')$puts(blnExcludeFolder,$ifgreater($strstr('|Edit|Edits|edit|edits|','|'%_directory%'|'),0,1,0))
14: $puts(REM,'blnExcludeFileExt : ')$puts(blnExcludeFileExt,$ifgreater($strstr('|mp3|m4a|','|'%_extension%'|'),0,0,1))
15: $puts(REM,'blnCountDown .....: ')$puts(blnCountDown,$ifgreater($add($get(blnExcludeFolder),$get(blnExcludeFileExt),$get(blnExcludeOutput)),0,1,0))
16: $puts(REM,'intOutputCount ...: ')$puts(intOutputCount,$ifgreater($get(blnCountDown),0,$sub($get(intOutputCount),1),$get(intOutputCount)))
17: $ifgreater($get(blnExcludeFileExt),0,'# Excluded FileExt: '%_extension%' | '%_path%,
18: $ifgreater($get(blnExcludeFolder),0,'# Excluded Folder : '%_folderpath%,
19: $ifgreater($get(blnExcludeOutput),0,'# Excluded File   : '%_path%,
20: 'Item Nr ..........: '$get(intOutputCount)
21: 'Item String ......: '$get(strOutputText))))
22: $loopend()
23: $repeat('=',80)


DD.20120608.2150.CEST
DetlevD
Please would you be so kind and show a preview file, how the end product podcast file, including example content, should look like?
Thank you in advance.

DD.20120609.1040.CEST
LaurenBacall
QUOTE (DetlevD @ Jun 9 2012, 18:41) *
Please would you be so kind and show a preview file, how the end product podcast file, including example content, should look like?


Absolutely, see attached zip file. Includes sample mp3 and flac files in a directory, the Export template, and the Export output XML. Note: things like the formatting of the %title% will be changed for the final release, and strings like 'YOUR TITLE HERE' would be edited by the user.

It should also be noted that the files to be Exported should be sorted by a column in Mp3Tag by the 'ReleaseTime' tag, with newest dates last.


QUOTE (DetlevD @ Jun 9 2012, 05:50) *
Here comes my Mp3tag Export Scripting code, that no one can pay.
I feel like after punching a stack of 1024 punch cards.
Hopefully it can help to solve your problem.


ohmy.gif What a piece of work! You've practically coded a micro application.

The code works perfectly and solves the problem, and although the Export script itself is completely unreadable when all the line feeds are removed I still think I know a way of documenting what each function does when I come to making the help file on this Export script.

Mp3Tag has the quirk of still leaving empty lines when an item in the loop is excluded, but that isn't an issue, as RSS readers don't care about blank spaces between elements.

I tried merging all the $loop code into a single line (using the $char(x) for various carriage returns and spaces) yet the empty lines still appear, however I'm not concerned, it doesn't affect anything.


From what I can tell the Export script is now ready for use. The only thing left is the required GUID tag, which a user would still have to manually tag each file with. A couple of days ago I submitted a feature request for a $guid() function, which hopefully will be considered for the next development build of Mp3Tag.


Thanks for all your efforts once again, I wouldn't have known how to accomplish such code with my limited knowledge.
DetlevD
QUOTE (LaurenBacall @ Jun 9 2012, 11:57) *
Absolutely, see attached zip file. ...

Thank you, I will look into.

QUOTE (LaurenBacall @ Jun 9 2012, 11:57) *
... The only thing left is the required GUID tag, ...

In the meantime you can fill the GUID tag with the same URL (which is also an unique value) as within the LINK tag.

QUOTE (LaurenBacall @ Jun 9 2012, 11:57) *
... Thanks for all your efforts once again, I wouldn't have known how to accomplish such code with my limited knowledge.

So I hope, that I will be mentioned in detail by my full name, for all the honors, you will earn for your assembly ;-) ... oops, wait a moment ... I didn't see your code yet.

DD.20120609.1241.CEST
DetlevD
QUOTE (LaurenBacall @ Jun 3 2012, 11:28) *
... How can I copy the weekdays and month names from the File Create timestamp ...

There is a possible simplification for the conversion of datetime from ISO 8601 to RFC 5322/2822/822 ...

$num($mid(%_datetime%,9,2),1)' '$regexp('01Jan02Feb03Mar04Apr05May06Jun07Jul08Aug09Sep10Oct11Nov12Dec','^.*'$mid(%_datetime%,6,2)'(\w\w\w).*$','$1')' '$left(%_datetime%,4)' '$mid(%_datetime%,12,8)

ISO 8601
2009-02-01T07:12:53
2009-02-01 07:12:53

Date RFC 5322/2822/822
Sun, 1 Feb 2009 07:12:53 GMT (obsolet)
Sun, 1 Feb 2009 07:12:53 +0000
1 Feb 2009 07:12:53 +0000
1 Feb 2009 07:12:53

The Mp3tag scripting code from above converts ...
From:
2009-02-01 07:12:53
To:
1 Feb 2009 07:12:53

Because Mp3tag does not provide datetime calculations, it is not possible to detect the name of the weekday from a given date automatically.
But this is not crucial, because the RFC quasi standard proposals make the weekday optional, also the time zone is optional.
If you need to set your specific time zone, then it can be added to the converted RFC datetime string, for example ...
1 Feb 2009 07:12:53 +0100

DD.20120610.0735.CEST


There I have published an action group, which computes the number and the name of a weekday, for a given ISO date ...
http://forums.mp3tag.de/index.php?showtopi...ost&p=76330

DD.20140316.1717.CET
DetlevD
QUOTE (LaurenBacall @ Jun 9 2012, 11:57) *
... From what I can tell the Export script is now ready for use. ...

The script needs more reliability when working with special characters in names and URLs.
Even UTF-8 XML needs a few characters to be escaped.
And an URL needs some more characters to be escaped.

I tremendously miss two functions in Mp3tag:
- HtmlEncode
- UrlEncode


Repeatedly those missing basic functionality messes up all joy on new coding adventures.

DD.20120610.1155.CEST
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.