Page 1 of 1

Get web-page and save jpg

Posted: Thursday 18 May 2017 10:52
by ensingg
I like to use the LUA script to go to a url (webcam) and save the jpg at that page to a file.

How can I access Web URL's in LUA and than save it ?

Re: Get web-page and save jpg

Posted: Tuesday 23 May 2017 9:49
by zicht
Most likely you cam has an url to get a snapshot.
in my case this could be http://ip:port/snapshot.jpg (foscam)
but if you add them to Domoticz, you can use domoticz to get the picture :)

What i use for sending picture on doorbel to telegram :

Code: Select all

function os.capture(cmd, raw)				-- os.command uitvoeren en resultaat daarvan lezen in string
  if (dbug == true) then print("Os.Capture") end
  local f = assert(io.popen(cmd, 'r'))
  local s = assert(f:read('*a'))
  f:close()
  if raw then return s end
  s = string.gsub(s, '^%s+', '')
  s = string.gsub(s, '%s+$', '')
  s = string.gsub(s, '[\n\r]+', ' ')
  return s
end

Code: Select all

function GetCam(idx,naam,save)
    if (dbug == true) then print("GetCam") end

	local Cam = 'idx='..idx..'" -OutFile "cam/'..naam..'.jpg"'
	commando = 'powershell Invoke-WebRequest -Uri "http://IP:Port/camsnapshot.jpg?'..Cam
	os.execute(commando) 
    if (save) then
		Cam = 'idx='..idx..'" -OutFile "cam/'..naam..now..'.jpg"'
		commando = 'powershell Invoke-WebRequest -Uri "http://IP:Port/camsnapshot.jpg?'..Cam
		os.execute(commando)
		SendText('Beeld opgeslagen '..naam..now)
	end

Code: Select all

function SendCam(naam)
    if (dbug == true) then print("SendCam") end

	commando = 'curl -s https://api.telegram.org/'..apikey..'/sendPhoto -F chat_id='..chat_id..' -F photo=@cam/'..naam..'.jpg'
	result = os.capture(commando)
	if (not(string.find(result, '"ok"'))) then print(result) end
end	

Code: Select all

		if (string.find(UtTekst, "CAM1")) then 
					if (string.find(UtTekst, "SAVE")) then save=true else save=false end
					GetCam("1","garage",save)
					SendCam("garage")
					Gedaan = "ok"

I use functions for easy reusing codeparts anywhere, Hope you can use some part of it for your purpose