It is currently March 29th, 2024, 1:46 pm

Rename files every hour with a LUA script

Discuss the use of Lua in Script measures.
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Rename files every hour with a LUA script

Post by Mordasius »

I'm been playing around with a skin to display a sequence of satellite images of weather in the Western Pacific which you can find here . This links to this feed which provides info and images for any active Tropical Storms in the Western Pacific.

Code: Select all

;  ############################################################################      
;  #    This bit of Typhoon Tracker cycles Five Satellite Images              #
;  #        http://i.imwx.com/images/sat/asiasat_600x405.jpg                  #
;  ############################################################################
[Rainmeter]
Author=Mordasius
Version=15102011
Update=1000
MiddleMouseDownAction=!Refresh

[Metadata]
Name=FiveWeatherMaps
Description= This part of Typhoon Tracker cycles through five satellite weather images of SE Asia.

[Variables]
MURL1L=http://i.imwx.com/looper/archive/asiasat_600x405/1L.jpg?1317292186933
MURL2L=http://i.imwx.com/looper/archive/asiasat_600x405/2L.jpg?1317292186933
MURL3L=http://i.imwx.com/looper/archive/asiasat_600x405/3L.jpg?1317292186933
MURL4L=http://i.imwx.com/looper/archive/asiasat_600x405/4L.jpg?1317292186933
MURL5L=http://i.imwx.com/looper/archive/asiasat_600x405/5L.jpg?1317292186933

SmallWidth=252
LargeWidth=600
Pause=1
NumMaps=6
;____________________________________________
;			STYLES
[sText]
StringAlign=CENTER
StringStyle=BOLD
FontFace=Segoe UI
FontColor=6c6c6c
FontSize=7
Clipstring=1
AntiAlias=1
;____________________________________________
; 			MEASURES
[mSmallWinXpos]
Measure=Calc
Formula=(#SCREENAREAWIDTH# - 340 )
UpdateDivider=36000
[mSmallWinYpos]
Measure=Calc
Formula=(#SCREENAREAHEIGHT# * 0.6) 
UpdateDivider=36000
[mLargeWinYpos]
Measure=Calc
Formula=( (#SCREENAREAHEIGHT#-530) * 0.5) 
UpdateDivider=36000
[mLargeWinXpos]
Measure=Calc
Formula=(#SCREENAREAWIDTH# - #LargeWidth# - 380)
UpdateDivider=36000

; [mStartPosition]
; Measure=Calc
; Formula=0
; IfEqualValue=0
; IfEqualAction=!Move [mSmallWinXpos] [mSmallWinYpos]
; UpdateDivider=36000
;------------------------------------------
[mMapImageOne]
Measure=Plugin
Plugin=WebParser
Url=#MURL1L#
UpdateRate=3600
Download=1
DownloadFile=1.jpg
[mMapImageTwo]
Measure=Plugin
Plugin=WebParser
Url=#MURL2L#
UpdateRate=3600
Download=1
DownloadFile=2.jpg
[mMapImageThree]
Measure=Plugin
Plugin=WebParser
Url=#MURL3L#
UpdateRate=3600
Download=1
DownloadFile=3.jpg
[mMapImageFour]
Measure=Plugin
Plugin=WebParser
Url=#MURL4L#
UpdateRate=3600
Download=1
DownloadFile=4.jpg
[mMapImageFive]
Measure=Plugin
Plugin=WebParser
Url=#MURL5L#
UpdateRate=3600
Download=1
DownloadFile=5.jpg
[cImageCounter]
Measure=Calc
Formula=( (cImageCounter+#Pause#) % #NumMaps# )
DynamicVariables=1
UpdateDivider=1
[cImageLabel]
Measure=Calc
Formula= ( #NumMaps# - cImageCounter - 1) 
Substitute="0":"Base Map","1":"1 Hour Ago","2":"2 Hours Ago","3":"3 Hours Ago","4":"4 Hours Ago","5":"5 Hours Ago"
;___________________________________________
; 			METERS
[SmallMapBackground]
Meter=Image
ImageName=SmallBackground.png
X=0
Y=0
UpdateDivider=36000
Group=SmallMap
[SmallCycleMaps]
Meter=Image
ImageName="#CURRENTPATH#DownloadFile\[cImageCounter].jpg"
LeftMouseUpAction=!Execute [!HideMeterGroup SmallMap]  [!Move [mLargeWinXpos] [mLargeWinYpos] ]  [!ShowMeterGroup LargeMaps] [!Redraw] 
X=42
Y=5
W=#SmallWidth#
PreserveAspectRatio=1
DynamicVariables=1
Group=SmallMap
[SmallMapHours]
Meter=STRING
MeasureName=cImageLabel
MeterStyle=sText
X=22
Y=32
W=45
H=30
Text=%1
Group=SmallMap
[SmallMapNubbin]
Meter=Image
ImageName=Nubbin.png
X=15
Y=( 144 - (  [cImageCounter] * 16 ) )
DynamicVariables=1
Group=SmallMap
[SmallPausePlay]
Meter=Image
ImageName=P#Pause#.png
X=15
Y=159
LeftMouseUpAction= !Execute [ !SetVariable Pause (#Pause#=0) ]
DynamicVariables=1
Group=SmallMap
;-------------------------------------------
[LargeCycleMaps]
Meter=Image
ImageName="#CURRENTPATH#DownloadFile\[cImageCounter].jpg"
X=6
Y=7
LeftMouseUpAction=!Execute [!HideMeterGroup LargeMaps]  [!Move [mSmallWinXpos] [mSmallWinYpos] ]  [!ShowMeterGroup SmallMap]  [!Redraw] 
DynamicVariables=1
Hidden=1
Group=LargeMaps
[LargeMapBackground]
Meter=Image
ImageName=LargeBackground.png
X=0
Y=0
UpdateDivider=3600
Hidden=1
Group=LargeMaps
[LargeMapHours]
Meter=STRING
MeasureName=cImageLabel
MeterStyle=sText 
FontColor=eeeeee
FontSize=9
X=308
Y=418
W=350
H=30
Text="Satellite Image: %1"
Hidden=1
Group=LargeMaps
[LargeMapNubbin]
Meter=Image
ImageName=Vnubbin.png
X=( 72 + ( [cImageCounter] * 20) )
Y=420
DynamicVariables=1
Hidden=1
Group=LargeMaps
[LargePausePlay]
Meter=Image
ImageName=P#Pause#.png
W=24
H=18
X=22
Y=418
LeftMouseUpAction= !Execute [ !SetVariable Pause (#Pause#=0) ]
DynamicVariables=1
Hidden=1
Group=LargeMaps
I've got the above working nicely but have now become a little greedy. There are five images available from here but I want to increase this to 10-12 images for each day. This means that I need to rename all the downloaded images (5.jpg = 6.jpg , 4.jpg = 5.jpg , etc ) every hour (update=3600) and also update #NumMaps# by 1 every hour.

I'm sure this can be done with a LUA script, but I'm f'd if I can see how to do it. :(
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Rename files every hour with a LUA script

Post by jsmorley »

Code: Select all

PROPERTIES =
{

}

function Initialize()

CurrPath = SKIN:GetVariable('CURRENTPATH')

end -- function Initialize

function Update()
   
os.rename(CurrPath.."OldName.txt", CurrPath.."NewName.txt" )

return "Something"
   
end -- function Update
That is how you rename / move files in Lua. Easy enough. The trick is going to be the "logic" you are going to have to come up with to know when to rename things and how you keep from stepping all over yourself. Should be fun.
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Rename files every hour with a LUA script

Post by Mordasius »

jsmorley wrote:

Code: Select all

PROPERTIES =

function Initialize()

CurrPath = SKIN:GetVariable('CURRENTPATH')

end -- function Initialize

function Update()
   
os.rename(CurrPath.."OldName.txt", CurrPath.."NewName.txt" )

return "Something"
   
end -- function Update
Ok.. that bit I get.. But what do I do to only call the LUA script once every hour after the images have been updated?
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Rename files every hour with a LUA script

Post by jsmorley »

I would make that rename a function in lua, then call it with a FinishAction= on the WebParser measure getting the images, using !CommandMeasure. Then only use the "renamed" versions in meters and such. That way the new ones coming in won't step on the old ones.

Let me know if that isn't clear, I can probably knock out a little example.
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Rename files every hour with a LUA script

Post by Mordasius »

jsmorley wrote:I would make that rename a function in lua, then call it with a FinishAction= on the WebParser measure getting the images, using !CommandMeasure. Then only use the "renamed" versions in meters and such. That way the new ones coming in won't step on the old ones.
I hear you, but the problem is that the images do not always get downloaded in sequence. So that makes a mess of whenever I try to use FinishAction= (which I tried before and got stuffed) e.g Image 03 gets downloaded before Image 02)
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Rename files every hour with a LUA script

Post by jsmorley »

Hmm.. Tricky. You could have a calc measure like:

[MeasureRunLua]
Measure=Calc
Formula=Counter % 3601
IfEqualValue=3600
IfEqualAction=!CommandMeasure ScriptMeasureName "FunctionName();"

Which would run the rename function in the script once an hour.

Then just don't have anything in the Update() function in the .lua, and set the measure to UpdateDivider=-1
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Rename files every hour with a LUA script

Post by Mordasius »

;-) The bottom line is that I do not want to rename files using the LUA script until one hour (update=3600) after the previous updates of measures and meters in the main skin. (oops - posted this while you were replying.... let me see how the pancake flattens.
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Rename files every hour with a LUA script

Post by Mordasius »

Thanks Sir Morley, I'll see how that works :welcome:

Update: The following post solved the problem thanks.
Last edited by Mordasius on October 18th, 2011, 3:24 pm, edited 2 times in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Rename files every hour with a LUA script

Post by jsmorley »

Mordasius wrote:;-) The bottom line is that I do not want to rename files using the LUA script until one hour (update=3600) after the previous updates of measures and meters in the main skin.
Well then, off the top of my head I would explore having an Update() in the measure that looks at variables in the lua called CurrentHour and OldHour or something.

function Initialize()

CurrentHour = 0
OldHour = 0

end -->Initialize

function Update()

if CurrentHour ~= OldHour then

RenameMyFiles()

return "Done"

end -->Update

function RenameMyFiles()

os.rename(blah, blah)
os.rename(blah, blah)

OldHour = CurrentHour

end -->RenameMyFiles

Then maybe a Calc measure in the skin that calls the script once an hour, based on Time measures:

[MeasureHour]
Measure=Time
Format=%H

[MeasureMinute]
Measure=Time
Format=%M

[MeasureFireLua]
Measure=Calc
Formula=[MeasureMinute]
IfEqualValue=0
IfEqualAction=!CommandMeasure ScriptMeasureName "CurrentHour = [MeasureHour];Update();"
DynamicVariables=1

I think something like that might get you there.
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Rename files every hour with a LUA script

Post by Mordasius »

jsmorley wrote:I think something like that might get you there.
Thanks, that was pretty much all that was needed. :)

I just had to tuck in an os.remove before the os.rename because LUA won't rename a file to an existing filename. The other little hiccup was that LUA doesn't have an os.copy type of command so I had to download a spare copy of 5.jpg to replace the one that gets renamed to 6.jpg. I tried to 'force' the measure to download the file with [!UpdateMeasure mMapImageFive] in the skin file but that didn't appear to work. So this is what I ended up with:

In the .ini file

Code: Select all

[mHour]
Measure=Time
Format=%H

[mMinute]
Measure=Time
Format=%M

[cTriggerScript]
Measure=Calc
Formula=[mMinute]
IfEqualValue=30
IfEqualAction=!CommandMeasure mRenamerScript "iCurrentHour = [mHour]; Update();"
DynamicVariables=1

[mRenamerScript]
Measure=Script
ScriptFile="#CURRENTPATH#TyphoonTracker.lua"
UpdateDivider=5
and the LUA script

Code: Select all

PROPERTIES = {}

function Initialize()
	iNumMaps = 0
    iCurrentHour = 0
    iPreviousHour = 0
	sCurrentPath = SKIN:GetVariable('CURRENTPATH')
end -->Initialize

function Update()

	iNumMaps = SKIN:GetVariable('NumMaps')
    if iCurrentHour ~= iPreviousHour 
	    then 
			RenameImageFiles() 
	    end

	return 'Script Done'
	
end -->Update

function RenameImageFiles()

	for i = iNumMaps, 5, -1 do	
	    os.remove( sCurrentPath.."DownloadFile\\"..(i+1)..".jpg" )
		os.rename( sCurrentPath.."DownloadFile\\"..i..".jpg", sCurrentPath.."DownloadFile\\"..(i+1)..".jpg" )	
	end
	
	os.rename( sCurrentPath.."DownloadFile\\0.jpg", sCurrentPath.."DownloadFile\\5.jpg" )
	SKIN:Bang('!SetVariable "NumMaps" "'..(iNumMaps+1)..'"')
	iPreviousHour = iCurrentHour

end -->RenameImageFiles
I'm still looking for a way to synchronise the downloading and renaming of files and think it will probably be best to do both in the LUA script. Can this be done by simply sending an !EnableMeasure bang to the measures downloading files with WebParser?