--// vlc.lua Spams VLC info to DC v0.699+
-- loads the HTTP module and any libraries it requires, but for the life of me I cannot get this to work for now
-- so the script has to use the system wget method to download the xml file(s)
--local http = require("socket.http")


local spamfile = "C:\\playlist.xml"
local spamfile2 = "C:\\status.xml"

dcpp:setListener( "ownChatOut", "vlc",
	function( hub, text )
		local s = string.lower( text )
		if text == "/vlc" then
   		
			os.execute ("\""..DC():GetAppPath().."\scripts\\wget.exe\" -P C:\\ -t 1 http://127.0.0.1:8080/requests/playlist.xml http://127.0.0.1:8080/requests/status.xml")
			vlc(hub)
			os.remove ( "C:\\playlist.xml" )
			os.remove ( "C:\\status.xml" )
     	 return 1
   		end
	end
)



function vlc(hub)
   local fileContents = {}
   local fileContents2 = {}
   local found = 0
   file = io.open(spamfile,"r")
   if file then
      for line in file:lines() do table.insert(fileContents,line) end
      file:close()
      
     	file2 = io.open(spamfile2,"r")
      	if file2 then
      		for line2 in file2:lines() do table.insert(fileContents2,line2) end
      		file2:close()
      
      		for i,v in ipairs(fileContents) do 
      			if string.find( v, "current=" ) then
      				local started = string.find(v, "name")
      				local ended = string.find(v, "\"", started + 6)
      				local filename = string.sub(v, (started + 6), (ended -1))
      				
                    --html translation from wget output
                    filename = string.gsub(filename, "&#039;", "'")
                    filename = string.gsub(filename, "&#163;", "£")
                    filename = string.gsub(filename, "Â£", "£")
                    filename = string.gsub(filename, "&#036;", "$")
                    filename = string.gsub(filename, "&#037;", "%")
                    filename = string.gsub(filename, "&amp;", "&")
                    filename = string.gsub(filename, "Ã ", "à")
                    filename = string.gsub(filename, "Ã¡", "á")
                    filename = string.gsub(filename, "Ã¢", "â")
                    filename = string.gsub(filename, "Ã¤", "ä")
                    filename = string.gsub(filename, "Ã¦", "æ")
                    filename = string.gsub(filename, "Ã§", "ç")
                    filename = string.gsub(filename, "Ã¨", "è")
                    filename = string.gsub(filename, "Ã©", "é")
                    filename = string.gsub(filename, "Ãª", "ê")
                    filename = string.gsub(filename, "Ã«", "ë")
                    filename = string.gsub(filename, "Ã¬", "ì")
                    filename = string.gsub(filename, "Ã®", "î")
                    filename = string.gsub(filename, "Ã¯", "ï")
                    filename = string.gsub(filename, "Ã±", "ñ")
                    filename = string.gsub(filename, "Ã²", "ò")
                    filename = string.gsub(filename, "Ã³", "ó")
                    filename = string.gsub(filename, "Ã´", "ô")
                    filename = string.gsub(filename, "Ãµ", "õ")
                    filename = string.gsub(filename, "Ã¶", "ö")
                    filename = string.gsub(filename, "Ã¹", "ù")
                    filename = string.gsub(filename, "Ãº", "ú")
                    filename = string.gsub(filename, "Ã»", "û")
                    filename = string.gsub(filename, "Ã¼", "ü")
                    filename = string.gsub(filename, "Â¾", "¾")
                    filename = string.gsub(filename, "Â½", "½")
                    filename = string.gsub(filename, "Â¼", "¼")
                    filename = string.gsub(filename, "Âµ", "µ")
                    filename = string.gsub(filename, "ÃŸ", "ß") 
                    filename = string.gsub(filename, "Â²", "²")
                    filename = string.gsub(filename, "Â³", "³")
                    filename = string.gsub(filename, "Â¹", "¹")
                    filename = string.gsub(filename, "Â°", "°")
                    filename = string.gsub(filename, "Â±", "±")
                    filename = string.gsub(filename, "Â¢", "¢")                    
                    
                    filename = string.gsub(filename, "Ã", "í") --must be at the end (others have Ã in their outputs)      				
      		
                    --File time/length
      				local filelengthend = string.find(fileContents2[6], "</length")
      				local filelength = string.sub(fileContents2[6], 11, filelengthend-1)
      				
      				local filecurrentend = string.find(fileContents2[7], "</time")
      				local filecurrent = string.sub(fileContents2[7], 9, filecurrentend-1)      
      				
      				local currentstatusend = string.find(fileContents2[8], "</state")
      				local filestatus = string.sub(fileContents2[8], 10, currentstatusend-1)         				
      				
      				--filehours = filecurrent/60
      				filelengthhours = math.floor(filelength/3600)
      				filelengthmins = math.floor((filelength/60)-(filelengthhours*60))
      				filelengthsecs = math.floor( filelength-(filelengthhours*3600)-(filelengthmins*60) )
      				
      				filecurrenthours = math.floor(filecurrent/3600)
      				filecurrentmins = math.floor((filecurrent/60)-(filecurrenthours*60))
      				filecurrentsecs = math.floor( filecurrent-(filecurrenthours*3600)-(filecurrentmins*60) )      
      				
      				if(filecurrentmins<10) then
      					filecurrentmins	= "0"..filecurrentmins	
      				end

      				if(filecurrentsecs<10) then
      					filecurrentsecs	= "0"..filecurrentsecs	
      				end      		
      				
      				if(filelengthmins<10) then
      					filelengthmins	= "0"..filelengthmins	
      				end    
      				
      				if(filelengthsecs<10) then
      					filelengthsecs	= "0"..filelengthsecs	
      				end    
      				      					
      				hub:sendChat("VLC "..filestatus..": "..filename.." ("..filecurrenthours..":"..filecurrentmins..":"..filecurrentsecs.."/"..filelengthhours..":"..filelengthmins..":"..filelengthsecs..")")
					found= = 1
      				
      			end  -- end if found current line
     	 		
      		end  --end for ipairs filecontents
     	
      		if(found == 0) then
  				hub:addLine("No file currently playing") 
  			end      		
      		
      	else	 	
			hub:addLine("Cannot find status.xml")
      	end	--end if file2 

   else
      hub:addLine("playlist.xml not present")
   end  --if found file
end  --end function


DC():PrintDebug("  ** Loaded vlc.lua **")
